xref: /openbmc/boost-dbus/include/dbus/connection.hpp (revision d620effb340c7f10b9e336927d4a8aa8eda2bdf8)
1 #include <string>
2 #include <boost/asio.hpp>
3 #include <dbus/match.hpp>
4 #include <dbus/filter.hpp>
5 #include <dbus/connection_service.hpp>
6 
7 namespace dbus {
8 
9 using std::string;
10 using namespace boost::asio;
11 
12 /// Root D-Bus IO object
13 /**
14  * A connection to a bus, through which messages may be sent or received.
15  */
16 class connection
17   : public basic_io_object<connection_service>
18 {
19 public:
20 
21   /// Constructor.
22   /**
23    * @param io_service The io_service object that the connection will use to
24    * wire D-Bus for asynchronous operation.
25    */
26   explicit connection(io_service& io)
27     : basic_io_object<connection_service>(io)
28   {
29   }
30 
31   //TODO: should these be constructors...?
32 
33   /// Open a connection to a specified address.
34   /**
35    * @note This function or open(unsigned) should be called only once to initialize
36    * a connection.
37    *
38    * @param address The address of the bus to connect to.
39    *
40    * @param shared Set false to open a private connection.
41    *
42    * @throws boost::system::system_error When opening the connection failed.
43    */
44   void open(const string& address, bool shared=true)
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    * @note This function or open(string) should be called only once to initialize
58    * a connection.
59    *
60    * @param bus_type The well-known bus to connect to.
61    *
62    * @param shared Set false to open a private connection.
63    *
64    * @throws boost::system::system_error When opening the connection failed.
65    */
66   // TODO: change this unsigned to an enumeration
67   void open(const unsigned bus_type, bool shared=true)
68   {
69     this->get_service().open(
70       this->get_implementation(),
71       bus_type,
72       shared);
73   }
74 
75   //TODO add error_code catchers to all these...
76 
77   /// Send a message.
78   /**
79    * @param m The message to send.
80    *
81    * @param timeout Time to wait for a reply. Passing 0 as the timeout means
82    * that you wish to ignore the reply. (Or catch it later somehow...)
83    *
84    * @return The reply received.
85    *
86    * @throws boost::system::system_error When the response timed out, if
87    * timeout was not 0.
88    */
89   template <typename Duration>
90   message send(message& m,
91       const Duration& timeout = timeout_default())
92   {
93     return this->get_service().send(
94       this->get_implementation(),
95       m,
96       timeout);
97   }
98 
99   /// Send a message asynchronously.
100   /**
101    * @param m The message to send.
102    *
103    * @param handler Handler for the reply.
104    *
105    * @return Asynchronous result
106    */
107   template<typename MessageHandler>
108   inline BOOST_ASIO_INITFN_RESULT_TYPE(MessageHandler,
109       void(boost::system::error_code, message))
110   async_send(message& m,
111       BOOST_ASIO_MOVE_ARG(MessageHandler) handler)
112   {
113     return this->get_service().async_send(
114       this->get_implementation(),
115       m,
116       BOOST_ASIO_MOVE_CAST(MessageHandler)(handler));
117   }
118 
119 
120   /// Create a new match.
121   match new_match(
122       BOOST_ASIO_MOVE_ARG(string) expression)
123   {
124     return match(BOOST_ASIO_MOVE_CAST(string)(expression));
125   }
126 
127   /// Create a new filter.
128   template<typename MessagePredicate>
129   filter new_filter(
130       BOOST_ASIO_MOVE_ARG(MessagePredicate) p)
131   {
132     return filter(BOOST_ASIO_MOVE_CAST(MessagePredicate)(p));
133   }
134 
135 };
136 
137 } // namespace dbus
138