Class AbstractTransport

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

public abstract class AbstractTransport extends Object implements Closeable
Base class for all transport types.
Since:
v3.2.0 - 2019-02-08
Author:
hypfvieh
  • 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
    • connectImpl

      protected abstract SocketChannel connectImpl() throws IOException
      Abstract method implemented by concrete sub classes to establish a connection.
      Returns:
      socket channel connected to DBus server
      Throws:
      IOException - when connection fails
    • acceptImpl

      protected abstract SocketChannel acceptImpl() throws IOException
      Method to accept new incoming listening connections.
      This is the place where accept() is called on the server socket created by bindImpl().
      Therefore this method will block until a client is connected.
      Returns:
      newly connected client socket
      Throws:
      IOException - when connection fails
      Since:
      5.0.0 - 2023-10-20
    • bindImpl

      protected abstract void bindImpl() throws IOException
      Method called to prepare listening for connections.
      This is usually the place where the ServerSocketChannel is created and bind() is called.
      Throws:
      IOException - when connection fails
      Since:
      5.0.0 - 2023-10-20
    • closeTransport

      protected abstract void closeTransport() throws IOException
      Method which is called to close a transport.
      Should be used to close all sockets and/or serversockets.
      Throws:
      IOException - when something fails while closing transport
      Since:
      5.0.0 - 2023-10-20
    • isBound

      protected abstract boolean isBound()
      Status of the server socket if this transport is configured to be a server connection.
      Must be false if bindImpl() was not called.
      Returns:
      boolean
      Since:
      5.0.0 - 2023-10-20
    • 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
    • isListening

      public final boolean isListening()
      True if this transport connection is a listening (server) connection.
      Returns:
      boolean
    • 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
    • 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
    • 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
    • getTransportConnection

      public TransportConnection getTransportConnection()
      Returns the current transport connection.
      Returns:
      TransportConnection, null if not connected yet
    • getMessageFactory

      public MessageFactory getMessageFactory()
      Currently configured message factory.
      Returns:
      factory
    • getTransportConfig

      public TransportConfig getTransportConfig()
    • isFileDescriptorSupported

      public boolean isFileDescriptorSupported()
    • toString

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

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