ca78dbb8cda84d718387fa6c1bb64a54c8d687bc
[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_message (connection,
41                                          message,
42                                          NULL, NULL))
43         {
44           g_print ("thread %d: send message failerd\n", threadnr);
45         }
46       dbus_message_unref (message);
47       
48       counter ++;
49     }
50
51   return NULL;
52 }
53
54 int
55 main (int argc, char *argv[])
56 {
57   GMainLoop *loop;
58   DBusResultCode result;
59   int i;
60
61   g_thread_init (NULL);
62   dbus_gthread_init ();
63
64   if(argc < 2)
65     {
66       g_error("Need an address as argv[1]\n");
67       return 1;
68     }
69
70   connection = dbus_connection_open (argv[1], &result);
71   if (connection == NULL)
72     {
73       g_printerr ("could not open connection\n");
74       return 1;
75     }
76
77   dbus_connection_setup_with_g_main (connection);
78
79   for (i = 0; i < N_TEST_THREADS; i++)
80     {
81       g_thread_create (thread_func, GINT_TO_POINTER (i), FALSE, NULL);
82     }
83
84   loop = g_main_loop_new (NULL, FALSE);
85   g_main_run (loop);  
86   
87   return 0;
88 }
89