Bugfixing, regenerated test files
[profile/ivi/common-api-dbus-runtime.git] / src / CommonAPI / DBus / DBusProxy.cpp
1 /* Copyright (C) 2013 BMW Group
2  * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
3  * Author: Juergen Gehring (juergen.gehring@bmw.de)
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "DBusProxy.h"
8 #include "DBusServiceRegistry.h"
9 #include "DBusUtils.h"
10
11 #include <algorithm>
12 #include <cassert>
13 #include <dbus/dbus.h>
14 #include <functional>
15 #include <CommonAPI/Event.h>
16
17 namespace CommonAPI {
18 namespace DBus {
19
20 DBusProxyStatusEvent::DBusProxyStatusEvent(DBusProxy* dbusProxy) :
21                 dbusProxy_(dbusProxy) {
22 }
23
24 void DBusProxyStatusEvent::onFirstListenerAdded(const Listener& listener) {
25     auto serviceStatusListener = std::bind(
26                     &DBusProxyStatusEvent::onServiceAvailableSignalHandler,
27                     this,
28                     std::placeholders::_1,
29                     std::placeholders::_2);
30
31     subscription_ = dbusProxy_->getDBusConnection()->getDBusServiceRegistry()->getServiceStatusEvent().subscribe(
32                     dbusProxy_->commonApiDomain_ + ":" + dbusProxy_->commonApiServiceId_ + ":" + dbusProxy_->commonApiParticipantId_,
33                     serviceStatusListener);
34 }
35
36 void DBusProxyStatusEvent::onLastListenerRemoved(const Listener& listener) {
37     dbusProxy_->getDBusConnection()->getDBusServiceRegistry()->getServiceStatusEvent().unsubscribe(subscription_);
38 }
39
40 SubscriptionStatus DBusProxyStatusEvent::onServiceAvailableSignalHandler(const std::string& name,
41                                                                          const AvailabilityStatus& availabilityStatus) {
42     AvailabilityStatus availability = availabilityStatus;
43
44     return notifyListeners(availability);
45 }
46
47
48 DBusProxy::DBusProxy(const std::string& commonApiAddress,
49                      const std::string& dbusInterfaceName,
50                      const std::string& dbusBusName,
51                      const std::string& dbusObjectPath,
52                      const std::shared_ptr<DBusProxyConnection>& dbusProxyConnection) :
53                          commonApiDomain_(split(commonApiAddress, ':')[0]),
54                          commonApiServiceId_(split(commonApiAddress, ':')[1]),
55                          commonApiParticipantId_(split(commonApiAddress, ':')[2]),
56                                                  dbusBusName_(dbusBusName),
57                                  dbusObjectPath_(dbusObjectPath),
58                                  dbusInterfaceName_(dbusInterfaceName),
59                                  statusEvent_(this),
60                                  interfaceVersionAttribute_(*this, "getInterfaceVersion"),
61                                  available_(false),
62                                  availableSet_(false),
63                                  connection_(dbusProxyConnection) {
64 }
65
66 DBusProxy::DBusProxy(const std::string& dbusInterfaceName,
67                      const std::string& dbusBusName,
68                      const std::string& dbusObjectPath,
69                      const std::shared_ptr<DBusProxyConnection>& connection,
70                      const bool isAlwaysAvailable):
71                          commonApiDomain_(""),
72                          commonApiServiceId_(""),
73                          commonApiParticipantId_(""),
74                          dbusBusName_(dbusBusName),
75                          dbusObjectPath_(dbusObjectPath),
76                          dbusInterfaceName_(dbusInterfaceName),
77                          statusEvent_(this),
78                          interfaceVersionAttribute_(*this, "getInterfaceVersion"),
79                          available_(isAlwaysAvailable),
80                          availableSet_(isAlwaysAvailable),
81                          connection_(connection) {
82 }
83
84 std::string DBusProxy::getAddress() const {
85     return commonApiDomain_ + ":" + commonApiServiceId_ + ":" + commonApiParticipantId_;
86 }
87
88 const std::string& DBusProxy::getDomain() const {
89     return commonApiDomain_;
90 }
91
92 const std::string& DBusProxy::getServiceId() const {
93     return commonApiServiceId_;
94 }
95
96 const std::string& DBusProxy::getInstanceId() const {
97     return commonApiParticipantId_;
98 }
99
100
101 bool DBusProxy::isAvailable() const {
102     if (!availableSet_) {
103         auto status = getDBusConnection()->getDBusServiceRegistry()->getReadyFuture().wait_for(std::chrono::milliseconds(1));
104         if (checkReady(status)) {
105             available_ = getDBusConnection()->getDBusServiceRegistry()->isServiceInstanceAlive(
106                             dbusInterfaceName_,
107                             dbusBusName_,
108                             dbusObjectPath_);
109             availableSet_ = true;
110         }
111     }
112     return available_;
113 }
114
115 bool DBusProxy::isAvailableBlocking() const {
116     if (!availableSet_) {
117         getDBusConnection()->getDBusServiceRegistry()->getReadyFuture().wait();
118         available_ = getDBusConnection()->getDBusServiceRegistry()->isServiceInstanceAlive(
119                         dbusInterfaceName_,
120                         dbusBusName_,
121                         dbusObjectPath_);
122         availableSet_ = true;
123     }
124     return available_;
125 }
126
127 ProxyStatusEvent& DBusProxy::getProxyStatusEvent() {
128     return statusEvent_;
129 }
130
131 InterfaceVersionAttribute& DBusProxy::getInterfaceVersionAttribute() {
132     return interfaceVersionAttribute_;
133 }
134
135 DBusMessage DBusProxy::createMethodCall(const char* methodName,
136                                         const char* methodSignature) const {
137     return DBusMessage::createMethodCall(
138                     dbusBusName_.c_str(),
139                     dbusObjectPath_.c_str(),
140                     dbusInterfaceName_.c_str(),
141                     methodName,
142                     methodSignature);
143 }
144
145 } // namespace DBus
146 } // namespace CommonAPI