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 //TODO add error_code catchers to all these 69 /// Send a message. 70 /** 71 * @param m The message to send. 72 * 73 * @return The reply received. 74 * 75 * @throws boost::system::system_error When the response timed out or 76 * there was some other error. 77 */ 78 message send(message& m) 79 { 80 return this->get_service().send( 81 this->get_implementation(), 82 m); 83 } 84 85 /// Send a message. 86 /** 87 * @param m The message to send. 88 * 89 * @param t Time to wait for a reply. Passing 0 as the timeout means 90 * that you wish to ignore the reply. (Or catch it later somehow...) 91 * 92 * @return The reply received. 93 * 94 * @throws boost::system::system_error When the response timed out (if 95 * timeout was not 0), or there was some other error. 96 */ 97 template <typename Duration> 98 message send(message& m, 99 const Duration& t) 100 { 101 return this->get_service().send( 102 this->get_implementation(), 103 m, 104 t); 105 } 106 107 /// Send a message asynchronously. 108 /** 109 * @param m The message to send. 110 * 111 * @param handler Handler for the reply. 112 * 113 * @return Asynchronous result 114 */ 115 template<typename MessageHandler> 116 inline BOOST_ASIO_INITFN_RESULT_TYPE(MessageHandler, 117 void(boost::system::error_code, message)) 118 async_send(message& m, 119 BOOST_ASIO_MOVE_ARG(MessageHandler) handler) 120 { 121 return this->get_service().async_send( 122 this->get_implementation(), 123 m, 124 BOOST_ASIO_MOVE_CAST(MessageHandler)(handler)); 125 } 126 127 /// Create a new match. 128 void new_match(match &m) 129 { 130 this->get_service().new_match( 131 this->get_implementation(), 132 m); 133 } 134 135 /// Destroy a match. 136 void delete_match(match& m) 137 { 138 this->get_service().delete_match( 139 this->get_implementation(), 140 m); 141 } 142 143 /// Create a new filter. 144 void new_filter(filter& f) 145 { 146 this->get_service().new_filter( 147 this->get_implementation(), 148 f); 149 } 150 151 /// Destroy a filter. 152 void delete_filter(filter& f) 153 { 154 this->get_service().delete_filter( 155 this->get_implementation(), 156 f); 157 } 158 159 }; 160 161 162 } // namespace dbus 163 164 165 #endif // DBUS_CONNECTION_HPP 166