Class AbstractTransport

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    AbstractUnixTransport, TcpTransport

    public abstract class AbstractTransport
    extends java.lang.Object
    implements java.io.Closeable
    Base class for all transport types.
    Since:
    v3.2.0 - 2019-02-08
    • Field Detail

      • TRANSPORT_ID_GENERATOR

        private static final java.util.concurrent.atomic.AtomicLong TRANSPORT_ID_GENERATOR
      • spiLoader

        private final java.util.ServiceLoader<ISocketProvider> spiLoader
      • logger

        private final org.slf4j.Logger logger
      • fileDescriptorSupported

        private boolean fileDescriptorSupported
      • transportId

        private final long transportId
    • Method Detail

      • writeMessage

        public void writeMessage​(Message _msg)
                          throws java.io.IOException
        Write a message to the underlying socket.
        Parameters:
        _msg - message to write
        Throws:
        java.io.IOException - on write error or if output was already closed or null
      • readMessage

        public Message readMessage()
                            throws java.io.IOException,
                                   DBusException
        Read a message from the underlying socket.
        Returns:
        read message, maybe null
        Throws:
        java.io.IOException - when input already close or null
        DBusException - when message could not be converted to a DBus message
      • isConnected

        public boolean isConnected()
        Returns true if inputReader and outputWriter are not yet closed.
        Returns:
        boolean
      • hasFileDescriptorSupport

        protected abstract boolean hasFileDescriptorSupport()
        Method to indicate if passing of file descriptors is allowed.
        Returns:
        true to allow FD passing, false otherwise
      • isAbstractAllowed

        @Deprecated(forRemoval=true,
                    since="4.2.0 - 2022-07-18")
        protected abstract boolean isAbstractAllowed()
        Deprecated, for removal: This API element is subject to removal in a future version.
        Is no longer used and will be removed
        Return true if the transport supports 'abstract' sockets.
        Returns:
        true if abstract sockets supported, false otherwise
      • connectImpl

        protected abstract java.nio.channels.SocketChannel connectImpl()
                                                                throws java.io.IOException
        Abstract method implemented by concrete sub classes to establish a connection using whatever transport type (e.g. TCP/Unix socket).
        Throws:
        java.io.IOException - when connection fails
      • connect

        public final java.nio.channels.SocketChannel connect()
                                                      throws java.io.IOException
        Establish connection on created transport.

        This method can only be used for non-listening connections.
        Trying to use this with listening addresses will throw an InvalidBusAddressException.

        Returns:
        SocketChannel of the created connection
        Throws:
        java.io.IOException - if connection fails
      • internalConnect

        private TransportConnection internalConnect()
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • setPreConnectCallback

        public void setPreConnectCallback​(java.util.function.Consumer<AbstractTransport> _run)
        Set a callback which will be called right before the connection will be established to the transport.
        Parameters:
        _run - runnable to execute, null if no callback should be executed
        Since:
        4.2.0 - 2022-07-20
      • authenticate

        private void authenticate​(java.nio.channels.SocketChannel _sock)
                           throws java.io.IOException
        Helper method to authenticate to DBus using SASL.
        Parameters:
        _sock - socketchannel
        Throws:
        java.io.IOException - on any error
      • createInputOutput

        private TransportConnection createInputOutput​(java.nio.channels.SocketChannel _socket)
        Setup message reader/writer. Will look for SPI provider first, if none is found default implementation is used. The default implementation does not support file descriptor passing!
        Parameters:
        _socket - socket to use
        Returns:
        TransportConnection with configured socket channel, reader and writer
      • getAddress

        protected BusAddress getAddress()
        Returns the BusAddress used for this transport.
        Returns:
        BusAddress, never null
      • getLogger

        protected org.slf4j.Logger getLogger()
        Get the logger in subclasses.
        Returns:
        Logger, never null
      • getSaslConfig

        protected SaslConfig getSaslConfig()
        Returns the current configuration used for SASL authentication.
        Returns:
        SaslConfig, never null
      • getSaslAuthMode

        @Deprecated(since="4.2.0 - 2022-07-22",
                    forRemoval=true)
        protected int getSaslAuthMode()
        Deprecated, for removal: This API element is subject to removal in a future version.
        please use getSaslConfig().getAuthMode() instead
        Set the SASL authentication mode.
      • getSaslMode

        @Deprecated(since="4.2.0 - 2022-07-22",
                    forRemoval=true)
        protected SASL.SaslMode getSaslMode()
        Deprecated, for removal: This API element is subject to removal in a future version.
        please use getSaslConfig().getMode() instead
        Set the SASL authentication mode.
      • setSaslMode

        @Deprecated(since="4.2.0 - 2022-07-22",
                    forRemoval=true)
        protected void setSaslMode​(SASL.SaslMode _saslMode)
        Deprecated, for removal: This API element is subject to removal in a future version.
        please use getSaslConfig().setMode(int) instead
        Set the SASL mode (server or client).
        Parameters:
        _saslMode - mode to set
      • setSaslAuthMode

        @Deprecated(since="4.2.0 - 2022-07-22",
                    forRemoval=true)
        protected void setSaslAuthMode​(int _mode)
        Deprecated, for removal: This API element is subject to removal in a future version.
        please use getSaslConfig().setSaslAuthMode(int) instead
        Set the SASL authentication mode.
        Parameters:
        _mode - mode to set
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException