com.adventnet.snmp.snmp2
Class UDPProtocolOptions

java.lang.Object
  |
  +--com.adventnet.snmp.snmp2.UDPProtocolOptions
All Implemented Interfaces:
ProtocolOptions, java.io.Serializable

public class UDPProtocolOptions
extends java.lang.Object
implements ProtocolOptions, java.io.Serializable

This is an implementation of ProtocolOptions class. The default protocol that is used for SNMP communications is UDP.
Now every packet that is sent through SnmpSession will go through the UDP implementation of SnmpTransportProvider and parameters that is needed for such operations should be given through this UDPProtocolOptions.

The UDPProtocolOptions that should be constructed before opening a SnmpSession should look like the following:

UDPProtocolOptions options = new UDPProtocolOptions();

// If the session should be opened on a particular port.
options.setLocalPort(localPort);
// If the session should be opened on particular local_address.
options.setLocalAddress(local_address);

SnmpSession session = new SnmpSession(api);
session.setProtocolOptions(options);
session.open();

Once SnmpSession is opened for SNMP communication, the default values such as remoteHost, remotePort can be set on this UDPProtocolOptions object. If the remoteHost and remotePort is not specified in the SnmpPDU object then the API will take it from the SnmpSession.

For setting the remoteHost and remotePort, the following should be done:
UDPProtocolOptions options = (UDPProtocolOptions)session.getProtocolOptions();
options.setRemoteHost(remoteHost);
options.setRemotePort(remotePort);

See Also:
Serialized Form

Constructor Summary
UDPProtocolOptions()
          The default constructor which will assign default values to all the variables in this class.
UDPProtocolOptions(java.net.InetAddress remoteAddress)
          Constructs a new UDPProtocolOptions object with remoteAddress as target address and 161 as the target port.
UDPProtocolOptions(java.net.InetAddress remoteAddress, int remotePort)
          Constructs a new UDPProtocolOptions object with remoteAddress and remotePort as target host and port.
UDPProtocolOptions(java.lang.String remoteHost)
          Constructs a new UDPProtocolOptions object with remoteHost as target host and 161 as the target port.
UDPProtocolOptions(java.lang.String remoteHost, int remotePort)
          Constructs a new UDPProtocolOptions object with remoteHost and remotePort as target host and port.
 
Method Summary
 java.lang.String[] getLocalAddresses()
          This method gets the local addresses for which the session should bind with.
 int getLocalPort()
          This method returns the local port at which the session should be opened.
 int getReceiveBufferSize()
          This method returns the previously set buffer size or zero.
 java.net.InetAddress getRemoteAddress()
          This method gets the remoteAddress as an InetAddress object.
 java.lang.String getRemoteHost()
          This method returns the remoteHost.
 int getRemotePort()
          This method gets the remotePort to which the SNMP packets should be sent.
 java.lang.String getSessionId()
          This is the method present in ProtocolOptions.
 void setLocalAddresses(java.lang.String[] addrs)
          This method sets the list of local addresses for which the session should bind.
 void setLocalPort(int port)
          This method sets the local port on which the session should be opened.
 void setReceiveBufferSize(int size)
          This method sets the buffer size of this receiver when the receiver is ready for receiving the next packet.
 void setRemoteAddress(java.net.InetAddress addr)
          This method sets the remoteAddress.
 void setRemoteHost(java.lang.String host)
          This method sets the remoteHost to this UDPProtocolOptions object.
 void setRemotePort(int port)
          This method sets the remotePort to which the SNMP packets should be sent.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UDPProtocolOptions

public UDPProtocolOptions()
The default constructor which will assign default values to all the variables in this class.

UDPProtocolOptions

public UDPProtocolOptions(java.lang.String remoteHost,
                          int remotePort)
Constructs a new UDPProtocolOptions object with remoteHost and remotePort as target host and port.
Parameters:
remoteHost - the target host to which a SNMP request is to be sent.
remotePort - the target port to which a SNMP request is to be sent.
Throws:
java.lang.IllegalArgumentException - If remoteHost is null or an empty string.
If the remotePort is less than zero or greater than 65535.

UDPProtocolOptions

public UDPProtocolOptions(java.net.InetAddress remoteAddress,
                          int remotePort)
Constructs a new UDPProtocolOptions object with remoteAddress and remotePort as target host and port.
Parameters:
remoteAddress - the target address to which a SNMP request is to be sent.
remotePort - the target port to which a SNMP request is to be sent.
Throws:
java.lang.IllegalArgumentException - If remoteAddress is null.
If the remotePort is less than zero or greater than 65535.

UDPProtocolOptions

public UDPProtocolOptions(java.lang.String remoteHost)
Constructs a new UDPProtocolOptions object with remoteHost as target host and 161 as the target port.
Parameters:
remoteHost - the target host to which a SNMP request is to be sent.
Throws:
java.lang.IllegalArgumentException - If remoteHost is null or an empty string.

UDPProtocolOptions

public UDPProtocolOptions(java.net.InetAddress remoteAddress)
Constructs a new UDPProtocolOptions object with remoteAddress as target address and 161 as the target port.
Parameters:
remoteAddress - the target address to which a SNMP request is to be sent.
Throws:
java.lang.IllegalArgumentException - If remoteAddress is null.
Method Detail

getSessionId

public java.lang.String getSessionId()
This is the method present in ProtocolOptions. This method will return the string of the form "remoteHostName:remotePort".
Specified by:
getSessionId in interface ProtocolOptions
Returns:
the string which is a combination of "hostname:port".

getRemoteHost

public java.lang.String getRemoteHost()
This method returns the remoteHost.
Returns:
the remoteHost that is previously set or null.

setRemoteHost

public void setRemoteHost(java.lang.String host)
This method sets the remoteHost to this UDPProtocolOptions object.
Parameters:
host - The remoteHost that is to be set.

getRemoteAddress

public java.net.InetAddress getRemoteAddress()
This method gets the remoteAddress as an InetAddress object.
Returns:
the remote address that is set previously.

setRemoteAddress

public void setRemoteAddress(java.net.InetAddress addr)
This method sets the remoteAddress.
Parameters:
addr - the remote inet address.

getLocalPort

public int getLocalPort()
This method returns the local port at which the session should be opened. This method will return the local port only for the UDPProtocolOptions instance that is used to open the session. All other instances will return the value zero.
Returns:
returns the local port at which the session is opened.

setLocalPort

public void setLocalPort(int port)
This method sets the local port on which the session should be opened. For a session to be opened in a particular port, then this local port should be specified and this UDPProtocolOptions instance should be set on the session.
Parameters:
port - the port at which the session should be opened.
Throws:
java.lang.IllegalArgumentException - If the port is negative or greater than 65535.

getLocalAddresses

public java.lang.String[] getLocalAddresses()
This method gets the local addresses for which the session should bind with.
Returns:
a string array containing a list of addresses for which the session has tried to bind.

setLocalAddresses

public void setLocalAddresses(java.lang.String[] addrs)
This method sets the list of local addresses for which the session should bind. The session will bind to the first successfull address and will ignore the rest. If any of the string in this array is given as empty or null it is taken as localhost, since java.net.InetAddress.getByName("") or java.net.InetAddress.getByName(null) will return only localhost.
Parameters:
addrs - the list of addresses to which the session should try binding.

getRemotePort

public int getRemotePort()
This method gets the remotePort to which the SNMP packets should be sent.
Returns:
the remote port to which the SNMP packet should be sent.

setRemotePort

public void setRemotePort(int port)
This method sets the remotePort to which the SNMP packets should be sent.
Parameters:
port - the remote port to which the SNMP packet should be sent.
Throws:
java.lang.IllegalArgumentException - If the port is negative or greater than 65535.

setReceiveBufferSize

public void setReceiveBufferSize(int size)
This method sets the buffer size of this receiver when the receiver is ready for receiving the next packet. It should be called before the "open()" of SnmpSession is called. If it is called after it, then this new value will come into effect only for the next packet that is to be received.

The following should be done to set the receive buffer size in the SnmpTransportProvider.
UDPProtocolOptions opt = (UDPProtocolOptions)session.getProtocolOptions();
if(opt != null)
{
opt.setReceiveBufferSize(size);
}
else
{
UDPProtocolOptions opt = new UDPProtocolOptions();
opt.setReceiveBufferSize(size);
session.setProtocolOptions(opt);
}
Usaually the protocolOptions in the session will not be null, unless session.setProtocolOptions() is called with a null value.

This method will accept only values that are greater than zero. If the value is less than or equal to zero, then an IllegalArgumentException will be thrown. The validity of the value that is passed should be done by the user. Any invalid value that is passed will not be set on the SnmpTransportProvider.

Parameters:
size - the size of the buffer.
Throws:
java.lang.IllegalArgumentException - if the value is 0 or is negative.

getReceiveBufferSize

public int getReceiveBufferSize()
This method returns the previously set buffer size or zero.

The UDPProtocolOptions object that is used to open the SnmpSession will return the present buffer size of the socket.

Returns:
the buffer size of this receiver.


Copyright (c)AdventNet Inc., 1996-2004