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& dbusBusName,
50 const std::string& dbusObjectPath,
51 const std::string& interfaceName,
52 const std::shared_ptr<DBusProxyConnection>& dbusProxyConnection) :
53 dbusBusName_(dbusBusName),
54 dbusObjectPath_(dbusObjectPath),
55 interfaceName_(interfaceName),
57 interfaceVersionAttribute_(*this, "getInterfaceVersion"),
60 connection_(dbusProxyConnection) {
63 DBusProxy::DBusProxy(const std::string& dbusBusName,
64 const std::string& dbusObjectPath,
65 const std::string& interfaceName,
66 const std::shared_ptr<DBusProxyConnection>& connection,
67 const bool isAlwaysAvailable) :
68 dbusBusName_(dbusBusName),
69 dbusObjectPath_(dbusObjectPath),
70 interfaceName_(interfaceName),
72 interfaceVersionAttribute_(*this, "getInterfaceVersion"),
73 available_(isAlwaysAvailable),
74 availableSet_(isAlwaysAvailable),
75 connection_(connection) {
78 std::string DBusProxy::getAddress() const {
79 return domain_ + ":" + interfaceName_ + ":" + dbusBusName_;
82 const std::string& DBusProxy::getDomain() const {
86 const std::string& DBusProxy::getServiceId() const {
87 return getInterfaceName();
90 const std::string& DBusProxy::getInstanceId() const {
95 bool DBusProxy::isAvailable() const {
97 auto status = getDBusConnection()->getDBusServiceRegistry()->getReadyFuture().wait_for(std::chrono::milliseconds(1));
98 if (checkReady(status)) {
99 available_ = getDBusConnection()->getDBusServiceRegistry()->isServiceInstanceAlive(getAddress());
100 availableSet_ = true;
106 bool DBusProxy::isAvailableBlocking() const {
108 if (!availableSet_) {
109 getDBusConnection()->getDBusServiceRegistry()->getReadyFuture().wait();
110 available_ = getDBusConnection()->getDBusServiceRegistry()->isServiceInstanceAlive(getAddress());
111 availableSet_ = true;
116 ProxyStatusEvent& DBusProxy::getProxyStatusEvent() {
120 InterfaceVersionAttribute& DBusProxy::getInterfaceVersionAttribute() {
121 return interfaceVersionAttribute_;
124 DBusMessage DBusProxy::createMethodCall(const char* methodName,
125 const char* methodSignature) const {
126 return DBusMessage::createMethodCall(
127 dbusBusName_.c_str(),
128 dbusObjectPath_.c_str(),
129 getInterfaceName().c_str(),
135 } // namespace CommonAPI