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