31 #include <dbus/dbus.h>
45 : conn(c) , dispatcher(NULL), server(s)
50 Connection::Private::Private(DBusBusType
type)
51 : dispatcher(NULL), server(NULL)
55 conn = dbus_bus_get_private(type, e);
57 if (e)
throw Error(e);
62 Connection::Private::~Private()
64 debug_log(
"terminating connection 0x%08x", conn);
68 if (dbus_connection_get_is_connected(conn))
70 std::vector<std::string>::iterator i = names.begin();
72 while (i != names.end())
74 debug_log(
"%s: releasing bus name %s", dbus_bus_get_unique_name(conn), i->c_str());
75 dbus_bus_release_name(conn, i->c_str(), NULL);
78 dbus_connection_close(conn);
80 dbus_connection_unref(conn);
83 void Connection::Private::init()
85 dbus_connection_ref(conn);
86 dbus_connection_ref(conn);
89 this, &Connection::Private::disconn_filter_function
92 dbus_connection_add_filter(conn, message_filter_stub, &disconn_filter, NULL);
94 dbus_connection_set_dispatch_status_function(conn, dispatch_status_stub,
this, 0);
95 dbus_connection_set_exit_on_disconnect(conn,
false);
98 void Connection::Private::detach_server()
119 bool Connection::Private::do_dispatch()
123 if (!dbus_connection_get_is_connected(conn))
132 return dbus_connection_dispatch(conn) != DBUS_DISPATCH_DATA_REMAINS;
135 void Connection::Private::dispatch_status_stub(DBusConnection *dc, DBusDispatchStatus status,
void *data)
137 Private *p =
static_cast<Private *
>(data);
141 case DBUS_DISPATCH_DATA_REMAINS:
142 debug_log(
"some dispatching to do on %p", dc);
143 p->dispatcher->queue_connection(p);
146 case DBUS_DISPATCH_COMPLETE:
147 debug_log(
"all dispatching done on %p", dc);
150 case DBUS_DISPATCH_NEED_MEMORY:
151 debug_log(
"connection %p needs memory", dc);
156 DBusHandlerResult Connection::Private::message_filter_stub(DBusConnection *conn, DBusMessage *dmsg,
void *data)
162 return slot && !slot->
empty() && slot->
call(msg)
163 ? DBUS_HANDLER_RESULT_HANDLED
164 : DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
167 bool Connection::Private::disconn_filter_function(
const Message &msg)
169 if (msg.
is_signal(DBUS_INTERFACE_LOCAL,
"Disconnected"))
171 debug_log(
"%p disconnected by local bus", conn);
172 dbus_connection_close(conn);
179 DBusDispatchStatus Connection::Private::dispatch_status()
181 return dbus_connection_get_dispatch_status(conn);
184 bool Connection::Private::has_something_to_dispatch()
186 return dispatch_status() == DBUS_DISPATCH_DATA_REMAINS;
209 DBusConnection *conn = priv
210 ? dbus_connection_open_private(address, e)
211 : dbus_connection_open(address, e);
213 if (e)
throw Error(e);
223 : _pvt(p), _timeout(-1)
229 : _pvt(c._pvt), _timeout(c._timeout)
231 dbus_connection_ref(
_pvt->conn);
236 dbus_connection_unref(
_pvt->conn);
241 debug_log(
"registering stubs for connection %p",
_pvt->conn);
245 if (!dispatcher)
throw ErrorFailed(
"no default dispatcher set for new connection");
253 dbus_connection_set_watch_functions(
255 Dispatcher::Private::on_add_watch,
256 Dispatcher::Private::on_rem_watch,
257 Dispatcher::Private::on_toggle_watch,
262 dbus_connection_set_timeout_functions(
264 Dispatcher::Private::on_add_timeout,
265 Dispatcher::Private::on_rem_timeout,
266 Dispatcher::Private::on_toggle_timeout,
283 bool r = dbus_bus_register(
_pvt->conn, e);
292 return dbus_connection_get_is_connected(
_pvt->conn);
298 dbus_connection_close(
_pvt->conn);
303 dbus_connection_set_exit_on_disconnect(
_pvt->conn, exit);
308 return dbus_bus_set_unique_name(
_pvt->conn, n);
313 return dbus_bus_get_unique_name(
_pvt->conn);
318 dbus_connection_flush(
_pvt->conn);
325 dbus_bus_add_match(
_pvt->conn, rule, e);
329 if (e)
throw Error(e);
337 dbus_bus_remove_match(
_pvt->conn, rule, e);
346 debug_log(
"DBus::Connection::remove_match: %s (%s).",
347 static_cast<DBusError *>(e)->message,
348 static_cast<DBusError *>(e)->name);
355 return dbus_connection_add_filter(
_pvt->conn, Private::message_filter_stub, &s, NULL);
361 dbus_connection_remove_filter(
_pvt->conn, Private::message_filter_stub, &s);
366 return dbus_connection_send(
_pvt->conn, msg.
_pvt->msg, serial);
376 reply = dbus_connection_send_with_reply_and_block(
_pvt->conn, msg.
_pvt->msg, this->_timeout, e);
380 reply = dbus_connection_send_with_reply_and_block(
_pvt->conn, msg.
_pvt->msg, timeout, e);
383 if (e)
throw Error(e);
390 DBusPendingCall *pending;
392 if (!dbus_connection_send_with_reply(
_pvt->conn, msg.
_pvt->msg, &pending, timeout))
410 int ret = dbus_bus_request_name(
_pvt->conn, name, flags, e);
414 if (e)
throw Error(e);
421 _pvt->names.push_back(name);
422 std::string match =
"destination='" +
_pvt->names.back() +
"'";
431 unsigned long ul = dbus_bus_get_unix_user(
_pvt->conn, sender, e);
433 if (e)
throw Error(e);
442 bool b = dbus_bus_name_has_owner(
_pvt->conn, name, e);
444 if (e)
throw Error(e);
458 bool b = dbus_bus_start_service_by_name(
_pvt->conn, name, flags, NULL, e);
460 if (e)
throw Error(e);