Package com.lucimber.dbus.connection
This package provides the core abstractions for establishing and managing D-Bus connections. It implements a pipeline-based architecture that allows for extensible message processing through custom handlers.
Getting Started
For first-time users: Start with Connection
interface documentation,
then see NettyConnection
for the concrete implementation. The
ConnectionConfig
class shows all available configuration options.
Core Concepts
Connection
The Connection
interface is the main entry point for D-Bus communication. It provides
methods for:
- Establishing connections to D-Bus daemons
- Sending method calls and receiving responses
- Managing connection lifecycle and health
- Accessing the handler pipeline
Pipeline Architecture
The handler pipeline processes messages in a configurable chain:
// Add custom handlers to the pipeline
Pipeline pipeline = connection.getPipeline();
pipeline.addLast("logger", new LoggingHandler());
pipeline.addLast("auth", new AuthenticationHandler());
pipeline.addLast("app", new ApplicationHandler());
Handler Types
InboundHandler
- Processes messages received from the D-Bus daemon
OutboundHandler
- Processes messages being sent to the D-Bus daemon
- Duplex Handlers (extend
AbstractDuplexHandler
) - Handle both inbound and outbound messages
Configuration
Connection behavior can be customized through ConnectionConfig
:
ConnectionConfig config = ConnectionConfig.builder()
.withAutoReconnectEnabled(true)
.withReconnectInitialDelay(Duration.ofSeconds(1))
.withMaxReconnectAttempts(10)
.withHealthCheckEnabled(true)
.withHealthCheckInterval(Duration.ofSeconds(30))
.withConnectTimeout(Duration.ofSeconds(10))
.withMethodCallTimeout(Duration.ofSeconds(30))
.build();
Connection Lifecycle
Connections follow a well-defined lifecycle:
- Creation: Connection instance is created with configuration
- Connection:
Connection.connect()
establishes the D-Bus connection - Authentication: SASL authentication is performed automatically
- Ready: Connection is ready for message exchange
- Health Monitoring: Optional health checks ensure connection validity
- Reconnection: Automatic reconnection on connection loss (if enabled)
- Shutdown:
AutoCloseable.close()
cleanly closes the connection
Event Handling
Connection events can be monitored through ConnectionEventListener
:
connection.addConnectionEventListener((conn, event) -> {
switch (event.getType()) {
case CONNECTED:
System.out.println("Connected to D-Bus");
break;
case DISCONNECTED:
System.out.println("Disconnected from D-Bus");
break;
case RECONNECTION_ATTEMPT:
System.out.println("Attempting reconnection...");
break;
}
});
Handler Examples
Simple Inbound Handler
public class LoggingHandler extends AbstractInboundHandler {
@Override
public void handleInboundMessage(Context ctx, InboundMessage msg) {
System.out.println("Received: " + msg.getType());
ctx.propagateInboundMessage(msg); // Always propagate
}
}
Signal Filter Handler
public class SignalFilterHandler extends AbstractInboundHandler {
private final Set<String> interestedInterfaces;
@Override
public void handleInboundMessage(Context ctx, InboundMessage msg) {
if (msg instanceof InboundSignal) {
InboundSignal signal = (InboundSignal) msg;
String interfaceName = signal.getInterfaceName()
.map(DBusString::toString)
.orElse("");
if (interestedInterfaces.contains(interfaceName)) {
ctx.propagateInboundMessage(msg);
}
// Filtered signals are not propagated
} else {
ctx.propagateInboundMessage(msg);
}
}
}
SASL Authentication
The com.lucimber.dbus.connection.sasl
sub-package provides SASL authentication
mechanisms for secure D-Bus communication.
-
ClassDescriptionAn abstract base class that combines both
InboundHandler
andOutboundHandler
interfaces to handle bidirectional message flow in aPipeline
.A skeletal implementation of theInboundHandler
interface.A skeletal implementation of theOutboundHandler
interface.Represents a virtual channel for communication over D-Bus.Configuration class for D-Bus connections containing timeout and other connection settings.Builder class for creating ConnectionConfig instances.Context interface providing callbacks and handlers for connection strategies.Represents a connection lifecycle event.Builder for creatingConnectionEvent
instances.Listener interface for connection lifecycle events.Types of connection events that can be fired during the connection lifecycle.Transport-agnostic handle representing an active D-Bus connection.A connection handler that monitors the health of a D-Bus connection by performing periodic ping operations using the standard D-Bus Peer.Ping method.Manages the lifecycle of a connection including connect, disconnect, and state transitions.Functional interface for connection operations.Functional interface for disconnection operations.A connection handler that implements automatic reconnection with exponential backoff when connection failures are detected.Represents the current state of a D-Bus connection.Strategy interface for different D-Bus connection transport types.Registry for connection strategies that automatically selects the appropriate strategy based on the socket address type and platform availability.Manages thread pools for D-Bus connections.A thread-safe pipeline ofOutboundHandler
instances associated with aConnection
.A dummy implementation ofConnection
for testing D-Bus applications without requiring a real D-Bus daemon, similar to Netty'sEmbeddedChannel
.Builder for creating DummyConnection instances with custom configuration.A handler that can be attached to aPipeline
to intercept and respond to connection state changes and user-defined events.Defines a component that can forward inbound events through aPipeline
.Defines a component that can forward outbound events through aPipeline
.Represents an ordered chain ofHandler
instances associated with aConnection
.Defines a component that can propagate user-defined events through aPipeline
.