1 // Copyright (c) Benjamin Kietzman (github.com/bkietz)
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef DBUS_CONNECTION_HPP
7 #define DBUS_CONNECTION_HPP
8 
9 #include <string>
10 #include <boost/asio.hpp>
11 #include <dbus/chrono.hpp>
12 #include <dbus/element.hpp>
13 #include <dbus/message.hpp>
14 #include <dbus/connection_service.hpp>
15 
16 namespace dbus {
17 
18 using namespace boost::asio;
19 
20 class filter;
21 class match;
22 
23 /// Root D-Bus IO object
24 /**
25  * A connection to a bus, through which messages may be sent or received.
26  */
27 class connection
28   : public basic_io_object<connection_service>
29 {
30 public:
31 
32   /// Open a connection to a specified address.
33   /**
34    * @param io_service The io_service object that the connection will use to
35    * wire D-Bus for asynchronous operation.
36    *
37    * @param address The address of the bus to connect to.
38    *
39    * @param shared Set false to open a private connection.
40    *
41    * @throws boost::system::system_error When opening the connection failed.
42    */
43   connection(io_service& io, const string& address, bool shared=true)
44     : basic_io_object<connection_service>(io)
45   {
46     this->get_service().open(
47       this->get_implementation(),
48       address,
49       shared);
50   }
51 
52   /// Open a connection to a well-known bus.
53   /**
54    * D-Bus connections are usually opened to well-known buses like the
55    * system or session bus.
56    *
57    * @param bus The well-known bus to connect to.
58    *
59    * @param shared Set false to open a private connection.
60    *
61    * @throws boost::system::system_error When opening the connection failed.
62    */
63   // TODO: change this unsigned to an enumeration
64   connection(io_service& io, const int bus, bool shared=true)
65     : basic_io_object<connection_service>(io)
66   {
67     this->get_service().open(
68       this->get_implementation(),
69       bus,
70       shared);
71   }
72 
73   /// Send a message.
74   /**
75    * @param m The message to send.
76    *
77    * @return The reply received.
78    *
79    * @throws boost::system::system_error When the response timed out or
80    * there was some other error.
81    */
82   message send(message& m)
83   {
84     return this->get_service().send(
85       this->get_implementation(),
86       m);
87   }
88 
89   /// Send a message.
90   /**
91    * @param m The message to send.
92    *
93    * @param t Time to wait for a reply. Passing 0 as the timeout means
94    * that you wish to ignore the reply. (Or catch it later somehow...)
95    *
96    * @return The reply received.
97    *
98    * @throws boost::system::system_error When the response timed out (if
99    * timeout was not 0), or there was some other error.
100    */
101   template <typename Duration>
102   message send(message& m,
103       const Duration& t)
104   {
105     return this->get_service().send(
106       this->get_implementation(),
107       m,
108       t);
109   }
110 
111   /// Send a message asynchronously.
112   /**
113    * @param m The message to send.
114    *
115    * @param handler Handler for the reply.
116    *
117    * @return Asynchronous result
118    */
119   template<typename MessageHandler>
120   inline BOOST_ASIO_INITFN_RESULT_TYPE(MessageHandler,
121       void(boost::system::error_code, message))
122   async_send(message& m,
123       BOOST_ASIO_MOVE_ARG(MessageHandler) handler)
124   {
125     return this->get_service().async_send(
126       this->get_implementation(),
127       m,
128       BOOST_ASIO_MOVE_CAST(MessageHandler)(handler));
129   }
130 
131   /// Create a new match.
132   void new_match(match &m)
133   {
134     this->get_service().new_match(
135       this->get_implementation(),
136       m);
137   }
138 
139   /// Destroy a match.
140   void delete_match(match& m)
141   {
142     this->get_service().delete_match(
143       this->get_implementation(),
144       m);
145   }
146 
147   /// Create a new filter.
148   void new_filter(filter& f)
149   {
150     this->get_service().new_filter(
151       this->get_implementation(),
152       f);
153   }
154 
155   /// Destroy a filter.
156   void delete_filter(filter& f)
157   {
158     this->get_service().delete_filter(
159       this->get_implementation(),
160       f);
161   }
162 
163 };
164 
165 
166 } // namespace dbus
167 
168 
169 #endif // DBUS_CONNECTION_HPP
170