Class ListeningContext
- All Implemented Interfaces:
Runnable
,ListeningContextFace
- Direct Known Subclasses:
DefaultTrapContext
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.
- Since:
- 4_14
- Version:
- $Revision: 3.12 $ $Date: 2009/03/05 13:24:00 $
- Author:
- Birgit Arkesteijn
- See Also:
-
Field Summary
Fields inherited from interface uk.co.westhawk.snmp.stack.ListeningContextFace
DEFAULT_TRAP_PORT
-
Constructor Summary
ConstructorDescriptionListeningContext
(int port) Constructor, using the Standard socket type.ListeningContext
(int port, String bindAddress) Constructor, using the Standard socket type.ListeningContext
(int port, String bindAddress, String typeSocketA) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
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.void
destroy()
This method will stop the thread listening for packets.Returns the local address the server will bind to When the address is null, the socket accepts connections on any/all local addresses.Returns the hash key.int
Returns the maximum number of bytes this context will read from the socket.int
getPort()
Returns the port number.Returns the type of socket.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.toString()
Returns a string representation of the object.
-
Constructor Details
-
ListeningContext
public ListeningContext(int port) Constructor, using the Standard socket type.- Parameters:
port
- The local port where packets are received- See Also:
-
ListeningContext
Constructor, using the Standard socket type. If bindAddress is null, it will accept connections on any/all local addresses. If you want to listen to- IPv4 only interfaces, use address "0.0.0.0"
- IPv6 only interfaces, use address "::"
- Parameters:
port
- The local port where packets are receivedbindAddress
- The local address the server will bind to- See Also:
-
ListeningContext
Constructor. If bindAddress is null, it will accept connections on any/all local addresses. If you want to listen to- IPv4 only interfaces, use address "0.0.0.0"
- IPv6 only interfaces, use address "::"
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.
- Parameters:
port
- The local port where packets are receivedbindAddress
- The local address the server will bind totypeSocketA
- The type of socket to use.- See Also:
-
-
Method Details
-
getPort
public int getPort()Description copied from interface:ListeningContextFace
Returns the port number.- Specified by:
getPort
in interfaceListeningContextFace
- Returns:
- The port no
-
getBindAddress
Description copied from interface:ListeningContextFace
Returns the local address the server will bind to When the address is null, the socket accepts connections on any/all local addresses.- Specified by:
getBindAddress
in interfaceListeningContextFace
- Returns:
- The bind address
-
getTypeSocket
Description copied from interface:ListeningContextFace
Returns the type of socket.- Specified by:
getTypeSocket
in interfaceListeningContextFace
- Returns:
- The type of socket
- See Also:
-
getMaxRecvSize
public int getMaxRecvSize()Description copied from interface:ListeningContextFace
Returns the maximum number of bytes this context will read from the socket. By default this will be set toMSS
(i.e. 1300).- Specified by:
getMaxRecvSize
in interfaceListeningContextFace
- Returns:
- The number
- See Also:
-
setMaxRecvSize
public void setMaxRecvSize(int no) Description copied from interface:ListeningContextFace
Sets the maximum number of bytes this context will read from the socket. By default this will be set toMSS
(i.e. 1300).- Specified by:
setMaxRecvSize
in interfaceListeningContextFace
- Parameters:
no
- The new size- See Also:
-
destroy
public void destroy()This method will stop the thread listening for packets. 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 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.
- Specified by:
destroy
in interfaceListeningContextFace
-
run
public void run()We wait for any incoming PDUs and fire a rawpdu received (undecoded) event if we do.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.
-
addRawPduListener
Description copied from interface:ListeningContextFace
Adds the specified PDU listener to receive the undecoded PDUs. When a PDU is received the PDU received event is fired to all listeners, until one of them consumes it.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.
- Specified by:
addRawPduListener
in interfaceListeningContextFace
- Parameters:
listener
- The listener object- Throws:
IOException
- Thrown when creating a listening socket fails- See Also:
-
RawPduReceivedSupport.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)
-
removeRawPduListener
Description copied from interface:ListeningContextFace
Removes the specified PDU listener. When there are no more listeners, calls destroy().- Specified by:
removeRawPduListener
in interfaceListeningContextFace
- Parameters:
listener
- The listener object
-
addUnhandledRawPduListener
Description copied from interface:ListeningContextFace
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().Only when a listener is added will this context create a listening socket.
- Specified by:
addUnhandledRawPduListener
in interfaceListeningContextFace
- Parameters:
listener
- The listener object- Throws:
IOException
- Thrown when creating a listening socket fails- See Also:
-
removeUnhandledRawPduListener
Description copied from interface:ListeningContextFace
Removes the specified unhandled PDU listener. When there are no more listeners, calls destroy().- Specified by:
removeUnhandledRawPduListener
in interfaceListeningContextFace
- Parameters:
listener
- The listener object
-
getHashKey
Returns the hash key. This key is built out of all properties. It serves as key for the pool of contexts.- Returns:
- The hash key
-
toString
Returns a string representation of the object.
-