- java.lang.Object
-
- org.snmp4j.transport.AbstractTransportMapping<TcpAddress>
-
- org.snmp4j.transport.TcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>
-
- org.snmp4j.transport.DefaultTcpTransportMapping
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,ConnectionOrientedTransportMapping<TcpAddress>,TransportMapping<TcpAddress>
public class DefaultTcpTransportMapping extends TcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>
TheDefaultTcpTransportMappingimplements 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
listenmethod is called, or when an outgoing request is sent using thesendMessagemethod.- Version:
- 3.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classDefaultTcpTransportMapping.ServerThreadstatic classDefaultTcpTransportMapping.SnmpMesssageLengthDecoderprotected classDefaultTcpTransportMapping.SocketEntry
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_MAX_BUSY_LOOPSThe maximum number of loops trying to read data from an incoming port but no data has been received.private static LogAdapterloggerprivate intmaxBusyLoopsprotected MessageLengthDecodermessageLengthDecoderprivate static intMIN_SNMP_HEADER_LENGTHprotected DefaultTcpTransportMapping.ServerThreadserverThread-
Fields inherited from class org.snmp4j.transport.TcpTransportMapping
connectionTimeout, openSocketOnSending, serverEnabled, socketCleaner, sockets, tcpAddress
-
Fields inherited from class org.snmp4j.transport.AbstractTransportMapping
asyncMsgProcessingSupported, listenWorkerTask, maxInboundMessageSize, suspendedAddresses, transportListener
-
-
Constructor Summary
Constructors Constructor Description DefaultTcpTransportMapping()Creates 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddBufferToReadBuffer(DefaultTcpTransportMapping.SocketEntry entry, java.nio.ByteBuffer byteBuffer)voidclose()Closes all open sockets and stops the internal server thread that processes messages and removes all queued requests and socket entries.protected TcpAddresscreateIncomingAddress(java.net.Socket s)TcpAddressgetListenAddress()Returns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.protected WorkerTaskgetListenerWorkerTask()Gets theWorkerTaskthat is responsible for receiving new messages.protected intgetMaxBusyLoops()intgetMaxInboundMessageSize()Gets the inbound buffer size for incoming requests.MessageLengthDecodergetMessageLengthDecoder()Returns theMessageLengthDecoderused by this transport mapping.CommonTimergetSocketCleaner()Gets theCommonTimerthat controls socket cleanup operations.TransportTypegetSupportedTransportType()Gets theTransportTypethisTransportMappingsupports depending onTcpTransportMapping.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.protected voidsetMaxBusyLoops(int maxBusyLoops)voidsetMaxInboundMessageSize(int maxInboundMessageSize)Sets the maximum buffer size for incoming requests.voidsetMessageLengthDecoder(MessageLengthDecoder messageLengthDecoder)Sets the message length decoder.protected voidsocketClosedRemotely(java.nio.channels.SelectionKey sk, java.nio.channels.SocketChannel readChannel, TcpAddress incomingAddress)-
Methods inherited from class org.snmp4j.transport.TcpTransportMapping
addTransportStateListener, cancelNonServerSelectionKey, close, closeSockets, fireConnectionStateChanged, getAddress, getConnectionTimeout, getListenWorkerTask, getSupportedAddressClass, isOpenSocketOnSending, isServerEnabled, removeTransportStateListener, setConnectionTimeout, setOpenSocketOnSending, setServerEnabled, setSocketOptions, timeoutSocket
-
Methods inherited from class org.snmp4j.transport.AbstractTransportMapping
addTransportListener, fireProcessMessage, getPriority, getThreadName, handleDroppedMessageToSend, isAsyncMsgProcessingSupported, isListening, removeAllTransportListeners, removeTransportListener, 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
resumeAddress, suspendAddress
-
Methods inherited from interface org.snmp4j.TransportMapping
addTransportListener, getSupportedAddressClasses, isAddressSupported, isAddressSupported, isListening, removeTransportListener
-
-
-
-
Field Detail
-
DEFAULT_MAX_BUSY_LOOPS
public static final int DEFAULT_MAX_BUSY_LOOPS
The 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:
- Constant Field Values
-
logger
private static final LogAdapter logger
-
serverThread
protected DefaultTcpTransportMapping.ServerThread serverThread
-
MIN_SNMP_HEADER_LENGTH
private static final int MIN_SNMP_HEADER_LENGTH
- See Also:
- Constant Field Values
-
messageLengthDecoder
protected MessageLengthDecoder messageLengthDecoder
-
maxBusyLoops
private int maxBusyLoops
-
-
Constructor Detail
-
DefaultTcpTransportMapping
public DefaultTcpTransportMapping() throws java.io.IOExceptionCreates a default TCP transport mapping with the server for incoming messages disabled.- Throws:
java.io.IOException- on failure of binding a local port.
-
DefaultTcpTransportMapping
public DefaultTcpTransportMapping(TcpAddress serverAddress, boolean serverEnabled) throws java.io.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:
java.io.IOException- if the given address cannot be bound.
-
DefaultTcpTransportMapping
public DefaultTcpTransportMapping(TcpAddress serverAddress) throws java.io.IOException
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:
java.io.IOException- if the given address cannot be bound.
-
-
Method Detail
-
listen
public void listen() throws java.io.IOExceptionListen 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:
java.net.SocketException- when the transport is already listening for incoming/outgoing messages.java.io.IOException- if the listen port could not be bound to the server thread.
-
getSupportedTransportType
public TransportType getSupportedTransportType()
Gets theTransportTypethisTransportMappingsupports depending onTcpTransportMapping.isServerEnabled().- Returns:
TransportType.anyifTcpTransportMapping.isServerEnabled()istrueandTransportType.senderotherwise.- Since:
- 3.2.0
-
getListenerWorkerTask
protected WorkerTask getListenerWorkerTask()
Description copied from class:AbstractTransportMappingGets theWorkerTaskthat is responsible for receiving new messages.- Specified by:
getListenerWorkerTaskin classAbstractTransportMapping<TcpAddress>- Returns:
- a
WorkerTaskinstance which is most likely aDefaultThreadFactory.WorkerThread.
-
close
public void close()
Closes all open sockets and stops the internal server thread that processes messages and removes all queued requests and socket entries.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfaceTransportMapping<TcpAddress>- Specified by:
closein classTcpTransportMapping<DefaultTcpTransportMapping.SocketEntry>
-
sendMessage
public void sendMessage(TcpAddress address, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) throws java.io.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 milli seconds the connection creation might take (if connection based).maxRetries- maximum retries during connection creation.- Throws:
java.io.IOException- if an IO exception occurs while trying to send the message.
-
getMessageLengthDecoder
public MessageLengthDecoder 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
public void setMessageLengthDecoder(MessageLengthDecoder messageLengthDecoder)
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
public CommonTimer getSocketCleaner()
Gets theCommonTimerthat controls socket cleanup operations.- Returns:
- a socket cleaner timer.
- Since:
- 3.0
-
getMaxInboundMessageSize
public int getMaxInboundMessageSize()
Gets the inbound 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.- Specified by:
getMaxInboundMessageSizein interfaceTransportMapping<TcpAddress>- Overrides:
getMaxInboundMessageSizein classAbstractTransportMapping<TcpAddress>- Returns:
- the maximum inbound buffer size in bytes.
-
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.
-
getMaxBusyLoops
protected int getMaxBusyLoops()
-
setMaxBusyLoops
protected void setMaxBusyLoops(int maxBusyLoops)
-
getListenAddress
public TcpAddress 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.
-
createIncomingAddress
protected TcpAddress createIncomingAddress(java.net.Socket s)
-
addBufferToReadBuffer
protected void addBufferToReadBuffer(DefaultTcpTransportMapping.SocketEntry entry, java.nio.ByteBuffer byteBuffer)
-
socketClosedRemotely
protected void socketClosedRemotely(java.nio.channels.SelectionKey sk, java.nio.channels.SocketChannel readChannel, TcpAddress incomingAddress) throws java.io.IOException- Throws:
java.io.IOException
-
-