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