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/. */
8 #include "DBusServiceRegistry.h"
13 #include <dbus/dbus.h>
15 #include <CommonAPI/Event.h>
20 DBusProxyStatusEvent::DBusProxyStatusEvent(DBusProxy* dbusProxy) :
21 dbusProxy_(dbusProxy) {
24 void DBusProxyStatusEvent::onFirstListenerAdded(const Listener& listener) {
25 auto serviceStatusListener = std::bind(
26 &DBusProxyStatusEvent::onServiceAvailableSignalHandler,
28 std::placeholders::_1,
29 std::placeholders::_2);
31 subscription_ = dbusProxy_->getDBusConnection()->getDBusServiceRegistry()->getServiceStatusEvent().subscribe(
32 dbusProxy_->commonApiDomain_ + ":" + dbusProxy_->commonApiServiceId_ + ":" + dbusProxy_->commonApiParticipantId_,
33 serviceStatusListener);
36 void DBusProxyStatusEvent::onLastListenerRemoved(const Listener& listener) {
37 dbusProxy_->getDBusConnection()->getDBusServiceRegistry()->getServiceStatusEvent().unsubscribe(subscription_);
40 SubscriptionStatus DBusProxyStatusEvent::onServiceAvailableSignalHandler(const std::string& name,
41 const AvailabilityStatus& availabilityStatus) {
42 AvailabilityStatus availability = availabilityStatus;
44 return notifyListeners(availability);
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),
60 interfaceVersionAttribute_(*this, "getInterfaceVersion"),
63 connection_(dbusProxyConnection) {
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):
72 commonApiServiceId_(""),
73 commonApiParticipantId_(""),
74 dbusBusName_(dbusBusName),
75 dbusObjectPath_(dbusObjectPath),
76 dbusInterfaceName_(dbusInterfaceName),
78 interfaceVersionAttribute_(*this, "getInterfaceVersion"),
79 available_(isAlwaysAvailable),
80 availableSet_(isAlwaysAvailable),
81 connection_(connection) {
84 std::string DBusProxy::getAddress() const {
85 return commonApiDomain_ + ":" + commonApiServiceId_ + ":" + commonApiParticipantId_;
88 const std::string& DBusProxy::getDomain() const {
89 return commonApiDomain_;
92 const std::string& DBusProxy::getServiceId() const {
93 return commonApiServiceId_;
96 const std::string& DBusProxy::getInstanceId() const {
97 return commonApiParticipantId_;
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(
109 availableSet_ = true;
115 bool DBusProxy::isAvailableBlocking() const {
116 if (!availableSet_) {
117 getDBusConnection()->getDBusServiceRegistry()->getReadyFuture().wait();
118 available_ = getDBusConnection()->getDBusServiceRegistry()->isServiceInstanceAlive(
122 availableSet_ = true;
127 ProxyStatusEvent& DBusProxy::getProxyStatusEvent() {
131 InterfaceVersionAttribute& DBusProxy::getInterfaceVersionAttribute() {
132 return interfaceVersionAttribute_;
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(),
146 } // namespace CommonAPI