23ec3f33103e2da69538ac889015d51ebcbee2c5
[platform/upstream/dbus.git] / glib / test-thread-client.c
1 #include <glib.h>
2 #include "dbus-glib.h"
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6
7 #include "test-thread.h"
8
9 DBusConnection *connection;
10
11 static  gpointer
12 thread_func (gpointer data)
13 {
14   gint32 threadnr = GPOINTER_TO_INT (data);
15   guint32 counter = 0;
16   DBusMessage *message;
17   char *str;
18
19   while (1)
20     {
21       message = dbus_message_new (NULL, "org.freedesktop.ThreadTest");
22
23       if (!dbus_message_append_int32 (message, threadnr))
24         {
25           g_print ("thread %d: append threadnr failed\n", threadnr);
26         }
27       
28       if (!dbus_message_append_uint32 (message, counter))
29         {
30           g_print ("thread %d: append counter (%d) failed\n", threadnr, counter);
31         }
32       
33       str = g_strdup_printf ("Thread %d-%d\n", threadnr, counter);
34       if (!dbus_message_append_string (message, str))
35         {
36           g_print ("thread %d: append string (%s) failed\n", threadnr, str);
37         }
38       g_free (str);
39
40       if (!dbus_connection_send (connection,
41                                  message,
42                                  NULL))
43         {
44           g_print ("thread %d: send message failed\n", threadnr);
45         }
46       
47       dbus_message_unref (message);
48       
49       counter ++;
50     }
51
52   return NULL;
53 }
54
55 int
56 main (int argc, char *argv[])
57 {
58   GMainLoop *loop;
59   DBusResultCode result;
60   int i;
61
62   g_thread_init (NULL);
63   dbus_gthread_init ();
64
65   if(argc < 2)
66     {
67       g_error("Need an address as argv[1]\n");
68       return 1;
69     }
70
71   connection = dbus_connection_open (argv[1], &result);
72   if (connection == NULL)
73     {
74       g_printerr ("could not open connection\n");
75       return 1;
76     }
77
78   dbus_connection_setup_with_g_main (connection);
79
80   for (i = 0; i < N_TEST_THREADS; i++)
81     {
82       g_thread_create (thread_func, GINT_TO_POINTER (i), FALSE, NULL);
83     }
84
85   loop = g_main_loop_new (NULL, FALSE);
86   g_main_run (loop);  
87   
88   return 0;
89 }
90