public class ListeningContext extends java.lang.Object implements ListeningContextFace, java.lang.Runnable
Two kind of listeners can be added;
the normal and unhandled PDU listeners.
The normal PDU listeners are added via the
addRawPduListener() method,
the unhandled PDU listeners are added via the
addUnhandledRawPduListener().
Both these listeners provide undecoded events.
The SnmpContext classes provide functionality for decoded PDU and
trap events. These classes will register themselves via the
addRawPduListener() to the ListeningContext object and
only pass the (decoded) event on if it matches their configuration.
On UNIX and Linux operating systems the default port where PDUs and traps are sent (i.e. 161 and 162) can only be opened as root.
Only one process can listen on a certain port. To prevent more than one ListeningContext listening on the same port, use the ListeningContextPool class.
ListeningContextPool,
AbstractSnmpContext.addTrapListener(uk.co.westhawk.snmp.event.TrapListener),
AbstractSnmpContext.addRequestPduListener(uk.co.westhawk.snmp.event.RequestPduListener)| Modifier and Type | Field and Description |
|---|---|
protected java.lang.String |
bindAddr |
protected int |
hostPort |
protected int |
maxRecvSize |
protected java.lang.String |
typeSocket |
DEFAULT_TRAP_PORT| Constructor and Description |
|---|
ListeningContext(int port)
Constructor, using the Standard socket type.
|
ListeningContext(int port,
java.lang.String bindAddress)
Constructor, using the Standard socket type.
|
ListeningContext(int port,
java.lang.String bindAddress,
java.lang.String typeSocketA)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addRawPduListener(RawPduListener listener)
Adds the specified PDU listener to receive the undecoded PDUs.
|
void |
addUnhandledRawPduListener(RawPduListener listener)
Adds the specified PDU listener to receive the undecoded PDUs when
it was not handled (i.e. not consumed) by any of the PDU listeners in
addRawPduListener().
|
void |
destroy()
This method will stop the thread listening for packets.
|
java.lang.String |
getBindAddress()
Returns the local address the server will bind to
When the address is null, the socket accepts connections on
any/all local addresses.
|
java.lang.String |
getHashKey()
Returns the hash key.
|
int |
getMaxRecvSize()
Returns the maximum number of bytes this context will read from the
socket.
|
int |
getPort()
Returns the port number.
|
java.lang.String |
getTypeSocket()
Returns the type of socket.
|
protected void |
processIncomingMessage(java.lang.String hostAddress,
int port,
byte[] bu)
Processes an incoming packet.
|
void |
removeRawPduListener(RawPduListener listener)
Removes the specified PDU listener.
|
void |
removeUnhandledRawPduListener(RawPduListener listener)
Removes the specified unhandled PDU listener.
|
void |
run()
We wait for any incoming PDUs and fire a rawpdu received (undecoded) event
if we do.
|
void |
setMaxRecvSize(int no)
Sets the maximum number of bytes this context will read from the
socket.
|
java.lang.String |
toString()
Returns a string representation of the object.
|
protected int maxRecvSize
protected java.lang.String typeSocket
protected int hostPort
protected java.lang.String bindAddr
public ListeningContext(int port)
port - The local port where packets are receivedSnmpContextBasisFace.STANDARD_SOCKETpublic ListeningContext(int port,
java.lang.String bindAddress)
port - The local port where packets are receivedbindAddress - The local address the server will bind toSnmpContextBasisFace.STANDARD_SOCKETpublic ListeningContext(int port,
java.lang.String bindAddress,
java.lang.String typeSocketA)
Note, the TCP_SOCKET does not provide functionality to send a response back. Listening on such a socket is only useful when listening for traps.
port - The local port where packets are receivedbindAddress - The local address the server will bind totypeSocketA - The type of socket to use.SnmpContextBasisFace.STANDARD_SOCKET,
SnmpContextBasisFace.TCP_SOCKETpublic int getPort()
ListeningContextFacegetPort in interface ListeningContextFacepublic java.lang.String getBindAddress()
ListeningContextFacegetBindAddress in interface ListeningContextFacepublic java.lang.String getTypeSocket()
ListeningContextFacegetTypeSocket in interface ListeningContextFaceSnmpContextBasisFace.STANDARD_SOCKET,
SnmpContextBasisFace.TCP_SOCKETpublic int getMaxRecvSize()
ListeningContextFaceMSS (i.e. 1300).getMaxRecvSize in interface ListeningContextFaceSnmpContextBasisFace.MSS,
ListeningContextFace.setMaxRecvSize(int),
AbstractSnmpContext.setMaxRecvSize(int)public void setMaxRecvSize(int no)
ListeningContextFaceMSS (i.e. 1300).setMaxRecvSize in interface ListeningContextFaceno - The new sizeSnmpContextBasisFace.MSS,
AbstractSnmpContext.getMaxRecvSize()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 that by calling this method the whole stack will stop listening for packets on this particular port! The listeners added via the SnmpContext classes are affected as well.
When you add a new listener, the context will start listening again.
Note: The thread(s) will not die immediately; this will take about half a minute.
destroy in interface ListeningContextFacepublic void run()
The undecoded events are fired to all normal listeners (added via addRawPduListener()), until one of them consumes it. The SnmpContext classes will consume the event if it matches their configuration.
If none of them consume the event, the undecoded events are fired to all unhandled PDU listeners (added via addUnhandledRawPduListener()), until one of them consumes it.
run in interface java.lang.RunnableRawPduReceivedSupport.fireRawPduReceived(int, java.lang.String, int, byte[]),
addRawPduListener(RawPduListener),
addUnhandledRawPduListener(RawPduListener)public void addRawPduListener(RawPduListener listener) throws java.io.IOException
ListeningContextFaceAll the SnmpContext objects use this method to listen for PDUs. When a SnmpContext object decodes the PDU succesfully, it will consume it.
Only when a listener is added will this context create a listening socket.
addRawPduListener in interface ListeningContextFacelistener - The listener objectjava.io.IOException - Thrown when creating a listening
socket failsRawPduReceivedSupport.fireRawPduReceived(int, java.lang.String, int, byte[]),
AbstractSnmpContext.addTrapListener(uk.co.westhawk.snmp.event.TrapListener),
AbstractSnmpContext.addRequestPduListener(uk.co.westhawk.snmp.event.RequestPduListener),
ListeningContextFace.addUnhandledRawPduListener(RawPduListener)public void removeRawPduListener(RawPduListener listener)
ListeningContextFaceremoveRawPduListener in interface ListeningContextFacelistener - The listener objectpublic void addUnhandledRawPduListener(RawPduListener listener) throws java.io.IOException
ListeningContextFaceOnly when a listener is added will this context create a listening socket.
addUnhandledRawPduListener in interface ListeningContextFacelistener - The listener objectjava.io.IOException - Thrown when creating a listening
socket failsListeningContextFace.addRawPduListener(RawPduListener)public void removeUnhandledRawPduListener(RawPduListener listener)
ListeningContextFaceremoveUnhandledRawPduListener in interface ListeningContextFacelistener - The listener objectpublic java.lang.String getHashKey()
public java.lang.String toString()
toString in class java.lang.Objectprotected void processIncomingMessage(java.lang.String hostAddress,
int port,
byte[] bu)
throws DecodingException,
java.io.IOException
DecodingExceptionjava.io.IOExceptionrun()