java.lang.Object
org.snmp4j.transport.AbstractTransportMapping<UdpAddress>
org.snmp4j.transport.UdpTransportMapping
org.snmp4j.transport.DefaultUdpTransportMapping
- All Implemented Interfaces:
Closeable
,AutoCloseable
,TransportMapping<UdpAddress>
- Direct Known Subclasses:
DTLSTM
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
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionprotected DefaultUdpTransportMapping.ListenThread
protected DatagramSocket
Fields inherited from class org.snmp4j.transport.UdpTransportMapping
udpAddress
Fields inherited from class org.snmp4j.transport.AbstractTransportMapping
asyncMsgProcessingSupported, connectionTimeout, listenWorkerTask, maxInboundMessageSize, socketCleaner, suspendedAddresses, transportListener, transportStateListeners
-
Constructor Summary
ConstructorDescriptionCreates 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
Modifier and TypeMethodDescriptionvoid
close()
Closes the socket and stops the listener thread.protected DatagramSocket
protected void
fireProcessMessage
(DatagramPacket packet, ByteBuffer bis, TransportStateReference stateReference) Returns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.int
Gets the requested receive buffer size for the underlying UDP socket.int
Returns the socket timeout.void
listen()
Starts the listener thread that accepts incoming messages.protected ByteBuffer
prepareInPacket
(DatagramPacket packet, byte[] buf, TransportStateReference tmStateReference) Prepare a network packet for the application.protected List
<DatagramPacket> prepareOutPackets
(UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference, DatagramSocket socket, long timeoutMillis, int maxRetries) Prepare an application message for sending over the network to the specified target address.protected DatagramSocket
renewSocketAfterException
(SocketException socketException, DatagramSocket failedSocket) If receiving new datagrams fails with aSocketException
, this method is called to renew the socket - if possible.void
sendMessage
(UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) Sends a message to the supplied address using this transport.void
setMaxInboundMessageSize
(int maxInboundMessageSize) void
setReceiveBufferSize
(int receiveBufferSize) Sets the receive buffer size, which should be greater than the maximum inbound message size.void
setSocketTimeout
(int socketTimeout) Sets the socket timeout in milliseconds.Methods inherited from class org.snmp4j.transport.UdpTransportMapping
getAddress, getSupportedAddressClass
Methods inherited from class org.snmp4j.transport.AbstractTransportMapping
addTransportListener, addTransportStateListener, fireConnectionStateChanged, fireProcessMessage, getListenWorkerTask, getMaxInboundMessageSize, getPriority, getSocketCleaner, getSuspendedAddresses, getThreadName, handleDroppedMessageToSend, isAsyncMsgProcessingSupported, isListening, removeAllTransportListeners, removeTransportListener, removeTransportStateListener, resumeAddress, setAsyncMsgProcessingSupported, setPriority, setThreadName, suspendAddress
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
getMaxOutboundMessageSize, getSupportedAddressClasses, getSupportedTransportType, isAddressSupported, isAddressSupported
-
Field Details
-
socket
-
listenerThread
-
-
Constructor Details
-
DefaultUdpTransportMapping
Creates a UDP transport with an arbitrary local port on all local interfaces.- Throws:
SocketException
- if socket binding fails.
-
DefaultUdpTransportMapping
public DefaultUdpTransportMapping(UdpAddress udpAddress, boolean reuseAddress) throws 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
- iftrue
addresses are reused which provides faster socket binding if an application is restarted for instance.- Throws:
SocketException
- if socket binding fails.- Since:
- 1.7.3
-
DefaultUdpTransportMapping
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:
IOException
- if socket binding fails.
-
-
Method Details
-
sendMessage
public void sendMessage(UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) throws IOException Description copied from class:AbstractTransportMapping
Sends a message to the supplied address using this transport. If the target address has been suspended, then instead actually sending the message on the wire, the methodAbstractTransportMapping.handleDroppedMessageToSend(Address, byte[], TransportStateReference, long, int)
will be called. To stop suspending of a target address, callAbstractTransportMapping.resumeAddress(Address)
for that address.- Specified by:
sendMessage
in interfaceTransportMapping<UdpAddress>
- Specified by:
sendMessage
in classUdpTransportMapping
- Parameters:
targetAddress
- anAddress
instance 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 milliseconds the connection creation might take (if connection based).maxRetries
- maximum retries during connection creation.- Throws:
IOException
- if any underlying IO operation fails.
-
prepareOutPackets
protected List<DatagramPacket> prepareOutPackets(UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference, DatagramSocket socket, long timeoutMillis, int maxRetries) throws 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
DatagramPacket
instances. By default this is a singleton list. - Throws:
IOException
- if the preparation of the network message fails (e.g. because the encryption handshake fails).
-
close
Closes the socket and stops the listener thread.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceTransportMapping<UdpAddress>
- Specified by:
close
in classUdpTransportMapping
- Throws:
IOException
- if the socket cannot be closed.
-
listen
Starts the listener thread that accepts incoming messages. The thread is started in daemon mode, and thus it will not block application termination. Nevertheless, theclose()
method should be called to stop the listen thread gracefully and free associated resources.- Specified by:
listen
in interfaceTransportMapping<UdpAddress>
- Specified by:
listen
in classUdpTransportMapping
- Throws:
IOException
- if the listen port could not be bound to the server thread.
-
ensureSocket
- Throws:
SocketException
-
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.
-
getListenAddress
Description copied from interface:TransportMapping
Returns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.- Specified by:
getListenAddress
in interfaceTransportMapping<UdpAddress>
- Overrides:
getListenAddress
in classUdpTransportMapping
- Returns:
- the address for incoming packets or
null
this transport mapping is not configured to listen for incoming packets.
-
renewSocketAfterException
protected DatagramSocket renewSocketAfterException(SocketException socketException, DatagramSocket failedSocket) throws SocketException If 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
null
if the listen thread should be terminated with the provided exception. - Throws:
SocketException
- a new socket exception if the socket could not be renewed.- Since:
- 2.2.2
-
fireProcessMessage
protected void fireProcessMessage(DatagramPacket packet, ByteBuffer bis, TransportStateReference stateReference) -
prepareInPacket
protected ByteBuffer prepareInPacket(DatagramPacket packet, byte[] buf, TransportStateReference tmStateReference) throws IOException Prepare a 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:
IOException
- if there occurs an IO exception during preparation.- Since:
- 3.0
-