Class AbstractConnectionOrientedTransportMapping<A extends Address, S extends AbstractSocketEntry<A>>
java.lang.Object
org.snmp4j.transport.AbstractTransportMapping<A>
org.snmp4j.transport.AbstractConnectionOrientedTransportMapping<A,S>
- Type Parameters:
A- the address type.S- the socket entry type.
- All Implemented Interfaces:
Closeable, AutoCloseable, ConnectionOrientedTransportMapping<A>, TransportMapping<A>
- Direct Known Subclasses:
TcpTransportMapping
public abstract class AbstractConnectionOrientedTransportMapping<A extends Address, S extends AbstractSocketEntry<A>>
extends AbstractTransportMapping<A>
implements ConnectionOrientedTransportMapping<A>
The
AbstractConnectionOrientedTransportMapping implements generic functions for a connection
oriented transport mapping for server and client connections.- Since:
- 3.7.0
- Author:
- Frank Fock
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected longprotected intThe maximum number of concurrently accepted inbound connections or 0 (the default) for no limit.protected boolean -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidvoidclose()Closes all open sockets and stops the internal server thread that processes messages.booleanCloses a connection to the supplied remote address if it is open forcefully (by TCP RST).booleanCloses a connection to the supplied remote address if it is open.protected voidcloseSockets(Map<A, S> sockets) longGets the connection timeout.longGets the graceful shutdown timeout for the server socket in milliseconds.protected intintGets the maximum number of concurrently accepted inbound connections.Gets an unmodifiable map of theAbstractSocketEntryinstances associated with this transport mapping.booleanChecks whether another inbound connection may be accepted.booleanChecks whether a server for incoming requests is enabled.voidsetConnectionTimeout(long connectionTimeout) Sets the connection timeout.voidsetGracefulShutdownTimeoutMillis(long gracefulShutdownTimeoutMillis) Sets the graceful shutdown timeout for the server socket in milliseconds.protected voidsetMaxBusyLoops(int maxBusyLoops) voidsetMaxInboundConnections(int maxInboundConnections) Sets the maximum number of concurrently accepted inbound connections.voidsetServerEnabled(boolean serverEnabled) Sets whether a server for incoming requests should be created when the transport is set into listen state.protected abstract <T> voidsetSocketOption(SocketOption<T> socketOption, T value) Sets the value of a specific socket option.protected voidsetSocketOptions(ServerSocket serverSocket) Sets optional server socket options.protected voidtimeoutSocket(AbstractSocketEntry<A> entry) abstract voidMethods inherited from class AbstractTransportMapping
addTransportAuditListener, addTransportListener, addTransportStateListener, fireConnectionStateChanged, fireProcessMessage, fireTransportAuditEvent, getAuditListenAddress, getListenAddress, getListenWorkerTask, getMaxInboundMessageSize, getPriority, getSocketCleaner, getSupportedAddressClass, getSuspendedAddresses, getThreadName, handleDroppedMessageToSend, isAsyncMsgProcessingSupported, isListening, isTransportAudit, listen, removeAllTransportAuditListeners, removeAllTransportListeners, removeTransportAuditListener, removeTransportListener, removeTransportStateListener, resumeAddress, sendMessage, setAsyncMsgProcessingSupported, setPriority, setThreadName, suspendAddressMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ConnectionOrientedTransportMapping
addTransportStateListener, getMessageLengthDecoder, getSocketCleaner, removeTransportStateListener, resumeAddress, setMessageLengthDecoder, suspendAddressMethods inherited from interface TransportMapping
addTransportListener, getListenAddress, getMaxInboundMessageSize, getMaxOutboundMessageSize, getSupportedAddressClass, getSupportedAddressClasses, getSupportedTransportType, isAddressSupported, isAddressSupported, isListening, listen, removeTransportListener, sendMessage
-
Field Details
-
serverEnabled
protected boolean serverEnabled -
sockets
-
gracefulShutdownTimeoutMillis
protected long gracefulShutdownTimeoutMillis -
maxInboundConnections
protected int maxInboundConnectionsThe maximum number of concurrently accepted inbound connections or 0 (the default) for no limit.- Since:
- 3.13.0
-
-
Constructor Details
-
AbstractConnectionOrientedTransportMapping
protected AbstractConnectionOrientedTransportMapping()
-
-
Method Details
-
timeoutSocket
-
getMaxInboundConnections
public int getMaxInboundConnections()Gets the maximum number of concurrently accepted inbound connections.- Returns:
- the maximum number of accepted connections or 0 (the default) if there is no limit.
- Since:
- 3.13.0
-
setMaxInboundConnections
public void setMaxInboundConnections(int maxInboundConnections) Sets the maximum number of concurrently accepted inbound connections. Further connection attempts are closed immediately while the limit is reached. Without a limit, a peer can create as many connections as the file descriptor limit of the process permits, where each connection allocates buffers and bookkeeping state.- Parameters:
maxInboundConnections- the maximum number of accepted connections, or 0 for no limit.- Since:
- 3.13.0
-
isInboundConnectionAcceptable
public boolean isInboundConnectionAcceptable()Checks whether another inbound connection may be accepted.- Returns:
trueifgetMaxInboundConnections()is not reached yet.- Since:
- 3.13.0
-
getGracefulShutdownTimeoutMillis
public long getGracefulShutdownTimeoutMillis()Gets the graceful shutdown timeout for the server socket in milliseconds.- Returns:
- the timeout in milliseconds or 0 if no timeout is set.
-
setGracefulShutdownTimeoutMillis
public void setGracefulShutdownTimeoutMillis(long gracefulShutdownTimeoutMillis) Sets the graceful shutdown timeout for the server socket in milliseconds. A value of 0 disables the timeout (default). If the timeout is exceeded, the server will forcefully close the server socket.- Parameters:
gracefulShutdownTimeoutMillis- the timeout in milliseconds.
-
getConnectionTimeout
public long getConnectionTimeout()Gets the connection timeout. This timeout specifies the time a connection may be idle before it is closed.- Specified by:
getConnectionTimeoutin interfaceConnectionOrientedTransportMapping<A extends Address>- Returns:
- long
the idle timeout in milliseconds. Unless
setConnectionTimeout(long)has been called, the default ofAbstractTransportMapping(60000 milliseconds) applies to server enabled transport mappings, whereas a transport mapping that only opens connections itself returns 0, that is, its connections are never closed because they are idle.Up to and including SNMP4J 3.12.2, this method returned 0 for all connection oriented transport mappings, because this class shadowed the
connectionTimeoutfield ofAbstractTransportMappingwith a field initialized to zero. That silently disabled the idle socket cleaner, so a peer could keep an unbounded number of accepted connections open forever.
-
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:
setConnectionTimeoutin interfaceConnectionOrientedTransportMapping<A extends Address>- Parameters:
connectionTimeout- the idle timeout in milliseconds. A zero or negative value will disable any timeout and connections of this transport mapping will stay opened until they are explicitly closed.
-
setSocketOptions
Sets optional server socket options. The default implementation does nothing.- Parameters:
serverSocket- theServerSocketto apply additional non-default options.
-
setSocketOption
protected abstract <T> void setSocketOption(SocketOption<T> socketOption, T value) throws IOException Sets the value of a specific socket option.This method allows configuring transport-specific socket options, which may impact the behavior of the underlying socket implementation. The specific options and the corresponding values depend on the platform and socket implementation.
- Type Parameters:
T- the type of the value associated with the socket option- Parameters:
socketOption- theSocketOptionto set; cannot be nullvalue- the value to be set for the specified socket option; may be null if the option supports null values- Throws:
UnsupportedOperationException- if the specified option is not supported by the underlying socket implementationIllegalArgumentException- if the specified value is not valid for the given socket optionIOException- if an I/O error occurs while attempting to set the socket option- Since:
- 3.10.0
-
isServerEnabled
public boolean isServerEnabled()Description copied from interface:ConnectionOrientedTransportMappingChecks whether a server for incoming requests is enabled.- Specified by:
isServerEnabledin interfaceConnectionOrientedTransportMapping<A extends Address>- Returns:
- boolean
-
setServerEnabled
public void setServerEnabled(boolean serverEnabled) Description copied from interface:ConnectionOrientedTransportMappingSets 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:
setServerEnabledin interfaceConnectionOrientedTransportMapping<A extends Address>- Parameters:
serverEnabled- iftrueif the transport will listens for incoming requests afterTransportMapping.listen()has been called.
-
closeSockets
-
close
Closes a connection to the supplied remote address if it is open forcefully (by TCP RST). This method is particularly useful when not using a timeout for remote connections.- Specified by:
closein interfaceConnectionOrientedTransportMapping<A extends Address>- Parameters:
remoteAddress- the address of the peer socket.- Returns:
trueif the connection has been closed andfalseif there was nothing to close.- Throws:
IOException- if the remote address cannot be closed due to an IO exception.- Since:
- 1.7.1
-
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.force- if true, the connection will be forcefully closed with TCP RST. If false, a TCP FIN message is sent on the outbound connection only.- Returns:
trueif the connection has been closed andfalseif there was nothing to close.- Throws:
IOException- if the remote address cannot be closed due to an IO exception.- Since:
- 3.10.0
-
close
Closes all open sockets and stops the internal server thread that processes messages.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceTransportMapping<A extends Address>- Specified by:
closein classAbstractTransportMapping<A extends Address>- Throws:
IOException- if an I/O error occurs while closing the resource
-
wakeupServerSelector
public abstract void wakeupServerSelector() -
getSockets
Gets an unmodifiable map of theAbstractSocketEntryinstances associated with this transport mapping.- Returns:
- an unmodifiable map from
AddresstoAbstractSocketEntry. - Since:
- 3.7.0
-
cancelNonServerSelectionKey
-
getMaxBusyLoops
protected int getMaxBusyLoops() -
setMaxBusyLoops
protected void setMaxBusyLoops(int maxBusyLoops)
-