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_COMMUNITY
DEFAULT_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.IOException
SnmpContext.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.IOException
SnmpContext.SnmpContext(String, int, String)
,
SnmpContextBasisFace.STANDARD_SOCKET
,
SnmpContextBasisFace.TCP_SOCKET
public 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.IOException
SnmpContextBasisFace.STANDARD_SOCKET
,
SnmpContextBasisFace.TCP_SOCKET
public 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.IOException
SnmpContextBasisFace.STANDARD_SOCKET
,
SnmpContextBasisFace.TCP_SOCKET
public int getVersion()
SnmpContextBasisFace
getVersion
in interface SnmpContextBasisFace
SnmpConstants.SNMP_VERSION_1
,
SnmpConstants.SNMP_VERSION_2c
,
SnmpConstants.SNMP_VERSION_3
public java.lang.String getHost()
SnmpContextBasisFace
getHost
in interface SnmpContextBasisFace
public int getPort()
SnmpContextBasisFace
getPort
in interface SnmpContextBasisFace
public java.lang.String getBindAddress()
SnmpContextBasisFace
getBindAddress
in interface SnmpContextBasisFace
public java.lang.String getTypeSocket()
SnmpContextBasisFace
getTypeSocket
in interface SnmpContextBasisFace
SnmpContextBasisFace.STANDARD_SOCKET
,
SnmpContextBasisFace.TCP_SOCKET
public java.lang.String getSendToHostAddress()
SnmpContextBasisFace
getSendToHostAddress
in interface SnmpContextBasisFace
ContextSocketFace.getSendToHostAddress()
public java.lang.String getReceivedFromHostAddress()
SnmpContextBasisFace
getReceivedFromHostAddress
in interface SnmpContextBasisFace
ContextSocketFace.getReceivedFromHostAddress()
public java.lang.String getCommunity()
SnmpContextFace
getCommunity
in interface SnmpContextFace
public void setCommunity(java.lang.String newCommunity)
SnmpContextFace
setCommunity
in interface SnmpContextFace
SnmpContextFace.DEFAULT_COMMUNITY
public boolean addPdu(Pdu pdu) throws java.io.IOException, PduException
SnmpContextBasisFace
addPdu
in interface SnmpContextBasisFace
pdu
- the PDUjava.io.IOException
PduException
public boolean removePdu(int requestId)
SnmpContextBasisFace
removePdu
in interface SnmpContextBasisFace
requestId
- 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 SnmpContextBasisFace
msg_type
- The message typerId
- The message iderrstat
- The error statuserrind
- The error indexve
- The varbind listobj
- Additional object (only used in SNMPv3)java.io.IOException
EncodingException
public void sendPacket(byte[] packet)
SnmpContextBasisFace
sendPacket
in interface SnmpContextBasisFace
packet
- The encoded packetpublic void destroy()
destroy
in interface SnmpContextBasisFace
public void destroyPool()
destroy()
public boolean isDestroyed()
SnmpContextBasisFace
isDestroyed
in interface SnmpContextBasisFace
protected SnmpContext getMatchingContext() throws java.io.IOException
java.io.IOException
getHashKey()
public void dumpContexts(java.lang.String title)
title
- The title of the dumppublic java.lang.String getHashKey()
getHashKey
in interface SnmpContextBasisFace
public void addTrapListener(TrapListener l) throws java.io.IOException
addTrapListener
in interface SnmpContextBasisFace
l
- The trap listenerjava.io.IOException
AbstractSnmpContext.addTrapListener(uk.co.westhawk.snmp.event.TrapListener)
public void removeTrapListener(TrapListener l) throws java.io.IOException
removeTrapListener
in interface SnmpContextBasisFace
l
- The trap listenerjava.io.IOException
AbstractSnmpContext.removeTrapListener(uk.co.westhawk.snmp.event.TrapListener)
public void addTrapListener(TrapListener l, int port) throws java.io.IOException
SnmpContextBasisFace
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.
addTrapListener
in interface SnmpContextBasisFace
l
- The trap listenerport
- The port the traps are received onjava.io.IOException
ListeningContextPool.ListeningContextPool(int, String, String)
,
ListeningContextPool.addRawPduListener(RawPduListener)
public void removeTrapListener(TrapListener l, int port) throws java.io.IOException
SnmpContextBasisFace
The 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 SnmpContextBasisFace
l
- The trap listenerport
- The port the traps are received onjava.io.IOException
ListeningContextPool.ListeningContextPool(int, String, String)
,
ListeningContextPool.removeRawPduListener(RawPduListener)
,
ListeningContextPool.removeRawPduListenerFromPool(RawPduListener)
public void addTrapListener(TrapListener l, ListeningContextPool lcontext) throws java.io.IOException
SnmpContextBasisFace
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.
addTrapListener
in interface SnmpContextBasisFace
l
- The trap listenerlcontext
- The listening contextjava.io.IOException
ListeningContextPool.ListeningContextPool(int, String, String)
,
ListeningContextPool.addRawPduListener(RawPduListener)
public void removeTrapListener(TrapListener l, ListeningContextPool lcontext) throws java.io.IOException
SnmpContextBasisFace
The 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 SnmpContextBasisFace
l
- The trap listenerlcontext
- The listening contextjava.io.IOException
ListeningContextPool.ListeningContextPool(int, String, String)
,
ListeningContextPool.removeRawPduListener(RawPduListener)
,
ListeningContextPool.removeRawPduListenerFromPool(RawPduListener)
public void addRequestPduListener(RequestPduListener l) throws java.io.IOException
SnmpContextBasisFace
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.
addRequestPduListener
in interface SnmpContextBasisFace
l
- The request PDU listenerjava.io.IOException
SnmpContextBasisFace.addRequestPduListener(RequestPduListener, int)
,
SnmpContextBasisFace.DEFAULT_PORT
public void removeRequestPduListener(RequestPduListener l) throws java.io.IOException
SnmpContextBasisFace
The 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 SnmpContextBasisFace
l
- The request PDU listenerjava.io.IOException
SnmpContextBasisFace.removeRequestPduListener(RequestPduListener, int)
,
SnmpContextBasisFace.DEFAULT_PORT
public void addRequestPduListener(RequestPduListener l, int port) throws java.io.IOException
SnmpContextBasisFace
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.
addRequestPduListener
in interface SnmpContextBasisFace
l
- The request PDU listenerport
- The port the request PDUs are received onjava.io.IOException
ListeningContextPool.ListeningContextPool(int, String, String)
,
ListeningContextPool.addRawPduListener(RawPduListener)
public void removeRequestPduListener(RequestPduListener l, int port) throws java.io.IOException
SnmpContextBasisFace
The 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 SnmpContextBasisFace
l
- The request PDU listenerport
- The port the request PDUs are received onjava.io.IOException
ListeningContextPool.ListeningContextPool(int, String, String)
,
ListeningContextPool.removeRawPduListener(RawPduListener)
,
ListeningContextPool.removeRawPduListenerFromPool(RawPduListener)
public void addRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws java.io.IOException
SnmpContextBasisFace
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.
addRequestPduListener
in interface SnmpContextBasisFace
l
- The request PDU listenerlcontext
- The listening contextjava.io.IOException
ListeningContextPool.ListeningContextPool(int, String, String)
,
ListeningContextPool.addRawPduListener(RawPduListener)
public void removeRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws java.io.IOException
SnmpContextBasisFace
The 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 SnmpContextBasisFace
l
- The request PDU listenerlcontext
- The listening contextjava.io.IOException
ListeningContextPool.ListeningContextPool(int, String, String)
,
ListeningContextPool.removeRawPduListener(RawPduListener)
,
ListeningContextPool.removeRawPduListenerFromPool(RawPduListener)
public Pdu processIncomingPdu(byte[] message) throws DecodingException, java.io.IOException
processIncomingPdu
in interface SnmpContextBasisFace
DecodingException
java.io.IOException
SnmpContext.processIncomingPdu(byte[])
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
clone
in interface SnmpContextBasisFace
clone
in class java.lang.Object
java.lang.CloneNotSupportedException
- Thrown when the constructor
generates an IOException or when in one of the Pool classes.