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_->dbusBusName_ + ":" + dbusProxy_->dbusObjectPath_ + ":" + dbusProxy_->interfaceName_,
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);
47 const std::string DBusProxy::domain_ = "local";
49 DBusProxy::DBusProxy(const std::string& commonApiAddress,
50 const std::string& dbusInterfaceName,
51 const std::string& dbusBusName,
52 const std::string& dbusObjectPath,
53 const std::shared_ptr<DBusProxyConnection>& dbusProxyConnection) :
54 commonApiDomain_(split(commonApiAddress, ':')[0]),
55 commonApiServiceId_(split(commonApiAddress, ':')[1]),
56 commonApiParticipantId_(split(commonApiAddress, ':')[2]),
57 dbusBusName_(dbusBusName),
58 dbusObjectPath_(dbusObjectPath),
59 interfaceName_(dbusInterfaceName),
61 interfaceVersionAttribute_(*this, "getInterfaceVersion"),
64 connection_(dbusProxyConnection) {
67 DBusProxy::DBusProxy(const std::string& commonApiAddress,
68 const std::string& dbusInterfaceName,
69 const std::string& dbusBusName,
70 const std::string& dbusObjectPath,
71 const std::shared_ptr<DBusProxyConnection>& connection,
72 const bool isAlwaysAvailable):
73 commonApiDomain_(split(commonApiAddress, ':')[0]),
74 commonApiServiceId_(split(commonApiAddress, ':')[1]),
75 commonApiParticipantId_(split(commonApiAddress, ':')[2]),
76 dbusBusName_(dbusBusName),
77 dbusObjectPath_(dbusObjectPath),
78 interfaceName_(dbusInterfaceName),
80 interfaceVersionAttribute_(*this, "getInterfaceVersion"),
81 available_(isAlwaysAvailable),
82 availableSet_(isAlwaysAvailable),
83 connection_(connection) {
86 std::string DBusProxy::getAddress() const {
87 return commonApiDomain_ + ":" + commonApiServiceId_ + ":" + commonApiParticipantId_;
90 const std::string& DBusProxy::getDomain() const {
91 return commonApiDomain_;
94 const std::string& DBusProxy::getServiceId() const {
95 return commonApiServiceId_;
98 const std::string& DBusProxy::getInstanceId() const {
99 return commonApiParticipantId_;
103 bool DBusProxy::isAvailable() const {
104 if (!availableSet_) {
105 auto status = getDBusConnection()->getDBusServiceRegistry()->getReadyFuture().wait_for(std::chrono::milliseconds(1));
106 if (checkReady(status)) {
107 available_ = getDBusConnection()->getDBusServiceRegistry()->isServiceInstanceAlive(getAddress());
108 availableSet_ = true;
114 bool DBusProxy::isAvailableBlocking() const {
116 if (!availableSet_) {
117 getDBusConnection()->getDBusServiceRegistry()->getReadyFuture().wait();
118 available_ = getDBusConnection()->getDBusServiceRegistry()->isServiceInstanceAlive(getAddress());
119 availableSet_ = true;
124 ProxyStatusEvent& DBusProxy::getProxyStatusEvent() {
128 InterfaceVersionAttribute& DBusProxy::getInterfaceVersionAttribute() {
129 return interfaceVersionAttribute_;
132 DBusMessage DBusProxy::createMethodCall(const char* methodName,
133 const char* methodSignature) const {
134 return DBusMessage::createMethodCall(
135 dbusBusName_.c_str(),
136 dbusObjectPath_.c_str(),
137 getInterfaceName().c_str(),
143 } // namespace CommonAPI