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
Nested ClassesModifier and TypeClassDescriptionprotected classstatic classprotected static class -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe maximum number of loops trying to read data from an incoming port but no data has been received.protected MessageLengthDecoderprotected DefaultTcpTransportMapping.ServerThreadFields inherited from class org.snmp4j.transport.TcpTransportMapping
openSocketOnSending, tcpAddressFields inherited from class org.snmp4j.transport.AbstractConnectionOrientedTransportMapping
serverEnabled, socketsFields inherited from class org.snmp4j.transport.AbstractTransportMapping
asyncMsgProcessingSupported, connectionTimeout, listenWorkerTask, maxInboundMessageSize, socketCleaner, suspendedAddresses, transportListener, transportStateListeners -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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 voidaddBufferToReadBuffer(DefaultTcpTransportMapping.SocketEntry entry, ByteBuffer byteBuffer) Returns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.Returns theMessageLengthDecoderused by this transport mapping.Gets theCommonTimerthat controls socket cleanup operations.Gets theTransportTypethisTransportMappingsupports depending onAbstractConnectionOrientedTransportMapping.isServerEnabled().voidlisten()Listen for incoming and outgoing requests.voidsendMessage(TcpAddress address, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) Sends a SNMP message to the supplied address.voidsetMaxInboundMessageSize(int maxInboundMessageSize) Sets the maximum buffer size for incoming requests.voidsetMessageLengthDecoder(MessageLengthDecoder messageLengthDecoder) Sets the message length decoder.protected voidsocketClosedRemotely(SelectionKey sk, SocketChannel readChannel, TcpAddress incomingAddress) voidMethods inherited from class org.snmp4j.transport.TcpTransportMapping
getAddress, getSupportedAddressClass, isOpenSocketOnSending, setOpenSocketOnSendingMethods inherited from class org.snmp4j.transport.AbstractConnectionOrientedTransportMapping
cancelNonServerSelectionKey, close, close, closeSockets, getConnectionTimeout, getMaxBusyLoops, getSockets, isServerEnabled, setConnectionTimeout, setMaxBusyLoops, setServerEnabled, setSocketOptions, timeoutSocketMethods 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, suspendAddressMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.snmp4j.transport.ConnectionOrientedTransportMapping
addTransportStateListener, close, getConnectionTimeout, isServerEnabled, removeTransportStateListener, resumeAddress, setConnectionTimeout, setServerEnabled, suspendAddressMethods 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- iftruethe server mode is enabled and incoming new connections are accepted. Usefalseto 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 theserverEnabledmember isfalsethe server for incoming requests is not started. This starts the internal server thread that processes messages.- Specified by:
listenin interfaceTransportMapping<TcpAddress>- Specified by:
listenin 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 theTransportTypethisTransportMappingsupports depending onAbstractConnectionOrientedTransportMapping.isServerEnabled().- Returns:
TransportType.anyifAbstractConnectionOrientedTransportMapping.isServerEnabled()istrueandTransportType.senderotherwise.- 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:
sendMessagein interfaceTransportMapping<TcpAddress>- Specified by:
sendMessagein classTcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>- Parameters:
address- anTcpAddress. AClassCastExceptionis thrown ifaddressis not aTcpAddressinstance.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:TcpTransportMappingReturns theMessageLengthDecoderused by this transport mapping.- Specified by:
getMessageLengthDecoderin interfaceConnectionOrientedTransportMapping<TcpAddress>- Specified by:
getMessageLengthDecoderin 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:
setMessageLengthDecoderin interfaceConnectionOrientedTransportMapping<TcpAddress>- Specified by:
setMessageLengthDecoderin classTcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>- Parameters:
messageLengthDecoder- aMessageLengthDecoderinstance.
-
getSocketCleaner
Gets theCommonTimerthat controls socket cleanup operations.- Specified by:
getSocketCleanerin interfaceConnectionOrientedTransportMapping<TcpAddress>- Overrides:
getSocketCleanerin 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:TransportMappingReturns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.- Specified by:
getListenAddressin interfaceTransportMapping<TcpAddress>- Overrides:
getListenAddressin classTcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>- Returns:
- the address for incoming packets or
nullthis 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
-