|
SNMP Stack 5_1 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--uk.co.westhawk.snmp.stack.ListeningContext
The ListeningContext class will enable this stack to receive packets. This class replaces the deprecated DefaultTrapContext class. The context will only start receiving (or listen for) packets when there is at least one listener registered.
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)| Field Summary | |
protected java.lang.String |
bindAddr
|
protected int |
hostPort
|
protected int |
maxRecvSize
|
protected java.lang.String |
typeSocket
|
| Fields inherited from interface uk.co.westhawk.snmp.stack.ListeningContextFace |
DEFAULT_TRAP_PORT, version_id |
| Constructor Summary | |
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. |
|
| Method Summary | |
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. |
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. |
protected void |
startListening()
Creates the socket and starts listening for PDUs if we didn't do so already. |
java.lang.String |
toString()
Returns a string representation of the object. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
protected int maxRecvSize
protected java.lang.String typeSocket
protected int hostPort
protected java.lang.String bindAddr
| Constructor Detail |
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,
SnmpContextBasisFace.NETSCAPE_SOCKET,
SnmpContextBasisFace.KVM_SOCKET| Method Detail |
public int getPort()
ListeningContextFace
getPort in interface ListeningContextFacepublic java.lang.String getBindAddress()
ListeningContextFace
getBindAddress in interface ListeningContextFacepublic java.lang.String getTypeSocket()
ListeningContextFace
getTypeSocket in interface ListeningContextFaceSnmpContextBasisFace.STANDARD_SOCKET,
SnmpContextBasisFace.TCP_SOCKET,
SnmpContextBasisFace.NETSCAPE_SOCKET,
SnmpContextBasisFace.KVM_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 object
java.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 ListeningContextFacelistener - The listener object
public void addUnhandledRawPduListener(RawPduListener listener)
throws java.io.IOException
ListeningContextFaceOnly when a listener is added will this context create a listening socket.
addUnhandledRawPduListener in interface ListeningContextFacejava.io.IOException - Thrown when creating a listening
socket failsListeningContextFace.addRawPduListener(RawPduListener)public void removeUnhandledRawPduListener(RawPduListener listener)
ListeningContextFace
removeUnhandledRawPduListener in interface ListeningContextFace
protected void startListening()
throws java.io.IOException
java.io.IOException - Thrown when the socket cannot be created.addRawPduListener(uk.co.westhawk.snmp.event.RawPduListener),
addUnhandledRawPduListener(uk.co.westhawk.snmp.event.RawPduListener)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.IOExceptionrun()
|
SNMP Stack 5_1 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||