Skip to content

Commit 677d7a1

Browse files
committed
gconnman_agent: Register object in worker thread
Call the register object in the dbus in the worker thread. The latter makes the callback to be executed also in the worker thread. The creation of the agent block the thread until the worker thread register the object. This solves #21. Signed-off-by: Eduardo Gonzalez <eduardo.gonzalez@amarulasolutions.com>
1 parent aaf42ad commit 677d7a1

3 files changed

Lines changed: 52 additions & 14 deletions

File tree

include/amarula/dbus/connman/gagent.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <gio/gio.h>
33
#include <glib.h>
44

5+
#include <amarula/dbus/gdbus.hpp>
56
#include <functional>
67
#include <string>
78
#include <utility>
@@ -15,8 +16,7 @@ class Agent {
1516
GDBusConnection *connection_{nullptr};
1617
std::string path_{"/net/amarula/gconnman/agent"};
1718

18-
explicit Agent(GDBusConnection *connection,
19-
const std::string &path = std::string());
19+
explicit Agent(DBus *dbus, const std::string &path = std::string());
2020

2121
using RequestInputCallback =
2222
std::function<GVariant *(const gchar *service, GVariant *fields)>;

src/dbus/gconnman_agent.cpp

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <amarula/dbus/connman/gagent.hpp>
66
#include <amarula/log.hpp>
7+
#include <condition_variable>
78
#include <iostream>
89
#include <stdexcept>
910
#include <string>
@@ -28,8 +29,8 @@ static constexpr const char *INTROSPECTION_XML =
2829
" </interface>"
2930
"</node>";
3031

31-
Agent::Agent(GDBusConnection *connection, const std::string &path)
32-
: connection_{connection} {
32+
Agent::Agent(DBus *dbus, const std::string &path)
33+
: connection_{dbus->connection()} {
3334
if (!path.empty()) {
3435
path_ = path;
3536
}
@@ -42,16 +43,54 @@ Agent::Agent(GDBusConnection *connection, const std::string &path)
4243
throw std::runtime_error(msg);
4344
}
4445

45-
registration_id_ = g_dbus_connection_register_object(
46-
connection, path_.c_str(), *(node_info_->interfaces), &INTERFACE_VTABLE,
47-
this, nullptr, &err);
46+
struct Data {
47+
Agent *self;
48+
std::mutex mtx;
49+
std::condition_variable cv;
50+
bool done{false};
51+
std::string error;
52+
};
53+
54+
auto data = Data{this};
55+
56+
g_main_context_invoke_full(
57+
dbus->context(), G_PRIORITY_HIGH,
58+
[](gpointer user_data) -> gboolean {
59+
auto *data = static_cast<Data *>(user_data);
60+
61+
GError *err = nullptr;
62+
63+
data->self->registration_id_ = g_dbus_connection_register_object(
64+
data->self->connection_, data->self->path_.c_str(),
65+
*(data->self->node_info_->interfaces), &INTERFACE_VTABLE,
66+
data->self, nullptr, &err);
67+
68+
if (data->self->registration_id_ == 0) {
69+
data->error =
70+
"Failed to register agent: " + std::string(err->message);
71+
g_error_free(err);
72+
}
73+
74+
return G_SOURCE_REMOVE;
75+
},
76+
&data,
77+
[](gpointer user_data) {
78+
auto *data = static_cast<Data *>(user_data);
79+
{
80+
std::lock_guard<std::mutex> const lock(data->mtx);
81+
data->done = true;
82+
}
83+
data->cv.notify_all();
84+
});
85+
86+
{
87+
std::unique_lock<std::mutex> lock(data.mtx);
88+
data.cv.wait(lock, [&] { return data.done; });
89+
}
4890

49-
if (registration_id_ == 0) {
50-
std::string const msg =
51-
"Failed to register agent: " + std::string(err->message);
52-
g_error_free(err);
91+
if (!data.error.empty()) {
5392
g_dbus_node_info_unref(node_info_);
54-
throw std::runtime_error(msg);
93+
throw std::runtime_error(data.error);
5594
}
5695
}
5796
Agent::~Agent() {

src/dbus/gconnman_manager.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ void ManaProperties::update(const gchar* key, GVariant* value) {
4343

4444
Manager::Manager(DBus* dbus, const std::string& agent_path)
4545
: DBusProxy(dbus, SERVICE, MANAGER_PATH, MANAGER_INTERFACE),
46-
agent_{
47-
std::unique_ptr<Agent>(new Agent(dbus->connection(), agent_path))} {
46+
agent_{std::unique_ptr<Agent>(new Agent(dbus, agent_path))} {
4847
setup_agent();
4948
get_technologies();
5049
get_services();

0 commit comments

Comments
 (0)