1 #ifndef DBUS_CONNECTION_HPP 2 #define DBUS_CONNECTION_HPP 3 4 #include <string> 5 #include <boost/asio.hpp> 6 #include <dbus/match.hpp> 7 #include <dbus/filter.hpp> 8 #include <dbus/chrono.hpp> 9 #include <dbus/message.hpp> 10 #include <dbus/connection_service.hpp> 11 12 namespace dbus { 13 14 using std::string; 15 using namespace boost::asio; 16 17 /// Root D-Bus IO object 18 /** 19 * A connection to a bus, through which messages may be sent or received. 20 */ 21 class connection 22 : public basic_io_object<connection_service> 23 { 24 public: 25 26 /// Open a connection to a specified address. 27 /** 28 * @param io_service The io_service object that the connection will use to 29 * wire D-Bus for asynchronous operation. 30 * 31 * @param address The address of the bus to connect to. 32 * 33 * @param shared Set false to open a private connection. 34 * 35 * @throws boost::system::system_error When opening the connection failed. 36 */ 37 connection(io_service& io, const string& address, bool shared=true) 38 : basic_io_object<connection_service>(io) 39 { 40 this->get_service().open( 41 this->get_implementation(), 42 address, 43 shared); 44 } 45 46 /// Open a connection to a well-known bus. 47 /** 48 * D-Bus connections are usually opened to well-known buses like the 49 * system or session bus. 50 * 51 * @param bus The well-known bus to connect to. 52 * 53 * @param shared Set false to open a private connection. 54 * 55 * @throws boost::system::system_error When opening the connection failed. 56 */ 57 // TODO: change this unsigned to an enumeration 58 connection(io_service& io, const int bus, bool shared=true) 59 : basic_io_object<connection_service>(io) 60 { 61 this->get_service().open( 62 this->get_implementation(), 63 bus, 64 shared); 65 } 66 67 //TODO add error_code catchers to all these 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 match new_match( 128 BOOST_ASIO_MOVE_ARG(string) expression) 129 { 130 match m(*this, 131 BOOST_ASIO_MOVE_CAST(string)(expression)); 132 133 this->get_service().new_match( 134 this->get_implementation(), 135 m); 136 137 return m; 138 } 139 140 /// Destroy a match. 141 void delete_match(match& m) 142 { 143 this->get_service().delete_match( 144 this->get_implementation(), 145 m); 146 } 147 148 /// Create a new filter. 149 template<typename MessagePredicate> 150 filter new_filter( 151 BOOST_ASIO_MOVE_ARG(MessagePredicate) p) 152 { 153 filter f(this->get_io_service(), 154 BOOST_ASIO_MOVE_CAST(MessagePredicate)(p)); 155 156 this->get_service().new_filter( 157 this->get_implementation(), 158 f); 159 160 return f; 161 } 162 163 void delete_filter(filter& f) 164 { 165 this->get_service().delete_filter( 166 this->get_implementation(), 167 f); 168 } 169 170 }; 171 172 173 // These need to be here so that connection is a complete 174 // type whose member delete_match can be called 175 match::~match() 176 { 177 this->get_connection().delete_match(*this); 178 } 179 180 filter::~filter() 181 { 182 this->get_connection().delete_filter(*this); 183 } 184 185 186 } // namespace dbus 187 188 189 #endif // DBUS_CONNECTION_HPP 190