~thomas-voss/dbus-cpp/add_fallback_objects

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright © 2012 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */

#include <core/dbus/bus.h>
#include <core/dbus/dbus.h>
#include <core/dbus/match_rule.h>
#include <core/dbus/object.h>

#include <core/dbus/traits/timeout.h>
#include <core/dbus/traits/watch.h>

#include "message_p.h"
#include "message_factory_impl.h"
#include "pending_call_impl.h"

namespace
{
struct VTable
{
    static void unregister_object_path(DBusConnection*, void* data)
    {
        delete static_cast<VTable*>(data);
    }

    static DBusHandlerResult on_new_message(
            DBusConnection*,
            DBusMessage* message,
            void* data)
    {
        auto thiz = static_cast<VTable*>(data);

        if (thiz->object->on_new_message(
                    core::dbus::Message::from_raw_message(
                        message)))
            return DBUS_HANDLER_RESULT_HANDLED;

        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

    std::shared_ptr<core::dbus::Object> object;
};

DBusHandlerResult static_handle_message(
        DBusConnection* connection,
        DBusMessage* message,
        void* user_data)
{
    (void) connection;
    auto thiz =
            static_cast<core::dbus::Bus*>(user_data);
    return static_cast<DBusHandlerResult>(
                thiz->handle_message(
                    core::dbus::Message::from_raw_message(
                        message)));
}

void init_libdbus_thread_support_and_install_shutdown_handler()
{
    static std::once_flag once;
    std::call_once(once, []() { dbus_threads_init_default(); std::atexit(dbus_shutdown); });
}
}

namespace core
{
namespace dbus
{

Bus::Name::Name(const std::string &name) : name(name)
{
}

const std::string& Bus::Name::as_string() const
{
    return name;
}

struct Bus::Private
{
    Private()
        : connection(nullptr),
          message_factory_impl(new impl::MessageFactory()),
          message_type_router([](const Message::Ptr& msg) { return msg->type(); }),
          signal_router([](const Message::Ptr& msg){ return msg->path(); })
    {
        init_libdbus_thread_support_and_install_shutdown_handler();
    }

    std::shared_ptr<DBusConnection> connection;
    std::shared_ptr<MessageFactory> message_factory_impl;
    Executor::Ptr executor;
    MessageTypeRouter message_type_router;
    SignalRouter signal_router;
};

Bus::MessageHandlerResult Bus::handle_message(const Message::Ptr& message)
{
    d->message_type_router(message);
    return Bus::MessageHandlerResult::not_yet_handled;
}

Bus::Bus(const std::string& address)
    : d(new Private())
{
    Error se;
    d->connection.reset(
                dbus_connection_open_private(address.c_str(), std::addressof(se.raw())),
                [](DBusConnection*){}
    );

    if (!d->connection)
        throw std::runtime_error(se.print());

    d->message_type_router.install_route(
                Message::Type::signal,
                std::bind(
                    &Bus::SignalRouter::operator(),
                    std::ref(d->signal_router),
                    std::placeholders::_1));

    dbus_connection_add_filter(
                d->connection.get(),
                static_handle_message,
                this,
                nullptr);

    auto message = dbus::Message::make_method_call(
                DBus::name(),
                DBus::path(),
                DBus::interface(),
                "Hello");

    auto reply = send_with_reply_and_block_for_at_most(message, std::chrono::seconds(1));

    if (reply->type() == Message::Type::error)
        throw std::runtime_error(reply->error().print());

    dbus_connection_set_exit_on_disconnect(d->connection.get(), FALSE);
}

Bus::Bus(WellKnownBus bus)
    : d(new Private())
{
    Error se;
    d->connection.reset(
                dbus_bus_get_private(static_cast<DBusBusType>(bus), std::addressof(se.raw())),
                [](DBusConnection*){}
    );

    if (!d->connection)
        throw std::runtime_error(se.print());

    d->message_type_router.install_route(
                Message::Type::signal,
                std::bind(
                    &Bus::SignalRouter::operator(),
                    std::ref(d->signal_router),
                    std::placeholders::_1));

    dbus_connection_add_filter(
                d->connection.get(),
                static_handle_message,
                this,
                nullptr);

    dbus_connection_set_exit_on_disconnect(d->connection.get(), FALSE);
}

Bus::~Bus() noexcept
{
    dbus_connection_remove_filter(d->connection.get(), static_handle_message, this);
    dbus_connection_close(d->connection.get());
    dbus_connection_unref(d->connection.get());
}

const std::shared_ptr<MessageFactory> Bus::message_factory()
{
    return d->message_factory_impl;
}

std::unique_ptr<Bus::Name> Bus::request_name_on_bus(
        const std::string& name,
        Bus::RequestNameFlag flags)
{
    Error error;
    auto rc = dbus_bus_request_name(
                d->connection.get(),
                name.c_str(),
                static_cast<unsigned int>(flags),
                std::addressof(error.raw()));

    std::unique_ptr<Bus::Name> result{new Bus::Name{name}};

    switch (rc)
    {
    case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER: return std::move(result);
    case DBUS_REQUEST_NAME_REPLY_IN_QUEUE: return std::move(result);
    case DBUS_REQUEST_NAME_REPLY_EXISTS: throw Bus::Errors::AlreadyOwned{}; break;
    case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER: throw Bus::Errors::AlreadyOwner{}; break;
    case -1: throw std::runtime_error(error.print());
    }

    return std::move(result);
}

void Bus::release_name_on_bus(std::unique_ptr<Bus::Name> name)
{
    Error error;
    dbus_bus_release_name(
                d->connection.get(),
                name->as_string().c_str(),
                std::addressof(error.raw()));
}

uint32_t Bus::send(const std::shared_ptr<Message>& msg)
{
    dbus_uint32_t serial;
    if (!dbus_connection_send(
                d->connection.get(),
                msg->d->dbus_message.get(),
                std::addressof(serial)))
        throw std::runtime_error("Problem sending message");

    return serial;
}

std::shared_ptr<Message> Bus::send_with_reply_and_block_for_at_most(
        const std::shared_ptr<Message>& msg,
        const std::chrono::milliseconds& milliseconds)
{
    // TODO(tvoss): Enable this once method handlers have been adjusted to
    // operate on contexts.
    // if (d->executor)
    // {
    //    throw std::logic_error("Calling Bus::send_with_reply_and_block_for_at_most on a "
    //                           "connection that is run by an executor, i.e., an event loop "
    //                           "is not supported as the underlying implementation in libdbus "
    //                           "is racy");
    //}

    Error se;

    auto result = dbus_connection_send_with_reply_and_block(
                d->connection.get(),
                msg->d->dbus_message.get(),
                milliseconds.count(),
                std::addressof(se.raw()));

    if (!result)
        throw std::runtime_error(se.print());

    return Message::from_raw_message(result);
}

PendingCall::Ptr Bus::send_with_reply_and_timeout(
        const std::shared_ptr<Message>& msg,
        const std::chrono::milliseconds& timeout)
{
    DBusPendingCall* pending_call;
    auto result = dbus_connection_send_with_reply(
                d->connection.get(),
                msg->d->dbus_message.get(),
                std::addressof(pending_call),
                timeout.count());

    if (result == FALSE)
        throw Errors::NoMemory{};

    if (!pending_call)
        throw std::runtime_error("Connection disconnected or tried to send fd's over a transport that does not support it");

    return impl::PendingCall::create(pending_call);
}

void Bus::add_match(const MatchRule& rule)
{
    Error se;
    dbus_bus_add_match(d->connection.get(), rule.as_string().c_str(), std::addressof(se.raw()));
    if (se)
        throw std::runtime_error(se.print());
}

void Bus::remove_match(const MatchRule& rule)
{
    Error se;
    dbus_bus_remove_match(d->connection.get(), rule.as_string().c_str(), std::addressof(se.raw()));
    if (se)
        throw std::runtime_error(se.print());
}

bool Bus::has_owner_for_name(const std::string& name)
{
    return dbus_bus_name_has_owner(d->connection.get(), name.c_str(), nullptr);
}

void Bus::install_executor(const Executor::Ptr& e)
{
    d->executor = e;
}

void Bus::stop()
{
    if (!d->executor)
        throw std::runtime_error("Missing executor, cannot stop.");
    d->executor->stop();
}

void Bus::run()
{
    if (!d->executor)
        throw std::runtime_error("Missing executor, cannot run.");
    d->executor->run();
}

void Bus::register_object_for_path(
        const types::ObjectPath& path,
        const std::shared_ptr<Object>& object)
{
    auto vtable = new DBusObjectPathVTable
    {
            VTable::unregister_object_path,
            VTable::on_new_message,
            nullptr,
            nullptr,
            nullptr,
            nullptr
    };

    Error e;
    auto result = dbus_connection_try_register_object_path(
                d->connection.get(),
                path.as_string().c_str(),
                vtable,
                new VTable{object},
                std::addressof(e.raw()));

    if (!result || e)
    {
        delete vtable;
        throw std::runtime_error(e.print());
    }
}

void Bus::unregister_object_path(
        const types::ObjectPath& path)
{
    dbus_connection_unregister_object_path(
                d->connection.get(),
                path.as_string().c_str());
}

Bus::SignalRouter& Bus::access_signal_router()
{
    return d->signal_router;
}

DBusConnection* Bus::raw() const
{
    return d->connection.get();
}
}
}