SNMP Stack 4_12

uk.co.westhawk.snmp.stack
Class DefaultTrapContext

java.lang.Object
  |
  +--uk.co.westhawk.snmp.stack.DefaultTrapContext
All Implemented Interfaces:
java.lang.Runnable

public class DefaultTrapContext
extends java.lang.Object
implements java.lang.Runnable

The DefaultTrapContext class will enable this stack to receive traps. Only one (1) instance of the DefaultTrapContext can exist. The context will only start receiving (or listen for) traps when there is at least one listener registered. Two kind of listeners can be added; the normal and unhandled trap listeners. The normal trap listeners are added via the addTrapListener() method, the unhandled trap listeners are added via the addUnhandledTrapListener().

Use one of the getInstance() methods to get the instance and add a trap listener. This class will fire undecoded trap events, i.e. the raw data is sent and no attempt is made to decode the data into a pdu.

The SnmpContext classes provide functionality for decoded trap events. These classes will register themselves to the DefaultTrapContext object and only pass the event on if it matches their configuration.

Note that because only one instance of this class can exist, the first call of getInstance() will define the settings (i.e. port number and socket type) for the lifetime of the stack. All the subsequent calls of getInstance() will return the existing instance, irrespective of the arguments.

On UNIX and Linux operating systems the default port where trap are send (i.e. 162) can only be opened as root.

Version:
$Revision: 3.7 $ $Date: 2002/11/04 17:49:48 $
Author:
Birgit Arkesteijn
See Also:
AbstractSnmpContext.addTrapListener(uk.co.westhawk.snmp.event.TrapListener)

Field Summary
static int DEFAULT_TRAP_PORT
          The default port number where we listen for traps.
protected  int maxRecvSize
           
 
Constructor Summary
protected DefaultTrapContext(int port)
          Constructor.
protected DefaultTrapContext(int port, java.lang.String typeSocketA)
          Constructor.
 
Method Summary
 void addTrapListener(TrapListener listener)
          Adds the specified trap listener to receive the undecoded traps.
 void addUnhandledTrapListener(TrapListener listener)
          Adds the specified trap listener to receive the undecoded traps when it was not handled (ie not consumed) by any of the trap listeners in addTrapListener().
 void destroy()
          This method will stop the thread listening for Traps.
static DefaultTrapContext getInstance(int port)
          Returns the instance of DefaultTrapContext.
static DefaultTrapContext getInstance(int port, java.lang.String typeSocketA)
          Returns the instance of DefaultTrapContext.
 int getMaxRecvSize()
          Returns the maximum number of bytes this context will read from the socket.
 void removeTrapListener(TrapListener listener)
          Removes the specified trap listener.
 void removeUnhandledTrapListener(TrapListener listener)
          Removes the specified unhandled trap listener.
 void run()
          We wait for any incoming traps and fire a trap 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()
          Starts listening for traps if we didn't do so already.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_TRAP_PORT

public static final int DEFAULT_TRAP_PORT
The default port number where we listen for traps.

maxRecvSize

protected int maxRecvSize
Constructor Detail

DefaultTrapContext

protected DefaultTrapContext(int port)
                      throws java.io.IOException
Constructor. The Standard socket type will be used.
Parameters:
port - The local port where traps are received
See Also:
SnmpContextBasisFace.STANDARD_SOCKET

DefaultTrapContext

protected DefaultTrapContext(int port,
                             java.lang.String typeSocketA)
                      throws java.io.IOException
Constructor. The typeSocket will indicate which type of socket to use. This way different handlers can be provided for Netscape or Standard JVM. The Netscape implementation will make the necessary security calls to access connections that are not the applet's webserver. The KVM version will be for small device support (e.g. Palm Pilot).
Parameters:
port - The local port where traps are received
typeSocketA - The type of socket to use.
See Also:
SnmpContextBasisFace.STANDARD_SOCKET, SnmpContextBasisFace.NETSCAPE_SOCKET, SnmpContextBasisFace.KVM_SOCKET
Method Detail

getInstance

public static DefaultTrapContext getInstance(int port)
                                      throws java.io.IOException
Returns the instance of DefaultTrapContext. It will create the instance if it didn't exists. See the note above.

getInstance

public static DefaultTrapContext getInstance(int port,
                                             java.lang.String typeSocketA)
                                      throws java.io.IOException
Returns the instance of DefaultTrapContext. It will create the instance if it didn't exists. See the note above.

startListening

protected void startListening()
Starts listening for traps if we didn't do so already.
See Also:
addTrapListener(uk.co.westhawk.snmp.event.TrapListener)

getMaxRecvSize

public int getMaxRecvSize()
Returns the maximum number of bytes this context will read from the socket. By default this will be set to MSS (i.e. 1300).
Returns:
The number
Since:
4_12
See Also:
SnmpContextBasisFace.MSS, setMaxRecvSize(int), AbstractSnmpContext.setMaxRecvSize(int)

setMaxRecvSize

public void setMaxRecvSize(int no)
Sets the maximum number of bytes this context will read from the socket. By default this will be set to MSS (i.e. 1300).
Parameters:
no - The new number
Since:
4_12
See Also:
SnmpContextBasisFace.MSS, AbstractSnmpContext.getMaxRecvSize()

destroy

public void destroy()
This method will stop the thread listening for Traps. It closes the socket. The traplisteners are removed just before the run() finishes.

Note that by calling this method the whole stack will stop listening for traps! The listeners added via the SnmpContext classes are effected as well.

Thanks to Balakrishnan (bala_tbn@yahoo.com) for pointing out that there was no way of stopping the listener.


run

public void run()
We wait for any incoming traps and fire a trap received (undecoded) event if we do.

The undecoded events are fired to all normal listeners (added via addTrapListener()), unless 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 trap listeners (added via addUnhandledTrapListener()), unless one of them consumes it.

Specified by:
run in interface java.lang.Runnable
See Also:
TrapReceivedSupport.fireTrapReceived(int, String, byte []), addTrapListener(TrapListener), addUnhandledTrapListener(TrapListener)

addTrapListener

public void addTrapListener(TrapListener listener)
Adds the specified trap listener to receive the undecoded traps. When a trap is received the trap received event is fired to all listeners, unless one of them consumes it.

All the SnmpContext objects use this method to listen for traps. When a SnmpContext object decodes the trap succesfully, it will consume it.

See Also:
TrapReceivedSupport.fireTrapReceived(int, String, byte []), AbstractSnmpContext.addTrapListener(TrapListener), addUnhandledTrapListener(TrapListener)

removeTrapListener

public void removeTrapListener(TrapListener listener)
Removes the specified trap listener.

addUnhandledTrapListener

public void addUnhandledTrapListener(TrapListener listener)
Adds the specified trap listener to receive the undecoded traps when it was not handled (ie not consumed) by any of the trap listeners in addTrapListener().
See Also:
addTrapListener(TrapListener)

removeUnhandledTrapListener

public void removeUnhandledTrapListener(TrapListener listener)
Removes the specified unhandled trap listener.

SNMP Stack 4_12