summaryrefslogtreecommitdiff
path: root/include/CommonAPI/DBus/DBusInstanceAvailabilityStatusChangedEvent.hpp
blob: 0f3dd249d329acaa1ce12b9e9f6abef663846858 (plain)
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
// Copyright (C) 2013-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#if !defined (COMMONAPI_INTERNAL_COMPILATION)
#error "Only <CommonAPI/CommonAPI.hpp> can be included directly, this file may disappear or change contents."
#endif

#ifndef COMMONAPI_DBUS_DBUSINSTANCEAVAILABILITYSTATUSCHANGED_EVENT_HPP_
#define COMMONAPI_DBUS_DBUSINSTANCEAVAILABILITYSTATUSCHANGED_EVENT_HPP_

#include <functional>
#include <future>
#include <string>
#include <vector>

#include <CommonAPI/ProxyManager.hpp>
#include <CommonAPI/DBus/DBusAddressTranslator.hpp>
#include <CommonAPI/DBus/DBusProxy.hpp>
#include <CommonAPI/DBus/DBusObjectManagerStub.hpp>
#include <CommonAPI/DBus/DBusInstanceAvailabilityStatusChangedEvent.hpp>
#include <CommonAPI/DBus/DBusTypes.hpp>

namespace CommonAPI {
namespace DBus {

// TODO Check to move logic to DBusServiceRegistry, now every proxy will deserialize the messages!
class DBusInstanceAvailabilityStatusChangedEvent:
                public ProxyManager::InstanceAvailabilityStatusChangedEvent,
                public DBusProxyConnection::DBusSignalHandler {
 public:
    DBusInstanceAvailabilityStatusChangedEvent(DBusProxy &_proxy, const std::string &_interfaceName) :
                    proxy_(_proxy),
                    observedInterfaceName_(_interfaceName) {
    }

    virtual ~DBusInstanceAvailabilityStatusChangedEvent() {
        proxy_.removeSignalMemberHandler(interfacesAddedSubscription_);
        proxy_.removeSignalMemberHandler(interfacesRemovedSubscription_);
    }

    virtual void onSignalDBusMessage(const DBusMessage& dbusMessage) {
        if (dbusMessage.hasMemberName("InterfacesAdded")) {
            onInterfacesAddedSignal(dbusMessage);
        } else if (dbusMessage.hasMemberName("InterfacesRemoved")) {
            onInterfacesRemovedSignal(dbusMessage);
        }
    }

 protected:
    virtual void onFirstListenerAdded(const Listener&) {
        interfacesAddedSubscription_ = proxy_.addSignalMemberHandler(
                        proxy_.getDBusAddress().getObjectPath(),
                        DBusObjectManagerStub::getInterfaceName(),
                        "InterfacesAdded",
                        "oa{sa{sv}}",
                        this,
                        false);

        interfacesRemovedSubscription_ = proxy_.addSignalMemberHandler(
        				proxy_.getDBusAddress().getObjectPath(),
                        DBusObjectManagerStub::getInterfaceName(),
                        "InterfacesRemoved",
                        "oas",
                        this,
                        false);
    }

    virtual void onLastListenerRemoved(const Listener&) {
        proxy_.removeSignalMemberHandler(interfacesAddedSubscription_);
        proxy_.removeSignalMemberHandler(interfacesRemovedSubscription_);
    }

 private:
    inline void onInterfacesAddedSignal(const DBusMessage &_message) {
        DBusInputStream dbusInputStream(_message);
        std::string dbusObjectPath;
        std::string dbusInterfaceName;
        DBusInterfacesAndPropertiesDict dbusInterfacesAndPropertiesDict;

        dbusInputStream >> dbusObjectPath;
        assert(!dbusInputStream.hasError());

        dbusInputStream.beginReadMapOfSerializableStructs();
        while (!dbusInputStream.readMapCompleted()) {
            dbusInputStream.align(8);
            dbusInputStream >> dbusInterfaceName;
            dbusInputStream.skipMap();
            assert(!dbusInputStream.hasError());
            if(dbusInterfaceName == observedInterfaceName_) {
                notifyInterfaceStatusChanged(dbusObjectPath, dbusInterfaceName, AvailabilityStatus::AVAILABLE);
            }
        }
        dbusInputStream.endReadMapOfSerializableStructs();
    }

    inline void onInterfacesRemovedSignal(const DBusMessage &_message) {
        DBusInputStream dbusInputStream(_message);
        std::string dbusObjectPath;
        std::vector<std::string> dbusInterfaceNames;

        dbusInputStream >> dbusObjectPath;
        assert(!dbusInputStream.hasError());

        dbusInputStream >> dbusInterfaceNames;
        assert(!dbusInputStream.hasError());

        for (const auto& dbusInterfaceName : dbusInterfaceNames) {
            if(dbusInterfaceName == observedInterfaceName_) {
                notifyInterfaceStatusChanged(dbusObjectPath, dbusInterfaceName, AvailabilityStatus::NOT_AVAILABLE);
            }
        }
    }

    void notifyInterfaceStatusChanged(const std::string &_objectPath,
                                      const std::string &_interfaceName,
                                      const AvailabilityStatus &_availability) {
        CommonAPI::Address itsAddress;
        DBusAddress itsDBusAddress(proxy_.getDBusAddress().getService(),
                				   _objectPath,
								   _interfaceName);

        DBusAddressTranslator::get()->translate(itsDBusAddress, itsAddress);

        notifyListeners(itsAddress.getAddress(), _availability);
    }


    DBusProxy &proxy_;
    std::string observedInterfaceName_;
    DBusProxyConnection::DBusSignalHandlerToken interfacesAddedSubscription_;
    DBusProxyConnection::DBusSignalHandlerToken interfacesRemovedSubscription_;
};

} // namespace DBus
} // namespace CommonAPI

#endif // COMMONAPI_DBUS_DBUSINSTANCEAVAILABILITYSTATUSCHANGEDEVENT_HPP_