Package org.freedesktop.dbus.connections
Class AbstractConnection
- java.lang.Object
-
- org.freedesktop.dbus.connections.AbstractConnection
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
DBusConnection,DirectConnection
public abstract class AbstractConnection extends java.lang.Object implements java.io.CloseableHandles a connection to DBus.
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.regex.PatternBUSNAME_REGEXprivate PendingCallbackManagercallbackManagerstatic java.util.regex.PatternCONNID_REGEXprivate java.util.Optional<IDisconnectCallback>disconnectCallbackprivate booleandisconnectingstatic java.util.regex.PatternDOLLAR_PATTERNprivate static byteendiannessLame method to setup endianness used on DBus messagesprivate java.util.Map<java.lang.String,ExportedObject>exportedObjectsprivate FallbackContainerfallbackContainerstatic booleanFLOAT_SUPPORTprivate java.util.Map<DBusMatchRule,java.util.Queue<DBusSigHandler<DBusSignal>>>genericHandledSignalsprivate java.util.Map<DBusMatchRule,java.util.Queue<DBusSigHandler<? extends DBusSignal>>>handledSignalsprivate java.util.Map<DBusInterface,RemoteObject>importedObjectsprivate static java.util.Map<java.lang.Thread,DBusCallInfo>INFOMAPprivate org.slf4j.Loggerloggerstatic intMAX_ARRAY_LENGTHstatic intMAX_NAME_LENGTHstatic java.util.regex.PatternOBJECT_REGEX_PATTERNprivate ObjectTreeobjectTreeprivate java.util.Map<java.lang.Long,MethodCall>pendingCallsprivate java.util.Queue<Error>pendingErrorQueueprivate IncomingMessageThreadreaderThreadprivate ReceivingServicereceivingServiceprivate java.util.concurrent.ExecutorServicesenderServicestatic java.lang.StringTCP_ADDRESS_PROPERTYDeprecated, for removal: This API element is subject to removal in a future version.is no longer in usestatic intTCP_CONNECT_TIMEOUTDeprecated, for removal: This API element is subject to removal in a future version.no longer usedprivate AbstractTransporttransportprivate TransportBuildertransportBuilderprivate booleanweakreferences
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractConnection(TransportConfig _transportConfig, ReceivingServiceConfig _rsCfg)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddFallback(java.lang.String _objectPrefix, DBusInterface _object)Export an object as a fallback object.protected abstract java.lang.AutoCloseableaddGenericSigHandler(DBusMatchRule _rule, DBusSigHandler<DBusSignal> _handler)Adds aDBusMatchRuleto with a generic signal handler.<T extends DBusSignal>
java.lang.AutoCloseableaddSigHandler(java.lang.Class<T> _type, DBusInterface _object, DBusSigHandler<T> _handler)Add a Signal Handler.<T extends DBusSignal>
java.lang.AutoCloseableaddSigHandler(java.lang.Class<T> _type, DBusSigHandler<T> _handler)Add a Signal Handler.protected abstract <T extends DBusSignal>
java.lang.AutoCloseableaddSigHandler(DBusMatchRule _rule, DBusSigHandler<T> _handler)Add a signal handler with the givenDBusMatchRuleto DBus.protected <T extends DBusSignal>
voidaddSigHandlerWithoutMatch(java.lang.Class<? extends DBusSignal> _signal, DBusSigHandler<T> _handler)DBusAsyncReply<?>callMethodAsync(DBusInterface _object, java.lang.String _method, java.lang.Object... _parameters)Call a method asynchronously and get a handle with which to get the reply.<A> voidcallWithCallback(DBusInterface _object, java.lang.String _m, CallbackHandler<A> _callback, java.lang.Object... _parameters)Call a method asynchronously and set a callback.voidchangeThreadCount(byte _newPoolSize)Deprecated, for removal: This API element is subject to removal in a future version.does nothing as threading has been changed significantlyvoidclose()Disconnect this session (for use in try-with-resources).private static java.lang.Class<?>[]createTypesArray(java.lang.Object... _parameters)voiddisconnect()Disconnect from the Bus.protected voiddisconnect(IDisconnectAction _before, IDisconnectAction _after)Special disconnect method which may be used whenever some cleanup before or after disconnection to DBus is required.voidexportObject(java.lang.String _objectPath, DBusInterface _object)Export an object so that its methods can be called on DBus.voidexportObject(DBusInterface _object)Export an object so that its methods can be called on DBus.protected <T extends DBusInterface>
java.util.List<java.lang.Class<?>>findMatchingTypes(java.lang.Class<T> _type, java.util.List<java.lang.String> _ifaces)If given type is null, will try to find suitable types by examining the given ifaces.BusAddressgetAddress()Returns the address this connection is connected to.static DBusCallInfogetCallInfo()Returns a structure with information on the current method call.IDisconnectCallbackgetDisconnectCallback()Returns the currently configured disconnect callback.static bytegetEndianness()Get current endianness to use.DBusExecutionExceptiongetError()Return any DBus error which has been received.abstract DBusInterfacegetExportedObject(java.lang.String _source, java.lang.String _path)Retrieves an remote object using source and path.abstract <T extends DBusInterface>
TgetExportedObject(java.lang.String _source, java.lang.String _path, java.lang.Class<T> _type)Retrieves an remote object using source and path.java.lang.StringgetExportedObject(DBusInterface _interface)protected java.util.Map<java.lang.String,ExportedObject>getExportedObjects()(package private) FallbackContainergetFallbackContainer()protected java.util.Map<DBusMatchRule,java.util.Queue<DBusSigHandler<DBusSignal>>>getGenericHandledSignals()protected java.util.Map<DBusMatchRule,java.util.Queue<DBusSigHandler<? extends DBusSignal>>>getHandledSignals()protected java.util.Map<DBusInterface,RemoteObject>getImportedObjects()abstract java.lang.StringgetMachineId()The generated UUID of this machine.protected ObjectTreegetObjectTree()protected java.util.Map<java.lang.Long,MethodCall>getPendingCalls()protected java.util.Queue<Error>getPendingErrorQueue()static bytegetSystemEndianness()Get the default system endianness.protected voidhandleException(Message _methodOrSignal, DBusExecutionException _exception)private voidhandleMessage(Error _err)private voidhandleMessage(DBusSignal _signal, boolean _useThreadPool)Handle a signal received on DBus.(package private) voidhandleMessage(Message _message)Handle received message from DBus.private voidhandleMessage(MethodCall _methodCall)private voidhandleMessage(MethodReturn _mr)protected voidinternalDisconnect(java.io.IOException _connectionError)Disconnects the DBus session.booleanisConnected()protected voidlisten()Start reading and sending messages.voidqueueCallback(MethodCall _call, java.lang.reflect.Method _method, CallbackHandler<?> _callback)(package private) MessagereadIncoming()voidremoveFallback(java.lang.String _objectprefix)Remove a fallbackprotected abstract voidremoveGenericSigHandler(DBusMatchRule _rule, DBusSigHandler<DBusSignal> _handler)Remove a generic signal handler with the givenDBusMatchRule.<T extends DBusSignal>
voidremoveSigHandler(java.lang.Class<T> _type, DBusInterface _object, DBusSigHandler<T> _handler)Remove a Signal Handler.<T extends DBusSignal>
voidremoveSigHandler(java.lang.Class<T> _type, DBusSigHandler<T> _handler)Remove a Signal Handler.protected abstract <T extends DBusSignal>
voidremoveSigHandler(DBusMatchRule _rule, DBusSigHandler<T> _handler)Remove a match rule with the givenDBusSigHandler.voidsendMessage(Message _message)Send a message or signal to the DBus daemon.private voidsendMessageInternally(Message _message)Send a message to DBus.voidsetDisconnectCallback(IDisconnectCallback _disconnectCallback)Set the callback which will be notified when a disconnection happens.static voidsetEndianness(byte _b)Set the endianness to use for all connections.voidsetWeakReferences(boolean _weakreferences)If set to true the bus will not hold a strong reference to exported objects.java.lang.StringtoString()voidunExportObject(java.lang.String _objectpath)Stop Exporting an object
-
-
-
Field Detail
-
FLOAT_SUPPORT
public static final boolean FLOAT_SUPPORT
-
BUSNAME_REGEX
public static final java.util.regex.Pattern BUSNAME_REGEX
-
CONNID_REGEX
public static final java.util.regex.Pattern CONNID_REGEX
-
OBJECT_REGEX_PATTERN
public static final java.util.regex.Pattern OBJECT_REGEX_PATTERN
-
DOLLAR_PATTERN
public static final java.util.regex.Pattern DOLLAR_PATTERN
-
MAX_ARRAY_LENGTH
public static final int MAX_ARRAY_LENGTH
- See Also:
- Constant Field Values
-
MAX_NAME_LENGTH
public static final int MAX_NAME_LENGTH
- See Also:
- Constant Field Values
-
TCP_CONNECT_TIMEOUT
@Deprecated(forRemoval=true, since="4.2.2 - 2022-12-23") public static final int TCP_CONNECT_TIMEOUTDeprecated, for removal: This API element is subject to removal in a future version.no longer usedConnect timeout, used for TCP only.- See Also:
- Constant Field Values
-
TCP_ADDRESS_PROPERTY
@Deprecated(since="4.2.0 - 2022-08-04", forRemoval=true) public static final java.lang.String TCP_ADDRESS_PROPERTYDeprecated, for removal: This API element is subject to removal in a future version.is no longer in useSystem property name containing the DBUS TCP SESSION address used by dbus-java DBusDaemon in TCP mode.- See Also:
- Constant Field Values
-
INFOMAP
private static final java.util.Map<java.lang.Thread,DBusCallInfo> INFOMAP
-
endianness
private static byte endianness
Lame method to setup endianness used on DBus messages
-
logger
private final org.slf4j.Logger logger
-
objectTree
private final ObjectTree objectTree
-
exportedObjects
private final java.util.Map<java.lang.String,ExportedObject> exportedObjects
-
importedObjects
private final java.util.Map<DBusInterface,RemoteObject> importedObjects
-
callbackManager
private final PendingCallbackManager callbackManager
-
fallbackContainer
private final FallbackContainer fallbackContainer
-
pendingErrorQueue
private final java.util.Queue<Error> pendingErrorQueue
-
handledSignals
private final java.util.Map<DBusMatchRule,java.util.Queue<DBusSigHandler<? extends DBusSignal>>> handledSignals
-
genericHandledSignals
private final java.util.Map<DBusMatchRule,java.util.Queue<DBusSigHandler<DBusSignal>>> genericHandledSignals
-
pendingCalls
private final java.util.Map<java.lang.Long,MethodCall> pendingCalls
-
readerThread
private final IncomingMessageThread readerThread
-
senderService
private final java.util.concurrent.ExecutorService senderService
-
receivingService
private final ReceivingService receivingService
-
transportBuilder
private final TransportBuilder transportBuilder
-
weakreferences
private boolean weakreferences
-
disconnecting
private volatile boolean disconnecting
-
transport
private AbstractTransport transport
-
disconnectCallback
private java.util.Optional<IDisconnectCallback> disconnectCallback
-
-
Constructor Detail
-
AbstractConnection
protected AbstractConnection(TransportConfig _transportConfig, ReceivingServiceConfig _rsCfg) throws DBusException
- Throws:
DBusException
-
-
Method Detail
-
getExportedObject
public abstract DBusInterface getExportedObject(java.lang.String _source, java.lang.String _path) throws DBusException
Retrieves an remote object using source and path. Will try to find suitable exported DBusInterface automatically.- Parameters:
_source- source_path- path- Returns:
DBusInterfacecompatible object- Throws:
DBusException
-
getExportedObject
public abstract <T extends DBusInterface> T getExportedObject(java.lang.String _source, java.lang.String _path, java.lang.Class<T> _type) throws DBusException
Retrieves an remote object using source and path. Will use the given type as object class.- Parameters:
_source- source_path- path_type- class of remote object- Returns:
DBusInterfacecompatible object- Throws:
DBusException
-
removeSigHandler
protected abstract <T extends DBusSignal> void removeSigHandler(DBusMatchRule _rule, DBusSigHandler<T> _handler) throws DBusException
Remove a match rule with the givenDBusSigHandler. The rule will only be removed from DBus if no other additional handlers are registered to the same rule.- Type Parameters:
T- signal type- Parameters:
_rule- rule to remove_handler- handler to remove- Throws:
DBusException- on error
-
addSigHandler
protected abstract <T extends DBusSignal> java.lang.AutoCloseable addSigHandler(DBusMatchRule _rule, DBusSigHandler<T> _handler) throws DBusException
Add a signal handler with the givenDBusMatchRuleto DBus. The rule will be added to DBus if it was not added before. If the rule was already added, the signal handler is added to the internal map receiving the same signal as the first (and additional) handlers for this rule.- Type Parameters:
T- signal type- Parameters:
_rule- rule to add_handler- handler to use- Returns:
- closeable that removes signal handler
- Throws:
DBusException- on error
-
removeGenericSigHandler
protected abstract void removeGenericSigHandler(DBusMatchRule _rule, DBusSigHandler<DBusSignal> _handler) throws DBusException
Remove a generic signal handler with the givenDBusMatchRule. The rule will only be removed from DBus if no other additional handlers are registered to the same rule.- Parameters:
_rule- rule to remove_handler- handler to remove- Throws:
DBusException- on error
-
addGenericSigHandler
protected abstract java.lang.AutoCloseable addGenericSigHandler(DBusMatchRule _rule, DBusSigHandler<DBusSignal> _handler) throws DBusException
Adds aDBusMatchRuleto with a generic signal handler. Generic signal handlers allow receiving different signals with the same handler. If the rule was already added, the signal handler is added to the internal map receiving the same signal as the first (and additional) handlers for this rule.- Parameters:
_rule- rule to add_handler- handler to use- Returns:
- closeable that removes signal handler
- Throws:
DBusException- on error
-
getMachineId
public abstract java.lang.String getMachineId()
The generated UUID of this machine.- Returns:
- String
-
findMatchingTypes
protected <T extends DBusInterface> java.util.List<java.lang.Class<?>> findMatchingTypes(java.lang.Class<T> _type, java.util.List<java.lang.String> _ifaces)
If given type is null, will try to find suitable types by examining the given ifaces. If a non-null type is given, returns the given type.- Type Parameters:
T- any DBusInterface compatible object- Parameters:
_type- type or null_ifaces- interfaces to examining when type is null- Returns:
- List
-
listen
protected void listen()
Start reading and sending messages.
-
getExportedObject
public java.lang.String getExportedObject(DBusInterface _interface) throws DBusException
- Throws:
DBusException
-
changeThreadCount
@Deprecated(forRemoval=true, since="4.1.0") public void changeThreadCount(byte _newPoolSize)Deprecated, for removal: This API element is subject to removal in a future version.does nothing as threading has been changed significantlyChange the number of worker threads to receive method calls and handle signals. Default is 4 threads- Parameters:
_newPoolSize- The new number of worker Threads to use.
-
setWeakReferences
public void setWeakReferences(boolean _weakreferences)
If set to true the bus will not hold a strong reference to exported objects. If they go out of scope they will automatically be unexported from the bus. The default is to hold a strong reference, which means objects must be explicitly unexported before they will be garbage collected.- Parameters:
_weakreferences- reference
-
exportObject
public void exportObject(java.lang.String _objectPath, DBusInterface _object) throws DBusExceptionExport an object so that its methods can be called on DBus.- Parameters:
_objectPath- The path to the object we are exposing. MUST be in slash-notation, like "/org/freedesktop/Local", and SHOULD end with a capitalised term. Only one object may be exposed on each path at any one time, but an object may be exposed on several paths at once._object- The object to export.- Throws:
DBusException- If the objectpath is already exporting an object. or if objectpath is incorrectly formatted,
-
exportObject
public void exportObject(DBusInterface _object) throws DBusException
Export an object so that its methods can be called on DBus. The path to the object will be taken from theDBusInterface.getObjectPath()method, make sure it is implemented and returns immutable value. If you want export object with multiple paths, please useexportObject(String, DBusInterface).- Parameters:
_object- The object to export.- Throws:
DBusException- If the object path is already exporting an object or if object path is incorrectly formatted.
-
addFallback
public void addFallback(java.lang.String _objectPrefix, DBusInterface _object) throws DBusExceptionExport an object as a fallback object. This object will have it's methods invoked for all paths starting with this object path.- Parameters:
_objectPrefix- The path below which the fallback handles calls. MUST be in slash-notation, like "/org/freedesktop/Local",_object- The object to export.- Throws:
DBusException- If the objectpath is incorrectly formatted,
-
removeFallback
public void removeFallback(java.lang.String _objectprefix)
Remove a fallback- Parameters:
_objectprefix- The prefix to remove the fallback for.
-
unExportObject
public void unExportObject(java.lang.String _objectpath)
Stop Exporting an object- Parameters:
_objectpath- The objectpath to stop exporting.
-
sendMessage
public void sendMessage(Message _message)
Send a message or signal to the DBus daemon.- Parameters:
_message- message to send
-
removeSigHandler
public <T extends DBusSignal> void removeSigHandler(java.lang.Class<T> _type, DBusSigHandler<T> _handler) throws DBusException
Remove a Signal Handler. Stops listening for this signal.- Type Parameters:
T- class extendingDBusSignal- Parameters:
_type- The signal to watch for._handler- the handler- Throws:
DBusException- If listening for the signal on the bus failed.java.lang.ClassCastException- If type is not a sub-type of DBusSignal.
-
removeSigHandler
public <T extends DBusSignal> void removeSigHandler(java.lang.Class<T> _type, DBusInterface _object, DBusSigHandler<T> _handler) throws DBusException
Remove a Signal Handler. Stops listening for this signal.- Type Parameters:
T- class extendingDBusSignal- Parameters:
_type- The signal to watch for._object- The object emitting the signal._handler- the handler- Throws:
DBusException- If listening for the signal on the bus failed.java.lang.ClassCastException- If type is not a sub-type of DBusSignal.
-
addSigHandler
public <T extends DBusSignal> java.lang.AutoCloseable addSigHandler(java.lang.Class<T> _type, DBusSigHandler<T> _handler) throws DBusException
Add a Signal Handler. Adds a signal handler to call when a signal is received which matches the specified type and name.- Type Parameters:
T- class extendingDBusSignal- Parameters:
_type- The signal to watch for._handler- The handler to call when a signal is received.- Returns:
- closeable that removes signal handler
- Throws:
DBusException- If listening for the signal on the bus failed.java.lang.ClassCastException- If type is not a sub-type of DBusSignal.
-
addSigHandler
public <T extends DBusSignal> java.lang.AutoCloseable addSigHandler(java.lang.Class<T> _type, DBusInterface _object, DBusSigHandler<T> _handler) throws DBusException
Add a Signal Handler. Adds a signal handler to call when a signal is received which matches the specified type, name and object.- Type Parameters:
T- class extendingDBusSignal- Parameters:
_type- The signal to watch for._object- The object from which the signal will be emitted_handler- The handler to call when a signal is received.- Returns:
- closeable that removes signal handler
- Throws:
DBusException- If listening for the signal on the bus failed.java.lang.ClassCastException- If type is not a sub-type of DBusSignal.
-
addSigHandlerWithoutMatch
protected <T extends DBusSignal> void addSigHandlerWithoutMatch(java.lang.Class<? extends DBusSignal> _signal, DBusSigHandler<T> _handler) throws DBusException
- Throws:
DBusException
-
disconnect
protected void disconnect(IDisconnectAction _before, IDisconnectAction _after)
Special disconnect method which may be used whenever some cleanup before or after disconnection to DBus is required.- Parameters:
_before- action execute before actual disconnect, null if not needed_after- action execute after disconnect, null if not needed
-
internalDisconnect
protected final void internalDisconnect(java.io.IOException _connectionError)
Disconnects the DBus session. This method is private as it should never be overwritten by subclasses, otherwise we have an endless recursion when usingdisconnect(IDisconnectAction, IDisconnectAction)which then will cause a StackOverflowError.- Parameters:
_connectionError- exception caused the disconnection (null if intended disconnect)
-
disconnect
public void disconnect()
Disconnect from the Bus.
-
close
public void close() throws java.io.IOExceptionDisconnect this session (for use in try-with-resources).- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
callWithCallback
public <A> void callWithCallback(DBusInterface _object, java.lang.String _m, CallbackHandler<A> _callback, java.lang.Object... _parameters)
Call a method asynchronously and set a callback. This handler will be called in a separate thread.- Type Parameters:
A- whatever- Parameters:
_object- The remote object on which to call the method._m- The name of the method on the interface to call._callback- The callback handler._parameters- The parameters to call the method with.
-
callMethodAsync
public DBusAsyncReply<?> callMethodAsync(DBusInterface _object, java.lang.String _method, java.lang.Object... _parameters)
Call a method asynchronously and get a handle with which to get the reply.- Parameters:
_object- The remote object on which to call the method._method- The name of the method on the interface to call._parameters- The parameters to call the method with.- Returns:
- A handle to the call.
-
createTypesArray
private static java.lang.Class<?>[] createTypesArray(java.lang.Object... _parameters)
-
handleException
protected void handleException(Message _methodOrSignal, DBusExecutionException _exception)
-
handleMessage
void handleMessage(Message _message) throws DBusException
Handle received message from DBus.- Parameters:
_message-- Throws:
DBusException
-
handleMessage
private void handleMessage(MethodCall _methodCall) throws DBusException
- Throws:
DBusException
-
handleMessage
private void handleMessage(DBusSignal _signal, boolean _useThreadPool)
Handle a signal received on DBus.- Parameters:
_signal- signal to handle_useThreadPool- whether to handle this signal in another thread or handle it byself
-
handleMessage
private void handleMessage(Error _err)
-
handleMessage
private void handleMessage(MethodReturn _mr)
-
queueCallback
public void queueCallback(MethodCall _call, java.lang.reflect.Method _method, CallbackHandler<?> _callback)
-
sendMessageInternally
private void sendMessageInternally(Message _message)
Send a message to DBus.- Parameters:
_message- message to send
-
readIncoming
Message readIncoming() throws DBusException
- Throws:
DBusException
-
getExportedObjects
protected java.util.Map<java.lang.String,ExportedObject> getExportedObjects()
-
getFallbackContainer
FallbackContainer getFallbackContainer()
-
getCallInfo
public static DBusCallInfo getCallInfo()
Returns a structure with information on the current method call.- Returns:
- the DBusCallInfo for this method call, or null if we are not in a method call.
-
getError
public DBusExecutionException getError()
Return any DBus error which has been received.- Returns:
- A DBusExecutionException, or null if no error is pending.
-
getAddress
public BusAddress getAddress()
Returns the address this connection is connected to.- Returns:
- new
BusAddressobject
-
isConnected
public boolean isConnected()
-
getPendingErrorQueue
protected java.util.Queue<Error> getPendingErrorQueue()
-
getHandledSignals
protected java.util.Map<DBusMatchRule,java.util.Queue<DBusSigHandler<? extends DBusSignal>>> getHandledSignals()
-
getGenericHandledSignals
protected java.util.Map<DBusMatchRule,java.util.Queue<DBusSigHandler<DBusSignal>>> getGenericHandledSignals()
-
getPendingCalls
protected java.util.Map<java.lang.Long,MethodCall> getPendingCalls()
-
getImportedObjects
protected java.util.Map<DBusInterface,RemoteObject> getImportedObjects()
-
getObjectTree
protected ObjectTree getObjectTree()
-
setEndianness
public static void setEndianness(byte _b)
Set the endianness to use for all connections. Defaults to the system architectures endianness.- Parameters:
_b- Message.Endian.BIG or Message.Endian.LITTLE
-
getEndianness
public static byte getEndianness()
Get current endianness to use.- Returns:
- Message.Endian.BIG or Message.Endian.LITTLE
-
getSystemEndianness
public static byte getSystemEndianness()
Get the default system endianness.- Returns:
- LITTLE or BIG
-
getDisconnectCallback
public IDisconnectCallback getDisconnectCallback()
Returns the currently configured disconnect callback.- Returns:
- callback or null if no callback registered
-
setDisconnectCallback
public void setDisconnectCallback(IDisconnectCallback _disconnectCallback)
Set the callback which will be notified when a disconnection happens. Use null to remove.- Parameters:
_disconnectCallback- callback to execute or null to remove
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-