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_SOCKET
public ListeningContext(int port, java.lang.String bindAddress)
port
- The local port where packets are receivedbindAddress
- The local address the server will bind toSnmpContextBasisFace.STANDARD_SOCKET
public 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_SOCKET
public int getPort()
ListeningContextFace
getPort
in interface ListeningContextFace
public java.lang.String getBindAddress()
ListeningContextFace
getBindAddress
in interface ListeningContextFace
public java.lang.String getTypeSocket()
ListeningContextFace
getTypeSocket
in interface ListeningContextFace
SnmpContextBasisFace.STANDARD_SOCKET
,
SnmpContextBasisFace.TCP_SOCKET
public int getMaxRecvSize()
ListeningContextFace
MSS
(i.e. 1300).getMaxRecvSize
in interface ListeningContextFace
SnmpContextBasisFace.MSS
,
ListeningContextFace.setMaxRecvSize(int)
,
AbstractSnmpContext.setMaxRecvSize(int)
public void setMaxRecvSize(int no)
ListeningContextFace
MSS
(i.e. 1300).setMaxRecvSize
in interface ListeningContextFace
no
- 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 ListeningContextFace
public 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.Runnable
RawPduReceivedSupport.fireRawPduReceived(int, java.lang.String, int, byte[])
,
addRawPduListener(RawPduListener)
,
addUnhandledRawPduListener(RawPduListener)
public void addRawPduListener(RawPduListener listener) throws java.io.IOException
ListeningContextFace
All 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 ListeningContextFace
listener
- 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)
ListeningContextFace
removeRawPduListener
in interface ListeningContextFace
listener
- The listener objectpublic void addUnhandledRawPduListener(RawPduListener listener) throws java.io.IOException
ListeningContextFace
Only when a listener is added will this context create a listening socket.
addUnhandledRawPduListener
in interface ListeningContextFace
listener
- The listener objectjava.io.IOException
- Thrown when creating a listening
socket failsListeningContextFace.addRawPduListener(RawPduListener)
public void removeUnhandledRawPduListener(RawPduListener listener)
ListeningContextFace
removeUnhandledRawPduListener
in interface ListeningContextFace
listener
- The listener objectpublic java.lang.String getHashKey()
public java.lang.String toString()
toString
in class java.lang.Object
protected void processIncomingMessage(java.lang.String hostAddress, int port, byte[] bu) throws DecodingException, java.io.IOException
DecodingException
java.io.IOException
run()