Class UnixSocketStreamTransportMapping

java.lang.Object
org.snmp4j.transport.AbstractTransportMapping<UnixDomainAddress>
org.snmp4j.transport.AbstractConnectionOrientedTransportMapping<UnixDomainAddress, UnixSocketStreamTransportMapping.SocketEntry>
org.snmp4j.transport.unix.UnixSocketStreamTransportMapping
All Implemented Interfaces:
Closeable, AutoCloseable, org.snmp4j.transport.ConnectionOrientedTransportMapping<UnixDomainAddress>, org.snmp4j.TransportMapping<UnixDomainAddress>

public class UnixSocketStreamTransportMapping extends org.snmp4j.transport.AbstractConnectionOrientedTransportMapping<UnixDomainAddress, UnixSocketStreamTransportMapping.SocketEntry>
A transport mapping implementation for Unix domain socket communication that leverages stream-oriented semantics. This class is built upon the AbstractConnectionOrientedTransportMapping, providing enhanced support for Unix domain socket addresses.

The class allows for sending and receiving messages, setting socket options, and managing Unix domain socket communication-specific behaviors. It supports asynchronous message handling and can handle multiple concurrent Unix domain socket-based connections.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected class 
     
    static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Path
    The socket path that has been bound by this transport mapping and that therefore has to be removed when this transport mapping is closed, or null if this transport mapping has not created a socket file.
    The Unix domain socket address to which this transport mapping is bound.
    The server thread responsible for handling incoming connections.

    Fields inherited from class org.snmp4j.transport.AbstractConnectionOrientedTransportMapping

    gracefulShutdownTimeoutMillis, maxInboundConnections, serverEnabled, sockets

    Fields inherited from class org.snmp4j.transport.AbstractTransportMapping

    asyncMsgProcessingSupported, connectionTimeout, listenWorkerTask, maxInboundMessageSize, socketCleaner, suspendedAddresses, transportAuditListeners, transportListener, transportStateListeners
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a UnixSocketStreamTransportMapping without specifying a Unix domain socket address.
    Constructs a UnixSocketStreamTransportMapping with the specified Unix domain socket address and server mode enabled, that is, incoming connections on that address are accepted.
    UnixSocketStreamTransportMapping(UnixDomainAddress listenAddress, boolean serverEnabled)
    Constructs a UnixSocketStreamTransportMapping with the specified Unix domain socket address and enables server mode on request.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    boolean
    close(UnixDomainAddress remoteAddress)
     
     
    org.snmp4j.transport.MessageLengthDecoder
     
    Class<? extends org.snmp4j.smi.Address>
     
    Set<Class<? extends org.snmp4j.smi.Address>>
     
    boolean
     
    void
     
    protected Optional<ByteBuffer>
    Read the next network packet for the application.
    protected void
    Removes the socket file that this transport mapping has created, if any.
    void
    sendMessage(UnixDomainAddress address, byte[] message, org.snmp4j.TransportStateReference tmStateReference, long timeoutMillis, int maxRetries)
     
    void
    setMessageLengthDecoder(org.snmp4j.transport.MessageLengthDecoder messageLengthDecoder)
     
    protected <T> void
    setSocketOption(SocketOption<T> socketOption, T value)
     
    void
     

    Methods inherited from class org.snmp4j.transport.AbstractConnectionOrientedTransportMapping

    cancelNonServerSelectionKey, close, closeSockets, getConnectionTimeout, getGracefulShutdownTimeoutMillis, getMaxBusyLoops, getMaxInboundConnections, getSockets, isInboundConnectionAcceptable, isServerEnabled, setConnectionTimeout, setGracefulShutdownTimeoutMillis, setMaxBusyLoops, setMaxInboundConnections, setServerEnabled, setSocketOptions, timeoutSocket

    Methods inherited from class org.snmp4j.transport.AbstractTransportMapping

    addTransportAuditListener, addTransportListener, addTransportStateListener, fireConnectionStateChanged, fireProcessMessage, fireTransportAuditEvent, getAuditListenAddress, getListenWorkerTask, getMaxInboundMessageSize, getPriority, getSocketCleaner, getSuspendedAddresses, getThreadName, handleDroppedMessageToSend, isAsyncMsgProcessingSupported, isTransportAudit, removeAllTransportAuditListeners, removeAllTransportListeners, removeTransportAuditListener, removeTransportListener, removeTransportStateListener, resumeAddress, setAsyncMsgProcessingSupported, setPriority, setThreadName, suspendAddress

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.snmp4j.transport.ConnectionOrientedTransportMapping

    addTransportStateListener, getSocketCleaner, removeTransportStateListener, resumeAddress, suspendAddress

    Methods inherited from interface org.snmp4j.TransportMapping

    addTransportListener, getMaxInboundMessageSize, getMaxOutboundMessageSize, getSupportedTransportType, isAddressSupported, isAddressSupported, removeTransportListener
  • Field Details

    • listenAddress

      protected UnixDomainAddress listenAddress
      The Unix domain socket address to which this transport mapping is bound.
    • serverThread

      The server thread responsible for handling incoming connections.
    • boundSocketPath

      protected volatile Path boundSocketPath
      The socket path that has been bound by this transport mapping and that therefore has to be removed when this transport mapping is closed, or null if this transport mapping has not created a socket file. Only a socket file that has been created by this transport mapping is ever removed, so that the socket of another SNMP entity that uses the same path is left alone.
      Since:
      1.2.2
  • Constructor Details

    • UnixSocketStreamTransportMapping

      public UnixSocketStreamTransportMapping(UnixDomainAddress listenAddress)
      Constructs a UnixSocketStreamTransportMapping with the specified Unix domain socket address and server mode enabled, that is, incoming connections on that address are accepted.
      Parameters:
      listenAddress - the Unix domain socket address to which this transport mapping is bound.
    • UnixSocketStreamTransportMapping

      public UnixSocketStreamTransportMapping(UnixDomainAddress listenAddress, boolean serverEnabled)
      Constructs a UnixSocketStreamTransportMapping with the specified Unix domain socket address and enables server mode on request.

      In client mode (serverEnabled is false) no socket is bound to listen for incoming connections. Unlike an IP based transport mapping, which can bind an ephemeral port for its outgoing connections, a Unix domain socket path is the address itself: listening on it in client mode would occupy the path of the transport mapping that serves incoming connections on it and fail with "Address already in use". The outgoing socket of a client mode transport mapping is bound to the given address instead, so that the peer can tell this SNMP entity apart from other entities that connect to it - an unbound Unix domain socket has no address at all, thus all peers that connect without binding their socket look alike to the entity that accepts their connections. See UnixSocketStreamTransportMapping.ServerThread.bindOutgoingSocket(SocketChannel).

      Parameters:
      listenAddress - the Unix domain socket address to which this transport mapping is bound.
      serverEnabled - if true the server mode is enabled and incoming new connections are accepted. Use false to allow outgoing messages and their responses only (client mode).
      Since:
      1.2.2
    • UnixSocketStreamTransportMapping

      public UnixSocketStreamTransportMapping()
      Constructs a UnixSocketStreamTransportMapping without specifying a Unix domain socket address. This constructor is useful when the address will be set later.
  • Method Details

    • setSocketOption

      protected <T> void setSocketOption(SocketOption<T> socketOption, T value) throws IOException
      Specified by:
      setSocketOption in class org.snmp4j.transport.AbstractConnectionOrientedTransportMapping<UnixDomainAddress, UnixSocketStreamTransportMapping.SocketEntry>
      Throws:
      IOException
    • getMessageLengthDecoder

      public org.snmp4j.transport.MessageLengthDecoder getMessageLengthDecoder()
    • setMessageLengthDecoder

      public void setMessageLengthDecoder(org.snmp4j.transport.MessageLengthDecoder messageLengthDecoder)
    • getSupportedAddressClass

      public Class<? extends org.snmp4j.smi.Address> getSupportedAddressClass()
      Specified by:
      getSupportedAddressClass in interface org.snmp4j.TransportMapping<UnixDomainAddress>
      Specified by:
      getSupportedAddressClass in class org.snmp4j.transport.AbstractTransportMapping<UnixDomainAddress>
    • getSupportedAddressClasses

      public Set<Class<? extends org.snmp4j.smi.Address>> getSupportedAddressClasses()
    • getListenAddress

      public UnixDomainAddress getListenAddress()
      Specified by:
      getListenAddress in interface org.snmp4j.TransportMapping<UnixDomainAddress>
      Specified by:
      getListenAddress in class org.snmp4j.transport.AbstractTransportMapping<UnixDomainAddress>
    • sendMessage

      public void sendMessage(UnixDomainAddress address, byte[] message, org.snmp4j.TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) throws IOException
      Specified by:
      sendMessage in interface org.snmp4j.TransportMapping<UnixDomainAddress>
      Specified by:
      sendMessage in class org.snmp4j.transport.AbstractTransportMapping<UnixDomainAddress>
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface org.snmp4j.TransportMapping<UnixDomainAddress>
      Overrides:
      close in class org.snmp4j.transport.AbstractConnectionOrientedTransportMapping<UnixDomainAddress, UnixSocketStreamTransportMapping.SocketEntry>
      Throws:
      IOException
    • removeBoundSocketFile

      protected void removeBoundSocketFile() throws IOException
      Removes the socket file that this transport mapping has created, if any. A socket file that has not been created by this transport mapping is never removed, because it may belong to another SNMP entity that is listening on the same path.
      Throws:
      IOException - if the socket file exists but could not be removed.
      Since:
      1.2.2
    • close

      public boolean close(UnixDomainAddress remoteAddress) throws IOException
      Specified by:
      close in interface org.snmp4j.transport.ConnectionOrientedTransportMapping<UnixDomainAddress>
      Overrides:
      close in class org.snmp4j.transport.AbstractConnectionOrientedTransportMapping<UnixDomainAddress, UnixSocketStreamTransportMapping.SocketEntry>
      Throws:
      IOException
    • listen

      public void listen() throws IOException
      Specified by:
      listen in interface org.snmp4j.TransportMapping<UnixDomainAddress>
      Specified by:
      listen in class org.snmp4j.transport.AbstractTransportMapping<UnixDomainAddress>
      Throws:
      IOException
    • isListening

      public boolean isListening()
      Specified by:
      isListening in interface org.snmp4j.TransportMapping<UnixDomainAddress>
      Overrides:
      isListening in class org.snmp4j.transport.AbstractTransportMapping<UnixDomainAddress>
    • wakeupServerSelector

      public void wakeupServerSelector()
      Specified by:
      wakeupServerSelector in class org.snmp4j.transport.AbstractConnectionOrientedTransportMapping<UnixDomainAddress, UnixSocketStreamTransportMapping.SocketEntry>
    • readNextInPacket

      protected Optional<ByteBuffer> readNextInPacket(SocketChannel socketChannel) throws IOException
      Read the next network packet for the application.
      Parameters:
      socketChannel - the socket channel to read the data from.
      Returns:
      a byte buffer with the application data of the packet.
      Throws:
      IOException - if there occurs an IO exception during preparation.
      Since:
      3.7