00001 #include <glib.h>
00002 #include "dbus-glib.h"
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <unistd.h>
00006
00007 #include "test-thread.h"
00008
00009 DBusConnection *connection;
00010
00011 static gpointer
00012 thread_func (gpointer data)
00013 {
00014 gint32 threadnr = GPOINTER_TO_INT (data);
00015 guint32 counter = 0;
00016 DBusMessageIter iter;
00017 DBusMessage *message;
00018 char *str;
00019
00020 while (1)
00021 {
00022 message = dbus_message_new ("org.freedesktop.ThreadTest", NULL);
00023
00024 dbus_message_append_iter_init (message, &iter);
00025
00026 if (!dbus_message_iter_append_int32 (&iter, threadnr))
00027 {
00028 g_print ("thread %d: append threadnr failed\n", threadnr);
00029 }
00030
00031 if (!dbus_message_iter_append_uint32 (&iter, counter))
00032 {
00033 g_print ("thread %d: append counter (%d) failed\n", threadnr, counter);
00034 }
00035
00036 str = g_strdup_printf ("Thread %d-%d\n", threadnr, counter);
00037 if (!dbus_message_iter_append_string (&iter, str))
00038 {
00039 g_print ("thread %d: append string (%s) failed\n", threadnr, str);
00040 }
00041 g_free (str);
00042
00043 if (!dbus_connection_send (connection,
00044 message,
00045 NULL))
00046 {
00047 g_print ("thread %d: send message failed\n", threadnr);
00048 }
00049
00050 dbus_message_unref (message);
00051
00052 counter ++;
00053 }
00054
00055 return NULL;
00056 }
00057
00058 int
00059 main (int argc, char *argv[])
00060 {
00061 GMainLoop *loop;
00062 DBusError error;
00063 int i;
00064
00065 g_thread_init (NULL);
00066 dbus_gthread_init ();
00067
00068 if(argc < 2)
00069 {
00070 g_error("Need an address as argv[1]\n");
00071 return 1;
00072 }
00073
00074 dbus_error_init (&error);
00075 connection = dbus_connection_open (argv[1], &error);
00076 if (connection == NULL)
00077 {
00078 g_printerr ("could not open connection: %s\n", error.message);
00079 dbus_error_free (&error);
00080 return 1;
00081 }
00082
00083 dbus_connection_setup_with_g_main (connection, NULL);
00084
00085 for (i = 0; i < N_TEST_THREADS; i++)
00086 {
00087 g_thread_create (thread_func, GINT_TO_POINTER (i), FALSE, NULL);
00088 }
00089
00090 loop = g_main_loop_new (NULL, FALSE);
00091 g_main_run (loop);
00092
00093 return 0;
00094 }
00095