Top Description Fields Constructors Methods
org.freedesktop.dbus.connections.transports

public abstract Class AbstractTransport

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
org.freedesktop.dbus.connections.transports.AbstractUnixTransport
Imports
org.freedesktop.dbus.connections.BusAddress, .SASL, org.freedesktop.dbus.connections.config.SaslConfig, .TransportConfig, org.freedesktop.dbus.exceptions.AuthenticationException, .DBusException, .InvalidBusAddressException, org.freedesktop.dbus.messages.Message, .MessageFactory, org.freedesktop.dbus.messages.constants.ArgumentType, org.freedesktop.dbus.spi.message.*, org.freedesktop.dbus.utils.IThrowingSupplier, org.slf4j.Logger, .LoggerFactory, java.io.Closeable, .IOException, java.nio.channels.SocketChannel, java.util.Objects, .Optional, .ServiceConfigurationError, .ServiceLoader, java.util.concurrent.atomic.AtomicLong, java.util.function.Consumer

Base class for all transport types.
Author
hypfvieh
Since
v3.2.0 - 2019-02-08

Field Summary

Modifier and TypeField and Description
private final BusAddress
private final TransportConfig
private boolean
private final Logger
private final MessageFactory
private final ServiceLoader<ISocketProvider>
private static final AtomicLong
private TransportConnection
private final long

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
protected abstract SocketChannel

Returns:

newly connected client socket
acceptImpl
()

Method to accept new incoming listening connections.

private void
authenticate(SocketChannel
socketchannel
_sock
)

Helper method to authenticate to DBus using SASL.

protected abstract void
bindImpl()

Method called to prepare listening for connections.

public final void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.

protected abstract void
closeTransport()

Method which is called to close a transport.

public final SocketChannel

Returns:

SocketChannel of the created connection
connect
()

Establish connection on created transport.

protected abstract SocketChannel

Returns:

socket channel connected to DBus server
connectImpl
()

Abstract method implemented by concrete sub classes to establish a connection.

private TransportConnection

Returns:

TransportConnection with configured socket channel, reader and writer
createInputOutput
(SocketChannel
socket to use
_socket
)

Setup message reader/writer.

protected BusAddress

Returns:

BusAddress, never null
getAddress
()

Returns the BusAddress used for this transport.

protected Logger

Returns:

Logger, never null
getLogger
()

Get the logger in subclasses.

public MessageFactory

Returns:

factory
getMessageFactory
()

Currently configured message factory.

protected SaslConfig

Returns:

SaslConfig, never null
getSaslConfig
()

Returns the current configuration used for SASL authentication.

public TransportConfig
public TransportConnection

Returns:

TransportConnection, null if not connected yet
getTransportConnection
()

Returns the current transport connection.

protected abstract boolean

Returns:

true to allow FD passing, false otherwise
hasFileDescriptorSupport
()

Method to indicate if passing of file descriptors is allowed.

private TransportConnection

Returns:

TransportConnection
internalConnect
(IThrowingSupplier<SocketChannel, IOException>
listen or connect call which will return a socket channel
_channelProvider
)

Method used internally to do the actual connect.

protected abstract boolean

Returns:

boolean
isBound
()

Status of the server socket if this transport is configured to be a server connection.

public synchronized boolean

Returns:

boolean
isConnected
()

Returns true if inputReader and outputWriter are not yet closed.

public boolean
public final boolean

Returns:

boolean
isListening
()

True if this transport connection is a listening (server) connection.

public final TransportConnection
listen()

Start listening on created transport.

public Message

Returns:

read message, maybe null
readMessage
()

Read a message from the underlying socket.

private void
runCallback(Consumer<AbstractTransport>
callback to execute
_callback
)

Runs a callback if not null.

public void
setPreConnectCallback(Consumer<AbstractTransport>
runnable to execute, null if no callback should be executed
_run
)

Set a callback which will be called right before the connection will be established to the transport.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public void
writeMessage(Message
message to write
_msg
)

Write a message to the underlying socket.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

addressback to summary
private final BusAddress address
configback to summary
private final TransportConfig config
fileDescriptorSupportedback to summary
private boolean fileDescriptorSupported
loggerback to summary
private final Logger logger
messageFactoryback to summary
private final MessageFactory messageFactory
spiLoaderback to summary
private final ServiceLoader<ISocketProvider> spiLoader
TRANSPORT_ID_GENERATORback to summary
private static final AtomicLong TRANSPORT_ID_GENERATOR
transportConnectionback to summary
private TransportConnection transportConnection
transportIdback to summary
private final long transportId

Constructor Detail

AbstractTransportback to summary
protected AbstractTransport(BusAddress _address, TransportConfig _config)

Method Detail

acceptImplback to summary
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:SocketChannel

newly connected client socket

Exceptions
IOException:
when connection fails
Since
5.0.0 - 2023-10-20
authenticateback to summary
private void authenticate(SocketChannel _sock) throws IOException

Helper method to authenticate to DBus using SASL.

Parameters
_sock:SocketChannel

socketchannel

Exceptions
IOException:
on any error
bindImplback to summary
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.

Exceptions
IOException:
when connection fails
Since
5.0.0 - 2023-10-20
closeback to summary
public final void close() throws IOException

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
closeTransportback to summary
protected abstract void closeTransport() throws IOException

Method which is called to close a transport.
Should be used to close all sockets and/or serversockets.

Exceptions
IOException:
when something fails while closing transport
Since
5.0.0 - 2023-10-20
connectback to summary
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

SocketChannel of the created connection

Exceptions
IOException:
if connection fails
connectImplback to summary
protected abstract SocketChannel connectImpl() throws IOException

Abstract method implemented by concrete sub classes to establish a connection.

Returns:SocketChannel

socket channel connected to DBus server

Exceptions
IOException:
when connection fails
createInputOutputback to summary
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:SocketChannel

socket to use

Returns:TransportConnection

TransportConnection with configured socket channel, reader and writer

getAddressback to summary
protected BusAddress getAddress()

Returns the BusAddress used for this transport.

Returns:BusAddress

BusAddress, never null

getLoggerback to summary
protected Logger getLogger()

Get the logger in subclasses.

Returns:Logger

Logger, never null

getMessageFactoryback to summary
public MessageFactory getMessageFactory()

Currently configured message factory.

Returns:MessageFactory

factory

getSaslConfigback to summary
protected SaslConfig getSaslConfig()

Returns the current configuration used for SASL authentication.

Returns:SaslConfig

SaslConfig, never null

getTransportConfigback to summary
public TransportConfig getTransportConfig()
getTransportConnectionback to summary
public TransportConnection getTransportConnection()

Returns the current transport connection.

Returns:TransportConnection

TransportConnection, null if not connected yet

hasFileDescriptorSupportback to summary
protected abstract boolean hasFileDescriptorSupport()

Method to indicate if passing of file descriptors is allowed.

Returns:boolean

true to allow FD passing, false otherwise

internalConnectback to summary
private TransportConnection internalConnect(IThrowingSupplier<SocketChannel, IOException> _channelProvider) throws IOException

Method used internally to do the actual connect.

Parameters
_channelProvider:IThrowingSupplier<SocketChannel, IOException>

listen or connect call which will return a socket channel

Returns:TransportConnection

TransportConnection

Exceptions
IOException:
when channel provider could not create a SocketChannel
isBoundback to summary
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

boolean

Since
5.0.0 - 2023-10-20
isConnectedback to summary
public synchronized boolean isConnected()

Returns true if inputReader and outputWriter are not yet closed.

Returns:boolean

boolean

isFileDescriptorSupportedback to summary
public boolean isFileDescriptorSupported()
isListeningback to summary
public final boolean isListening()

True if this transport connection is a listening (server) connection.

Returns:boolean

boolean

listenback to summary
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

TransportConnection containing created SocketChannel and IMessageReader/IMessageWriter

Exceptions
IOException:
if connection fails
readMessageback to summary
public Message readMessage() throws IOException, DBusException

Read a message from the underlying socket.

Returns:Message

read message, maybe null

Exceptions
IOException:
when input already close or null
DBusException:
when message could not be converted to a DBus message
runCallbackback to summary
private void runCallback(Consumer<AbstractTransport> _callback)

Runs a callback if not null.

Parameters
_callback:Consumer<AbstractTransport>

callback to execute

setPreConnectCallbackback to summary
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:Consumer<AbstractTransport>

runnable to execute, null if no callback should be executed

Since
4.2.0 - 2022-07-20
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
writeMessageback to summary
public void writeMessage(Message _msg) throws IOException

Write a message to the underlying socket.

Parameters
_msg:Message

message to write

Exceptions
IOException:
on write error or if output was already closed or null