org.snmp4j.transport
Class DefaultUdpTransportMapping

java.lang.Object
  extended by org.snmp4j.transport.AbstractTransportMapping
      extended by org.snmp4j.transport.UdpTransportMapping
          extended by org.snmp4j.transport.DefaultUdpTransportMapping
All Implemented Interfaces:
TransportMapping

public class DefaultUdpTransportMapping
extends UdpTransportMapping

The DefaultUdpTransportMapping implements a UDP transport mapping based on Java standard IO and using an internal thread for listening on the inbound socket.

Version:
1.9
Author:
Frank Fock

Field Summary
protected  WorkerTask listener
           
protected  org.snmp4j.transport.DefaultUdpTransportMapping.ListenThread listenerThread
           
protected  java.net.DatagramSocket socket
           
 
Fields inherited from class org.snmp4j.transport.UdpTransportMapping
udpAddress
 
Fields inherited from class org.snmp4j.transport.AbstractTransportMapping
asyncMsgProcessingSupported, maxInboundMessageSize, transportListener
 
Constructor Summary
DefaultUdpTransportMapping()
          Creates a UDP transport with an arbitrary local port on all local interfaces.
DefaultUdpTransportMapping(UdpAddress udpAddress)
          Creates a UDP transport on the specified address.
DefaultUdpTransportMapping(UdpAddress udpAddress, boolean reuseAddress)
          Creates a UDP transport with optional reusing the address if is currently in timeout state (TIME_WAIT) after the connection is closed.
 
Method Summary
 void close()
          Closes the socket and stops the listener thread.
 int getPriority()
          Returns the priority of the internal listen thread.
 int getReceiveBufferSize()
          Gets the requested receive buffer size for the underlying UDP socket.
 int getSocketTimeout()
          Returns the socket timeout.
 java.lang.String getThreadName()
          Returns the name of the listen thread.
 boolean isListening()
          Returns true if the transport mapping is listening for incoming messages.
 void listen()
          Starts the listener thread that accepts incoming messages.
 void sendMessage(Address targetAddress, byte[] message)
          Sends a message to the supplied address using this transport.
 void setMaxInboundMessageSize(int maxInboundMessageSize)
           
 void setPriority(int newPriority)
          Changes the priority of the listen thread for this UDP transport mapping.
 void setReceiveBufferSize(int receiveBufferSize)
          Sets the receive buffer size, which should be > the maximum inbound message size.
 void setSocketTimeout(int socketTimeout)
          Sets the socket timeout in milliseconds.
 void setThreadName(java.lang.String name)
          Sets the name of the listen thread for this UDP transport mapping.
 
Methods inherited from class org.snmp4j.transport.UdpTransportMapping
getAddress, getListenAddress, getSupportedAddressClass
 
Methods inherited from class org.snmp4j.transport.AbstractTransportMapping
addMessageDispatcher, addTransportListener, fireProcessMessage, getMaxInboundMessageSize, isAsyncMsgProcessingSupported, removeMessageDispatcher, removeTransportListener, setAsyncMsgProcessingSupported
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

socket

protected java.net.DatagramSocket socket

listener

protected WorkerTask listener

listenerThread

protected org.snmp4j.transport.DefaultUdpTransportMapping.ListenThread listenerThread
Constructor Detail

DefaultUdpTransportMapping

public DefaultUdpTransportMapping()
                           throws java.io.IOException
Creates a UDP transport with an arbitrary local port on all local interfaces.

Throws:
java.io.IOException - if socket binding fails.

DefaultUdpTransportMapping

public DefaultUdpTransportMapping(UdpAddress udpAddress,
                                  boolean reuseAddress)
                           throws java.io.IOException
Creates a UDP transport with optional reusing the address if is currently in timeout state (TIME_WAIT) after the connection is closed.

Parameters:
udpAddress - the local address for sending and receiving of UDP messages.
reuseAddress - if true addresses are reused which provides faster socket binding if an application is restarted for instance.
Throws:
java.io.IOException - if socket binding fails.
Since:
1.7.3

DefaultUdpTransportMapping

public DefaultUdpTransportMapping(UdpAddress udpAddress)
                           throws java.io.IOException
Creates a UDP transport on the specified address. The address will not be reused if it is currently in timeout state (TIME_WAIT).

Parameters:
udpAddress - the local address for sending and receiving of UDP messages.
Throws:
java.io.IOException - if socket binding fails.
Method Detail

sendMessage

public void sendMessage(Address targetAddress,
                        byte[] message)
                 throws java.io.IOException
Description copied from interface: TransportMapping
Sends a message to the supplied address using this transport.

Specified by:
sendMessage in interface TransportMapping
Specified by:
sendMessage in class UdpTransportMapping
Parameters:
targetAddress - an Address instance denoting the target address.
message - the whole message as an array of bytes.
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Closes the socket and stops the listener thread.

Specified by:
close in interface TransportMapping
Specified by:
close in class UdpTransportMapping
Throws:
java.io.IOException

listen

public void listen()
            throws java.io.IOException
Starts the listener thread that accepts incoming messages. The thread is started in daemon mode and thus it will not block application terminated. Nevertheless, the close() method should be called to stop the listen thread gracefully and free associated ressources.

Specified by:
listen in interface TransportMapping
Specified by:
listen in class UdpTransportMapping
Throws:
java.io.IOException

setPriority

public void setPriority(int newPriority)
Changes the priority of the listen thread for this UDP transport mapping. This method has no effect, if called before listen() has been called for this transport mapping.

Parameters:
newPriority - the new priority.
Since:
1.2.2
See Also:
Thread.setPriority(int)

getPriority

public int getPriority()
Returns the priority of the internal listen thread.

Returns:
a value between Thread.MIN_PRIORITY and Thread.MAX_PRIORITY.
Since:
1.2.2

setThreadName

public void setThreadName(java.lang.String name)
Sets the name of the listen thread for this UDP transport mapping. This method has no effect, if called before listen() has been called for this transport mapping.

Parameters:
name - the new thread name.
Since:
1.6

getThreadName

public java.lang.String getThreadName()
Returns the name of the listen thread.

Returns:
the thread name if in listening mode, otherwise null.
Since:
1.6

setMaxInboundMessageSize

public void setMaxInboundMessageSize(int maxInboundMessageSize)

getSocketTimeout

public int getSocketTimeout()
Returns the socket timeout. 0 returns implies that the option is disabled (i.e., timeout of infinity).

Returns:
the socket timeout setting.

getReceiveBufferSize

public int getReceiveBufferSize()
Gets the requested receive buffer size for the underlying UDP socket. This size might not reflect the actual size of the receive buffer, which is implementation specific.

Returns:
<=0 if the default buffer size of the OS is used, or a value >0 if the user specified a buffer size.

setReceiveBufferSize

public void setReceiveBufferSize(int receiveBufferSize)
Sets the receive buffer size, which should be > the maximum inbound message size. This method has to be called before listen() to be effective.

Parameters:
receiveBufferSize - an integer value >0 and > AbstractTransportMapping.getMaxInboundMessageSize().

setSocketTimeout

public void setSocketTimeout(int socketTimeout)
Sets the socket timeout in milliseconds.

Parameters:
socketTimeout - the socket timeout for incoming messages in milliseconds. A timeout of zero is interpreted as an infinite timeout.

isListening

public boolean isListening()
Description copied from interface: TransportMapping
Returns true if the transport mapping is listening for incoming messages. For connection oriented transport mappings this is a prerequisite to be able to send SNMP messages. For connectionless transport mappings it is a prerequisite to be able to receive responses.

Returns:
true if this transport mapping is listening for messages.

Copyright 2005-2010 Frank Fock (SNMP4J.org)

Copyright © 2011 SNMP4J.org. All Rights Reserved.