summaryrefslogtreecommitdiff
path: root/include/CommonAPI/DBus/DBusSelectiveEvent.hpp
blob: 961063a727381305daed37f82bed2eb1e54096d1 (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
// 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_DBUSSELECTIVEEVENT_HPP_
#define COMMONAPI_DBUS_DBUSSELECTIVEEVENT_HPP_

#include <CommonAPI/DBus/DBusEvent.hpp>

namespace CommonAPI {
namespace DBus {

template<typename EventType_, typename... Arguments_>
class DBusSelectiveEvent: public DBusEvent<EventType_, Arguments_...> {
public:
    typedef typename DBusEvent<EventType_, Arguments_...>::Listener Listener;
    typedef DBusEvent<EventType_, Arguments_...> DBusEventBase;

    DBusSelectiveEvent(DBusProxy &_proxy,
                       const char *_name, const char *_signature,
                       std::tuple<Arguments_...> _arguments)
        : DBusEventBase(_proxy, _name, _signature, _arguments) {
    }

    DBusSelectiveEvent(DBusProxy &_proxy,
                       const char *_name, const char *_signature,
                       const char *_path, const char *_interface,
                       std::tuple<Arguments_...> _arguments)
        : DBusEventBase(_proxy, _name, _signature, _path, _interface, _arguments) {
    }

    virtual ~DBusSelectiveEvent() {}

    virtual void onError(const CommonAPI::CallStatus status) {
        this->notifyError(status);
    }

protected:
    void onFirstListenerAdded(const Listener &) {
        bool success;
        this->subscription_
            = static_cast<DBusProxy&>(this->proxy_).subscribeForSelectiveBroadcastOnConnection(
                success, this->path_, this->interface_, this->name_, this->signature_, this);
                
        if (success == false) {
            // Call error listener with an error code
            this->notifyError(CommonAPI::CallStatus::SUBSCRIPTION_REFUSED);
        }
    }

    void onLastListenerRemoved(const Listener &) {
        static_cast<DBusProxy&>(this->proxy_).unsubscribeFromSelectiveBroadcast(
                        this->name_, this->subscription_, this);
    }
};

} // namespace DBus
} // namespace CommonAPI

#endif // COMMONAPI_DBUS_DBUSSELECTIVEEVENT_HPP_