Package org.snmp4j.transport
Class DefaultTcpTransportMapping
java.lang.Object
org.snmp4j.transport.AbstractTransportMapping<TcpAddress>
org.snmp4j.transport.TcpTransportMapping
org.snmp4j.transport.DefaultTcpTransportMapping
- All Implemented Interfaces:
ConnectionOrientedTransportMapping<TcpAddress>
,TransportMapping<TcpAddress>
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:
- 2.8.5
- Author:
- Frank Fock
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
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 WorkerTask
protected DefaultTcpTransportMapping.ServerThread
protected CommonTimer
Fields inherited from class org.snmp4j.transport.TcpTransportMapping
openSocketOnSending, suspendedAddresses, tcpAddress
Fields inherited from class org.snmp4j.transport.AbstractTransportMapping
asyncMsgProcessingSupported, maxInboundMessageSize, maxOutboundMessageSize, transportListener
-
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. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addBufferToReadBuffer
(org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry entry, ByteBuffer byteBuffer) void
close()
Closes all open sockets and stops the internal server thread that processes messages and removes all queued requests and socket entries.boolean
close
(TcpAddress remoteAddress) Closes a connection to the supplied remote address, if it is open.long
Gets the connection timeout.Returns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.protected int
int
Gets the inbound buffer size for incoming requests.Returns theMessageLengthDecoder
used by this transport mapping.int
Returns the priority of the internal listen thread.Returns the name of the listen thread.boolean
Returnstrue
if the transport mapping is listening for incoming messages.boolean
Checks whether a server for incoming requests is enabled.void
listen()
Listen for incoming and outgoing requests.protected Object
removeSocketEntry
(TcpAddress incomingAddress) void
sendMessage
(TcpAddress address, byte[] message, TransportStateReference tmStateReference) Sends a SNMP message to the supplied address.void
setConnectionTimeout
(long connectionTimeout) Sets the connection timeout.protected void
setMaxBusyLoops
(int maxBusyLoops) void
setMaxInboundMessageSize
(int maxInboundMessageSize) Sets the maximum buffer size for incoming requests.void
setMessageLengthDecoder
(MessageLengthDecoder messageLengthDecoder) Sets the message length decoder.void
setPriority
(int newPriority) Changes the priority of the server thread for this TCP transport mapping.void
setServerEnabled
(boolean serverEnabled) Sets whether a server for incoming requests should be created when the transport is set into listen state.protected void
setSocketOptions
(ServerSocket serverSocket) Sets optional server socket options.void
setThreadName
(String name) Sets the name of the listen thread for this UDP transport mapping.protected void
socketClosedRemotely
(SelectionKey sk, SocketChannel readChannel, TcpAddress incomingAddress) Methods inherited from class org.snmp4j.transport.TcpTransportMapping
addTransportStateListener, cancelNonServerSelectionKey, fireConnectionStateChanged, getAddress, getSupportedAddressClass, handleDroppedMessageToSend, isOpenSocketOnSending, removeTransportStateListener, resumeAddress, setOpenSocketOnSending, suspendAddress
Methods inherited from class org.snmp4j.transport.AbstractTransportMapping
addTransportListener, fireProcessMessage, getMaxOutboundMessageSize, isAsyncMsgProcessingSupported, removeAllTransportListeners, 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
addTransportListener, getMaxOutboundMessageSize, 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:
-
sockets
-
server
-
serverThread
-
socketCleaner
-
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
Creates a default TCP transport mapping that binds to the given address (interface) on the local host.- 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
- 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.
-
setPriority
public void setPriority(int newPriority) Changes the priority of the server thread for this TCP transport mapping. This method has no effect, if called beforelisten()
has been called for this transport mapping or if SNMP4J is configured to use a non-default thread factory.- Parameters:
newPriority
- the new priority.- Since:
- 1.2.2
- See Also:
-
getPriority
public int getPriority()Returns the priority of the internal listen thread.- Returns:
- a value between
Thread.MIN_PRIORITY
andThread.MAX_PRIORITY
. - Since:
- 1.2.2
-
setThreadName
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
Returns the name of the listen thread.- Returns:
- the thread name if in listening mode, otherwise
null
. - Since:
- 1.6
-
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:
close
in interfaceTransportMapping<TcpAddress>
- Specified by:
close
in classTcpTransportMapping
-
close
Closes a connection to the supplied remote address, if it is open. This method is particularly useful when not using a timeout for remote connections.- Parameters:
remoteAddress
- the address of the peer socket.- Returns:
true
if the connection has been closed andfalse
if there was nothing to close.- Throws:
IOException
- if the remote address cannot be closed due to an IO exception.- Since:
- 1.7.1
-
sendMessage
public void sendMessage(TcpAddress address, byte[] message, TransportStateReference tmStateReference) throws IOException Sends a SNMP message to the supplied address.- Specified by:
sendMessage
in interfaceTransportMapping<TcpAddress>
- Specified by:
sendMessage
in classTcpTransportMapping
- 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.- Throws:
IOException
- if an IO exception occurs while trying to send the message.
-
getConnectionTimeout
public long getConnectionTimeout()Gets the connection timeout. This timeout specifies the time a connection may be idle before it is closed.- Returns:
- long the idle timeout in milliseconds.
-
setConnectionTimeout
public void setConnectionTimeout(long connectionTimeout) Sets the connection timeout. This timeout specifies the time a connection may be idle before it is closed.- Specified by:
setConnectionTimeout
in interfaceConnectionOrientedTransportMapping<TcpAddress>
- Specified by:
setConnectionTimeout
in classTcpTransportMapping
- Parameters:
connectionTimeout
- the idle timeout in milliseconds. A zero or negative value will disable any timeout and connections opened by this transport mapping will stay opened until they are explicitly closed.
-
isServerEnabled
public boolean isServerEnabled()Checks whether a server for incoming requests is enabled.- Returns:
- boolean
-
getMessageLengthDecoder
Description copied from class:TcpTransportMapping
Returns theMessageLengthDecoder
used by this transport mapping.- Specified by:
getMessageLengthDecoder
in interfaceConnectionOrientedTransportMapping<TcpAddress>
- Specified by:
getMessageLengthDecoder
in classTcpTransportMapping
- Returns:
- a MessageLengthDecoder instance.
-
setServerEnabled
public void setServerEnabled(boolean serverEnabled) Sets whether a server for incoming requests should be created when the transport is set into listen state. Setting this value has no effect until thelisten()
method is called (if the transport is already listening,close()
has to be called before).- Parameters:
serverEnabled
- iftrue
if the transport will listens for incoming requests afterlisten()
has been called.
-
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
- Parameters:
messageLengthDecoder
- aMessageLengthDecoder
instance.
-
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:
getMaxInboundMessageSize
in interfaceTransportMapping<TcpAddress>
- Overrides:
getMaxInboundMessageSize
in 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.
-
isListening
public boolean isListening()Description copied from interface:TransportMapping
Returnstrue
if 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:
true
if this transport mapping is listening for messages.
-
getMaxBusyLoops
protected int getMaxBusyLoops() -
setMaxBusyLoops
protected void setMaxBusyLoops(int maxBusyLoops) -
setSocketOptions
Sets optional server socket options. The default implementation does nothing.- Parameters:
serverSocket
- theServerSocket
to apply additional non-default options.
-
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
- Returns:
- the address for incoming packets or
null
this transport mapping is not configured to listen for incoming packets.
-
addBufferToReadBuffer
protected void addBufferToReadBuffer(org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry entry, ByteBuffer byteBuffer) -
socketClosedRemotely
protected void socketClosedRemotely(SelectionKey sk, SocketChannel readChannel, TcpAddress incomingAddress) throws IOException - Throws:
IOException
-
removeSocketEntry
-