00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <config.h>
00025 #include <glib.h>
00026 #include "dbus-glib.h"
00027 #include <stdlib.h>
00028
00029 #define N_CLIENT_THREADS 1
00030 #define N_ITERATIONS 1000
00031 #define PAYLOAD_SIZE 30
00032 #define ECHO_MESSAGE "org.freedesktop.DBus.Test.EchoProfile"
00033 static const char *address;
00034 static unsigned char *payload;
00035
00036 static void
00037 send_echo_message (DBusConnection *connection)
00038 {
00039 DBusMessage *message;
00040
00041 message = dbus_message_new (ECHO_MESSAGE, NULL);
00042 dbus_message_append_args (message,
00043 DBUS_TYPE_STRING, "Hello World!",
00044 DBUS_TYPE_INT32, 123456,
00045 #if 1
00046 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
00047 payload, PAYLOAD_SIZE,
00048 #endif
00049 DBUS_TYPE_INVALID);
00050
00051 dbus_connection_send (connection, message, NULL);
00052 dbus_message_unref (message);
00053 dbus_connection_flush (connection);
00054 }
00055
00056 static DBusHandlerResult
00057 client_filter (DBusMessageHandler *handler,
00058 DBusConnection *connection,
00059 DBusMessage *message,
00060 void *user_data)
00061 {
00062 int *iterations = user_data;
00063
00064 if (dbus_message_has_name (message, DBUS_MESSAGE_LOCAL_DISCONNECT))
00065 {
00066 g_printerr ("Client thread disconnected\n");
00067 exit (1);
00068 }
00069 else if (dbus_message_has_name (message,
00070 ECHO_MESSAGE))
00071 {
00072 *iterations += 1;
00073 if (*iterations >= N_ITERATIONS)
00074 {
00075 g_print ("Completed %d iterations\n", N_ITERATIONS);
00076 exit (0);
00077 }
00078 send_echo_message (connection);
00079 }
00080
00081 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
00082 }
00083
00084 static void*
00085 thread_func (void *data)
00086 {
00087 DBusError error;
00088 GMainContext *context;
00089 GMainLoop *loop;
00090 DBusMessageHandler *handler;
00091 DBusConnection *connection;
00092 int iterations;
00093
00094 g_printerr ("Starting client thread\n");
00095
00096 dbus_error_init (&error);
00097 connection = dbus_connection_open (address, &error);
00098 if (connection == NULL)
00099 {
00100 g_printerr ("could not open connection: %s\n", error.message);
00101 dbus_error_free (&error);
00102 exit (1);
00103 }
00104
00105 iterations = 1;
00106
00107 handler = dbus_message_handler_new (client_filter,
00108 &iterations, NULL);
00109
00110 if (!dbus_connection_add_filter (connection,
00111 handler))
00112 g_error ("no memory");
00113
00114
00115
00116 context = g_main_context_new ();
00117 loop = g_main_loop_new (context, FALSE);
00118
00119 dbus_connection_setup_with_g_main (connection, context);
00120
00121 g_printerr ("Client thread sending message to prime pingpong\n");
00122 send_echo_message (connection);
00123 g_printerr ("Client thread sent message\n");
00124
00125 g_printerr ("Client thread entering main loop\n");
00126 g_main_loop_run (loop);
00127 g_printerr ("Client thread exiting main loop\n");
00128
00129 g_main_loop_unref (loop);
00130 g_main_context_unref (context);
00131
00132 return NULL;
00133 }
00134
00135 static DBusHandlerResult
00136 server_filter (DBusMessageHandler *handler,
00137 DBusConnection *connection,
00138 DBusMessage *message,
00139 void *user_data)
00140 {
00141 if (dbus_message_has_name (message, DBUS_MESSAGE_LOCAL_DISCONNECT))
00142 {
00143 g_printerr ("Server thread disconnected\n");
00144 exit (1);
00145 }
00146 else if (dbus_message_has_name (message,
00147 ECHO_MESSAGE))
00148 {
00149 send_echo_message (connection);
00150 }
00151
00152 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
00153 }
00154
00155 static void
00156 new_connection_callback (DBusServer *server,
00157 DBusConnection *new_connection,
00158 void *user_data)
00159 {
00160 DBusMessageHandler *handler;
00161
00162 dbus_connection_ref (new_connection);
00163 dbus_connection_setup_with_g_main (new_connection, NULL);
00164
00165 handler = dbus_message_handler_new (server_filter,
00166 NULL, NULL);
00167
00168 if (!dbus_connection_add_filter (new_connection,
00169 handler))
00170 g_error ("no memory");
00171
00172
00173
00174 }
00175
00176 int
00177 main (int argc, char *argv[])
00178 {
00179 GMainLoop *loop;
00180 DBusError error;
00181 DBusServer *server;
00182 int i;
00183
00184 g_thread_init (NULL);
00185 dbus_gthread_init ();
00186
00187 dbus_error_init (&error);
00188 server = dbus_server_listen ("unix:tmpdir="DBUS_TEST_SOCKET_DIR,
00189 &error);
00190 if (server == NULL)
00191 {
00192 g_printerr ("Could not start server: %s\n",
00193 error.message);
00194 return 1;
00195 }
00196
00197 address = dbus_server_get_address (server);
00198 payload = g_malloc (PAYLOAD_SIZE);
00199
00200 dbus_server_set_new_connection_function (server,
00201 new_connection_callback,
00202 NULL, NULL);
00203
00204 loop = g_main_loop_new (NULL, FALSE);
00205
00206 dbus_server_setup_with_g_main (server, NULL);
00207
00208 for (i = 0; i < N_CLIENT_THREADS; i++)
00209 {
00210 g_thread_create (thread_func, NULL, FALSE, NULL);
00211 }
00212
00213 g_printerr ("Server thread entering main loop\n");
00214 g_main_loop_run (loop);
00215 g_printerr ("Server thread exiting main loop\n");
00216
00217 dbus_server_unref (server);
00218
00219 g_main_loop_unref (loop);
00220
00221 return 0;
00222 }
00223