Class AbstractTransport

java.lang.Object
org.freedesktop.dbus.connections.transports.AbstractTransport
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
AbstractUnixTransport, TcpTransport

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

    • TRANSPORT_ID_GENERATOR

      private static final AtomicLong TRANSPORT_ID_GENERATOR
    • spiLoader

      private final ServiceLoader<ISocketProvider> spiLoader
    • logger

      private final org.slf4j.Logger logger
    • address

      private final BusAddress address
    • transportConnection

      private TransportConnection transportConnection
    • fileDescriptorSupported

      private boolean fileDescriptorSupported
    • transportId

      private final long transportId
    • config

      private final TransportConfig config
  • Constructor Details

  • Method Details

    • writeMessage

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

      public Message readMessage() throws IOException, DBusException
      Read a message from the underlying socket.
      Returns:
      read message, maybe null
      Throws:
      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 SocketChannel connectImpl() throws IOException
      Abstract method implemented by concrete sub classes to establish a connection using whatever transport type (e.g. TCP/Unix socket).
      Throws:
      IOException - when connection fails
    • connect

      public final SocketChannel connect() throws 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:
      IOException - if connection fails
    • listen

      public final TransportConnection listen() throws IOException
      Start listening on created transport.

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

      Will return the TransportConnection as soon as a client connects.
      Therefore this method should be called in a loop to accept multiple clients

      Returns:
      TransportConnection containing created SocketChannel and IMessageReader/IMessageWriter
      Throws:
      IOException - if connection fails
    • internalConnect

      private TransportConnection internalConnect() throws IOException
      Throws:
      IOException
    • setPreConnectCallback

      public void setPreConnectCallback(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(SocketChannel _sock) throws IOException
      Helper method to authenticate to DBus using SASL.
      Parameters:
      _sock - socketchannel
      Throws:
      IOException - on any error
    • createInputOutput

      private TransportConnection createInputOutput(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 String toString()
      Overrides:
      toString in class Object
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException