Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | #ifdef HAVE_CONFIG_H |
| 2 | #include <config.h> |
| 3 | #endif |
| 4 | |
| 5 | #include "echo-client.h" |
| 6 | #include <iostream> |
| 7 | #include <pthread.h> |
| 8 | #include <signal.h> |
| 9 | #include <stdio.h> |
| 10 | #include <cstring> |
| 11 | |
| 12 | using namespace std; |
| 13 | |
| 14 | static const char *ECHO_SERVER_NAME = "org.freedesktop.DBus.Examples.Echo"; |
| 15 | static const char *ECHO_SERVER_PATH = "/org/freedesktop/DBus/Examples/Echo"; |
| 16 | |
| 17 | EchoClient::EchoClient(DBus::Connection &connection, const char *path, const char *name) |
| 18 | : DBus::ObjectProxy(connection, path, name) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | void EchoClient::Echoed(const DBus::Variant &value) |
| 23 | { |
| 24 | cout << "!"; |
| 25 | } |
| 26 | |
| 27 | static const size_t THREADS = 3; |
| 28 | |
| 29 | static bool spin = true; |
| 30 | |
| 31 | EchoClient *g_client = NULL; |
| 32 | |
| 33 | DBus::Pipe *thread_pipe_list[THREADS]; |
| 34 | |
| 35 | DBus::BusDispatcher dispatcher; |
| 36 | DBus::DefaultTimeout *timeout; |
| 37 | |
| 38 | void *greeter_thread(void *arg) |
| 39 | { |
| 40 | char idstr[16]; |
| 41 | size_t i = (size_t) arg; |
| 42 | |
| 43 | snprintf(idstr, sizeof(idstr), "%lu", pthread_self()); |
| 44 | |
| 45 | thread_pipe_list[i]->write(idstr, strlen(idstr) + 1); |
| 46 | |
| 47 | cout << idstr << " done (" << i << ")" << endl; |
| 48 | |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | void niam(int sig) |
| 53 | { |
| 54 | spin = false; |
| 55 | |
| 56 | dispatcher.leave(); |
| 57 | } |
| 58 | |
| 59 | void handler1(const void *data, void *buffer, unsigned int nbyte) |
| 60 | { |
| 61 | char *str = (char *) buffer; |
| 62 | cout << "buffer1: " << str << ", size: " << nbyte << endl; |
| 63 | for (int i = 0; i < 30 && spin; ++i) |
| 64 | { |
| 65 | cout << "call1: " << g_client->Hello(str) << endl; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void handler2(const void *data, void *buffer, unsigned int nbyte) |
| 70 | { |
| 71 | char *str = (char *) buffer; |
| 72 | cout << "buffer2: " << str << ", size: " << nbyte << endl; |
| 73 | for (int i = 0; i < 30 && spin; ++i) |
| 74 | { |
| 75 | cout << "call2: " << g_client->Hello(str) << endl; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void handler3(const void *data, void *buffer, unsigned int nbyte) |
| 80 | { |
| 81 | char *str = (char *) buffer; |
| 82 | cout << "buffer3: " << str << ", size: " << nbyte << endl; |
| 83 | for (int i = 0; i < 30 && spin; ++i) |
| 84 | { |
| 85 | cout << "call3: " << g_client->Hello(str) << endl; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | int main() |
| 90 | { |
| 91 | size_t i; |
| 92 | |
| 93 | signal(SIGTERM, niam); |
| 94 | signal(SIGINT, niam); |
| 95 | |
| 96 | DBus::_init_threading(); |
| 97 | |
| 98 | DBus::default_dispatcher = &dispatcher; |
| 99 | |
| 100 | // increase DBus-C++ frequency |
| 101 | new DBus::DefaultTimeout(100, false, &dispatcher); |
| 102 | |
| 103 | DBus::Connection conn = DBus::Connection::SessionBus(); |
| 104 | |
| 105 | EchoClient client(conn, ECHO_SERVER_PATH, ECHO_SERVER_NAME); |
| 106 | g_client = &client; |
| 107 | |
| 108 | pthread_t threads[THREADS]; |
| 109 | |
| 110 | thread_pipe_list[0] = dispatcher.add_pipe(handler1, NULL); |
| 111 | thread_pipe_list[1] = dispatcher.add_pipe(handler2, NULL); |
| 112 | thread_pipe_list[2] = dispatcher.add_pipe(handler3, NULL); |
| 113 | for (i = 0; i < THREADS; ++i) |
| 114 | { |
| 115 | pthread_create(threads + i, NULL, greeter_thread, (void *) i); |
| 116 | } |
| 117 | |
| 118 | dispatcher.enter(); |
| 119 | |
| 120 | cout << "terminating" << endl; |
| 121 | |
| 122 | for (i = 0; i < THREADS; ++i) |
| 123 | { |
| 124 | pthread_join(threads[i], NULL); |
| 125 | } |
| 126 | |
| 127 | dispatcher.del_pipe(thread_pipe_list[0]); |
| 128 | dispatcher.del_pipe(thread_pipe_list[1]); |
| 129 | dispatcher.del_pipe(thread_pipe_list[2]); |
| 130 | |
| 131 | return 0; |
| 132 | } |