8a1e44cbb067fe669b4eb5a79218ef24d01bcc02
[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   DBusMessageIter iter;
17   DBusMessage *message;
18   char *str;
19
20   while (1)
21     {
22       message = dbus_message_new_method_call ("org.freedesktop.ThreadTest",
23                                               "TestMethod", NULL);
24
25       dbus_message_append_iter_init (message, &iter);
26
27       if (!dbus_message_iter_append_int32 (&iter, threadnr))
28         {
29           g_print ("thread %d: append threadnr failed\n", threadnr);
30         }
31       
32       if (!dbus_message_iter_append_uint32 (&iter, counter))
33         {
34           g_print ("thread %d: append counter (%d) failed\n", threadnr, counter);
35         }
36       
37       str = g_strdup_printf ("Thread %d-%d\n", threadnr, counter);
38       if (!dbus_message_iter_append_string (&iter, str))
39         {
40           g_print ("thread %d: append string (%s) failed\n", threadnr, str);
41         }
42       g_free (str);
43
44       if (!dbus_connection_send (connection,
45                                  message,
46                                  NULL))
47         {
48           g_print ("thread %d: send message failed\n", threadnr);
49         }
50       
51       dbus_message_unref (message);
52       
53       counter ++;
54     }
55
56   return NULL;
57 }
58
59 int
60 main (int argc, char *argv[])
61 {
62   GMainLoop *loop;
63   DBusError error;
64   int i;
65
66   g_thread_init (NULL);
67   dbus_gthread_init ();
68
69   if(argc < 2)
70     {
71       g_error("Need an address as argv[1]\n");
72       return 1;
73     }
74
75   dbus_error_init (&error);
76   connection = dbus_connection_open (argv[1], &error);
77   if (connection == NULL)
78     {
79       g_printerr ("could not open connection: %s\n", error.message);
80       dbus_error_free (&error);
81       return 1;
82     }
83
84   dbus_connection_setup_with_g_main (connection, NULL);
85
86   for (i = 0; i < N_TEST_THREADS; i++)
87     {
88       g_thread_create (thread_func, GINT_TO_POINTER (i), FALSE, NULL);
89     }
90
91   loop = g_main_loop_new (NULL, FALSE);
92   g_main_run (loop);  
93   
94   return 0;
95 }
96