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 TypeMethodDescriptionbooleanAdds a PDU to the context.voidAdds the specified request pdu listener to receive PDUs on the default request pdu port 161 from the host that matches this context.voidaddRequestPduListener(RequestPduListener l, int port) Adds the specified request pdu listener to receive PDUs on the specified port from the host that matches this context.voidaddRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) Adds the specified request pdu listener to receive PDUs on the specified listening context that matches this context.voidAdds the specified trap listener to receive traps on the default trap port 162 from the host that matches this context.voidaddTrapListener(TrapListener l, int port) Adds the specified trap listener to receive traps on the specified port from the host that matches this context.voidaddTrapListener(TrapListener l, ListeningContextPool lcontext) Adds the specified trap listener to receive traps on the specified listening context that matches this context.abstract Objectclone()Returns a clone of this SnmpContext.voiddestroy()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 StringReturns the hash key.getHost()Returns the host.Deprecated.intReturns the maximum number of bytes this context will read from the socket.intgetPort()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 intReturns the SNMP version of this context.booleanReturns whether or not this context has been destroyed.abstract PduprocessIncomingPdu(byte[] message) Processes an incoming pdu (but not a response).voidInvoked when an undecoded pdu is received.booleanremovePdu(int rid) Removes a PDU from the context.voidRemoves the specified request pdu listener from listening for packets on the default request pdu port 161.voidremoveRequestPduListener(RequestPduListener l, int port) Removes the specified request pdu listener from listening for packets on the specified port.voidRemoves the specified request pdu listener from listening for packets on the specified listening context.voidRemoves the specified trap listener from listening for packets on the default trap port 162.voidremoveTrapListener(TrapListener l, int port) Removes the specified trap listener from listening for packets on the specified port.voidremoveTrapListener(TrapListener l, ListeningContextPool lcontext) Removes the specified trap listener from listening for packets on the specified listening context.voidrun()We wait for any incoming packets.voidsendPacket(byte[] p) Sends an encoded PDU.voidsetMaxRecvSize(int no) Sets the maximum number of bytes this context will read from the socket.voidstop()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:
 encodePacketin 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:
 IOExceptionEncodingException
 - 
processIncomingPdu
Processes an incoming pdu (but not a response). Has to be overload by each context.- Specified by:
 processIncomingPduin interfaceSnmpContextBasisFace- Throws:
 DecodingExceptionIOException- See Also:
 
 - 
getVersion
public abstract int getVersion()Returns the SNMP version of this context. Has to be overload by each context.- Specified by:
 getVersionin interfaceSnmpContextBasisFace- Returns:
 - The version
 - See Also:
 
 - 
getHost
Description copied from interface:SnmpContextBasisFaceReturns the host.- Specified by:
 getHostin 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:SnmpContextBasisFaceReturns 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:
 getSendToHostAddressin interfaceSnmpContextBasisFace- Returns:
 - The IP address of the host the packets where sent to.
 - See Also:
 
 - 
getReceivedFromHostAddress
Description copied from interface:SnmpContextBasisFaceReturns 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:
 getReceivedFromHostAddressin interfaceSnmpContextBasisFace- Returns:
 - The IP address of the (latest) host the packets where received from.
 - See Also:
 
 - 
getPort
public int getPort()Description copied from interface:SnmpContextBasisFaceReturns the port number.- Specified by:
 getPortin interfaceSnmpContextBasisFace- Returns:
 - The port no
 
 - 
getBindAddress
Description copied from interface:SnmpContextBasisFaceReturns the local bind address. If bindAddress is null, then the system will pick up a valid local address to bind the socket.- Specified by:
 getBindAddressin interfaceSnmpContextBasisFace- Returns:
 - The local bind address
 
 - 
getTypeSocket
Description copied from interface:SnmpContextBasisFaceReturns the type of socket.- Specified by:
 getTypeSocketin 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->Zthe thread is transmitting a Pdu- 
 The last character represents the context's recv thread:
 
0there isn't one1it exists but isn't running2it 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:
 destroyin interfaceSnmpContextBasisFace- See Also:
 
 - 
isDestroyed
public boolean isDestroyed()Description copied from interface:SnmpContextBasisFaceReturns whether or not this context has been destroyed.- Specified by:
 isDestroyedin 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:SnmpContextBasisFaceSends 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:
 sendPacketin interfaceSnmpContextBasisFace- Parameters:
 p- The encoded packet
 - 
removePdu
public boolean removePdu(int rid) Description copied from interface:SnmpContextBasisFaceRemoves 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:
 removePduin interfaceSnmpContextBasisFace- Parameters:
 rid- the PDU request id- Returns:
 - whether the PDU has been successfully removed
 
 - 
addPdu
Description copied from interface:SnmpContextBasisFaceAdds 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:
 addPduin interfaceSnmpContextBasisFace- Parameters:
 p- the PDU- Returns:
 - whether the PDU has been successfully added
 - Throws:
 IOExceptionPduException
 - 
addTrapListener
Description copied from interface:SnmpContextBasisFaceAdds 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:
 addTrapListenerin interfaceSnmpContextBasisFace- Parameters:
 l- The trap listener- Throws:
 IOException- See Also:
 
 - 
removeTrapListener
Description copied from interface:SnmpContextBasisFaceRemoves 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:
 removeTrapListenerin interfaceSnmpContextBasisFace- Parameters:
 l- The trap listener- Throws:
 IOException- See Also:
 
 - 
addTrapListener
Description copied from interface:SnmpContextBasisFaceAdds 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:
 addTrapListenerin interfaceSnmpContextBasisFace- Parameters:
 l- The trap listenerport- The port the traps are received on- Throws:
 IOException- See Also:
 
 - 
removeTrapListener
Description copied from interface:SnmpContextBasisFaceRemoves 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:
 removeTrapListenerin interfaceSnmpContextBasisFace- Parameters:
 l- The trap listenerport- The port the traps are received on- Throws:
 IOException- See Also:
 
 - 
addTrapListener
Description copied from interface:SnmpContextBasisFaceAdds 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:
 addTrapListenerin interfaceSnmpContextBasisFace- Parameters:
 l- The trap listenerlcontext- The listening context- Throws:
 IOException- See Also:
 
 - 
removeTrapListener
Description copied from interface:SnmpContextBasisFaceRemoves 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:
 removeTrapListenerin interfaceSnmpContextBasisFace- Parameters:
 l- The trap listenerlcontext- The listening context- Throws:
 IOException- See Also:
 
 - 
addRequestPduListener
Description copied from interface:SnmpContextBasisFaceAdds 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:
 addRequestPduListenerin interfaceSnmpContextBasisFace- Parameters:
 l- The request PDU listener- Throws:
 IOException- See Also:
 
 - 
removeRequestPduListener
Description copied from interface:SnmpContextBasisFaceRemoves 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:
 removeRequestPduListenerin interfaceSnmpContextBasisFace- Parameters:
 l- The request PDU listener- Throws:
 IOException- See Also:
 
 - 
addRequestPduListener
Description copied from interface:SnmpContextBasisFaceAdds 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:
 addRequestPduListenerin interfaceSnmpContextBasisFace- Parameters:
 l- The request PDU listenerport- The port the request PDUs are received on- Throws:
 IOException- See Also:
 
 - 
removeRequestPduListener
Description copied from interface:SnmpContextBasisFaceRemoves 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:
 removeRequestPduListenerin 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:SnmpContextBasisFaceAdds 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:
 addRequestPduListenerin 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:SnmpContextBasisFaceRemoves 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:
 removeRequestPduListenerin 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:
 rawPduReceivedin interfaceRawPduListener- See Also:
 
 - 
clone
Returns a clone of this SnmpContext.- Specified by:
 clonein 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:
 getHashKeyin interfaceSnmpContextBasisFace- Returns:
 - The hash key
 - Since:
 - 4_14
 
 
 - 
 
getSendToHostAddress()