public class SnmpContextPool extends java.lang.Object implements SnmpContextFace
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.
SnmpContext,
SnmpContextv2cPool,
SnmpContextv3Pool| Modifier and Type | Field and Description |
|---|---|
protected java.lang.String |
bindAddr |
protected java.lang.String |
community |
protected SnmpContext |
context |
protected static java.util.Hashtable |
contextPool |
protected java.lang.String |
hostname |
protected int |
hostPort |
protected java.lang.String |
socketType |
DEFAULT_COMMUNITYDEFAULT_PORT, MAXPDU, MSS, STANDARD_SOCKET, TCP_SOCKET| Constructor and Description |
|---|
SnmpContextPool(java.lang.String host,
int port)
Constructor, using the Standard socket.
|
SnmpContextPool(java.lang.String host,
int port,
java.lang.String typeSocket)
Constructor.
|
SnmpContextPool(java.lang.String host,
int port,
java.lang.String comm,
java.lang.String typeSocket)
Constructor.
|
SnmpContextPool(java.lang.String host,
int port,
java.lang.String comm,
java.lang.String bindAddress,
java.lang.String typeSocket)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
addPdu(Pdu pdu)
Adds a PDU to the context.
|
void |
addRequestPduListener(RequestPduListener l)
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 |
addTrapListener(TrapListener l)
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.
|
java.lang.Object |
clone()
This method is not supported.
|
void |
destroy()
Releases the resources held by this context.
|
void |
destroyPool()
Destroys all the contexts (v1 and v2c) in the pool and empties the pool.
|
void |
dumpContexts(java.lang.String title)
Dumps the pool of contexts.
|
byte[] |
encodePacket(byte msg_type,
int rId,
int errstat,
int errind,
java.util.Enumeration ve,
java.lang.Object obj)
Encodes a PDU packet.
|
java.lang.String |
getBindAddress()
Returns the local bind address.
|
java.lang.String |
getCommunity()
Returns the community name.
|
java.lang.String |
getHashKey()
Returns the hash key.
|
java.lang.String |
getHost()
Returns the host.
|
protected SnmpContext |
getMatchingContext()
Returns a context from the pool.
|
int |
getPort()
Returns the port number.
|
java.lang.String |
getReceivedFromHostAddress()
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.
|
java.lang.String |
getSendToHostAddress()
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.
|
java.lang.String |
getTypeSocket()
Returns the type of socket.
|
int |
getVersion()
Returns the SNMP version of the context.
|
boolean |
isDestroyed()
Returns whether or not this context has been destroyed.
|
Pdu |
processIncomingPdu(byte[] message)
Processes the incoming PDU with the current context.
|
boolean |
removePdu(int requestId)
Removes a PDU from the context.
|
void |
removeRequestPduListener(RequestPduListener l)
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 |
removeRequestPduListener(RequestPduListener l,
ListeningContextPool lcontext)
Removes the specified request pdu listener from listening for packets
on the specified listening context.
|
void |
removeTrapListener(TrapListener l)
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(java.lang.String newCommunity)
Sets the community name.
|
java.lang.String |
toString()
Returns a string representation of the object.
|
protected static java.util.Hashtable contextPool
protected SnmpContext context
protected java.lang.String hostname
protected java.lang.String socketType
protected java.lang.String bindAddr
protected int hostPort
protected java.lang.String community
public SnmpContextPool(java.lang.String host,
int port)
throws java.io.IOException
host - The host to which the PDU will be sentport - The port where the SNMP server will bejava.io.IOExceptionSnmpContext.SnmpContext(String, int)public SnmpContextPool(java.lang.String host,
int port,
java.lang.String typeSocket)
throws java.io.IOException
host - The host to which the PDU will be sentport - The port where the SNMP server will betypeSocket - The type of socket to use.java.io.IOExceptionSnmpContext.SnmpContext(String, int, String),
SnmpContextBasisFace.STANDARD_SOCKET,
SnmpContextBasisFace.TCP_SOCKETpublic SnmpContextPool(java.lang.String host,
int port,
java.lang.String comm,
java.lang.String typeSocket)
throws java.io.IOException
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.java.io.IOExceptionSnmpContextBasisFace.STANDARD_SOCKET,
SnmpContextBasisFace.TCP_SOCKETpublic SnmpContextPool(java.lang.String host,
int port,
java.lang.String comm,
java.lang.String bindAddress,
java.lang.String typeSocket)
throws java.io.IOException
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.java.io.IOExceptionSnmpContextBasisFace.STANDARD_SOCKET,
SnmpContextBasisFace.TCP_SOCKETpublic int getVersion()
SnmpContextBasisFacegetVersion in interface SnmpContextBasisFaceSnmpConstants.SNMP_VERSION_1,
SnmpConstants.SNMP_VERSION_2c,
SnmpConstants.SNMP_VERSION_3public java.lang.String getHost()
SnmpContextBasisFacegetHost in interface SnmpContextBasisFacepublic int getPort()
SnmpContextBasisFacegetPort in interface SnmpContextBasisFacepublic java.lang.String getBindAddress()
SnmpContextBasisFacegetBindAddress in interface SnmpContextBasisFacepublic java.lang.String getTypeSocket()
SnmpContextBasisFacegetTypeSocket in interface SnmpContextBasisFaceSnmpContextBasisFace.STANDARD_SOCKET,
SnmpContextBasisFace.TCP_SOCKETpublic java.lang.String getSendToHostAddress()
SnmpContextBasisFacegetSendToHostAddress in interface SnmpContextBasisFaceContextSocketFace.getSendToHostAddress()public java.lang.String getReceivedFromHostAddress()
SnmpContextBasisFacegetReceivedFromHostAddress in interface SnmpContextBasisFaceContextSocketFace.getReceivedFromHostAddress()public java.lang.String getCommunity()
SnmpContextFacegetCommunity in interface SnmpContextFacepublic void setCommunity(java.lang.String newCommunity)
SnmpContextFacesetCommunity in interface SnmpContextFaceSnmpContextFace.DEFAULT_COMMUNITYpublic boolean addPdu(Pdu pdu) throws java.io.IOException, PduException
SnmpContextBasisFaceaddPdu in interface SnmpContextBasisFacepdu - the PDUjava.io.IOExceptionPduExceptionpublic boolean removePdu(int requestId)
SnmpContextBasisFaceremovePdu in interface SnmpContextBasisFacerequestId - the PDU request idpublic byte[] encodePacket(byte msg_type,
int rId,
int errstat,
int errind,
java.util.Enumeration ve,
java.lang.Object obj)
throws java.io.IOException,
EncodingException
encodePacket in interface SnmpContextBasisFacemsg_type - The message typerId - The message iderrstat - The error statuserrind - The error indexve - The varbind listobj - Additional object (only used in SNMPv3)java.io.IOExceptionEncodingExceptionpublic void sendPacket(byte[] packet)
SnmpContextBasisFacesendPacket in interface SnmpContextBasisFacepacket - The encoded packetpublic void destroy()
destroy in interface SnmpContextBasisFacepublic void destroyPool()
destroy()public boolean isDestroyed()
SnmpContextBasisFaceisDestroyed in interface SnmpContextBasisFaceprotected SnmpContext getMatchingContext() throws java.io.IOException
java.io.IOExceptiongetHashKey()public void dumpContexts(java.lang.String title)
title - The title of the dumppublic java.lang.String getHashKey()
getHashKey in interface SnmpContextBasisFacepublic void addTrapListener(TrapListener l) throws java.io.IOException
addTrapListener in interface SnmpContextBasisFacel - The trap listenerjava.io.IOExceptionAbstractSnmpContext.addTrapListener(uk.co.westhawk.snmp.event.TrapListener)public void removeTrapListener(TrapListener l) throws java.io.IOException
removeTrapListener in interface SnmpContextBasisFacel - The trap listenerjava.io.IOExceptionAbstractSnmpContext.removeTrapListener(uk.co.westhawk.snmp.event.TrapListener)public void addTrapListener(TrapListener l, int port) throws java.io.IOException
SnmpContextBasisFaceThe 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.
addTrapListener in interface SnmpContextBasisFacel - The trap listenerport - The port the traps are received onjava.io.IOExceptionListeningContextPool.ListeningContextPool(int, String, String),
ListeningContextPool.addRawPduListener(RawPduListener)public void removeTrapListener(TrapListener l, int port) throws java.io.IOException
SnmpContextBasisFaceThe listener will not be removed from all ListeningContext objects that are in the ListeningContextPool. In order to do that, use ListeningContextPool.removeTrapListenerFromPool()
removeTrapListener in interface SnmpContextBasisFacel - The trap listenerport - The port the traps are received onjava.io.IOExceptionListeningContextPool.ListeningContextPool(int, String, String),
ListeningContextPool.removeRawPduListener(RawPduListener),
ListeningContextPool.removeRawPduListenerFromPool(RawPduListener)public void addTrapListener(TrapListener l, ListeningContextPool lcontext) throws java.io.IOException
SnmpContextBasisFaceThe 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.
addTrapListener in interface SnmpContextBasisFacel - The trap listenerlcontext - The listening contextjava.io.IOExceptionListeningContextPool.ListeningContextPool(int, String, String),
ListeningContextPool.addRawPduListener(RawPduListener)public void removeTrapListener(TrapListener l, ListeningContextPool lcontext) throws java.io.IOException
SnmpContextBasisFaceThe listener will not be removed from all ListeningContext objects that are in the ListeningContextPool. In order to do that, use ListeningContextPool.removeTrapListenerFromPool()
removeTrapListener in interface SnmpContextBasisFacel - The trap listenerlcontext - The listening contextjava.io.IOExceptionListeningContextPool.ListeningContextPool(int, String, String),
ListeningContextPool.removeRawPduListener(RawPduListener),
ListeningContextPool.removeRawPduListenerFromPool(RawPduListener)public void addRequestPduListener(RequestPduListener l) throws java.io.IOException
SnmpContextBasisFaceThe 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.
addRequestPduListener in interface SnmpContextBasisFacel - The request PDU listenerjava.io.IOExceptionSnmpContextBasisFace.addRequestPduListener(RequestPduListener, int),
SnmpContextBasisFace.DEFAULT_PORTpublic void removeRequestPduListener(RequestPduListener l) throws java.io.IOException
SnmpContextBasisFaceThe listener will not be removed from all ListeningContext objects that are in the ListeningContextPool. In order to do that, use ListeningContextPool.removeRequestPduListenerFromPool()
removeRequestPduListener in interface SnmpContextBasisFacel - The request PDU listenerjava.io.IOExceptionSnmpContextBasisFace.removeRequestPduListener(RequestPduListener, int),
SnmpContextBasisFace.DEFAULT_PORTpublic void addRequestPduListener(RequestPduListener l, int port) throws java.io.IOException
SnmpContextBasisFaceThe 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.
addRequestPduListener in interface SnmpContextBasisFacel - The request PDU listenerport - The port the request PDUs are received onjava.io.IOExceptionListeningContextPool.ListeningContextPool(int, String, String),
ListeningContextPool.addRawPduListener(RawPduListener)public void removeRequestPduListener(RequestPduListener l, int port) throws java.io.IOException
SnmpContextBasisFaceThe listener will not be removed from all ListeningContext objects that are in the ListeningContextPool. In order to do that, use ListeningContextPool.removeRequestPduListenerFromPool()
removeRequestPduListener in interface SnmpContextBasisFacel - The request PDU listenerport - The port the request PDUs are received onjava.io.IOExceptionListeningContextPool.ListeningContextPool(int, String, String),
ListeningContextPool.removeRawPduListener(RawPduListener),
ListeningContextPool.removeRawPduListenerFromPool(RawPduListener)public void addRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws java.io.IOException
SnmpContextBasisFaceThe 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.
addRequestPduListener in interface SnmpContextBasisFacel - The request PDU listenerlcontext - The listening contextjava.io.IOExceptionListeningContextPool.ListeningContextPool(int, String, String),
ListeningContextPool.addRawPduListener(RawPduListener)public void removeRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws java.io.IOException
SnmpContextBasisFaceThe listener will not be removed from all ListeningContext objects that are in the ListeningContextPool. In order to do that, use ListeningContextPool.removeRequestPduListenerFromPool()
removeRequestPduListener in interface SnmpContextBasisFacel - The request PDU listenerlcontext - The listening contextjava.io.IOExceptionListeningContextPool.ListeningContextPool(int, String, String),
ListeningContextPool.removeRawPduListener(RawPduListener),
ListeningContextPool.removeRawPduListenerFromPool(RawPduListener)public Pdu processIncomingPdu(byte[] message) throws DecodingException, java.io.IOException
processIncomingPdu in interface SnmpContextBasisFaceDecodingExceptionjava.io.IOExceptionSnmpContext.processIncomingPdu(byte[])public java.lang.String toString()
toString in class java.lang.Objectpublic java.lang.Object clone()
throws java.lang.CloneNotSupportedException
clone in interface SnmpContextBasisFaceclone in class java.lang.Objectjava.lang.CloneNotSupportedException - Thrown when the constructor
generates an IOException or when in one of the Pool classes.