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 Details

    • serverEnabled

      protected boolean serverEnabled
    • sockets

      protected Map<A extends Address, S extends AbstractSocketEntry<A>> sockets
    • gracefulShutdownTimeoutMillis

      protected long gracefulShutdownTimeoutMillis
    • connectionTimeout

      protected long connectionTimeout
  • Constructor Details

    • AbstractConnectionOrientedTransportMapping

      protected AbstractConnectionOrientedTransportMapping()
  • Method Details

    • timeoutSocket

      protected void timeoutSocket(AbstractSocketEntry<A> entry)
    • 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:
      getConnectionTimeout in interface ConnectionOrientedTransportMapping<A extends Address>
      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 interface ConnectionOrientedTransportMapping<A extends Address>
      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.
    • setSocketOptions

      protected void setSocketOptions(ServerSocket serverSocket)
      Sets optional server socket options. The default implementation does nothing.
      Parameters:
      serverSocket - the ServerSocket to 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 - the SocketOption to set; cannot be null
      value - 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 implementation
      IllegalArgumentException - if the specified value is not valid for the given socket option
      IOException - 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: ConnectionOrientedTransportMapping
      Checks whether a server for incoming requests is enabled.
      Specified by:
      isServerEnabled in interface ConnectionOrientedTransportMapping<A extends Address>
      Returns:
      boolean
    • 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 the TransportMapping.listen() method is called (if the transport is already listening, TransportMapping.close() has to be called before).
      Specified by:
      setServerEnabled in interface ConnectionOrientedTransportMapping<A extends Address>
      Parameters:
      serverEnabled - if true if the transport will listens for incoming requests after TransportMapping.listen() has been called.
    • closeSockets

      protected void closeSockets(Map<A,S> sockets)
    • close

      public boolean close(A remoteAddress) throws IOException
      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:
      close in interface ConnectionOrientedTransportMapping<A extends Address>
      Parameters:
      remoteAddress - the address of the peer socket.
      Returns:
      true if the connection has been closed and false if there was nothing to close.
      Throws:
      IOException - if the remote address cannot be closed due to an IO exception.
      Since:
      1.7.1
    • close

      public boolean close(A remoteAddress, boolean force) throws 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.
      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:
      true if the connection has been closed and false if there was nothing to close.
      Throws:
      IOException - if the remote address cannot be closed due to an IO exception.
      Since:
      3.10.0
    • close

      public void close() throws IOException
      Closes all open sockets and stops the internal server thread that processes messages.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface TransportMapping<A extends Address>
      Specified by:
      close in class AbstractTransportMapping<A extends Address>
      Throws:
      IOException - if an I/O error occurs while closing the resource
    • wakeupServerSelector

      public abstract void wakeupServerSelector()
    • getSockets

      public Map<A,S> getSockets()
      Gets an unmodifiable map of the AbstractSocketEntry instances associated with this transport mapping.
      Returns:
      an unmodifiable map from Address to AbstractSocketEntry.
      Since:
      3.7.0
    • cancelNonServerSelectionKey

      protected void cancelNonServerSelectionKey(SelectionKey sk)
    • getMaxBusyLoops

      protected int getMaxBusyLoops()
    • setMaxBusyLoops

      protected void setMaxBusyLoops(int maxBusyLoops)