xref: /openbmc/boost-dbus/include/dbus/connection_service.hpp (revision a8b4eac45384d53a3848c17591ec02c8a02448dc)
191cdbe46SBenjamin Kietzman // Copyright (c) Benjamin Kietzman (github.com/bkietz)
291cdbe46SBenjamin Kietzman //
391cdbe46SBenjamin Kietzman // Distributed under the Boost Software License, Version 1.0. (See accompanying
491cdbe46SBenjamin Kietzman // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
591cdbe46SBenjamin Kietzman 
6fc79e461SBenjamin Kietzman #ifndef DBUS_CONNECTION_SERVICE_HPP
7fc79e461SBenjamin Kietzman #define DBUS_CONNECTION_SERVICE_HPP
8fc79e461SBenjamin Kietzman 
9d620effbSBenjamin Kietzman #include <boost/asio.hpp>
10da3eeb6aSEd Tanous #include <boost/asio/io_service.hpp>
11d620effbSBenjamin Kietzman 
12a1051121SBenjamin Kietzman #include <dbus/detail/async_send_op.hpp>
13da3eeb6aSEd Tanous #include <dbus/element.hpp>
14da3eeb6aSEd Tanous #include <dbus/error.hpp>
15da3eeb6aSEd Tanous #include <dbus/message.hpp>
16fc79e461SBenjamin Kietzman 
17a3cc3d57SBenjamin Kietzman #include <dbus/impl/connection.ipp>
18a3cc3d57SBenjamin Kietzman 
19d620effbSBenjamin Kietzman namespace dbus {
20cd8b76a3SBenjamin Kietzman namespace bus {
21cd8b76a3SBenjamin Kietzman static const int session = DBUS_BUS_SESSION;
22cd8b76a3SBenjamin Kietzman static const int system = DBUS_BUS_SYSTEM;
23cd8b76a3SBenjamin Kietzman static const int starter = DBUS_BUS_STARTER;
24cd8b76a3SBenjamin Kietzman }  // namespace bus
25cd8b76a3SBenjamin Kietzman 
2617826fe7SBenjamin Kietzman class filter;
2717826fe7SBenjamin Kietzman class match;
28da3eeb6aSEd Tanous class connection;
2917826fe7SBenjamin Kietzman 
30*a8b4eac4SEd Tanous class connection_service
31*a8b4eac4SEd Tanous     : public boost::asio::detail::service_base<connection_service> {
32d620effbSBenjamin Kietzman  public:
33a3cc3d57SBenjamin Kietzman   typedef impl::connection implementation_type;
34d620effbSBenjamin Kietzman 
connection_service(boost::asio::io_service & io)35da3eeb6aSEd Tanous   inline explicit connection_service(boost::asio::io_service& io)
36da3eeb6aSEd Tanous       : boost::asio::detail::service_base<connection_service>(io) {}
37d620effbSBenjamin Kietzman 
construct(implementation_type & impl)38da3eeb6aSEd Tanous   inline void construct(implementation_type& impl) {}
39d620effbSBenjamin Kietzman 
destroy(implementation_type & impl)40da3eeb6aSEd Tanous   inline void destroy(implementation_type& impl) {}
41d620effbSBenjamin Kietzman 
shutdown_service()42da3eeb6aSEd Tanous   inline void shutdown_service() {
43cd8b76a3SBenjamin Kietzman     // TODO is there anything that needs shutting down?
44d620effbSBenjamin Kietzman   }
45d620effbSBenjamin Kietzman 
open(implementation_type & impl,const string & address)46da3eeb6aSEd Tanous   inline void open(implementation_type& impl, const string& address) {
47da3eeb6aSEd Tanous     boost::asio::io_service& io = this->get_io_service();
48d620effbSBenjamin Kietzman 
49cfc0655fSBenjamin Kietzman     impl.open(io, address);
50d620effbSBenjamin Kietzman   }
51d620effbSBenjamin Kietzman 
open(implementation_type & impl,const int bus=bus::system)52da3eeb6aSEd Tanous   inline void open(implementation_type& impl, const int bus = bus::system) {
53da3eeb6aSEd Tanous     boost::asio::io_service& io = this->get_io_service();
54d620effbSBenjamin Kietzman 
55cfc0655fSBenjamin Kietzman     impl.open(io, bus);
56cd8b76a3SBenjamin Kietzman   }
57d620effbSBenjamin Kietzman 
send(implementation_type & impl,message & m)58da3eeb6aSEd Tanous   inline message send(implementation_type& impl, message& m) {
59b55ed5d5SBenjamin Kietzman     return impl.send_with_reply_and_block(m);
60d620effbSBenjamin Kietzman   }
61d620effbSBenjamin Kietzman 
62d620effbSBenjamin Kietzman   template <typename Duration>
send(implementation_type & impl,message & m,const Duration & timeout)63*a8b4eac4SEd Tanous   inline message send(implementation_type& impl, message& m,
64*a8b4eac4SEd Tanous                       const Duration& timeout) {
65cd8b76a3SBenjamin Kietzman     if (timeout == Duration::zero()) {
66d620effbSBenjamin Kietzman       // TODO this can return false if it failed
67b55ed5d5SBenjamin Kietzman       impl.send(m);
68377e76abSEd Tanous       // TODO(ed) rework API to seperate blocking and non blocking sends
6977e62c83SEd Tanous       return message(nullptr);
70d620effbSBenjamin Kietzman     } else {
71da3eeb6aSEd Tanous       return impl.send_with_reply_and_block(
72da3eeb6aSEd Tanous           m, std::chrono::milliseconds(timeout).count());
73d620effbSBenjamin Kietzman     }
74d620effbSBenjamin Kietzman   }
75d620effbSBenjamin Kietzman 
76d620effbSBenjamin Kietzman   template <typename MessageHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(MessageHandler,void (boost::system::error_code,message))77d620effbSBenjamin Kietzman   inline BOOST_ASIO_INITFN_RESULT_TYPE(MessageHandler,
78d620effbSBenjamin Kietzman                                        void(boost::system::error_code, message))
79da3eeb6aSEd Tanous       async_send(implementation_type& impl, message& m,
80da3eeb6aSEd Tanous                  BOOST_ASIO_MOVE_ARG(MessageHandler) handler) {
812003615dSBenjamin Kietzman     // begin asynchronous operation
822003615dSBenjamin Kietzman     impl.start(this->get_io_service());
832003615dSBenjamin Kietzman 
84*a8b4eac4SEd Tanous     boost::asio::detail::async_result_init<
85*a8b4eac4SEd Tanous         MessageHandler, void(boost::system::error_code, message)>
86da3eeb6aSEd Tanous         init(BOOST_ASIO_MOVE_CAST(MessageHandler)(handler));
87da3eeb6aSEd Tanous     detail::async_send_op<typename boost::asio::handler_type<
88da3eeb6aSEd Tanous         MessageHandler, void(boost::system::error_code, message)>::type>(
89a1051121SBenjamin Kietzman         this->get_io_service(),
90a1051121SBenjamin Kietzman         BOOST_ASIO_MOVE_CAST(MessageHandler)(init.handler))(impl, m);
91a1051121SBenjamin Kietzman 
92a1051121SBenjamin Kietzman     return init.result.get();
93d620effbSBenjamin Kietzman   }
94d620effbSBenjamin Kietzman 
95da3eeb6aSEd Tanous  private:
96da3eeb6aSEd Tanous   friend connection;
97da3eeb6aSEd Tanous   inline void new_match(implementation_type& impl, match& m);
98cd8b76a3SBenjamin Kietzman 
99da3eeb6aSEd Tanous   inline void delete_match(implementation_type& impl, match& m);
100cd8b76a3SBenjamin Kietzman 
101da3eeb6aSEd Tanous   inline void new_filter(implementation_type& impl, filter& f);
102cd8b76a3SBenjamin Kietzman 
103da3eeb6aSEd Tanous   inline void delete_filter(implementation_type& impl, filter& f);
104d620effbSBenjamin Kietzman };
105d620effbSBenjamin Kietzman 
106d620effbSBenjamin Kietzman }  // namespace dbus
107fc79e461SBenjamin Kietzman 
108fc79e461SBenjamin Kietzman #endif  // DBUS_CONNECTION_SERVICE_HPP
109