Class NettyConnection

java.lang.Object
com.lucimber.dbus.netty.NettyConnection
All Implemented Interfaces:
Connection, AutoCloseable

public final class NettyConnection extends Object implements Connection
  • Constructor Details

  • Method Details

    • newSystemBusConnection

      public static NettyConnection newSystemBusConnection()
      Creates a connection for the standard system bus path. (Typically /var/run/dbus/system_bus_socket)
      Returns:
      A new instance.
      Throws:
      UnsupportedOperationException - if native transport for UDS is not available.
    • newSystemBusConnection

      public static NettyConnection newSystemBusConnection(ConnectionConfig config)
      Creates a connection for the standard system bus path with custom configuration. (Typically /var/run/dbus/system_bus_socket)
      Parameters:
      config - The connection configuration to use
      Returns:
      A new instance.
      Throws:
      UnsupportedOperationException - if native transport for UDS is not available.
    • newSessionBusConnection

      public static NettyConnection newSessionBusConnection()
      Creates a connection for the standard session bus path. (Path is usually obtained from DBUS_SESSION_BUS_ADDRESS env var)
      Returns:
      A new instance.
      Throws:
      UnsupportedOperationException - if native transport for UDS is not available or address not found.
    • newSessionBusConnection

      public static NettyConnection newSessionBusConnection(ConnectionConfig config)
      Creates a connection for the standard session bus path with custom configuration. (Path is usually obtained from DBUS_SESSION_BUS_ADDRESS env var)
      Parameters:
      config - The connection configuration to use
      Returns:
      A new instance.
      Throws:
      UnsupportedOperationException - if native transport for UDS is not available or address not found.
    • connect

      public CompletionStage<Void> connect()
      Description copied from interface: Connection
      Initiates a connection to a D-Bus instance.
      Specified by:
      connect in interface Connection
      Returns:
      a CompletionStage that completes when the connection is established, or exceptionally if the attempt fails.
    • isConnected

      public boolean isConnected()
      Description copied from interface: Connection
      Indicates whether the connection has been successfully established and is active.
      Specified by:
      isConnected in interface Connection
      Returns:
      true if the connection is active, false otherwise.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • getNextSerial

      public DBusUInt32 getNextSerial()
      Description copied from interface: Connection
      Generates and returns the next unique serial number for outbound messages.

      Serial numbers are used to correlate requests and replies and are unique per connection.

      Specified by:
      getNextSerial in interface Connection
      Returns:
      a unique DBusUInt32 serial number for an OutboundMessage.
    • getPipeline

      public Pipeline getPipeline()
      Description copied from interface: Connection
      Retrieves the associated Pipeline for this connection.
      Specified by:
      getPipeline in interface Connection
      Returns:
      the Pipeline instance used by this connection.
    • sendRequest

      public CompletionStage<InboundMessage> sendRequest(OutboundMessage msg)
      Description copied from interface: Connection
      Sends the given OutboundMessage over this connection, bypassing the pipeline.

      This method is intended for simple request-response interactions where no additional pipeline-based processing is needed. The returned CompletionStage is completed directly with the corresponding InboundMessage response or exceptionally if an error occurs.

      Note: Use this method only for straightforward communication scenarios that do not require handler involvement or advanced message routing.

      Specified by:
      sendRequest in interface Connection
      Parameters:
      msg - the outbound message to send.
      Returns:
      a CompletionStage that completes with the corresponding inbound response message, or fails exceptionally on error.
    • sendAndRouteResponse

      public void sendAndRouteResponse(OutboundMessage msg, CompletionStage<Void> future)
      Description copied from interface: Connection
      Sends the given OutboundMessage over the connection and completes the provided future when the message has been written to the D-Bus transport.

      The outbound message is transmitted directly over the connection and does not pass through the outbound pipeline. However, the corresponding InboundMessage response will be delivered through the pipeline, allowing it to be processed by registered InboundHandlers.

      This method is intended for scenarios where custom or advanced processing of responses is needed, while keeping message transmission efficient.

      Specified by:
      sendAndRouteResponse in interface Connection
      Parameters:
      msg - the outbound message to send.
      future - the CompletionStage to complete once the message is written or if an error occurs.
    • getConfig

      public ConnectionConfig getConfig()
      Description copied from interface: Connection
      Retrieves the configuration for this connection.
      Specified by:
      getConfig in interface Connection
      Returns:
      the ConnectionConfig instance used by this connection.
    • getState

      public ConnectionState getState()
      Description copied from interface: Connection
      Gets the current connection state.
      Specified by:
      getState in interface Connection
      Returns:
      the current ConnectionState
    • addConnectionEventListener

      public void addConnectionEventListener(ConnectionEventListener listener)
      Description copied from interface: Connection
      Adds a connection event listener to receive notifications about connection events.
      Specified by:
      addConnectionEventListener in interface Connection
      Parameters:
      listener - the listener to add
    • removeConnectionEventListener

      public void removeConnectionEventListener(ConnectionEventListener listener)
      Description copied from interface: Connection
      Removes a connection event listener.
      Specified by:
      removeConnectionEventListener in interface Connection
      Parameters:
      listener - the listener to remove
    • triggerHealthCheck

      public CompletionStage<Void> triggerHealthCheck()
      Description copied from interface: Connection
      Manually triggers a health check if health monitoring is enabled.
      Specified by:
      triggerHealthCheck in interface Connection
      Returns:
      a CompletionStage that completes when the health check is triggered
    • getReconnectAttemptCount

      public int getReconnectAttemptCount()
      Description copied from interface: Connection
      Gets the current number of reconnection attempts.
      Specified by:
      getReconnectAttemptCount in interface Connection
      Returns:
      the current reconnection attempt count
    • cancelReconnection

      public void cancelReconnection()
      Description copied from interface: Connection
      Cancels any pending reconnection attempts.
      Specified by:
      cancelReconnection in interface Connection
    • resetReconnectionState

      public void resetReconnectionState()
      Description copied from interface: Connection
      Resets the reconnection state, clearing attempt count and timers.
      Specified by:
      resetReconnectionState in interface Connection