~thomas-voss/dbus-cpp/force_gcc_4.7_and_symbols

« back to all changes in this revision

Viewing changes to src/core/dbus/service_watcher.cpp

  • Committer: Tarmac
  • Author(s): Pete Woods
  • Date: 2014-02-03 12:21:44 UTC
  • mfrom: (24.3.36 trunk)
  • Revision ID: tarmac-20140203122144-u15nf8142o7cplj3
Add service watcher class to allow watching DBus name ownership changes.

Approved by PS Jenkins bot, Thomas Voß.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2014 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Pete Woods <pete.woods@canonical.com>
 
17
 */
 
18
 
 
19
#include <core/dbus/dbus.h>
 
20
#include <core/dbus/match_rule.h>
 
21
#include <core/dbus/object.h>
 
22
#include <core/dbus/service_watcher.h>
 
23
#include <core/dbus/signal.h>
 
24
 
 
25
namespace core
 
26
{
 
27
namespace dbus
 
28
{
 
29
 
 
30
namespace {
 
31
struct NameOwnerChanged
 
32
{
 
33
    static const std::string& name()
 
34
    {
 
35
        static const std::string s { "NameOwnerChanged" };
 
36
        return s;
 
37
    }
 
38
 
 
39
    typedef DBus Interface;
 
40
    typedef std::tuple<std::string, std::string, std::string> ArgumentType;
 
41
};
 
42
}
 
43
 
 
44
struct dbus::ServiceWatcher::Private
 
45
{
 
46
    void signal_handler(const NameOwnerChanged::ArgumentType& args)
 
47
    {
 
48
        const std::string& old_owner(std::get<1>(args));
 
49
        const std::string& new_owner(std::get<2>(args));
 
50
 
 
51
        owner_changed(old_owner, new_owner);
 
52
 
 
53
        if (new_owner.empty())
 
54
        {
 
55
            service_unregistered();
 
56
        }
 
57
        else
 
58
        {
 
59
            service_registered();
 
60
        }
 
61
    }
 
62
 
 
63
    core::Signal<std::string, std::string> owner_changed;
 
64
    core::Signal<void> service_registered;
 
65
    core::Signal<void> service_unregistered;
 
66
    std::shared_ptr<Object> object;
 
67
    std::shared_ptr<
 
68
            core::dbus::Signal<NameOwnerChanged, NameOwnerChanged::ArgumentType>> signal;
 
69
};
 
70
 
 
71
dbus::ServiceWatcher::ServiceWatcher(std::shared_ptr<Object> object,
 
72
        const std::string& name, DBus::WatchMode watch_mode) :
 
73
        d(new Private())
 
74
{
 
75
    d->object = object;
 
76
 
 
77
    MatchRule::MatchArgs match_args{ { 0, name } };
 
78
 
 
79
    switch (watch_mode)
 
80
    {
 
81
    case DBus::WatchMode::owner_change:
 
82
        break;
 
83
    case DBus::WatchMode::registration:
 
84
        match_args.push_back({ 1, std::string() });
 
85
        break;
 
86
    case DBus::WatchMode::unregistration:
 
87
        match_args.push_back({ 2, std::string() });
 
88
        break;
 
89
    }
 
90
 
 
91
    d->signal = d->object->get_signal<NameOwnerChanged>();
 
92
 
 
93
    d->signal->connect_with_match_args(
 
94
            std::bind(&Private::signal_handler, d, std::placeholders::_1), match_args);
 
95
}
 
96
 
 
97
const core::Signal<std::string, std::string>& dbus::ServiceWatcher::owner_changed() const
 
98
{
 
99
    return d->owner_changed;
 
100
}
 
101
 
 
102
const core::Signal<void>& dbus::ServiceWatcher::service_registered() const
 
103
{
 
104
    return d->service_registered;
 
105
}
 
106
 
 
107
const core::Signal<void>& dbus::ServiceWatcher::service_unregistered() const
 
108
{
 
109
    return d->service_unregistered;
 
110
}
 
111
 
 
112
}
 
113
}