1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
/*
* Copyright © 2012 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
*/
#ifndef CORE_DBUS_IMPL_OBJECT_H_
#define CORE_DBUS_IMPL_OBJECT_H_
#include <core/dbus/bus.h>
#include <core/dbus/match_rule.h>
#include <core/dbus/message_router.h>
#include <core/dbus/message_streaming_operators.h>
#include <core/dbus/result.h>
#include <core/dbus/signal.h>
#include <core/dbus/interfaces/properties.h>
#include <core/dbus/traits/service.h>
#include <core/dbus/types/any.h>
#include <core/dbus/types/object_path.h>
#include <core/dbus/types/variant.h>
#include <core/dbus/types/stl/map.h>
#include <core/dbus/types/stl/string.h>
#include <core/dbus/types/stl/tuple.h>
#include <core/dbus/types/stl/vector.h>
#include <functional>
#include <future>
#include <iostream>
#include <map>
#include <memory>
#include <string>
namespace core
{
namespace dbus
{
template<typename Signal, typename... Args>
inline void Object::emit_signal(const Args& ... args)
{
auto msg_factory = parent->get_connection()->message_factory();
auto msg = msg_factory->make_signal(
object_path.as_string(),
traits::Service<typename Signal::Interface>::interface_name(),
Signal::name());
if (!msg)
throw std::runtime_error("No memory available to allocate DBus message");
auto writer = msg->writer();
encode_message(writer, args...);
parent->get_connection()->send(msg);
}
template<typename Method, typename ResultType, typename... Args>
inline Result<ResultType> Object::invoke_method_synchronously(const Args& ... args)
{
auto msg_factory = parent->get_connection()->message_factory();
auto msg = msg_factory->make_method_call(
parent->get_name(),
object_path.as_string(),
traits::Service<typename Method::Interface>::interface_name().c_str(),
Method::name());
if (!msg)
throw std::runtime_error("No memory available to allocate DBus message");
auto writer = msg->writer();
encode_message(writer, args...);
auto reply = parent->get_connection()->send_with_reply_and_block_for_at_most(
msg,
Method::default_timeout());
return Result<ResultType>::from_message(reply);
}
template<typename Method, typename ResultType, typename... Args>
inline Result<ResultType> Object::transact_method(const Args& ... args)
{
return invoke_method_asynchronously<Method, ResultType, Args...>(args...).get();
}
template<typename Method, typename ResultType, typename... Args>
inline std::future<Result<ResultType>> Object::invoke_method_asynchronously(const Args& ... args)
{
auto msg_factory = parent->get_connection()->message_factory();
auto msg = msg_factory->make_method_call(
parent->get_name(),
object_path.as_string(),
traits::Service<typename Method::Interface>::interface_name().c_str(),
Method::name());
if (!msg)
throw std::runtime_error("No memory available to allocate DBus message");
auto writer = msg->writer();
encode_message(writer, args...);
auto pending_call =
parent->get_connection()->send_with_reply_and_timeout(
msg, Method::default_timeout());
auto promise = std::make_shared<std::promise<Result<ResultType>>>();
auto future = promise->get_future();
pending_call->then([promise](const Message::Ptr& reply)
{
promise->set_value(Result<ResultType>::from_message(reply));
});
return future;
}
template<typename Method, typename ResultType, typename... Args>
inline void Object::invoke_method_asynchronously_with_callback(
std::function<void(const Result<ResultType>&)> cb,
const Args& ... args)
{
auto msg_factory = parent->get_connection()->message_factory();
auto msg = msg_factory->make_method_call(
parent->get_name(),
object_path.as_string(),
traits::Service<typename Method::Interface>::interface_name().c_str(),
Method::name());
if (!msg)
throw std::runtime_error("No memory available to allocate DBus message");
auto writer = msg->writer();
encode_message(writer, args...);
auto pending_call =
parent->get_connection()->send_with_reply_and_timeout(
msg, Method::default_timeout());
pending_call->then([cb](const Message::Ptr& reply)
{
cb(Result<ResultType>::from_message(reply));
});
}
template<typename PropertyDescription>
inline std::shared_ptr<Property<PropertyDescription>>
Object::get_property()
{
typedef Property<PropertyDescription> PropertyType;
// Creating a stub property for a remote object/property instance
// requires the following steps:
// [1.] Look up if we already have a property instance available in the cache,
// leveraging the tuple (path, interface, name) as key.
// [1.1] If yes: return the property.
// [1.2] If no: Create a new proeprty instance and:
// [1.2.1] Make it known to the cache.
// [1.2.2] Wire it up for property_changed signal receiving.
// [1.2.3] Communicate a new match rule to the dbus daemon to enable reception.
if (parent->is_stub())
{
auto itf = traits::Service<typename PropertyDescription::Interface>::interface_name();
auto name = PropertyDescription::name();
auto ekey = std::make_tuple(path(), itf, name);
auto property = Object::property_cache<PropertyDescription>().retrieve_value_for_key(ekey);
if (property)
{
return property;
}
auto mr = MatchRule()
.type(Message::Type::signal)
.interface(traits::Service<interfaces::Properties>::interface_name())
.member(interfaces::Properties::Signals::PropertiesChanged::name());
property = PropertyType::make_property(shared_from_this());
Object::property_cache<PropertyDescription>().insert_value_for_key(ekey, property);
// We only ever do this once per object.
std::call_once(add_match_once, [&]()
{
// [1.2.4] Inform the dbus daemon that we would like to receive the respective signals.
add_match(mr);
});
// [1.2.2] Enable dispatching of changes.
std::weak_ptr<PropertyType> wp{property};
property_changed_vtable[std::make_tuple(itf, name)] = [wp](const types::Variant& arg)
{
if (auto sp = wp.lock())
sp->handle_changed(arg);
};
return property;
}
return PropertyType::make_property(shared_from_this());
}
template<typename Interface>
inline std::map<std::string, types::Variant>
Object::get_all_properties()
{
return
std::move(
invoke_method_synchronously<
interfaces::Properties::GetAll,
std::map<std::string, types::Variant>
>(traits::Service<Interface>::interface_name()).value());
}
template<typename SignalDescription>
inline const std::shared_ptr<Signal<SignalDescription, typename SignalDescription::ArgumentType>>
Object::get_signal()
{
typedef Signal<SignalDescription, typename SignalDescription::ArgumentType> SignalType;
auto signal =
SignalType::make_signal(shared_from_this(),
traits::Service<typename SignalDescription::Interface>::interface_name(),
SignalDescription::name());
return signal;
}
inline std::shared_ptr<Object> Object::add_object_for_path(const types::ObjectPath& path)
{
return std::shared_ptr<Object>(new Object(parent, path));
}
template<typename Method>
inline void Object::install_method_handler(const MethodHandler& handler)
{
static const dbus::Object::MethodKey key
{
dbus::traits::Service<typename Method::Interface>::interface_name(),
Method::name()
};
method_router.install_route(key, handler);
}
template<typename Method>
inline void Object::uninstall_method_handler()
{
static const dbus::Object::MethodKey key
{
dbus::traits::Service<typename Method::Interface>::interface_name(),
Method::name()
};
method_router.uninstall_route(key);
}
inline bool Object::is_stub() const
{
return parent->is_stub();
}
inline bool Object::on_new_message(const Message::Ptr& msg)
{
return method_router(msg);
}
inline const types::ObjectPath& Object::path() const
{
return object_path;
}
inline Object::Object(
const std::shared_ptr<Service> parent,
const types::ObjectPath& path)
: parent(parent),
object_path(path),
signal_router
{
[](const Message::Ptr& msg)
{
return SignalKey {msg->interface(), msg->member()};
}
},
method_router
{
[](const Message::Ptr& msg)
{
return MethodKey {msg->interface(), msg->member()};
}
},
get_property_router
{
[](const Message::Ptr& msg)
{
std::string interface, member;
msg->reader() >> interface >> member;
return PropertyKey{interface, member};
}
},
set_property_router
{
[](const Message::Ptr& msg)
{
std::string interface, member;
msg->reader() >> interface >> member;
return PropertyKey {interface, member};
}
}
{
parent->get_connection()->access_signal_router().install_route(
object_path,
std::bind(&MessageRouter<SignalKey>::operator(),
std::ref(signal_router),
std::placeholders::_1));
if (!parent->is_stub())
{
install_method_handler<interfaces::Properties::Get>(
std::bind(
&MessageRouter<PropertyKey>::operator(),
std::ref(get_property_router),
std::placeholders::_1));
install_method_handler<interfaces::Properties::Set>(
std::bind(
&MessageRouter<PropertyKey>::operator(),
std::ref(set_property_router),
std::placeholders::_1));
} else
{
// We centrally route org.freedesktop.DBus.Properties.PropertiesChanged
// through the object, which in turn routes via a custom Property cache.
signal_router.install_route(
SignalKey{
traits::Service<interfaces::Properties>::interface_name(),
interfaces::Properties::Signals::PropertiesChanged::name()
},
// Passing 'this' is fine as the lifetime of the signal_router is upper limited
// by the lifetime of 'this'.
[this](const Message::Ptr& msg)
{
interfaces::Properties::Signals::PropertiesChanged::ArgumentType arg;
msg->reader() >> arg;
on_properties_changed(arg);
});
}
}
inline Object::~Object()
{
parent->get_connection()->access_signal_router().uninstall_route(object_path);
parent->get_connection()->unregister_object_path(object_path);
auto mr = MatchRule()
.type(Message::Type::signal)
.interface(traits::Service<interfaces::Properties>::interface_name())
.member(interfaces::Properties::Signals::PropertiesChanged::name());
try
{
remove_match(mr);
} catch(...)
{
// We consciously drop all possible exceptions here. There is hardly
// anything we can do about the error anyway.
}
}
inline void Object::add_match(const MatchRule& rule)
{
parent->add_match(rule.path(object_path));
}
inline void Object::remove_match(const MatchRule& rule)
{
parent->remove_match(rule.path(object_path));
}
inline void Object::on_properties_changed(
const interfaces::Properties::Signals::PropertiesChanged::ArgumentType& arg)
{
const auto& interface = std::get<0>(arg);
const auto& changed_values = std::get<1>(arg);
for (const auto& value : changed_values)
{
auto it = property_changed_vtable.find(std::make_tuple(interface, value.first));
if (it != property_changed_vtable.end())
{
it->second(value.second);
}
}
}
template<typename PropertyDescription>
inline core::dbus::ThreadSafeLifetimeConstrainedCache<
core::dbus::Object::CacheKey,
core::dbus::Property<PropertyDescription>>&
core::dbus::Object::property_cache()
{
static core::dbus::ThreadSafeLifetimeConstrainedCache<
core::dbus::Object::CacheKey,
core::dbus::Property<PropertyDescription>
> cache;
return cache;
}
}
}
#endif // CORE_DBUS_IMPL_OBJECT_H_
|