java.lang.Object
org.snmp4j.transport.AbstractTransportMapping<TcpAddress>
org.snmp4j.transport.AbstractConnectionOrientedTransportMapping<TcpAddress,DefaultTcpTransportMapping.SocketEntry>
org.snmp4j.transport.TcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>
org.snmp4j.transport.DefaultTcpTransportMapping
- All Implemented Interfaces:
Closeable
,AutoCloseable
,ConnectionOrientedTransportMapping<TcpAddress>
,TransportMapping<TcpAddress>
public class DefaultTcpTransportMapping
extends TcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>
The
DefaultTcpTransportMapping
implements a TCP transport
mapping with the Java 1.4 new IO API.
It uses a single thread for processing incoming and outgoing messages.
The thread is started when the listen
method is called, or
when an outgoing request is sent using the sendMessage
method.
- Version:
- 3.0
- Author:
- Frank Fock
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
static class
protected static class
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The maximum number of loops trying to read data from an incoming port but no data has been received.protected MessageLengthDecoder
protected DefaultTcpTransportMapping.ServerThread
Fields inherited from class org.snmp4j.transport.TcpTransportMapping
openSocketOnSending, tcpAddress
Fields inherited from class org.snmp4j.transport.AbstractConnectionOrientedTransportMapping
serverEnabled, sockets
Fields inherited from class org.snmp4j.transport.AbstractTransportMapping
asyncMsgProcessingSupported, connectionTimeout, listenWorkerTask, maxInboundMessageSize, socketCleaner, suspendedAddresses, transportListener, transportStateListeners
-
Constructor Summary
ConstructorDescriptionCreates a default TCP transport mapping with the server for incoming messages disabled.DefaultTcpTransportMapping
(TcpAddress serverAddress) Creates a default TCP transport mapping that binds to the given address (interface) on the local host and enables server mode (i.e.DefaultTcpTransportMapping
(TcpAddress serverAddress, boolean serverEnabled) Creates a default TCP transport mapping that binds to the given address (interface) on the local host and enables server mode on request. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addBufferToReadBuffer
(DefaultTcpTransportMapping.SocketEntry entry, ByteBuffer byteBuffer) Returns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.Returns theMessageLengthDecoder
used by this transport mapping.Gets theCommonTimer
that controls socket cleanup operations.Gets theTransportType
thisTransportMapping
supports depending onAbstractConnectionOrientedTransportMapping.isServerEnabled()
.void
listen()
Listen for incoming and outgoing requests.void
sendMessage
(TcpAddress address, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) Sends a SNMP message to the supplied address.void
setMaxInboundMessageSize
(int maxInboundMessageSize) Sets the maximum buffer size for incoming requests.void
setMessageLengthDecoder
(MessageLengthDecoder messageLengthDecoder) Sets the message length decoder.protected void
socketClosedRemotely
(SelectionKey sk, SocketChannel readChannel, TcpAddress incomingAddress) void
Methods inherited from class org.snmp4j.transport.TcpTransportMapping
getAddress, getSupportedAddressClass, isOpenSocketOnSending, setOpenSocketOnSending
Methods inherited from class org.snmp4j.transport.AbstractConnectionOrientedTransportMapping
cancelNonServerSelectionKey, close, close, closeSockets, getConnectionTimeout, getMaxBusyLoops, getSockets, isServerEnabled, setConnectionTimeout, setMaxBusyLoops, setServerEnabled, setSocketOptions, timeoutSocket
Methods inherited from class org.snmp4j.transport.AbstractTransportMapping
addTransportListener, addTransportStateListener, fireConnectionStateChanged, fireProcessMessage, getListenWorkerTask, getMaxInboundMessageSize, getPriority, 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.transport.ConnectionOrientedTransportMapping
addTransportStateListener, close, getConnectionTimeout, isServerEnabled, removeTransportStateListener, resumeAddress, setConnectionTimeout, setServerEnabled, suspendAddress
Methods inherited from interface org.snmp4j.TransportMapping
addTransportListener, close, getMaxInboundMessageSize, getMaxOutboundMessageSize, getSupportedAddressClasses, isAddressSupported, isAddressSupported, isListening, removeTransportListener
-
Field Details
-
DEFAULT_MAX_BUSY_LOOPS
public static final int DEFAULT_MAX_BUSY_LOOPSThe maximum number of loops trying to read data from an incoming port but no data has been received. A value of 0 or less disables the check.- See Also:
-
serverThread
-
messageLengthDecoder
-
-
Constructor Details
-
DefaultTcpTransportMapping
Creates a default TCP transport mapping with the server for incoming messages disabled.- Throws:
IOException
- on failure of binding a local port.
-
DefaultTcpTransportMapping
public DefaultTcpTransportMapping(TcpAddress serverAddress, boolean serverEnabled) throws IOException Creates a default TCP transport mapping that binds to the given address (interface) on the local host and enables server mode on request.- Parameters:
serverAddress
- the TcpAddress instance that describes the server address to listen on incoming connection requests.serverEnabled
- iftrue
the server mode is enabled and incoming new connections are accepted. Usefalse
to allow outgoing messages and their responses only (client mode).- Throws:
IOException
- if the given address cannot be bound.
-
DefaultTcpTransportMapping
Creates a default TCP transport mapping that binds to the given address (interface) on the local host and enables server mode (i.e. accepts incoming new connections).- Parameters:
serverAddress
- the TcpAddress instance that describes the server address to listen on incoming connection requests.- Throws:
IOException
- if the given address cannot be bound.
-
-
Method Details
-
listen
Listen for incoming and outgoing requests. If theserverEnabled
member isfalse
the server for incoming requests is not started. This starts the internal server thread that processes messages.- Specified by:
listen
in interfaceTransportMapping<TcpAddress>
- Specified by:
listen
in classTcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>
- Throws:
SocketException
- when the transport is already listening for incoming/outgoing messages.IOException
- if the listen port could not be bound to the server thread.
-
getSupportedTransportType
Gets theTransportType
thisTransportMapping
supports depending onAbstractConnectionOrientedTransportMapping.isServerEnabled()
.- Returns:
TransportType.any
ifAbstractConnectionOrientedTransportMapping.isServerEnabled()
istrue
andTransportType.sender
otherwise.- Since:
- 3.2.0
-
sendMessage
public void sendMessage(TcpAddress address, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) throws IOException Sends a SNMP message to the supplied address.- Specified by:
sendMessage
in interfaceTransportMapping<TcpAddress>
- Specified by:
sendMessage
in classTcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>
- Parameters:
address
- anTcpAddress
. AClassCastException
is thrown ifaddress
is not aTcpAddress
instance.message
- byte[] the message to sent.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 an IO exception occurs while trying to send the message.
-
getMessageLengthDecoder
Description copied from class:TcpTransportMapping
Returns theMessageLengthDecoder
used by this transport mapping.- Specified by:
getMessageLengthDecoder
in interfaceConnectionOrientedTransportMapping<TcpAddress>
- Specified by:
getMessageLengthDecoder
in classTcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>
- Returns:
- a MessageLengthDecoder instance.
-
setMessageLengthDecoder
Sets the message length decoder. Default message length decoder is theDefaultTcpTransportMapping.SnmpMesssageLengthDecoder
. The message length decoder must be able to decode the total length of a message for this transport mapping protocol(s).- Specified by:
setMessageLengthDecoder
in interfaceConnectionOrientedTransportMapping<TcpAddress>
- Specified by:
setMessageLengthDecoder
in classTcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>
- Parameters:
messageLengthDecoder
- aMessageLengthDecoder
instance.
-
getSocketCleaner
Gets theCommonTimer
that controls socket cleanup operations.- Specified by:
getSocketCleaner
in interfaceConnectionOrientedTransportMapping<TcpAddress>
- Overrides:
getSocketCleaner
in classAbstractTransportMapping<TcpAddress>
- Returns:
- a socket cleaner timer.
- Since:
- 3.0
-
setMaxInboundMessageSize
public void setMaxInboundMessageSize(int maxInboundMessageSize) Sets the maximum buffer size for incoming requests. When SNMP packets are received that are longer than this maximum size, the messages will be silently dropped and the connection will be closed.- Parameters:
maxInboundMessageSize
- the length of the inbound buffer in bytes.
-
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<TcpAddress>
- Overrides:
getListenAddress
in classTcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>
- Returns:
- the address for incoming packets or
null
this transport mapping is not configured to listen for incoming packets.
-
wakeupServerSelector
public void wakeupServerSelector() -
addBufferToReadBuffer
protected void addBufferToReadBuffer(DefaultTcpTransportMapping.SocketEntry entry, ByteBuffer byteBuffer) -
socketClosedRemotely
protected void socketClosedRemotely(SelectionKey sk, SocketChannel readChannel, TcpAddress incomingAddress) throws IOException - Throws:
IOException
-