com.sun.jaw.snmp.manager
Class SnmpSocket

java.lang.Object
  |
  +--com.sun.jaw.snmp.manager.SnmpSocket

public final class SnmpSocket
extends java.lang.Object
implements java.lang.Runnable

This class creates a SNMP Datagram Socket. This class has methods helpful to send SNMP packets to an arbitrary port of a specified device. It also runs a thread that is devoted to receiving messages on the socket. It marks a socket as a default socket. All operations are directed to the this default socket.
A user can create a new socket and use it exclusively for certain operations.
A socket imposes an upper limit on size of response packet. Any packet which exceeds this limit is truncated. By default, this limit is SnmpConst.defaultSnmpResponsePktSize. It can be changed using setResponsePktSize(int).


Constructor Summary
SnmpSocket(DatagramHandlerIf rspHdlr)
          Creates a new SnmpSocket object.
 
Method Summary
 void close()
          Closes the socket and its associated resources.
static SnmpSocket createNewDefaultSnmpSocket()
          Creates a new SnmpSocket object.
 void deregister(java.lang.Object obj)
          Removes the specified object to its list indicating that the object is using the socket.
 void finalize()
          Closes the socket and its associated resources.
 java.util.Enumeration getAllRegisteredUsers()
          Gets a list of objects (mostly SnmpSession objects), which have registered to use this socket.
static SnmpSocket getDefaultSnmpSocket()
          Gets the default socket object.
 int getInPkts()
          Gets the number of packets received.
 int getOutPkts()
          Gets the number of packets sent.
 int getPktsErrors()
          Gets the number of errors that occured.
 int getResponsePktSize()
          Get the maximum size allowed for response packet.
 int getUserCount()
          Gets the number of objects using this socket.
 boolean isValid()
          Checks if the socket is initialised correctly and if it is still active.
 void performResetPktStatistics()
          Reset all the counters.
 void register(java.lang.Object obj)
          Adds the specified object to its list indicating that the object is using the socket.
 void run()
          Dispatcher method for this socket thread.
 void sendPacket(byte[] buff, int length, java.net.InetAddress addr, int port)
          Sends a datagram packet to a specified device at specified port.
 void sendPacket(java.net.DatagramPacket dgrmpkt)
          Sends a datagram packet to a specified device at specified port.
 void setDefaultSnmpSocket()
          Defines the current socket as being the default one.
 void setResponsePktSize(int size)
          Set the maximum size allowed for response packet.
 java.lang.String toString()
          Returns the detailed statistics of the socket and activity stats.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SnmpSocket

public SnmpSocket(DatagramHandlerIf rspHdlr)
           throws java.net.SocketException
Creates a new SnmpSocket object.
Parameters:
rspHdlr - A Datagram handler.
Throws:
java.net.SocketException - A socket could not be created.
Method Detail

createNewDefaultSnmpSocket

public static final SnmpSocket createNewDefaultSnmpSocket()
                                                   throws java.net.SocketException
Creates a new SnmpSocket object.
Returns:
The new SnmpSocket.
Throws:
java.net.SocketException - A socket could not be created.

getDefaultSnmpSocket

public static final SnmpSocket getDefaultSnmpSocket()
Gets the default socket object.
Returns:
The default socket object.

setDefaultSnmpSocket

public final void setDefaultSnmpSocket()
                                throws java.net.SocketException
Defines the current socket as being the default one.
Throws:
java.net.SocketException - A socket could not be created.

getPktsErrors

public final int getPktsErrors()
Gets the number of errors that occured.
Returns:
The number of errors that occured.

getInPkts

public final int getInPkts()
Gets the number of packets received.
Returns:
The number of packets received.

getOutPkts

public final int getOutPkts()
Gets the number of packets sent.
Returns:
The number of packets sent.

performResetPktStatistics

public final void performResetPktStatistics()
Reset all the counters.

getAllRegisteredUsers

public final java.util.Enumeration getAllRegisteredUsers()
Gets a list of objects (mostly SnmpSession objects), which have registered to use this socket.
Returns:
Enumeration which gives a list of all users using this socket.

getUserCount

public final int getUserCount()
Gets the number of objects using this socket.
Returns:
The count of objects using this socket.

toString

public java.lang.String toString()
Returns the detailed statistics of the socket and activity stats.
Returns:
The detailed statistics of the socket and activity stats.
Overrides:
toString in class java.lang.Object

register

public final void register(java.lang.Object obj)
Adds the specified object to its list indicating that the object is using the socket. This method is similar to reference counting.
Parameters:
obj - The object to add.

deregister

public final void deregister(java.lang.Object obj)
Removes the specified object to its list indicating that the object is using the socket.
Parameters:
obj - The object to be removed.

getResponsePktSize

public final int getResponsePktSize()
Get the maximum size allowed for response packet. Any packet which exceeds this limit will be truncated. The default value is SnmpConst.defaultSnmpResponsePktSize.

setResponsePktSize

public final void setResponsePktSize(int size)
Set the maximum size allowed for response packet.

sendPacket

public void sendPacket(byte[] buff,
                       int length,
                       java.net.InetAddress addr,
                       int port)
                throws java.io.IOException
Sends a datagram packet to a specified device at specified port. It also updates packet statistics.
Parameters:
buff - The packet data.
length - The packet length.
addr - The destination address.
port - The destination port number.
Throws:
java.io.IOException - Signals that an I/O exception of some sort has occurred.

sendPacket

public void sendPacket(java.net.DatagramPacket dgrmpkt)
                throws java.io.IOException
Sends a datagram packet to a specified device at specified port. It also updates packet statistics.
Parameters:
dgrmpkt - The datagram packet.
Throws:
java.io.IOException - Signals that an I/O exception of some sort has occurred.

close

public void close()
Closes the socket and its associated resources.

Note: the default socket can not be closed directly through a call to close.


isValid

public boolean isValid()
Checks if the socket is initialised correctly and if it is still active.
Returns:
True if the socket is initialised correctly and if it is still active; false otherwise.

run

public void run()
Dispatcher method for this socket thread. This is the dispatcher method which goes in an endless-loop and waits for receiving datagram packets on the socket.
Specified by:
run in interface java.lang.Runnable

finalize

public void finalize()
Closes the socket and its associated resources.
Overrides:
finalize in class java.lang.Object