Class AbstractSnmpContext
- All Implemented Interfaces:
Runnable
,EventListener
,RawPduListener
,SnmpContextBasisFace
- Direct Known Subclasses:
SnmpContext
,SnmpContextv3Basis
destroy()
should be called when the context is no longer
used. This is the only way the threads will be stopped and garbage
collected.
- Version:
- $Revision: 3.33 $ $Date: 2009/03/05 12:48:04 $
- Author:
- Tim Panton
- See Also:
-
Field Summary
Fields inherited from interface uk.co.westhawk.snmp.stack.SnmpContextBasisFace
DEFAULT_PORT, MAXPDU, MSS, STANDARD_SOCKET, TCP_SOCKET
-
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 to receive traps on the default trap port 162 from the host that matches this context.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.abstract Object
clone()
Returns a clone of this SnmpContext.void
destroy()
This method will stop the thread.abstract byte[]
encodePacket
(byte msg_type, int rId, int errstat, int errind, Enumeration ve, Object obj) Encodes a PDU.Returns the local bind address.Returns the thread usage of the AbstractSnmpContext.abstract String
Returns the hash key.getHost()
Returns the host.Deprecated.int
Returns the maximum number of bytes this context will read from the socket.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.abstract int
Returns the SNMP version of this context.boolean
Returns whether or not this context has been destroyed.abstract Pdu
processIncomingPdu
(byte[] message) Processes an incoming pdu (but not a response).void
Invoked when an undecoded pdu is received.boolean
removePdu
(int rid) 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 from listening for packets on the default trap port 162.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
run()
We wait for any incoming packets.void
sendPacket
(byte[] p) Sends an encoded PDU.void
setMaxRecvSize
(int no) Sets the maximum number of bytes this context will read from the socket.void
stop()
Deprecated.As of version 4_12, should usedestroy()
-
Method Details
-
encodePacket
public abstract byte[] encodePacket(byte msg_type, int rId, int errstat, int errind, Enumeration ve, Object obj) throws IOException, EncodingException Encodes a 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. Has to be overload by each context.- 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
-
processIncomingPdu
Processes an incoming pdu (but not a response). Has to be overload by each context.- Specified by:
processIncomingPdu
in interfaceSnmpContextBasisFace
- Throws:
DecodingException
IOException
- See Also:
-
getVersion
public abstract int getVersion()Returns the SNMP version of this context. Has to be overload by each 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
-
getHostAddress
Deprecated.As of 4_14, usegetSendToHostAddress()
Returns the IP address string aaa.bbb.ccc.ddd (IPv4) or a:b:c:d:e:f:g:h (IPv6) of the host.- Returns:
- The IP address of the host
-
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:
-
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:
-
getMaxRecvSize
public int getMaxRecvSize()Returns the maximum number of bytes this context will read from the socket. By default this will be set toMSS
(i.e. 1300).- Returns:
- The number
- Since:
- 4_12
- See Also:
-
setMaxRecvSize
public void setMaxRecvSize(int no) Sets the maximum number of bytes this context will read from the socket. By default this will be set toMSS
(i.e. 1300). The default size seems a reasonable size. The problem usually occurs when sending Bulk requests.If a packet arrives that is bigger than the maximum size of received bytes, the stack will try to decode it nevertheless. The usual error that will occur is:
Error message: "Incorrect packet. No of bytes received less than packet length."
Although UDP datagrams can be fragmented (fragmentation is part of the network layer (IP), not the transport layer (UDP/TCP)), some firewalls reject incoming fragments. Therefor it is best not to set maxRecvSize higher than the largest packet size you can get through your network topology.
Thanks to Pete Kazmier (pete@kazmier.com) for the suggestion.
Note, this property is NOT supported in any of the SNMPContextXXPool classes.- Parameters:
no
- The new number- Since:
- 4_12
- See Also:
-
getDebugString
Returns the thread usage of the AbstractSnmpContext. It returns a String in the form of=PO=QR--------------0
.The String represents the array of transmitters. Each character represents a transmitter slot. The transmitters form a thread pool of a maximum size, MAXPDU. Each transmitter is used to wait for one PDU response at a given moment in time. When the response is received the transmitter will stop running, but is not destroyed. It will be reused.
Meaning of each character:
-
a transmitter slot has not yet been allocated a thread=
there is a thread but it is idleA->Z
the thread is transmitting a Pdu-
The last character represents the context's recv thread:
0
there isn't one1
it exists but isn't running2
it exists and is alive.
- Returns:
- The thread usage of the AbstractSnmpContext
- Since:
- 4_12
-
destroy
public void destroy()This method will stop the thread. All transmitters, PDUs in flight and traplisteners will be removed when run() finishes.It closes the socket. The thread will actually stop/finish when the run() finishes. Since the socket is closed, the run() will fall through almost instantly.
Note: The thread(s) will not die immediately; this will take about half a minute.
- Specified by:
destroy
in interfaceSnmpContextBasisFace
- 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
-
stop
public void stop()Deprecated.As of version 4_12, should usedestroy()
This method will stop the thread. All transmitters, PDUs in flight and traplisteners will be removed when run() finishes.It does NOT close the socket. The thread will actually stop/finish when the run() finishes. That is when a packet arrives on the socket or when the socket times out.
We have deprecated this method since there is no point in stopping the context, but not destroying it. The context cannot start again anyway. The difference between destroy() and stop() was not very clear.
- See Also:
-
run
public void run()We wait for any incoming packets. After receiving one, decode the packet into an Pdu. The Pdu will notify the observers waiting for an response.Thanks to Chris Barlock <barlock@us.ibm.com> who reported a NullPointerException in run() on variable 'me' and introduced the variable stopRequested.
-
sendPacket
public void sendPacket(byte[] p) 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:
p
- The encoded packet
-
removePdu
public boolean removePdu(int rid) 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:
rid
- the PDU request id- Returns:
- whether the PDU has been successfully removed
-
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:
p
- the PDU- Returns:
- whether the PDU has been successfully added
- Throws:
IOException
PduException
-
addTrapListener
Description copied from interface:SnmpContextBasisFace
Adds the specified trap listener to receive traps on the default trap port 162 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 listener- Throws:
IOException
- See Also:
-
removeTrapListener
Description copied from interface:SnmpContextBasisFace
Removes the specified trap listener from listening for packets on the default trap port 162.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 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:
-
rawPduReceived
Invoked when an undecoded pdu is received. First the version and the hostaddress are checked, if correct an attempt is made to decode the pdu. When successful the original event is consumed and a decoded pdu event is passed on the listeners.- Specified by:
rawPduReceived
in interfaceRawPduListener
- See Also:
-
clone
Returns a clone of this SnmpContext.- 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
-
getHashKey
Returns the hash key. This key is built out of all properties.- Specified by:
getHashKey
in interfaceSnmpContextBasisFace
- Returns:
- The hash key
- Since:
- 4_14
-
getSendToHostAddress()