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 // Workaround for libstdc++ bug
8 #ifndef _GLIBCXX_USE_NANOSLEEP
9 #define _GLIBCXX_USE_NANOSLEEP
12 #include "DBusProxy.h"
13 #include "DBusServiceRegistry.h"
14 #include "DBusUtils.h"
22 DBusProxyStatusEvent::DBusProxyStatusEvent(DBusProxy* dbusProxy) :
23 dbusProxy_(dbusProxy) {
26 void DBusProxyStatusEvent::onListenerAdded(const CancellableListener& listener) {
27 if (dbusProxy_->isAvailable())
28 listener(AvailabilityStatus::AVAILABLE);
32 DBusProxy::DBusProxy(const std::string& commonApiAddress,
33 const std::string& dbusInterfaceName,
34 const std::string& dbusBusName,
35 const std::string& dbusObjectPath,
36 const std::shared_ptr<DBusProxyConnection>& dbusConnection):
37 DBusProxyBase(split(commonApiAddress, ':')[1],
38 split(commonApiAddress, ':')[2],
43 dbusProxyStatusEvent_(this),
44 availabilityStatus_(AvailabilityStatus::UNKNOWN),
45 interfaceVersionAttribute_(*this, "getInterfaceVersion") {
46 const std::string commonApiDomain = split(commonApiAddress, ':')[0];
47 assert(commonApiDomain == "local");
49 dbusConnection->getDBusServiceRegistry()->registerAvailabilityListener(
51 std::bind(&DBusProxy::onDBusServiceInstanceStatus, this, std::placeholders::_1));
54 DBusProxy::~DBusProxy() {
57 bool DBusProxy::isAvailable() const {
58 return (availabilityStatus_ == AvailabilityStatus::AVAILABLE);
61 bool DBusProxy::isAvailableBlocking() const {
62 if (availabilityStatus_ == AvailabilityStatus::UNKNOWN) {
63 std::chrono::milliseconds singleWaitDuration(100);
65 // Wait for the service registry
66 while (availabilityStatus_ == AvailabilityStatus::UNKNOWN) {
67 std::this_thread::sleep_for(singleWaitDuration);
74 ProxyStatusEvent& DBusProxy::getProxyStatusEvent() {
75 return dbusProxyStatusEvent_;
78 InterfaceVersionAttribute& DBusProxy::getInterfaceVersionAttribute() {
79 return interfaceVersionAttribute_;
82 void DBusProxy::onDBusServiceInstanceStatus(const AvailabilityStatus& availabilityStatus) {
83 availabilityStatus_ = availabilityStatus;
85 dbusProxyStatusEvent_.notifyListeners(availabilityStatus);
89 } // namespace CommonAPI