Module org.snmp4j
Package org.snmp4j.transport
Class TcpTransportMapping<S extends AbstractSocketEntry>
- java.lang.Object
-
- org.snmp4j.transport.AbstractTransportMapping<TcpAddress>
-
- org.snmp4j.transport.TcpTransportMapping<S>
-
- Type Parameters:
S
- defines the type ofAbstractSocketEntry
used by this transport mapping.
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,ConnectionOrientedTransportMapping<TcpAddress>
,TransportMapping<TcpAddress>
- Direct Known Subclasses:
DefaultTcpTransportMapping
,TLSTM
public abstract class TcpTransportMapping<S extends AbstractSocketEntry> extends AbstractTransportMapping<TcpAddress> implements ConnectionOrientedTransportMapping<TcpAddress>
TheTcpTransportMapping
is the abstract base class for TCP transport mappings.- Version:
- 3.4.4
-
-
Field Summary
Fields Modifier and Type Field Description protected long
connectionTimeout
private static LogAdapter
logger
protected boolean
openSocketOnSending
Enable or disable automatic (re)opening the communication socket when sending a messageprotected boolean
serverEnabled
protected CommonTimer
socketCleaner
protected java.util.Map<Address,S>
sockets
protected TcpAddress
tcpAddress
private java.util.List<TransportStateListener>
transportStateListeners
-
Fields inherited from class org.snmp4j.transport.AbstractTransportMapping
asyncMsgProcessingSupported, listenWorkerTask, maxInboundMessageSize, suspendedAddresses, transportListener
-
-
Constructor Summary
Constructors Constructor Description TcpTransportMapping(TcpAddress tcpAddress)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addTransportStateListener(TransportStateListener l)
Adds a transport state listener that is to be informed about connection state changes.protected void
cancelNonServerSelectionKey(java.nio.channels.SelectionKey sk)
abstract void
close()
Closes the transport an releases all bound resources synchronously.boolean
close(TcpAddress remoteAddress)
Closes a connection to the supplied remote address, if it is open.protected void
closeSockets(java.util.Map<Address,S> sockets)
protected void
fireConnectionStateChanged(TransportStateEvent change)
TcpAddress
getAddress()
Returns the transport address that is used by this transport mapping for sending and receiving messages.long
getConnectionTimeout()
Gets the connection timeout.TcpAddress
getListenAddress()
Returns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.WorkerTask
getListenWorkerTask()
abstract MessageLengthDecoder
getMessageLengthDecoder()
Returns theMessageLengthDecoder
used by this transport mapping.java.lang.Class<? extends Address>
getSupportedAddressClass()
Gets the primaryAddress
class that is supported by this transport mapping.boolean
isOpenSocketOnSending()
Iftrue
and methodlisten()
has not been called yet or the connection has been closed or reset, thenlisten()
will be called to open the communication socket when a message is being sent usingsendMessage(TcpAddress, byte[], TransportStateReference, long, int)
.boolean
isServerEnabled()
Checks whether a server for incoming requests is enabled.abstract void
listen()
Listen for incoming messages.void
removeTransportStateListener(TransportStateListener l)
Removes the supplied transport state listener.abstract void
sendMessage(TcpAddress address, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries)
Sends a message to the supplied address using this transport.void
setConnectionTimeout(long connectionTimeout)
Sets the connection timeout.abstract void
setMessageLengthDecoder(MessageLengthDecoder messageLengthDecoder)
Sets theMessageLengthDecoder
that decodes the total message length from the header of a message.void
setOpenSocketOnSending(boolean openSocketOnSending)
Activate or deactivate autolisten()
whensendMessage(TcpAddress, byte[], TransportStateReference, long, int)
is called but there is no listening socket.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(java.net.ServerSocket serverSocket)
Sets optional server socket options.protected void
timeoutSocket(AbstractServerSocket<TcpAddress> entry)
-
Methods inherited from class org.snmp4j.transport.AbstractTransportMapping
addTransportListener, fireProcessMessage, getListenerWorkerTask, getMaxInboundMessageSize, 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
getSocketCleaner, resumeAddress, suspendAddress
-
Methods inherited from interface org.snmp4j.TransportMapping
addTransportListener, getMaxInboundMessageSize, getSupportedAddressClasses, getSupportedTransportType, isAddressSupported, isAddressSupported, isListening, removeTransportListener
-
-
-
-
Field Detail
-
logger
private static final LogAdapter logger
-
tcpAddress
protected TcpAddress tcpAddress
-
sockets
protected java.util.Map<Address,S extends AbstractSocketEntry> sockets
-
connectionTimeout
protected long connectionTimeout
-
socketCleaner
protected CommonTimer socketCleaner
-
serverEnabled
protected boolean serverEnabled
-
transportStateListeners
private transient java.util.List<TransportStateListener> transportStateListeners
-
openSocketOnSending
protected boolean openSocketOnSending
Enable or disable automatic (re)opening the communication socket when sending a message
-
-
Constructor Detail
-
TcpTransportMapping
public TcpTransportMapping(TcpAddress tcpAddress)
-
-
Method Detail
-
getSupportedAddressClass
public java.lang.Class<? extends Address> getSupportedAddressClass()
Description copied from interface:TransportMapping
Gets the primaryAddress
class that is supported by this transport mapping.- Specified by:
getSupportedAddressClass
in interfaceTransportMapping<S extends AbstractSocketEntry>
- Specified by:
getSupportedAddressClass
in classAbstractTransportMapping<TcpAddress>
- Returns:
- a subclass of
Address
.
-
getAddress
public TcpAddress getAddress()
Returns the transport address that is used by this transport mapping for sending and receiving messages.- Returns:
- the
Address
used by this transport mapping. The returned instance must not be modified!
-
timeoutSocket
protected void timeoutSocket(AbstractServerSocket<TcpAddress> entry)
-
getListenAddress
public TcpAddress 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<S extends AbstractSocketEntry>
- Returns:
- the address for incoming packets or
null
this transport mapping is not configured to listen for incoming packets.
-
close
public boolean close(TcpAddress remoteAddress) throws java.io.IOException
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.- Specified by:
close
in interfaceConnectionOrientedTransportMapping<S extends AbstractSocketEntry>
- Parameters:
remoteAddress
- the address of the peer socket.- Returns:
true
if the connection has been closed andfalse
if there was nothing to close.- Throws:
java.io.IOException
- if the remote address cannot be closed due to an IO exception.- Since:
- 1.7.1
-
sendMessage
public abstract void sendMessage(TcpAddress address, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) throws java.io.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<S extends AbstractSocketEntry>
- Specified by:
sendMessage
in classAbstractTransportMapping<TcpAddress>
- Parameters:
address
- 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 milli seconds the connection creation might take (if connection based).maxRetries
- maximum retries during connection creation.- Throws:
java.io.IOException
- if any underlying IO operation fails.
-
isOpenSocketOnSending
public boolean isOpenSocketOnSending()
Iftrue
and methodlisten()
has not been called yet or the connection has been closed or reset, thenlisten()
will be called to open the communication socket when a message is being sent usingsendMessage(TcpAddress, byte[], TransportStateReference, long, int)
.- Returns:
true
ifsendMessage(TcpAddress, byte[], TransportStateReference, long, int)
will ensure that a server socket is there for receiving responses,false
otherwise.- Since:
- 3.4.4
-
setOpenSocketOnSending
public void setOpenSocketOnSending(boolean openSocketOnSending)
Activate or deactivate autolisten()
whensendMessage(TcpAddress, byte[], TransportStateReference, long, int)
is called but there is no listening socket.- Parameters:
openSocketOnSending
-true
ifsendMessage(TcpAddress, byte[], TransportStateReference, long, int)
should ensure that server socket is available for communication,false
iflisten()
must be called explicitly.- Since:
- 3.4.4
-
listen
public abstract void listen() throws java.io.IOException
Description copied from interface:TransportMapping
Listen for incoming messages. For connection oriented transports, this method needs to be called beforeTransportMapping.sendMessage(A, byte[], org.snmp4j.TransportStateReference, long, int)
is called for the first time.- Specified by:
listen
in interfaceTransportMapping<S extends AbstractSocketEntry>
- Specified by:
listen
in classAbstractTransportMapping<TcpAddress>
- Throws:
java.io.IOException
- if an IO operation exception occurs while starting the listener.
-
close
public abstract void close() throws java.io.IOException
Description copied from interface:TransportMapping
Closes the transport an releases all bound resources synchronously.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceTransportMapping<S extends AbstractSocketEntry>
- Specified by:
close
in classAbstractTransportMapping<TcpAddress>
- Throws:
java.io.IOException
- if any IO operation for the close fails.
-
isServerEnabled
public boolean isServerEnabled()
Description copied from interface:ConnectionOrientedTransportMapping
Checks whether a server for incoming requests is enabled.- Specified by:
isServerEnabled
in interfaceConnectionOrientedTransportMapping<S extends AbstractSocketEntry>
- Returns:
- boolean
-
getMessageLengthDecoder
public abstract MessageLengthDecoder getMessageLengthDecoder()
Returns theMessageLengthDecoder
used by this transport mapping.- Specified by:
getMessageLengthDecoder
in interfaceConnectionOrientedTransportMapping<S extends AbstractSocketEntry>
- Returns:
- a MessageLengthDecoder instance.
- Since:
- 1.7
-
setServerEnabled
public void setServerEnabled(boolean serverEnabled)
Description copied from interface:ConnectionOrientedTransportMapping
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 theTransportMapping.listen()
method is called (if the transport is already listening,TransportMapping.close()
has to be called before).- Specified by:
setServerEnabled
in interfaceConnectionOrientedTransportMapping<S extends AbstractSocketEntry>
- Parameters:
serverEnabled
- iftrue
if the transport will listens for incoming requests afterTransportMapping.listen()
has been called.
-
setMessageLengthDecoder
public abstract void setMessageLengthDecoder(MessageLengthDecoder messageLengthDecoder)
Sets theMessageLengthDecoder
that decodes the total message length from the header of a message.- Specified by:
setMessageLengthDecoder
in interfaceConnectionOrientedTransportMapping<S extends AbstractSocketEntry>
- Parameters:
messageLengthDecoder
- a MessageLengthDecoder instance.- Since:
- 1.7
-
getConnectionTimeout
public long getConnectionTimeout()
Gets the connection timeout. This timeout specifies the time a connection may be idle before it is closed.- Specified by:
getConnectionTimeout
in interfaceConnectionOrientedTransportMapping<S extends AbstractSocketEntry>
- 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<S extends AbstractSocketEntry>
- 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.
-
addTransportStateListener
public void addTransportStateListener(TransportStateListener l)
Description copied from interface:ConnectionOrientedTransportMapping
Adds a transport state listener that is to be informed about connection state changes.- Specified by:
addTransportStateListener
in interfaceConnectionOrientedTransportMapping<S extends AbstractSocketEntry>
- Parameters:
l
- a TransportStateListener.
-
removeTransportStateListener
public void removeTransportStateListener(TransportStateListener l)
Description copied from interface:ConnectionOrientedTransportMapping
Removes the supplied transport state listener.- Specified by:
removeTransportStateListener
in interfaceConnectionOrientedTransportMapping<S extends AbstractSocketEntry>
- Parameters:
l
- a TransportStateListener.
-
fireConnectionStateChanged
protected void fireConnectionStateChanged(TransportStateEvent change)
-
setSocketOptions
protected void setSocketOptions(java.net.ServerSocket serverSocket)
Sets optional server socket options. The default implementation does nothing.- Parameters:
serverSocket
- theServerSocket
to apply additional non-default options.
-
getListenWorkerTask
public WorkerTask getListenWorkerTask()
-
cancelNonServerSelectionKey
protected void cancelNonServerSelectionKey(java.nio.channels.SelectionKey sk)
-
-