Package org.opennms.protocols.snmp
Class SnmpSession
- java.lang.Object
-
- org.opennms.protocols.snmp.SnmpSession
-
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.
- Version:
- 1.1.1.1 2001/11/11 17:27:22
- Author:
- Brian Weaver , OpenNMS
- See Also:
SnmpHandler
,SnmpPacketHandler
,SnmpPortal
-
-
Field Summary
Fields Modifier and Type Field 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 messagestatic int
ERROR_IOEXCEPTION
This is the command passed to the SnmpHandler if an IOException occurs while attempting to transmit the requeststatic int
ERROR_TIMEOUT
This is the command passed to the SnmpHandler if a timeout occurs.
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cancel(int requestId)
Cancels the current outstanding reqeust as defined by the SnmpPduPacket's requestId method.void
close()
Used to close the session.SnmpSyntax
get(SnmpObjectId oid)
SnmpSyntax[]
get(SnmpObjectId[] oids)
AsnEncoder
getAsnEncoder()
Gets the AsnEncoder for the session.SnmpSyntax[]
getBulk(int nonRepeaters, int maxReptitions, SnmpObjectId id)
SnmpSyntax[]
getBulk(int nonRepeaters, int maxRepititions, SnmpObjectId[] oids)
SnmpHandler
getDefaultHandler()
Gets the default SnmpHandler for the session.SnmpSyntax
getNext(SnmpObjectId oid)
SnmpSyntax[]
getNext(SnmpObjectId[] oids)
int
getOutstandingCount()
Returns the number of outstanding request for the agent.SnmpPeer
getPeer()
Gets the current peer object.SnmpPduPacket
getResponse(SnmpPduPacket request)
boolean
isClosed()
Returns true if theclose
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.SnmpSyntax[]
set(SnmpObjectId[] oids, SnmpSyntax[] values)
SnmpSyntax
set(SnmpObjectId oid, SnmpSyntax value)
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.
-
-
-
Field Detail
-
ERROR_TIMEOUT
public static final int ERROR_TIMEOUT
This is the command passed to the SnmpHandler if a timeout occurs. All errors are less than zero.- See Also:
SnmpHandler
, Constant Field Values
-
ERROR_IOEXCEPTION
public static final int ERROR_IOEXCEPTION
This is the command passed to the SnmpHandler if an IOException occurs while attempting to transmit the request- See Also:
SnmpHandler
, Constant Field Values
-
ERROR_ENCODING
public static final int ERROR_ENCODING
This is the command passed to the SnmpHandler if an encoding exception is generated when attempting to send an SnmpPduRequest message- See Also:
SnmpHandler
, Constant Field Values
-
-
Constructor Detail
-
SnmpSession
public SnmpSession(java.net.InetAddress peer) throws java.net.SocketException
The default SnmpSession constructor. The object is constructed with a default SnmpPeer object.- Parameters:
peer
- The peer agent- Throws:
java.net.SocketException
- If thrown it is from the creation of a DatagramSocket.- See Also:
SnmpPeer
-
SnmpSession
public SnmpSession(SnmpPeer peer) throws java.net.SocketException
Constructs the SnmpSession with the specific SnmpPeer.- Parameters:
peer
- The SnmpPeer used to configure this session- Throws:
java.net.SocketException
- If thrown it is from the creation of a DatagramSocket.- See Also:
SnmpPeer
-
SnmpSession
public SnmpSession(java.net.InetAddress peer, SnmpParameters params) throws java.net.SocketException
Constructs the SnmpSession with the specific parameters. The parameters are associated with the default SnmpPeer object.- Parameters:
peer
- The peer address for agentparams
- The SnmpParameters to configure with this session- Throws:
java.net.SocketException
- If thrown it is from the creation of a DatagramSocket.- See Also:
SnmpPeer
,SnmpParameters
-
-
Method Detail
-
getDefaultHandler
public SnmpHandler getDefaultHandler()
Gets the default SnmpHandler for the session. If the handler has never been set via the setDefaultHandler() method then a null will be returned.- Returns:
- The default SnmpHandler, a null if one has never been registered.
-
setDefaultHandler
public void setDefaultHandler(SnmpHandler hdl)
Sets the default SnmpHandler.- Parameters:
hdl
- The new default handler
-
getPeer
public SnmpPeer getPeer()
Gets the current peer object.- Returns:
- The current SnmpPeer object
-
setPeer
public void setPeer(SnmpPeer peer)
Sets the passed SnmpPeer object to the one used for all new SNMP communications. This includes any outstanding retries.- Parameters:
peer
- The SnmpPeer object for the sesison
-
getOutstandingCount
public int getOutstandingCount()
Returns the number of outstanding request for the agent. An outstanding request is one that has no yet responded to the query.- Returns:
- The number of outstanding request
- Throws:
java.lang.IllegalStateException
- Throw if the session has been closed.
-
cancel
public void cancel(int requestId)
Cancels the current outstanding reqeust as defined by the SnmpPduPacket's requestId method.- Parameters:
requestId
- The request to cancel- Throws:
java.lang.IllegalStateException
- Throw if the session has been closed.- See Also:
SnmpPduPacket
-
send
public int send(SnmpPduPacket pdu, SnmpHandler handler)
Send the SNMP PDU to the remote agent and invokes the specified handler when the packet is recieve. This is a non-blocking call.- Parameters:
pdu
- The pdu to encode and sendhandler
- The handler object for this request- Returns:
- The request identifier for the newly sent PDU.
- Throws:
SnmpHandlerNotDefinedException
- Thrown if the handler is nulljava.lang.IllegalStateException
- Thrown if the session has been closed.
-
send
public int send(SnmpPduPacket pdu)
Sends the SNMP PDU to the remote agent and uses the default SnmpHandler to process the request. This is a non-blocking call- Parameters:
pdu
- The pdu to encode and send- Returns:
- The request identifier for the newly sent PDU.
- Throws:
SnmpHandlerNotDefinedException
- Thrown if the handler is nulljava.lang.IllegalStateException
- Thrown if the session has been closed.
-
send
public int send(SnmpPduTrap pdu, SnmpHandler handler)
Send the SNMP PDU Trap to the remote agent. This is a non-blocking call.- Parameters:
pdu
- The pdu to encode and sendhandler
- The handler object for this request- Returns:
- The request identifier for the newly sent PDU.
- Throws:
SnmpHandlerNotDefinedException
- Thrown if the handler is nulljava.lang.IllegalStateException
- Thrown if the session has been closed.
-
send
public int send(SnmpPduTrap pdu)
Sends the SNMP PDU Trap to the remote agent. This is a non-blocking call- Parameters:
pdu
- The pdu to encode and send- Returns:
- The request identifier for the newly sent PDU.
- Throws:
SnmpHandlerNotDefinedException
- Thrown if the handler is nulljava.lang.IllegalStateException
- Thrown if the session has been closed.
-
isClosed
public boolean isClosed()
Returns true if theclose
method has been called. The session cannot be used to send request afterclose
has been executed.
-
close
public void close()
Used to close the session. Once called the session should be considered invalid and unusable.- Throws:
java.lang.IllegalStateException
- Thrown if the session has already been closed by another thread.
-
setAsnEncoder
public void setAsnEncoder(AsnEncoder encoder)
Sets the default encoder.- Parameters:
encoder
- The new encoder
-
getAsnEncoder
public AsnEncoder getAsnEncoder()
Gets the AsnEncoder for the session.- Returns:
- the AsnEncoder
-
registerSyntaxObject
public static void registerSyntaxObject(SnmpSyntax object)
Allows library users to register new ASN.1 types with the SNMP library. The object must support all methods of the SnmpSyntax interface. The object is registered globally with the library and is visible to all session after it is registered.- Parameters:
object
- The new SnmpSyntax object to register
-
getNext
public SnmpSyntax getNext(SnmpObjectId oid)
-
getNext
public SnmpSyntax[] getNext(SnmpObjectId[] oids)
-
get
public SnmpSyntax get(SnmpObjectId oid)
-
set
public SnmpSyntax set(SnmpObjectId oid, SnmpSyntax value)
-
set
public SnmpSyntax[] set(SnmpObjectId[] oids, SnmpSyntax[] values)
-
get
public SnmpSyntax[] get(SnmpObjectId[] oids)
-
getBulk
public SnmpSyntax[] getBulk(int nonRepeaters, int maxReptitions, SnmpObjectId id)
-
getBulk
public SnmpSyntax[] getBulk(int nonRepeaters, int maxRepititions, SnmpObjectId[] oids)
-
getResponse
public SnmpPduPacket getResponse(SnmpPduPacket request)
-
-