- java.lang.Object
-
- org.snmp4j.transport.AbstractTransportMapping<UdpAddress>
-
- org.snmp4j.transport.UdpTransportMapping
-
- org.snmp4j.transport.DefaultUdpTransportMapping
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,TransportMapping<UdpAddress>
- Direct Known Subclasses:
DTLSTM
public class DefaultUdpTransportMapping extends UdpTransportMapping
TheDefaultUdpTransportMappingimplements 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classDefaultUdpTransportMapping.ListenThread
-
Field Summary
Fields Modifier and Type Field Description protected WorkerTasklistenerprotected DefaultUdpTransportMapping.ListenThreadlistenerThreadprotected java.net.DatagramSocketsocket-
Fields inherited from class org.snmp4j.transport.AbstractTransportMapping
asyncMsgProcessingSupported, maxInboundMessageSize, transportListener
-
Fields inherited from class org.snmp4j.transport.UdpTransportMapping
udpAddress
-
-
Constructor Summary
Constructors Constructor Description 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the socket and stops the listener thread.protected java.net.DatagramSocketensureSocket()protected voidfireProcessMessage(java.net.DatagramPacket packet, java.nio.ByteBuffer bis, TransportStateReference stateReference)UdpAddressgetListenAddress()Returns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.intgetPriority()Returns the priority of the internal listen thread.intgetReceiveBufferSize()Gets the requested receive buffer size for the underlying UDP socket.intgetSocketTimeout()Returns the socket timeout.java.lang.StringgetThreadName()Returns the name of the listen thread.booleanisListening()Returnstrueif the transport mapping is listening for incoming messages.voidlisten()Starts the listener thread that accepts incoming messages.protected java.nio.ByteBufferprepareInPacket(java.net.DatagramPacket packet, byte[] buf, TransportStateReference tmStateReference)Prepare an network packet for the application.protected java.util.List<java.net.DatagramPacket>prepareOutPackets(UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference, java.net.DatagramSocket socket, long timeoutMillis, int maxRetries)Prepare an application message for sending over the network to the specified target address.protected java.net.DatagramSocketrenewSocketAfterException(java.net.SocketException socketException, java.net.DatagramSocket failedSocket)If receiving new datagrams fails with aSocketException, this method is called to renew the socket - if possible.voidsendMessage(UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries)Sends a message to the supplied address using this transport.voidsetMaxInboundMessageSize(int maxInboundMessageSize)voidsetPriority(int newPriority)Changes the priority of the listen thread for this UDP transport mapping.voidsetReceiveBufferSize(int receiveBufferSize)Sets the receive buffer size, which should be greater than the maximum inbound message size.voidsetSocketTimeout(int socketTimeout)Sets the socket timeout in milliseconds.voidsetThreadName(java.lang.String name)Sets the name of the listen thread for this UDP transport mapping.-
Methods inherited from class org.snmp4j.transport.AbstractTransportMapping
addTransportListener, fireProcessMessage, getMaxInboundMessageSize, isAsyncMsgProcessingSupported, removeTransportListener, setAsyncMsgProcessingSupported
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.snmp4j.TransportMapping
getSupportedAddressClasses, getSupportedTransportType, isAddressSupported, isAddressSupported
-
Methods inherited from class org.snmp4j.transport.UdpTransportMapping
getAddress, getSupportedAddressClass
-
-
-
-
Field Detail
-
socket
protected java.net.DatagramSocket socket
-
listener
protected WorkerTask listener
-
listenerThread
protected DefaultUdpTransportMapping.ListenThread listenerThread
-
-
Constructor Detail
-
DefaultUdpTransportMapping
public DefaultUdpTransportMapping() throws java.net.SocketExceptionCreates a UDP transport with an arbitrary local port on all local interfaces.- Throws:
java.net.SocketException- if socket binding fails.
-
DefaultUdpTransportMapping
public DefaultUdpTransportMapping(UdpAddress udpAddress, boolean reuseAddress) throws java.net.SocketException
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- iftrueaddresses are reused which provides faster socket binding if an application is restarted for instance.- Throws:
java.net.SocketException- 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(UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) throws java.io.IOException
Description copied from interface:TransportMappingSends a message to the supplied address using this transport.- Specified by:
sendMessagein interfaceTransportMapping<UdpAddress>- Specified by:
sendMessagein classUdpTransportMapping- Parameters:
targetAddress- anAddressinstance denoting the target address.message- the whole message as an array of bytes.tmStateReference- the (optional) transport model state reference as defined by RFC 5590 section 6.1.timeoutMillis- maximum number of milli seconds the connection creation might take (if connection based).maxRetries- maximum retries during connection creation.- Throws:
java.io.IOException- if any underlying IO operation fails.
-
prepareOutPackets
protected java.util.List<java.net.DatagramPacket> prepareOutPackets(UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference, java.net.DatagramSocket socket, long timeoutMillis, int maxRetries) throws java.io.IOException
Prepare an application message for sending over the network to the specified target address.- Parameters:
targetAddress- the UDP address the message will be sent to.message- the application message to send.tmStateReference- the transport state reference associated with this message.socket- the socket that will send the message over the network. @return an ByteBuffer that contains the network representation of the message (i.e. encrypted).timeoutMillis- maximum number of milli seconds the connection creation might take (if connection based). Use 0 for responses or transport mappings that do not require connection establishment.maxRetries- maximum retries during connection creation. Use 0 for responses.- Returns:
- a list of prepared
DatagramPacketinstances. By default this is a singleton list. - Throws:
java.io.IOException- if the preparation of the network message fails (e.g. because the encryption handshake fails).
-
close
public void close() throws java.io.IOExceptionCloses the socket and stops the listener thread.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfaceTransportMapping<UdpAddress>- Specified by:
closein classUdpTransportMapping- Throws:
java.io.IOException- if the socket cannot be closed.
-
listen
public void listen() throws java.io.IOExceptionStarts the listener thread that accepts incoming messages. The thread is started in daemon mode and thus it will not block application terminated. Nevertheless, theclose()method should be called to stop the listen thread gracefully and free associated resources.- Specified by:
listenin interfaceTransportMapping<UdpAddress>- Specified by:
listenin classUdpTransportMapping- Throws:
java.io.IOException- if the listen port could not be bound to the server thread.
-
ensureSocket
protected java.net.DatagramSocket ensureSocket() throws java.net.SocketException- Throws:
java.net.SocketException
-
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 beforelisten()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_PRIORITYandThread.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 beforelisten()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 greater than the maximum inbound message size. This method has to be called beforelisten()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:TransportMappingReturnstrueif 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:
trueif this transport mapping is listening for messages.
-
getListenAddress
public UdpAddress getListenAddress()
Description copied from interface:TransportMappingReturns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.- Specified by:
getListenAddressin interfaceTransportMapping<UdpAddress>- Overrides:
getListenAddressin classUdpTransportMapping- Returns:
- the address for incoming packets or
nullthis transport mapping is not configured to listen for incoming packets.
-
renewSocketAfterException
protected java.net.DatagramSocket renewSocketAfterException(java.net.SocketException socketException, java.net.DatagramSocket failedSocket) throws java.net.SocketExceptionIf receiving new datagrams fails with aSocketException, this method is called to renew the socket - if possible.- Parameters:
socketException- the exception that occurred.failedSocket- the socket that caused the exception. By default, he socket will be closed in order to be able to reopen it. Implementations may also try to reuse the socket, in dependence of thesocketException.- Returns:
- the new socket or
nullif the listen thread should be terminated with the provided exception. - Throws:
java.net.SocketException- a new socket exception if the socket could not be renewed.- Since:
- 2.2.2
-
fireProcessMessage
protected void fireProcessMessage(java.net.DatagramPacket packet, java.nio.ByteBuffer bis, TransportStateReference stateReference)
-
prepareInPacket
protected java.nio.ByteBuffer prepareInPacket(java.net.DatagramPacket packet, byte[] buf, TransportStateReference tmStateReference) throws java.io.IOExceptionPrepare an network packet for the application.- Parameters:
packet- the incoming network datagram packet.buf- the buffer of the packet.tmStateReference- the transport state reference.- Returns:
- a byte buffer with the application data of the packet.
- Throws:
java.io.IOException- if there occurs an IO exception during preparation.- Since:
- 3.0
-
-