Advertisement
Guest User

Untitled

a guest
Feb 9th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. #include <giomm.h>
  2. #include <thread>
  3.  
  4. int server_main();
  5. int client_main();
  6.  
  7. int main() {
  8. Gio::init();
  9. std::thread thr_server([](){ server_main(); });
  10. sleep(1); // give some time to server to register
  11. std::thread thr_client([](){ client_main(); });
  12. sleep(10); // wait for the client to finish
  13. }
  14.  
  15. #include <giomm.h>
  16. #include <iostream>
  17.  
  18. namespace {
  19. static Glib::RefPtr<Gio::DBus::NodeInfo> introspection_data;
  20.  
  21. static Glib::ustring introspection_xml =
  22. "<node name='/org/glibmm/DBusExample'>"
  23. " <interface name='org.glibmm.DBusExample'>"
  24. " <method name='Method'>"
  25. " </method>"
  26. " </interface>"
  27. "</node>";
  28.  
  29. guint registered_id = 0;
  30. }
  31.  
  32. static void on_method_call(const Glib::RefPtr<Gio::DBus::Connection>& /* connection */,
  33. const Glib::ustring& /* sender */, const Glib::ustring& /* object_path */,
  34. const Glib::ustring& /* interface_name */, const Glib::ustring& method_name,
  35. const Glib::VariantContainerBase& parameters,
  36. const Glib::RefPtr<Gio::DBus::MethodInvocation>& invocation)
  37. {
  38. if(method_name == "Method") {
  39. std::cout << "Method was calledn";
  40. }
  41. }
  42.  
  43. const Gio::DBus::InterfaceVTable interface_vtable(sigc::ptr_fun(&on_method_call));
  44.  
  45. void on_bus_acquired(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& /* name */)
  46. {
  47. std::cout << "on_bus_acquiredn";
  48. try {
  49. registered_id = connection->register_object("/org/glibmm/DBusExample",
  50. introspection_data->lookup_interface(),
  51. interface_vtable);
  52. }
  53. catch(const Glib::Error& ex) {
  54. std::cerr << "Registration of object failed." << std::endl;
  55. }
  56.  
  57. return;
  58. }
  59.  
  60. void on_name_acquired(const Glib::RefPtr<Gio::DBus::Connection>& /* connection */, const Glib::ustring& /* name */)
  61. {}
  62.  
  63. void on_name_lost(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& /* name */) {
  64. connection->unregister_object(registered_id);
  65. }
  66.  
  67. int server_main()
  68. {
  69. try {
  70. introspection_data = Gio::DBus::NodeInfo::create_for_xml(introspection_xml);
  71. }
  72. catch(const Glib::Error& ex) {
  73. std::cerr << "Unable to create introspection data: " << ex.what() <<
  74. "." << std::endl;
  75. return 1;
  76. }
  77.  
  78. const guint id = Gio::DBus::own_name(Gio::DBus::BUS_TYPE_SESSION,
  79. "org.glibmm.DBusExample",
  80. sigc::ptr_fun(&on_bus_acquired),
  81. sigc::ptr_fun(&on_name_acquired),
  82. sigc::ptr_fun(&on_name_lost));
  83.  
  84. //Keep the service running
  85. auto loop = Glib::MainLoop::create();
  86. loop->run();
  87.  
  88. Gio::DBus::unown_name(id);
  89.  
  90. return EXIT_SUCCESS;
  91. }
  92.  
  93. #include <giomm.h>
  94. #include <iostream>
  95.  
  96. Glib::RefPtr<Glib::MainLoop> loop;
  97.  
  98. // A main loop idle callback to quit when the main loop is idle.
  99. bool on_main_loop_idle() {
  100. std::cout << "loop_idlen";
  101. loop->quit();
  102. return false;
  103. }
  104.  
  105. void on_dbus_proxy_available(Glib::RefPtr<Gio::AsyncResult>& result)
  106. {
  107. auto proxy = Gio::DBus::Proxy::create_finish(result);
  108.  
  109. if(!proxy) {
  110. std::cerr << "The proxy to the user's session bus was not successfully "
  111. "created." << std::endl;
  112. loop->quit();
  113. return;
  114. }
  115.  
  116. try {
  117. std::cout << "Calling...n";
  118.  
  119. proxy->call_sync("Method");
  120.  
  121. std::cout << "It works!n";
  122. }
  123. catch(const Glib::Error& error) {
  124. std::cerr << "Got an error: '" << error.what() << "'." << std::endl;
  125. }
  126.  
  127. // Connect an idle callback to the main loop to quit when the main loop is
  128. // idle now that the method call is finished.
  129. Glib::signal_idle().connect(sigc::ptr_fun(&on_main_loop_idle));
  130. }
  131.  
  132. int client_main() {
  133. loop = Glib::MainLoop::create();
  134.  
  135. auto connection =
  136. Gio::DBus::Connection::get_sync(Gio::DBus::BUS_TYPE_SESSION);
  137.  
  138. if(!connection) {
  139. std::cerr << "The user's session bus is not available." << std::endl;
  140. return 1;
  141. }
  142.  
  143. // Create the proxy to the bus asynchronously.
  144. Gio::DBus::Proxy::create(connection, "org.glibmm.DBusExample",
  145. "/org/glibmm/DBusExample", "org.glibmm.DBusExample",
  146. sigc::ptr_fun(&on_dbus_proxy_available));
  147.  
  148. loop->run();
  149.  
  150. return EXIT_SUCCESS;
  151. }
  152.  
  153. ./test
  154. on_bus_acquired
  155. Calling...
  156. <it hangs>
  157.  
  158. #include <giomm.h>
  159.  
  160. int server_main();
  161. int client_main();
  162.  
  163. int main() {
  164. Gio::init();
  165. auto childid = fork();
  166. if (childid == 0) {
  167. server_main();
  168. } else {
  169. sleep(1);
  170. client_main();
  171. }
  172. }
  173.  
  174. ./test
  175. on_bus_acquired
  176. Calling...
  177. Method was called
  178. It works!
  179.  
  180. #include <giomm.h>
  181. #include <thread>
  182.  
  183. int server_main();
  184. int client_main();
  185.  
  186. int main() {
  187. Gio::init();
  188. server_main();
  189. client_main();
  190. auto loop = Glib::MainLoop::create();
  191. loop->run();
  192. }
  193.  
  194. Glib::VariantContainerBase result;
  195. invocation->return_value(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement