Connection point to the DBus. More...
#include <connection.h>
Inherits std::enable_shared_from_this< Connection >.
Public Member Functions | |
~Connection () | |
operator bool () const | |
True if this is a valid connection; false otherwise. More... | |
bool | is_valid () const |
True if this is a valid connection; false otherwise. More... | |
bool | is_registered () const |
True if this connection is already registered. More... | |
bool | bus_register () |
Registers this connection with the bus. More... | |
std::string | unique_name () const |
Gets the unique name of the connection as assigned by the message bus. More... | |
RequestNameResponse | request_name (const std::string &name, unsigned int flags=0) |
Request the given name on the bus. More... | |
ReleaseNameResponse | release_name (const std::string &name) |
Release the specified name, after requesting it via request_name. More... | |
bool | name_has_owner (const std::string &name) const |
Check to see if the given name currently has an owner. More... | |
StartReply | start_service (const std::string &name, uint32_t flags=0) const |
start_service More... | |
bool | add_match (const std::string &rule) |
void | add_match_nonblocking (const std::string &rule) |
bool | remove_match (const std::string &rule) |
bool | is_connected () const |
bool | is_authenticated () const |
bool | is_anonymous () const |
const char * | server_id () const |
uint32_t | send (const std::shared_ptr< const Message > message) |
Queues up the message to be sent on the bus. More... | |
Connection & | operator<< (std::shared_ptr< const Message > msg) |
Blindly sends the message on the connection. More... | |
std::shared_ptr< ReturnMessage > | send_with_reply_blocking (std::shared_ptr< const CallMessage > msg, int timeout_milliseconds=-1) |
Send a CallMessage, and wait for the reply. More... | |
void | flush () |
Flushes all data out to the bus. More... | |
DispatchStatus | dispatch_status () const |
DispatchStatus | dispatch () |
Dispatch the connection. More... | |
int | unix_fd () const |
int | socket () const |
bool | has_messages_to_send () |
sigc::signal< void()> & | signal_needs_dispatch () |
This signal is emitted whenever we need to be dispatched. More... | |
std::shared_ptr< Object > | create_object (const std::string &path, ThreadForCalling calling=ThreadForCalling::DispatcherThread) |
Create and return a new object, registering the object automatically. More... | |
RegistrationStatus | register_object (std::shared_ptr< Object > object, ThreadForCalling calling=ThreadForCalling::DispatcherThread) |
Register an object with this connection. More... | |
bool | change_object_calling_thread (std::shared_ptr< Object > object, ThreadForCalling calling) |
Change the thread that the methods on this object will be called from. More... | |
bool | change_object_proxy_calling_thread (std::shared_ptr< ObjectProxy > object, ThreadForCalling calling) |
Change the thread that the signals on this ObjectProxy will be called from. More... | |
std::shared_ptr< ObjectProxy > | create_object_proxy (const std::string &path, ThreadForCalling calling=ThreadForCalling::DispatcherThread) |
std::shared_ptr< ObjectProxy > | create_object_proxy (const std::string &destination, const std::string &path, ThreadForCalling calling=ThreadForCalling::DispatcherThread) |
bool | register_object_proxy (std::shared_ptr< ObjectProxy > obj, ThreadForCalling calling=ThreadForCalling::DispatcherThread) |
Add an ObjectProxy to this Connection. More... | |
bool | unregister_object (const std::string &path) |
template<typename... T_arg> | |
std::shared_ptr< SignalProxy< T_arg... > > | create_free_signal_proxy (const SignalMatchRule &rule, ThreadForCalling calling=ThreadForCalling::DispatcherThread) |
Create and return a signal proxy that lets you listen to signals sent on the DBus as a free proxy. More... | |
std::shared_ptr< SignalProxyBase > | add_free_signal_proxy (std::shared_ptr< SignalProxyBase > Signal, ThreadForCalling calling=ThreadForCalling::DispatcherThread) |
Adds the given signal proxy to the connection. More... | |
bool | remove_free_signal_proxy (std::shared_ptr< SignalProxyBase > proxy) |
Remove a free signal proxy, so that it will not be called anymore. More... | |
const std::vector< std::shared_ptr< SignalProxyBase > > | get_free_signal_proxies () |
Gets all the free signal handlers. More... | |
std::vector< std::shared_ptr< SignalProxyBase > > | get_free_signal_proxies (const std::string &interface_name) |
Gets the signal handlers for a specific interface. More... | |
std::vector< std::shared_ptr< SignalProxyBase > > | get_free_signal_proxies (const std::string &interface_name, const std::string &member) |
Gets the signal handlers for a specific interface and member. More... | |
template<class T_arg > | |
std::shared_ptr< Signal< T_arg > > | create_free_signal (const std::string &path, const std::string &interface_name, const std::string &member) |
Create a free signal, that when it is emitted will send that signal over the DBus. More... | |
std::string | introspect (const std::string &destination, const std::string &path) |
void | set_dispatching_thread (std::thread::id tid) |
Set the ID of the thread that all of the dispatching hapens from. More... | |
void | add_thread_dispatcher (std::weak_ptr< ThreadDispatcher > disp) |
Add a thread dispatcher that will handle messages for a given thread. More... | |
Static Public Member Functions | |
static std::shared_ptr< Connection > | create (BusType type) |
Connects to a bus daemon. More... | |
static std::shared_ptr< Connection > | create (std::string address) |
Create a new connection, connecting to the specified address. More... | |
Private Member Functions | |
Connection (BusType type) | |
Connection (std::string address) | |
void | notify_dispatcher_or_dispatch () |
Depending on what thread this is called from, will either notify the dispatcher that we need to be dispatched, or will do the dispatching needed. More... | |
uint32_t | write_single_message (std::shared_ptr< const Message > msg) |
Write a single message, return the serial of this message. More... | |
void | process_single_message () |
void | remove_invalid_threaddispatchers_and_associated_objects () |
void | send_error_on_handler_result (std::shared_ptr< const CallMessage > msg, HandlerResult result) |
Send an error back to the calling application based on HandlerResult. More... | |
void | process_call_message (std::shared_ptr< const CallMessage > msg) |
void | process_signal_message (std::shared_ptr< const SignalMessage > msg) |
std::thread::id | thread_id_from_calling (ThreadForCalling calling) |
DBUS_CXX_PROPAGATE_CONST (std::unique_ptr< priv_data >) m_priv | |
Connection point to the DBus.
|
private |
References LOGGER_NAME, DBus::priv::Transport::open_transport(), DBus::SESSION, SIMPLELOGGER_DEBUG, SIMPLELOGGER_ERROR, DBus::STARTER, DBus::SYSTEM, and DBus::type().
Referenced by create().
|
private |
References LOGGER_NAME, DBus::priv::Transport::open_transport(), and SIMPLELOGGER_ERROR.
DBus::Connection::~Connection | ( | ) |
std::shared_ptr< SignalProxyBase > DBus::Connection::add_free_signal_proxy | ( | std::shared_ptr< SignalProxyBase > | Signal, |
ThreadForCalling | calling = ThreadForCalling::DispatcherThread |
||
) |
Adds the given signal proxy to the connection.
References add_match(), LOGGER_NAME, SIMPLELOGGER_DEBUG, SIMPLELOGGER_ERROR, and thread_id_from_calling().
Referenced by create_free_signal_proxy().
bool DBus::Connection::add_match | ( | const std::string & | rule | ) |
References is_valid(), LOGGER_NAME, and SIMPLELOGGER_DEBUG.
Referenced by add_free_signal_proxy().
void DBus::Connection::add_match_nonblocking | ( | const std::string & | rule | ) |
void DBus::Connection::add_thread_dispatcher | ( | std::weak_ptr< ThreadDispatcher > | disp | ) |
Add a thread dispatcher that will handle messages for a given thread.
This method must be called from the thread that this ThreadDispatcher is handling messages for.
This is a weak_ptr, as the intention is to simply declare a local ThreadDispatcher object inside of the thread. When the thread exits, the ThreadDispatcher will be deconstructed. The next time a message for this thread comes in, anything that is associated with this thread will become unexported.
disp | The thread dispatcher to associate with this connection. |
bool DBus::Connection::bus_register | ( | ) |
Registers this connection with the bus.
It is safe to call this method multiple times.
References DBus::DBusDaemonProxy::create(), and is_registered().
bool DBus::Connection::change_object_calling_thread | ( | std::shared_ptr< Object > | object, |
ThreadForCalling | calling | ||
) |
Change the thread that the methods on this object will be called from.
Note that this object must already be registered with register_object.
object | |
calling |
References DBus::DispatcherThread.
bool DBus::Connection::change_object_proxy_calling_thread | ( | std::shared_ptr< ObjectProxy > | object, |
ThreadForCalling | calling | ||
) |
Change the thread that the signals on this ObjectProxy will be called from.
Note that this ObjectProxy must be related to this connection
object | The ObjectProxy that has the signals to listen for |
calling |
References DBus::CurrentThread.
|
static |
Connects to a bus daemon.
The returned Connection will have authenticated with the bus at this point, but it will not be registered. bus_register() must be called - the Dispatcher should do this automatically.
is_private | if true a private connection will be created. Otherwise a shared connection is created. |
References Connection(), and DBus::type().
Referenced by DBus::StandaloneDispatcher::create_connection().
|
static |
Create a new connection, connecting to the specified address.
This address needs to be in the in DBus transport format (e.g. unix:path=/tmp/dbus-test). The returned connection will have authenticated with the bus at this point, but it will not be registered. bus_register() must be called - the Dispatcher should do this automatically.
address |
References Connection().
|
inline |
|
inline |
Create and return a signal proxy that lets you listen to signals sent on the DBus as a free proxy.
A free proxy is one that is not attached to an interface.
Normally, you want to add signal proxies via Interface::add_signal.
This method may be used when you need more control over the match rule for the signal that you want to listen to.
This method automatically calls add_signal_proxy.
References add_free_signal_proxy().
std::shared_ptr< Object > DBus::Connection::create_object | ( | const std::string & | path, |
ThreadForCalling | calling = ThreadForCalling::DispatcherThread |
||
) |
Create and return a new object, registering the object automatically.
If the registering fails, an invalid pointer will be returned.
path | The path of a new object, e.g. /opt/freedesktop/DBus |
calling | How to call this object(what thread). Defaults to being the dispatching thread. |
References DBus::Object::create(), register_object(), and DBus::Success.
std::shared_ptr< ObjectProxy > DBus::Connection::create_object_proxy | ( | const std::string & | destination, |
const std::string & | path, | ||
ThreadForCalling | calling = ThreadForCalling::DispatcherThread |
||
) |
References DBus::ObjectProxy::create(), and register_object_proxy().
std::shared_ptr< ObjectProxy > DBus::Connection::create_object_proxy | ( | const std::string & | path, |
ThreadForCalling | calling = ThreadForCalling::DispatcherThread |
||
) |
References DBus::ObjectProxy::create(), and register_object_proxy().
|
private |
DispatchStatus DBus::Connection::dispatch | ( | ) |
Dispatch the connection.
Dispatching involves writing messages to the bus, reading messages from the bus, and acting on those messages, if available. This method can only be called from the dispatching thread, and will throw an exception if it is called from the wrong thread.
This method, when called, will process at most one message. This is required so that responses to method calls will appear to be fully blocking.
References DBus::COMPLETE, DBus::DATA_REMAINS, flush(), is_valid(), LOGGER_NAME, process_single_message(), and SIMPLELOGGER_DEBUG.
Referenced by notify_dispatcher_or_dispatch().
DispatchStatus DBus::Connection::dispatch_status | ( | ) | const |
References DBus::COMPLETE, and is_valid().
void DBus::Connection::flush | ( | ) |
Flushes all data out to the bus.
This should generally be called from the dispatching thread, but it should be able to be called from any thread.
References is_valid().
Referenced by dispatch().
const std::vector< std::shared_ptr< SignalProxyBase > > DBus::Connection::get_free_signal_proxies | ( | ) |
Gets all the free signal handlers.
Referenced by get_free_signal_proxies().
std::vector< std::shared_ptr< SignalProxyBase > > DBus::Connection::get_free_signal_proxies | ( | const std::string & | interface_name | ) |
Gets the signal handlers for a specific interface.
References get_free_signal_proxies().
std::vector< std::shared_ptr< SignalProxyBase > > DBus::Connection::get_free_signal_proxies | ( | const std::string & | interface_name, |
const std::string & | member | ||
) |
Gets the signal handlers for a specific interface and member.
References get_free_signal_proxies().
bool DBus::Connection::has_messages_to_send | ( | ) |
References is_valid().
std::string DBus::Connection::introspect | ( | const std::string & | destination, |
const std::string & | path | ||
) |
bool DBus::Connection::is_anonymous | ( | ) | const |
bool DBus::Connection::is_authenticated | ( | ) | const |
bool DBus::Connection::is_connected | ( | ) | const |
bool DBus::Connection::is_registered | ( | ) | const |
True if this connection is already registered.
Referenced by bus_register().
bool DBus::Connection::is_valid | ( | ) | const |
True if this is a valid connection; false otherwise.
Referenced by add_match(), dispatch(), dispatch_status(), flush(), has_messages_to_send(), request_name(), send(), send_with_reply_blocking(), socket(), unique_name(), and unix_fd().
bool DBus::Connection::name_has_owner | ( | const std::string & | name | ) | const |
Check to see if the given name currently has an owner.
Note that usage of this method in the form if( !connection->name_has_owner( "foo.bar" ) ){ connection->request_name( "foo.bar" ); }
may be subject to race conditions, as multiple applications may be attempting to request the bus name at the same time. Instead, you should set the flags in the request_name method to do this in a bus-atomic manner.
name |
|
private |
Depending on what thread this is called from, will either notify the dispatcher that we need to be dispatched, or will do the dispatching needed.
References DBus::DATA_REMAINS, and dispatch().
Referenced by send(), and send_with_reply_blocking().
DBus::Connection::operator bool | ( | ) | const |
True if this is a valid connection; false otherwise.
Connection & DBus::Connection::operator<< | ( | std::shared_ptr< const Message > | msg | ) |
Blindly sends the message on the connection.
Since you don't get any kind of handle back from this, you should really only use it for sending method returns and signals.
References send().
|
private |
|
private |
Referenced by process_single_message().
|
private |
References DBus::CALL, DBus::ERROR, LOGGER_NAME, process_call_message(), process_signal_message(), DBus::RETURN, DBus::SIGNAL, and SIMPLELOGGER_ERROR.
Referenced by dispatch().
RegistrationStatus DBus::Connection::register_object | ( | std::shared_ptr< Object > | object, |
ThreadForCalling | calling = ThreadForCalling::DispatcherThread |
||
) |
Register an object with this connection.
The path that this object is accessible at is part of the object. You may also select which thread the object's methods should be called on.
object | The object to export |
calling | The thread in which this object's methods will be called in. Defaults to being the dispatching thread. |
References DBus::DispatcherThread, DBus::Failed_Invalid_Object, DBus::Failed_Path_in_Use, LOGGER_NAME, SIMPLELOGGER_DEBUG, and DBus::Success.
Referenced by create_object().
bool DBus::Connection::register_object_proxy | ( | std::shared_ptr< ObjectProxy > | obj, |
ThreadForCalling | calling = ThreadForCalling::DispatcherThread |
||
) |
Add an ObjectProxy to this Connection.
Note that calling create_object_proxy() will call this method automatically.
obj | The ObjectProxy to register with this connection |
References thread_id_from_calling().
Referenced by create_object_proxy().
ReleaseNameResponse DBus::Connection::release_name | ( | const std::string & | name | ) |
Release the specified name, after requesting it via request_name.
name | The name to release on the bus |
References DBUSCXX_RELEASE_NAME_REPLY_NON_EXISTENT, DBUSCXX_RELEASE_NAME_REPLY_NOT_OWNER, DBUSCXX_RELEASE_NAME_REPLY_RELEASED, DBus::NameNonExistant, DBus::NameReleased, and DBus::NotOwner.
bool DBus::Connection::remove_free_signal_proxy | ( | std::shared_ptr< SignalProxyBase > | proxy | ) |
Remove a free signal proxy, so that it will not be called anymore.
proxy |
References LOGGER_NAME, remove_match(), and SIMPLELOGGER_DEBUG.
|
private |
Referenced by process_call_message().
bool DBus::Connection::remove_match | ( | const std::string & | rule | ) |
Referenced by remove_free_signal_proxy().
RequestNameResponse DBus::Connection::request_name | ( | const std::string & | name, |
unsigned int | flags = 0 |
||
) |
Request the given name on the bus.
name | The name to request, e.g. "com.example.foo" |
flags | Any flags with the name; see DBUSCXX_NAME_FLAG_XXX macros |
References DBus::AlreadyOwner, DBUSCXX_REQUEST_NAME_REPLY_ALREADY_OWNER, DBUSCXX_REQUEST_NAME_REPLY_EXISTS, DBUSCXX_REQUEST_NAME_REPLY_IN_QUEUE, DBUSCXX_REQUEST_NAME_REPLY_PRIMARY_OWNER, is_valid(), DBus::NameExists, DBus::NameInQueue, and DBus::PrimaryOwner.
uint32_t DBus::Connection::send | ( | const std::shared_ptr< const Message > | message | ) |
Queues up the message to be sent on the bus.
message | The message to send |
References is_valid(), and notify_dispatcher_or_dispatch().
Referenced by operator<<(), process_call_message(), and send_error_on_handler_result().
|
private |
Send an error back to the calling application based on HandlerResult.
No-op if the result indicates that there is no error.
References DBUSCXX_ERROR_FAILED, DBUSCXX_ERROR_UNKNOWN_INTERFACE, DBUSCXX_ERROR_UNKNOWN_METHOD, DBus::Handled, DBus::Invalid_Interface, DBus::Invalid_Method, DBus::Invalid_Path, and send().
Referenced by process_call_message().
std::shared_ptr< ReturnMessage > DBus::Connection::send_with_reply_blocking | ( | std::shared_ptr< const CallMessage > | msg, |
int | timeout_milliseconds = -1 |
||
) |
Send a CallMessage, and wait for the reply.
If a timeout is processed, this will throw ErrorNoReply
msg | The message to send |
timeout_milliseconds | How long to wait for. If -1, will wait the maximum time |
References DBus::ERROR, is_valid(), LOGGER_NAME, notify_dispatcher_or_dispatch(), DBus::RETURN, DBus::SIGNAL, SIMPLELOGGER_DEBUG, DBus::priv::wait_for_fd_activity(), and write_single_message().
Referenced by introspect().
const char * DBus::Connection::server_id | ( | ) | const |
void DBus::Connection::set_dispatching_thread | ( | std::thread::id | tid | ) |
Set the ID of the thread that all of the dispatching hapens from.
If a blocking call is attempted on the non-dispatching thread, that thread will block until the dispatching thread handles the data.
By default, the dispatching thread is the thread that this Connection was created in. If using the default Dispatcher, this will be set automatically.
tid |
sigc::signal< void() > & DBus::Connection::signal_needs_dispatch | ( | ) |
This signal is emitted whenever we need to be dispatched.
Cannot call dispatch() in a slot connected to this signal.
Any slots that listen to this signal must be threadsafe, as this may be emitted from any thread.
int DBus::Connection::socket | ( | ) | const |
References is_valid().
StartReply DBus::Connection::start_service | ( | const std::string & | name, |
uint32_t | flags = 0 |
||
) | const |
start_service
name | |
flags |
References DBus::ALREADY_RUNNING, DBUSCXX_START_REPLY_ALREADY_RUNNING, DBUSCXX_START_REPLY_SUCCESS, and DBus::SUCCESS.
|
private |
References DBus::CurrentThread.
Referenced by add_free_signal_proxy(), and register_object_proxy().
std::string DBus::Connection::unique_name | ( | ) | const |
Gets the unique name of the connection as assigned by the message bus.
References is_valid().
int DBus::Connection::unix_fd | ( | ) | const |
References is_valid().
bool DBus::Connection::unregister_object | ( | const std::string & | path | ) |
|
private |
Write a single message, return the serial of this message.
This should me called with a lock on m_outgoingLock
msg |
Referenced by send_with_reply_blocking().