public class SnmpSession
extends java.lang.Object
The SnmpSession is the main connection between the SNMP manager and the SNMP Agent. All the request flow through this class. To use the SnmpSession class a SnmpHandler class must be defined to process any errors or responses through the library.
Once the session is created the creator must call close() to ensure an orderly release of threads and resources.
SnmpHandler
,
SnmpPacketHandler
,
SnmpPortal
Modifier and Type | Class and Description |
---|---|
private static class |
SnmpSession.ByteArrayInfo
Encapsulates a byte array and the number of
bytes of valid data in the array.
|
private class |
SnmpSession.CleanupRequest
This class is used to periodically cleanup the
outstanding request that have expired.
|
private class |
SnmpSession.SessionHandler
Inner class SessionHandler implements the interface
SnmpPacketHandler to handle callbacks from the SnmpPortal
|
Modifier and Type | Field and Description |
---|---|
static int |
ERROR_ENCODING
This is the command passed to the SnmpHandler
if an encoding exception is generated when
attempting to send an SnmpPduRequest message
|
static int |
ERROR_IOEXCEPTION
This is the command passed to the SnmpHandler
if an IOException occurs while attempting to
transmit the request
|
static int |
ERROR_RUNTIME
This is the command passed to the SnmpHandler
if an unexpected runtime exception was generated when
attempting to send an SnmpPduRequest message
|
static int |
ERROR_TIMEOUT
This is the command passed to the SnmpHandler
if a timeout occurs.
|
private static int |
EXPIRED_REQUEST_CLEANUP_FREQUENCY |
private SnmpHandler |
m_defHandler
The default SNMP callback handler.
|
(package private) AsnEncoder |
m_encoder
ASN encoder
|
private SnmpPeer |
m_peer
The SNMP peer to whom this session will communicate
with.
|
private SnmpPortal |
m_portal
the receiver thread
|
private java.util.Map |
m_requestsMap
Used to contain a list of outstanding request
for the session.
|
private boolean |
m_stopRun
If the boolean variable is set then
the destroy() method must have been called
|
private java.lang.Object |
m_sync
Provides a synchronization point
|
private SnmpTimer |
m_timer
The timer object used to schedule the SnmpRequest
timeouts.
|
Constructor and Description |
---|
SnmpSession(java.net.InetAddress peer)
The default SnmpSession constructor.
|
SnmpSession(java.net.InetAddress peer,
SnmpParameters params)
Constructs the SnmpSession with the specific
parameters.
|
SnmpSession(SnmpPeer peer)
Constructs the SnmpSession with the specific SnmpPeer.
|
SnmpSession(SnmpPeer peer,
int threadPoolSize,
int receiveBuffer,
int sendBuffer) |
Modifier and Type | Method and Description |
---|---|
void |
cancel(int requestId)
Cancels the current outstanding reqeust as defined
by the SnmpPduPacket's requestId method.
|
private void |
cleanupExpiredRequests() |
void |
close()
Used to close the session.
|
private SnmpSession.ByteArrayInfo |
encode(SnmpPduTrap pdu)
This method is used to encode the passed protocol data unit
and return the encoding.
|
private SnmpSession.ByteArrayInfo |
encode(SnmpPeer peer,
SnmpPduPacket pdu)
This method is used to encode the passed protocol data unit
and return the encoding.
|
AsnEncoder |
getAsnEncoder()
Gets the AsnEncoder for the session.
|
SnmpHandler |
getDefaultHandler()
Gets the default SnmpHandler for the session.
|
int |
getOutstandingCount()
Returns the number of outstanding request for
the agent.
|
SnmpPeer |
getPeer()
Gets the current peer object.
|
(package private) SnmpTimer |
getTimer()
Returns the internal timer object for the
SNMP Session.
|
boolean |
isClosed()
Returns true if the
close method has
been called. |
static void |
registerSyntaxObject(SnmpSyntax object)
Allows library users to register new ASN.1 types
with the SNMP library.
|
int |
send(SnmpPduPacket pdu)
Sends the SNMP PDU to the remote agent and uses the
default SnmpHandler to process the request.
|
int |
send(SnmpPduPacket pdu,
SnmpHandler handler)
Send the SNMP PDU to the remote agent and invokes the
specified handler when the packet is recieve.
|
int |
send(SnmpPduTrap pdu)
Sends the SNMP PDU Trap to the remote agent.
|
int |
send(SnmpPduTrap pdu,
SnmpHandler handler)
Send the SNMP PDU Trap to the remote agent.
|
void |
setAsnEncoder(AsnEncoder encoder)
Sets the default encoder.
|
void |
setDefaultHandler(SnmpHandler hdl)
Sets the default SnmpHandler.
|
void |
setPeer(SnmpPeer peer)
Sets the passed SnmpPeer object to the one
used for all new SNMP communications.
|
(package private) void |
snmpInternalError(SnmpRequest request,
int errorCode) |
void |
snmpTimeoutError(SnmpRequest request) |
(package private) void |
transmit(SnmpRequest req)
Transmits the specified SnmpRequest to the SnmpPeer defined
by the session.
|
public static final int ERROR_TIMEOUT
SnmpHandler
,
Constant Field Valuespublic static final int ERROR_IOEXCEPTION
SnmpHandler
,
Constant Field Valuespublic static final int ERROR_ENCODING
SnmpHandler
,
Constant Field Valuespublic static final int ERROR_RUNTIME
SnmpHandler
,
Constant Field Valuesprivate java.util.Map m_requestsMap
private SnmpPeer m_peer
SnmpParameters
private SnmpTimer m_timer
SnmpSession.CleanupRequest
,
SnmpRequest
private SnmpHandler m_defHandler
AsnEncoder m_encoder
private java.lang.Object m_sync
private boolean m_stopRun
private SnmpPortal m_portal
private static final int EXPIRED_REQUEST_CLEANUP_FREQUENCY
public SnmpSession(java.net.InetAddress peer) throws java.net.SocketException
peer
- The peer agentjava.net.SocketException
- If thrown it is from the creation
of a DatagramSocket.SnmpPeer
public SnmpSession(SnmpPeer peer) throws java.net.SocketException
peer
- The SnmpPeer used to configure this sessionjava.net.SocketException
- If thrown it is from the creation
of a DatagramSocket.SnmpPeer
public SnmpSession(java.net.InetAddress peer, SnmpParameters params) throws java.net.SocketException
peer
- The peer address for agentparams
- The SnmpParameters to configure with this sessionjava.net.SocketException
- If thrown it is from the creation
of a DatagramSocket.SnmpPeer
,
SnmpParameters
public SnmpSession(SnmpPeer peer, int threadPoolSize, int receiveBuffer, int sendBuffer) throws java.net.SocketException
java.net.SocketException
private SnmpSession.ByteArrayInfo encode(SnmpPeer peer, SnmpPduPacket pdu) throws SnmpPduEncodingException, AsnEncodingException
This method is used to encode the passed protocol data unit and return the encoding. The encoding is performed using the default encoder for the session and limits the size to a 16K buffer.
pdu
- The pdu to encodeSnmpPduEncodingException
- Thrown if an encoding exception
occurs at the session levelAsnEncodingException
- Thrown
if an encoding exception occurs in the AsnEncoder object.private SnmpSession.ByteArrayInfo encode(SnmpPduTrap pdu) throws AsnEncodingException
This method is used to encode the passed protocol data unit and return the encoding. The encoding is performed using the default encoder for the session and limits the size to a 16K buffer.
pdu
- The pdu to encodeSnmpPduEncodingException
- Thrown if an encoding exception
occurs at the session levelAsnEncodingException
- Thrown
if an encoding exception occurs in the AsnEncoder object.SnmpTimer getTimer()
void transmit(SnmpRequest req) throws SnmpPduEncodingException, AsnEncodingException, java.io.IOException
req
- The SnmpRequest to transmitSnmpPduEncodingException
- Thrown if an encoding exception
occurs at the session levelAsnEncodingException
- Thrown
if an encoding exception occurs in the AsnEncoder object.java.io.IOException
- Thrown if an error occurs sending the
encoded datagramSnmpRequest
,
SnmpParameters
,
SnmpPeer
void snmpInternalError(SnmpRequest request, int errorCode)
public void snmpTimeoutError(SnmpRequest request)
public SnmpHandler getDefaultHandler()
public void setDefaultHandler(SnmpHandler hdl)
hdl
- The new default handlerpublic SnmpPeer getPeer()
public void setPeer(SnmpPeer peer)
peer
- The SnmpPeer object for the sesisonpublic int getOutstandingCount()
java.lang.IllegalStateException
- Throw if the session has been closed.public void cancel(int requestId)
requestId
- The request to canceljava.lang.IllegalStateException
- Throw if the session has been closed.SnmpPduPacket
public int send(SnmpPduPacket pdu, SnmpHandler handler)
pdu
- The pdu to encode and sendhandler
- The handler object for this requestSnmpHandlerNotDefinedException
- Thrown if the handler is nulljava.lang.IllegalStateException
- Thrown if the session has been closed.public int send(SnmpPduPacket pdu)
pdu
- The pdu to encode and sendSnmpHandlerNotDefinedException
- Thrown if the handler is nulljava.lang.IllegalStateException
- Thrown if the session has been closed.public int send(SnmpPduTrap pdu, SnmpHandler handler)
pdu
- The pdu to encode and sendhandler
- The handler object for this requestSnmpHandlerNotDefinedException
- Thrown if the handler is nulljava.lang.IllegalStateException
- Thrown if the session has been closed.public int send(SnmpPduTrap pdu)
pdu
- The pdu to encode and sendSnmpHandlerNotDefinedException
- Thrown if the handler is nulljava.lang.IllegalStateException
- Thrown if the session has been closed.public boolean isClosed()
close
method has
been called. The session cannot be used to send
request after close
has been executed.public void close()
java.lang.IllegalStateException
- Thrown if the session has already
been closed by another thread.public void setAsnEncoder(AsnEncoder encoder)
encoder
- The new encoderpublic AsnEncoder getAsnEncoder()
public static void registerSyntaxObject(SnmpSyntax object)
object
- The new SnmpSyntax object to registerprivate void cleanupExpiredRequests()
Generated by iurt on 2015.