SNMP Stack 4_12

uk.co.westhawk.snmp.stack
Class SnmpContextPool

java.lang.Object
  |
  +--uk.co.westhawk.snmp.stack.SnmpContextPool
All Implemented Interfaces:
SnmpContextBasisFace, SnmpContextFace
Direct Known Subclasses:
SnmpContextv2cPool

public class SnmpContextPool
extends java.lang.Object
implements SnmpContextFace

This class contains the pool of SNMP v1 contexts. This class reuses the existings contexts instead of creating a new one everytime.

Everytime a property changes the pool is checked for a SnmpContext context that matches all the new properties of this class. If no such context exists, a new one is made. The Pdus associated with the old context remain associated with the old context.

A counter indicates the number of times the context is referenced. The counter is decreased when destroy is called. When the counter reaches zero, the context is released.

Note that because the underlying context can change when a property is changed and the Pdus remain associated with the old context, all properties have to be set BEFORE a Pdu is send.

Thanks to Seon Lee (slee@virtc.com) for reporting thread safety problems.

Version:
$Revision: 3.14 $ $Date: 2002/11/06 11:27:59 $
Author:
Birgit Arkesteijn
See Also:
SnmpContext, SnmpContextv2cPool, SnmpContextv3Pool

Field Summary
protected  java.lang.String community
           
protected  SnmpContext context
           
protected static java.util.Hashtable contextPool
           
protected  java.lang.String hostAddr
           
protected  int hostPort
           
protected  java.lang.String socketType
           
 
Fields inherited from interface uk.co.westhawk.snmp.stack.SnmpContextFace
version_id
 
Fields inherited from interface uk.co.westhawk.snmp.stack.SnmpContextBasisFace
DEFAULT_PORT, KVM_SOCKET, MAXPDU, MSS, NETSCAPE_SOCKET, STANDARD_SOCKET
 
Constructor Summary
SnmpContextPool(java.lang.String host, int port)
          Constructor.
SnmpContextPool(java.lang.String host, int port, java.lang.String typeSocket)
          Constructor.
SnmpContextPool(java.lang.String host, int port, java.lang.String comm, java.lang.String typeSocket)
          Constructor.
 
Method Summary
 boolean addPdu(Pdu pdu)
          Adds a pdu.
 void addTrapListener(TrapListener l)
          Adds the specified trap listener.
 void destroy()
          Releases the resources held by this context.
 void dumpContexts(java.lang.String title)
          Dumps the pool of contexts.
 byte[] encodePacket(byte msg_type, int rId, int errstat, int errind, java.util.Enumeration ve)
          Encodes a pdu packet.
 java.lang.String getCommunity()
          Returns the community name.
 java.lang.String getHashKey()
          Returns the hash key.
 java.lang.String getHost()
          Returns the host
protected  SnmpContext getMatchingContext()
          Returns a context from the pool.
 int getPort()
          Returns the port number
 java.lang.String getTypeSocket()
          Returns the type socket
 int getVersion()
          Return the SNMP version of the context.
 Pdu processIncomingTrap(byte[] message)
          Processes the incoming trap with the current context.
 boolean removePdu(int requestId)
          Removes a pdu.
 void removeTrapListener(TrapListener l)
          Removes the specified trap listener.
 void sendPacket(byte[] packet)
          Pass a correctly encoded SNMP packet, and we'l send it...
 void setCommunity(java.lang.String newCommunity)
          Sets the community name.
 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

contextPool

protected static java.util.Hashtable contextPool

context

protected SnmpContext context

hostAddr

protected java.lang.String hostAddr

socketType

protected java.lang.String socketType

hostPort

protected int hostPort

community

protected java.lang.String community
Constructor Detail

SnmpContextPool

public SnmpContextPool(java.lang.String host,
                       int port)
                throws java.io.IOException
Constructor.
Parameters:
host - The host to which the Pdu will send
port - The port where the SNMP server will be
See Also:
SnmpContext.SnmpContext(String, int)

SnmpContextPool

public SnmpContextPool(java.lang.String host,
                       int port,
                       java.lang.String typeSocket)
                throws java.io.IOException
Constructor.
Parameters:
host - The host to which the Pdu will send
port - The port where the SNMP server will be
typeSocket - The type of socket to use.
See Also:
SnmpContext.SnmpContext(String, int, String), SnmpContextBasisFace.STANDARD_SOCKET, SnmpContextBasisFace.NETSCAPE_SOCKET, SnmpContextBasisFace.KVM_SOCKET

SnmpContextPool

public SnmpContextPool(java.lang.String host,
                       int port,
                       java.lang.String comm,
                       java.lang.String typeSocket)
                throws java.io.IOException
Constructor.

Thanks to Ernest Jones (EJones@netopia.com) for suggesting this constructor.

Parameters:
host - The host to which the Pdu will send
port - The port where the SNMP server will be
comm - The community name.
typeSocket - The type of socket to use.
Since:
4_12
See Also:
SnmpContextBasisFace.STANDARD_SOCKET, SnmpContextBasisFace.NETSCAPE_SOCKET, SnmpContextBasisFace.KVM_SOCKET
Method Detail

getVersion

public int getVersion()
Return the SNMP version of the context.
Specified by:
getVersion in interface SnmpContextBasisFace
Returns:
The version

getHost

public java.lang.String getHost()
Returns the host
Specified by:
getHost in interface SnmpContextBasisFace
Returns:
The host

getPort

public int getPort()
Returns the port number
Specified by:
getPort in interface SnmpContextBasisFace
Returns:
The port no

getTypeSocket

public java.lang.String getTypeSocket()
Returns the type socket
Specified by:
getTypeSocket in interface SnmpContextBasisFace
Returns:
The type socket

getCommunity

public java.lang.String getCommunity()
Returns the community name.
Specified by:
getCommunity in interface SnmpContextFace
See Also:
SnmpContext.getCommunity()

setCommunity

public void setCommunity(java.lang.String newCommunity)
Sets the community name.
Specified by:
setCommunity in interface SnmpContextFace
See Also:
SnmpContext.setCommunity(String)

addPdu

public boolean addPdu(Pdu pdu)
               throws java.io.IOException,
                      PduException
Adds a pdu.
Specified by:
addPdu in interface SnmpContextBasisFace
Parameters:
pdu - the Pdu
Returns:
whether the pdu has been successfully added

removePdu

public boolean removePdu(int requestId)
Removes a pdu.
Specified by:
removePdu in interface SnmpContextBasisFace
Parameters:
rid - the Pdu request id
Returns:
whether the pdu has been successfully removed

encodePacket

public byte[] encodePacket(byte msg_type,
                           int rId,
                           int errstat,
                           int errind,
                           java.util.Enumeration ve)
                    throws java.io.IOException,
                           EncodingException
Encodes a pdu packet.
Specified by:
encodePacket in interface SnmpContextBasisFace
Following copied from interface: uk.co.westhawk.snmp.stack.SnmpContextBasisFace
Returns:
The encoded packet

sendPacket

public void sendPacket(byte[] packet)
Pass a correctly encoded SNMP packet, and we'l send it...
Specified by:
sendPacket in interface SnmpContextBasisFace
Following copied from interface: uk.co.westhawk.snmp.stack.SnmpContextBasisFace
Parameters:
packet - The encoded packet

destroy

public void destroy()
Releases the resources held by this context. This method will decrement the reference counter. When the reference counter reaches zero the actual context is removed from the pool and destroyed.
Specified by:
destroy in interface SnmpContextBasisFace

getMatchingContext

protected SnmpContext getMatchingContext()
                                  throws java.io.IOException
Returns a context from the pool. This methods checks for an existing context that matches all our properties. If such a context does not exist a new one is created and added to the pool.
Returns:
A context from the pool
See Also:
getHashKey()

dumpContexts

public void dumpContexts(java.lang.String title)
Dumps the pool of contexts. This is for debug purposes.
Parameters:
title - The title of the dump

getHashKey

public java.lang.String getHashKey()
Returns the hash key. This key is built out of all properties. It serves as key for the hashtable of (v1) contexts.
Returns:
The hash key

addTrapListener

public void addTrapListener(TrapListener l)
                     throws java.io.IOException
Adds the specified trap listener. The listener will be added to the current context, not to all the contexts in the hashtable.
Specified by:
addTrapListener in interface SnmpContextBasisFace
See Also:
AbstractSnmpContext.addTrapListener(uk.co.westhawk.snmp.event.TrapListener)

removeTrapListener

public void removeTrapListener(TrapListener l)
                        throws java.io.IOException
Removes the specified trap listener. The listener will be removed from the current context, not from all the contexts in the hashtable.
Specified by:
removeTrapListener in interface SnmpContextBasisFace
See Also:
AbstractSnmpContext.removeTrapListener(uk.co.westhawk.snmp.event.TrapListener)

processIncomingTrap

public Pdu processIncomingTrap(byte[] message)
                        throws DecodingException,
                               java.io.IOException
Processes the incoming trap with the current context.
Specified by:
processIncomingTrap in interface SnmpContextBasisFace
See Also:
SnmpContext.processIncomingTrap(byte[])

toString

public java.lang.String toString()
Returns a string representation of the object.
Overrides:
toString in class java.lang.Object
Returns:
The string

SNMP Stack 4_12