public abstract class AbstractSnmpContext extends java.lang.Object implements SnmpContextBasisFace, java.lang.Runnable, RawPduListener
destroy()
should be called when the context is no longer
used. This is the only way the threads will be stopped and garbage
collected.
SnmpContext
,
SnmpContextv2c
,
SnmpContextv3
Modifier and Type | Field and Description |
---|---|
protected boolean |
anyPduExpectingResponse |
protected java.lang.String |
bindAddr |
protected java.lang.String |
hostname |
protected int |
hostPort |
protected boolean |
isDestroyed |
protected int |
maxRecvSize |
protected RequestPduReceivedSupport |
pduSupport |
protected TrapReceivedSupport |
trapSupport |
protected java.lang.String |
typeSocket |
DEFAULT_PORT, MAXPDU, MSS, STANDARD_SOCKET, TCP_SOCKET
Modifier | Constructor and Description |
---|---|
protected |
AbstractSnmpContext(java.lang.String host,
int port)
Constructor.
|
protected |
AbstractSnmpContext(java.lang.String host,
int port,
java.lang.String typeSocketA)
Constructor.
|
protected |
AbstractSnmpContext(java.lang.String host,
int port,
java.lang.String bindAddress,
java.lang.String typeSocketA)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
protected void |
activate()
Creates and starts the Receive thread that allows this context to
receive packets.
|
boolean |
addPdu(Pdu p)
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 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 java.lang.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,
java.util.Enumeration ve,
java.lang.Object obj)
Encodes a PDU.
|
protected void |
freeTransmitters()
Frees the transmitters.
|
java.lang.String |
getBindAddress()
Returns the local bind address.
|
java.lang.String |
getDebugString()
Returns the thread usage of the AbstractSnmpContext.
|
abstract java.lang.String |
getHashKey()
Returns the hash key.
|
java.lang.String |
getHost()
Returns the host.
|
java.lang.String |
getHostAddress()
Deprecated.
As of 4_14, use
getSendToHostAddress() |
int |
getMaxRecvSize()
Returns the maximum number of bytes this context will read from the
socket.
|
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.
|
abstract int |
getVersion()
Returns the SNMP version of this context.
|
boolean |
isDestroyed()
Returns whether or not this context has been destroyed.
|
abstract Pdu |
processIncomingPdu(byte[] message)
Processes an incoming pdu (but not a response).
|
protected abstract void |
processIncomingResponse(java.io.ByteArrayInputStream in)
Processes an incoming response.
|
void |
rawPduReceived(RawPduEvent evt)
Invoked when an undecoded pdu is received.
|
boolean |
removePdu(int rid)
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 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 use
destroy() |
protected java.lang.String typeSocket
protected java.lang.String hostname
protected java.lang.String bindAddr
protected int hostPort
protected int maxRecvSize
protected boolean isDestroyed
protected boolean anyPduExpectingResponse
protected RequestPduReceivedSupport pduSupport
protected TrapReceivedSupport trapSupport
protected AbstractSnmpContext(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
SnmpContextBasisFace.STANDARD_SOCKET
protected AbstractSnmpContext(java.lang.String host, int port, java.lang.String typeSocketA) throws java.io.IOException
host
- The host to which the Pdu will be sentport
- The port where the SNMP server will betypeSocketA
- The type of socket to use.java.io.IOException
SnmpContextBasisFace.STANDARD_SOCKET
,
SnmpContextBasisFace.TCP_SOCKET
protected AbstractSnmpContext(java.lang.String host, int port, java.lang.String bindAddress, java.lang.String typeSocketA) throws java.io.IOException
host
- The host to which the Pdu will be sentport
- The port where the SNMP server will bebindAddress
- The local address the server will bind totypeSocketA
- The type of socket to use.java.io.IOException
- Thrown when the socket cannot be
created.SnmpContextBasisFace.STANDARD_SOCKET
,
SnmpContextBasisFace.TCP_SOCKET
protected abstract void processIncomingResponse(java.io.ByteArrayInputStream in) throws DecodingException, java.io.IOException
DecodingException
java.io.IOException
run()
public abstract 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 abstract Pdu processIncomingPdu(byte[] message) throws DecodingException, java.io.IOException
processIncomingPdu
in interface SnmpContextBasisFace
DecodingException
java.io.IOException
rawPduReceived(uk.co.westhawk.snmp.event.RawPduEvent)
public abstract int getVersion()
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 java.lang.String getHostAddress()
getSendToHostAddress()
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 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 int getMaxRecvSize()
MSS
(i.e. 1300).SnmpContextBasisFace.MSS
,
setMaxRecvSize(int)
public void setMaxRecvSize(int no)
MSS
(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.
no
- The new numberSnmpContextBasisFace.MSS
,
getMaxRecvSize()
public java.lang.String getDebugString()
=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 Pdu0
there isn't one1
it exists but isn't running 2
it exists and is alive.public void destroy()
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.
destroy
in interface SnmpContextBasisFace
ListeningContext.destroy()
,
ListeningContextPool.destroyPool()
public boolean isDestroyed()
SnmpContextBasisFace
isDestroyed
in interface SnmpContextBasisFace
public void stop()
destroy()
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.
destroy()
public void run()
Thanks to Chris Barlock
run
in interface java.lang.Runnable
public void sendPacket(byte[] p)
SnmpContextBasisFace
sendPacket
in interface SnmpContextBasisFace
p
- The encoded packetpublic boolean removePdu(int rid)
SnmpContextBasisFace
removePdu
in interface SnmpContextBasisFace
rid
- the PDU request idpublic boolean addPdu(Pdu p) throws java.io.IOException, PduException
SnmpContextBasisFace
addPdu
in interface SnmpContextBasisFace
p
- the PDUjava.io.IOException
PduException
public void addTrapListener(TrapListener l) 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 listenerjava.io.IOException
SnmpContextBasisFace.addTrapListener(TrapListener, int)
,
ListeningContextFace.DEFAULT_TRAP_PORT
public void removeTrapListener(TrapListener 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.removeTrapListenerFromPool()
removeTrapListener
in interface SnmpContextBasisFace
l
- The trap listenerjava.io.IOException
SnmpContextBasisFace.removeTrapListener(TrapListener, int)
,
ListeningContextFace.DEFAULT_TRAP_PORT
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 void rawPduReceived(RawPduEvent evt)
rawPduReceived
in interface RawPduListener
RawPduReceivedSupport.fireRawPduReceived(int, java.lang.String, int, byte[])
,
RequestPduReceivedSupport.fireRequestPduReceived(uk.co.westhawk.snmp.stack.Pdu, int)
,
TrapReceivedSupport.fireTrapReceived(uk.co.westhawk.snmp.stack.Pdu, int)
protected void activate()
protected void freeTransmitters()
public abstract 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.public abstract java.lang.String getHashKey()
getHashKey
in interface SnmpContextBasisFace