Class SnmpContextPool
- All Implemented Interfaces:
SnmpContextBasisFace
,SnmpContextFace
- Direct Known Subclasses:
SnmpContextv2cPool
Every time a property changes the pool is checked for a SnmpContext context that matches all the new properties of this class. If no such context exists, a new one is made. The PDUs associated with the old context remain associated with the old context.
A counter indicates the number of times the context is referenced.
The counter is decreased when destroy()
is called.
When the counter reaches zero, the context is released.
Note that because the underlying context can change when a property is changed and the PDUs remain associated with the old context, all properties have to be set BEFORE a PDU is sent.
Thanks to Seon Lee (slee@virtc.com) for reporting thread safety problems.
- Version:
- $Revision: 3.22 $ $Date: 2009/03/05 13:27:41 $
- Author:
- Birgit Arkesteijn
- See Also:
-
Field Summary
Fields inherited from interface uk.co.westhawk.snmp.stack.SnmpContextBasisFace
DEFAULT_PORT, MAXPDU, MSS, STANDARD_SOCKET, TCP_SOCKET
Fields inherited from interface uk.co.westhawk.snmp.stack.SnmpContextFace
DEFAULT_COMMUNITY
-
Constructor Summary
ConstructorsConstructorDescriptionSnmpContextPool
(String host, int port) Constructor, using the Standard socket.SnmpContextPool
(String host, int port, String typeSocket) Constructor.SnmpContextPool
(String host, int port, String comm, String typeSocket) Constructor.SnmpContextPool
(String host, int port, String comm, String bindAddress, String typeSocket) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds a PDU to the context.void
Adds the specified request pdu listener to receive PDUs on the default request pdu port 161 from the host that matches this context.void
addRequestPduListener
(RequestPduListener l, int port) Adds the specified request pdu listener to receive PDUs on the specified port from the host that matches this context.void
addRequestPduListener
(RequestPduListener l, ListeningContextPool lcontext) Adds the specified request pdu listener to receive PDUs on the specified listening context that matches this context.void
Adds the specified trap listener.void
addTrapListener
(TrapListener l, int port) Adds the specified trap listener to receive traps on the specified port from the host that matches this context.void
addTrapListener
(TrapListener l, ListeningContextPool lcontext) Adds the specified trap listener to receive traps on the specified listening context that matches this context.clone()
This method is not supported.void
destroy()
Releases the resources held by this context.void
Destroys all the contexts (v1 and v2c) in the pool and empties the pool.void
dumpContexts
(String title) Dumps the pool of contexts.byte[]
encodePacket
(byte msg_type, int rId, int errstat, int errind, Enumeration ve, Object obj) Encodes a PDU packet.Returns the local bind address.Returns the community name.Returns the hash key.getHost()
Returns the host.int
getPort()
Returns the port number.Returns the IP address string aaa.bbb.ccc.ddd (IPv4) or a:b:c:d:e:f:g:h (IPv6) of the (latest) host the packets where received from.Returns the IP address string aaa.bbb.ccc.ddd (IPv4) or a:b:c:d:e:f:g:h (IPv6) of the host the packets where sent to.Returns the type of socket.int
Returns the SNMP version of the context.boolean
Returns whether or not this context has been destroyed.processIncomingPdu
(byte[] message) Processes the incoming PDU with the current context.boolean
removePdu
(int requestId) Removes a PDU from the context.void
Removes the specified request pdu listener from listening for packets on the default request pdu port 161.void
removeRequestPduListener
(RequestPduListener l, int port) Removes the specified request pdu listener from listening for packets on the specified port.void
Removes the specified request pdu listener from listening for packets on the specified listening context.void
Removes the specified trap listener.void
removeTrapListener
(TrapListener l, int port) Removes the specified trap listener from listening for packets on the specified port.void
removeTrapListener
(TrapListener l, ListeningContextPool lcontext) Removes the specified trap listener from listening for packets on the specified listening context.void
sendPacket
(byte[] packet) Sends an encoded PDU.void
setCommunity
(String newCommunity) Sets the community name.toString()
Returns a string representation of the object.
-
Constructor Details
-
SnmpContextPool
Constructor, using the Standard socket.- Parameters:
host
- The host to which the PDU will be sentport
- The port where the SNMP server will be- Throws:
IOException
- See Also:
-
SnmpContextPool
Constructor. Parameter typeSocket should be either STANDARD_SOCKET, TCP_SOCKET or a fully qualified classname.- Parameters:
host
- The host to which the PDU will be sentport
- The port where the SNMP server will betypeSocket
- The type of socket to use.- Throws:
IOException
- See Also:
-
SnmpContextPool
Constructor. Parameter typeSocket should be either STANDARD_SOCKET, TCP_SOCKET or a fully qualified classname.- Parameters:
host
- The host to which the PDU will be sentport
- The port where the SNMP server will becomm
- The community name.typeSocket
- The type of socket to use.- Throws:
IOException
- Since:
- 4_12
- See Also:
-
SnmpContextPool
public SnmpContextPool(String host, int port, String comm, String bindAddress, String typeSocket) throws IOException Constructor. Parameter typeSocket should be either STANDARD_SOCKET, TCP_SOCKET or a fully qualified classname.- Parameters:
host
- The host to which the PDU will be sentport
- The port where the SNMP server will becomm
- The community name.bindAddress
- The local address the server will bind totypeSocket
- The type of socket to use.- Throws:
IOException
- Since:
- 4_14
- See Also:
-
-
Method Details
-
getVersion
public int getVersion()Description copied from interface:SnmpContextBasisFace
Returns the SNMP version of the context.- Specified by:
getVersion
in interfaceSnmpContextBasisFace
- Returns:
- The version
- See Also:
-
getHost
Description copied from interface:SnmpContextBasisFace
Returns the host.- Specified by:
getHost
in interfaceSnmpContextBasisFace
- Returns:
- The host
-
getPort
public int getPort()Description copied from interface:SnmpContextBasisFace
Returns the port number.- Specified by:
getPort
in interfaceSnmpContextBasisFace
- Returns:
- The port no
-
getBindAddress
Description copied from interface:SnmpContextBasisFace
Returns the local bind address. If bindAddress is null, then the system will pick up a valid local address to bind the socket.- Specified by:
getBindAddress
in interfaceSnmpContextBasisFace
- Returns:
- The local bind address
-
getTypeSocket
Description copied from interface:SnmpContextBasisFace
Returns the type of socket.- Specified by:
getTypeSocket
in interfaceSnmpContextBasisFace
- Returns:
- The type of socket
- See Also:
-
getSendToHostAddress
Description copied from interface:SnmpContextBasisFace
Returns the IP address string aaa.bbb.ccc.ddd (IPv4) or a:b:c:d:e:f:g:h (IPv6) of the host the packets where sent to.- Specified by:
getSendToHostAddress
in interfaceSnmpContextBasisFace
- Returns:
- The IP address of the host the packets where sent to.
- See Also:
-
getReceivedFromHostAddress
Description copied from interface:SnmpContextBasisFace
Returns the IP address string aaa.bbb.ccc.ddd (IPv4) or a:b:c:d:e:f:g:h (IPv6) of the (latest) host the packets where received from.- Specified by:
getReceivedFromHostAddress
in interfaceSnmpContextBasisFace
- Returns:
- The IP address of the (latest) host the packets where received from.
- See Also:
-
getCommunity
Description copied from interface:SnmpContextFace
Returns the community name.- Specified by:
getCommunity
in interfaceSnmpContextFace
-
setCommunity
Description copied from interface:SnmpContextFace
Sets the community name. This community name will be used for all PDUs sent with this context. The default community name is public.- Specified by:
setCommunity
in interfaceSnmpContextFace
- See Also:
-
addPdu
Description copied from interface:SnmpContextBasisFace
Adds a PDU to the context. This is for internal use only and should NOT be called by the developer. This is called by the the Pdu itself and is added to the interface to cover the different kind of Contexts.- Specified by:
addPdu
in interfaceSnmpContextBasisFace
- Parameters:
pdu
- the PDU- Returns:
- whether the PDU has been successfully added
- Throws:
IOException
PduException
-
removePdu
public boolean removePdu(int requestId) Description copied from interface:SnmpContextBasisFace
Removes a PDU from the context. This is for internal use only and should NOT be called by the developer. This is called by the the PDU itself and is added to the interface to cover the different kind of Contexts.- Specified by:
removePdu
in interfaceSnmpContextBasisFace
- Parameters:
requestId
- the PDU request id- Returns:
- whether the PDU has been successfully removed
-
encodePacket
public byte[] encodePacket(byte msg_type, int rId, int errstat, int errind, Enumeration ve, Object obj) throws IOException, EncodingException Encodes a PDU packet.- Specified by:
encodePacket
in interfaceSnmpContextBasisFace
- Parameters:
msg_type
- The message typerId
- The message iderrstat
- The error statuserrind
- The error indexve
- The varbind listobj
- Additional object (only used in SNMPv3)- Returns:
- The encoded packet
- Throws:
IOException
EncodingException
-
sendPacket
public void sendPacket(byte[] packet) Description copied from interface:SnmpContextBasisFace
Sends an encoded PDU. This is for internal use only and should NOT be called by the developer. This is called by the the PDU itself and is added to the interface to cover the different kind of Contexts.- Specified by:
sendPacket
in interfaceSnmpContextBasisFace
- Parameters:
packet
- The encoded packet
-
destroy
public void destroy()Releases the resources held by this context. This method will decrement the reference counter. When the reference counter reaches zero the actual context is removed from the pool and destroyed.- Specified by:
destroy
in interfaceSnmpContextBasisFace
-
destroyPool
public void destroyPool()Destroys all the contexts (v1 and v2c) in the pool and empties the pool. The underlying implementation uses the same hashtable for both the v1 and the v2c contexts.- Since:
- 4_14
- See Also:
-
isDestroyed
public boolean isDestroyed()Description copied from interface:SnmpContextBasisFace
Returns whether or not this context has been destroyed.- Specified by:
isDestroyed
in interfaceSnmpContextBasisFace
-
dumpContexts
Dumps the pool of contexts. This is for debug purposes.- Parameters:
title
- The title of the dump
-
getHashKey
Returns the hash key. This key is built out of all properties. It serves as key for the hashtable of (v1) contexts.- Specified by:
getHashKey
in interfaceSnmpContextBasisFace
- Returns:
- The hash key
-
addTrapListener
Adds the specified trap listener. The listener will be added to the current context, not to all the contexts in the hashtable.- Specified by:
addTrapListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The trap listener- Throws:
IOException
- See Also:
-
removeTrapListener
Removes the specified trap listener. The listener will be removed from the current context, not from all the contexts in the hashtable.- Specified by:
removeTrapListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The trap listener- Throws:
IOException
- See Also:
-
addTrapListener
Description copied from interface:SnmpContextBasisFace
Adds the specified trap listener to receive traps on the specified port from the host that matches this context.The ListeningContext class will do the actual listening for traps. This context will add itself to a ListeningContextPool object and will only pass the event to its listeners if the pdu matches this context and is a trap pdu.
- Specified by:
addTrapListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The trap listenerport
- The port the traps are received on- Throws:
IOException
- See Also:
-
removeTrapListener
Description copied from interface:SnmpContextBasisFace
Removes the specified trap listener from listening for packets on the specified port.The listener will not be removed from all ListeningContext objects that are in the ListeningContextPool. In order to do that, use ListeningContextPool.removeTrapListenerFromPool()
- Specified by:
removeTrapListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The trap listenerport
- The port the traps are received on- Throws:
IOException
- See Also:
-
addTrapListener
Description copied from interface:SnmpContextBasisFace
Adds the specified trap listener to receive traps on the specified listening context that matches this context.The ListeningContext class will do the actual listening for traps. This context will add itself to a ListeningContextPool object and will only pass the event to its listeners if the pdu matches this context and is a trap pdu.
- Specified by:
addTrapListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The trap listenerlcontext
- The listening context- Throws:
IOException
- See Also:
-
removeTrapListener
Description copied from interface:SnmpContextBasisFace
Removes the specified trap listener from listening for packets on the specified listening context.The listener will not be removed from all ListeningContext objects that are in the ListeningContextPool. In order to do that, use ListeningContextPool.removeTrapListenerFromPool()
- Specified by:
removeTrapListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The trap listenerlcontext
- The listening context- Throws:
IOException
- See Also:
-
addRequestPduListener
Description copied from interface:SnmpContextBasisFace
Adds the specified request pdu listener to receive PDUs on the default request pdu port 161 from the host that matches this context.The ListeningContext class will do the actual listening for PDUs. This context will add itself to a ListeningContextPool object and will only pass the event to its listeners if the pdu matches this context and is a request pdu.
Don't use the TCP_SOCKET when listening for request PDUs. It doesn't provide functionality to send a response back.
- Specified by:
addRequestPduListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The request PDU listener- Throws:
IOException
- See Also:
-
removeRequestPduListener
Description copied from interface:SnmpContextBasisFace
Removes the specified request pdu listener from listening for packets on the default request pdu port 161.The listener will not be removed from all ListeningContext objects that are in the ListeningContextPool. In order to do that, use ListeningContextPool.removeRequestPduListenerFromPool()
- Specified by:
removeRequestPduListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The request PDU listener- Throws:
IOException
- See Also:
-
addRequestPduListener
Description copied from interface:SnmpContextBasisFace
Adds the specified request pdu listener to receive PDUs on the specified port from the host that matches this context.The ListeningContext class will do the actual listening for PDUs. This context will add itself to a ListeningContextPool object and will only pass the event to its listeners if the pdu matches this context and is a request pdu.
Don't use the TCP_SOCKET when listening for request PDUs. It doesn't provide functionality to send a response back.
- Specified by:
addRequestPduListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The request PDU listenerport
- The port the request PDUs are received on- Throws:
IOException
- See Also:
-
removeRequestPduListener
Description copied from interface:SnmpContextBasisFace
Removes the specified request pdu listener from listening for packets on the specified port.The listener will not be removed from all ListeningContext objects that are in the ListeningContextPool. In order to do that, use ListeningContextPool.removeRequestPduListenerFromPool()
- Specified by:
removeRequestPduListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The request PDU listenerport
- The port the request PDUs are received on- Throws:
IOException
- See Also:
-
addRequestPduListener
public void addRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws IOException Description copied from interface:SnmpContextBasisFace
Adds the specified request pdu listener to receive PDUs on the specified listening context that matches this context.The ListeningContext class will do the actual listening for PDUs. This context will add itself to a ListeningContextPool object and will only pass the event to its listeners if the pdu matches this context and is a request pdu.
Don't use the TCP_SOCKET when listening for request PDUs. It doesn't provide functionality to send a response back.
- Specified by:
addRequestPduListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The request PDU listenerlcontext
- The listening context- Throws:
IOException
- See Also:
-
removeRequestPduListener
public void removeRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws IOException Description copied from interface:SnmpContextBasisFace
Removes the specified request pdu listener from listening for packets on the specified listening context.The listener will not be removed from all ListeningContext objects that are in the ListeningContextPool. In order to do that, use ListeningContextPool.removeRequestPduListenerFromPool()
- Specified by:
removeRequestPduListener
in interfaceSnmpContextBasisFace
- Parameters:
l
- The request PDU listenerlcontext
- The listening context- Throws:
IOException
- See Also:
-
processIncomingPdu
Processes the incoming PDU with the current context.- Specified by:
processIncomingPdu
in interfaceSnmpContextBasisFace
- Throws:
DecodingException
IOException
- See Also:
-
toString
Returns a string representation of the object. -
clone
This method is not supported. It will throw a CloneNotSupportedException.- Specified by:
clone
in interfaceSnmpContextBasisFace
- Throws:
CloneNotSupportedException
- Thrown when the constructor generates an IOException or when in one of the Pool classes.- Since:
- 4_14
-