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"
14 #include <dbus/dbus.h>
16 #include <CommonAPI/Event.h>
21 DBusProxyStatusEvent::DBusProxyStatusEvent(DBusProxy* dbusProxy) :
22 dbusProxy_(dbusProxy) {
25 void DBusProxyStatusEvent::onFirstListenerAdded(const Listener& listener) {
26 auto serviceStatusListener = std::bind(
27 &DBusProxyStatusEvent::onServiceAvailableSignalHandler,
29 std::placeholders::_1,
30 std::placeholders::_2);
32 subscription_ = dbusProxy_->getDBusConnection()->getDBusServiceRegistry()->getServiceStatusEvent().subscribe(
33 dbusProxy_->dbusBusName_ + ":" + dbusProxy_->dbusObjectPath_ + ":" + dbusProxy_->interfaceName_,
34 serviceStatusListener);
37 void DBusProxyStatusEvent::onLastListenerRemoved(const Listener& listener) {
38 dbusProxy_->getDBusConnection()->getDBusServiceRegistry()->getServiceStatusEvent().unsubscribe(subscription_);
41 SubscriptionStatus DBusProxyStatusEvent::onServiceAvailableSignalHandler(const std::string& name,
42 const AvailabilityStatus& availabilityStatus) {
43 AvailabilityStatus availability = availabilityStatus;
45 return notifyListeners(availability);
48 const std::string DBusProxy::domain_ = "local";
50 DBusProxy::DBusProxy(const std::string& dbusBusName,
51 const std::string& dbusObjectPath,
52 const std::string& interfaceName,
53 const std::shared_ptr<DBusProxyConnection>& dbusProxyConnection) :
54 dbusBusName_(dbusBusName),
55 dbusObjectPath_(dbusObjectPath),
56 interfaceName_(interfaceName),
58 interfaceVersionAttribute_(*this, "getInterfaceVersion"),
61 connection_(dbusProxyConnection) {
64 DBusProxy::DBusProxy(const std::string& dbusBusName,
65 const std::string& dbusObjectPath,
66 const std::string& interfaceName,
67 const std::shared_ptr<DBusProxyConnection>& connection,
68 const bool isAlwaysAvailable) :
69 dbusBusName_(dbusBusName),
70 dbusObjectPath_(dbusObjectPath),
71 interfaceName_(interfaceName),
73 interfaceVersionAttribute_(*this, "getInterfaceVersion"),
74 available_(isAlwaysAvailable),
75 availableSet_(isAlwaysAvailable),
76 connection_(connection) {
79 std::string DBusProxy::getAddress() const {
80 return domain_ + ":" + interfaceName_ + ":" + dbusBusName_;
83 const std::string& DBusProxy::getDomain() const {
87 const std::string& DBusProxy::getServiceId() const {
88 return getInterfaceName();
91 const std::string& DBusProxy::getInstanceId() const {
96 bool DBusProxy::isAvailable() const {
98 auto status = getDBusConnection()->getDBusServiceRegistry()->getReadyFuture().wait_for(std::chrono::milliseconds(1));
99 if (checkReady(status)) {
100 available_ = getDBusConnection()->getDBusServiceRegistry()->isServiceInstanceAlive(getAddress());
101 availableSet_ = true;
107 bool DBusProxy::isAvailableBlocking() const {
109 if (!availableSet_) {
110 getDBusConnection()->getDBusServiceRegistry()->getReadyFuture().wait();
111 available_ = getDBusConnection()->getDBusServiceRegistry()->isServiceInstanceAlive(getAddress());
112 availableSet_ = true;
117 ProxyStatusEvent& DBusProxy::getProxyStatusEvent() {
121 InterfaceVersionAttribute& DBusProxy::getInterfaceVersionAttribute() {
122 return interfaceVersionAttribute_;
125 DBusMessage DBusProxy::createMethodCall(const char* methodName,
126 const char* methodSignature) const {
127 return DBusMessage::createMethodCall(
128 dbusBusName_.c_str(),
129 dbusObjectPath_.c_str(),
130 getInterfaceName().c_str(),
136 } // namespace CommonAPI