2005-04-23 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / test / test-service.c
1
2 #include "test-utils.h"
3
4 static DBusLoop *loop;
5 static dbus_bool_t already_quit = FALSE;
6
7 static void
8 quit (void)
9 {
10   if (!already_quit)
11     {
12       _dbus_loop_quit (loop);
13       already_quit = TRUE;
14     }
15 }
16
17 static void
18 die (const char *message)
19 {
20   fprintf (stderr, "*** test-service: %s", message);
21   exit (1);
22 }
23
24 static DBusHandlerResult
25 handle_echo (DBusConnection     *connection,
26              DBusMessage        *message)
27 {
28   DBusError error;
29   DBusMessage *reply;
30   char *s;
31
32   _dbus_verbose ("sending reply to Echo method\n");
33   
34   dbus_error_init (&error);
35   
36   if (!dbus_message_get_args (message,
37                               &error,
38                               DBUS_TYPE_STRING, &s,
39                               DBUS_TYPE_INVALID))
40     {
41       reply = dbus_message_new_error (message,
42                                       error.name,
43                                       error.message);
44
45       if (reply == NULL)
46         die ("No memory\n");
47
48       if (!dbus_connection_send (connection, reply, NULL))
49         die ("No memory\n");
50
51       dbus_message_unref (reply);
52
53       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
54     }
55
56   reply = dbus_message_new_method_return (message);
57   if (reply == NULL)
58     die ("No memory\n");
59   
60   if (!dbus_message_append_args (reply,
61                                  DBUS_TYPE_STRING, &s,
62                                  DBUS_TYPE_INVALID))
63     die ("No memory");
64   
65   if (!dbus_connection_send (connection, reply, NULL))
66     die ("No memory\n");
67
68   fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
69   
70   dbus_message_unref (reply);
71     
72   return DBUS_HANDLER_RESULT_HANDLED;
73 }
74
75 static void
76 path_unregistered_func (DBusConnection  *connection,
77                         void            *user_data)
78 {
79   /* connection was finalized */
80 }
81
82 static DBusHandlerResult
83 path_message_func (DBusConnection  *connection,
84                    DBusMessage     *message,
85                    void            *user_data)
86 {
87   if (dbus_message_is_method_call (message,
88                                    "org.freedesktop.TestSuite",
89                                    "Echo"))
90     return handle_echo (connection, message);
91   else if (dbus_message_is_method_call (message,
92                                         "org.freedesktop.TestSuite",
93                                         "Exit"))
94     {
95       dbus_connection_disconnect (connection);
96       quit ();
97       return DBUS_HANDLER_RESULT_HANDLED;
98     }
99   else if (dbus_message_is_method_call (message,
100                                         "org.freedesktop.TestSuite",
101                                         "EmitFoo"))
102     {
103       /* Emit the Foo signal */
104       DBusMessage *signal;
105       double v_DOUBLE;
106
107       _dbus_verbose ("emitting signal Foo\n");
108       
109       signal = dbus_message_new_signal ("/org/freedesktop/TestSuite",
110                                         "org.freedesktop.TestSuite",
111                                         "Foo");
112       if (signal == NULL)
113         die ("No memory\n");
114
115       v_DOUBLE = 42.6;
116       if (!dbus_message_append_args (signal,
117                                      DBUS_TYPE_DOUBLE, &v_DOUBLE,
118                                      DBUS_TYPE_INVALID))
119         die ("No memory");
120   
121       if (!dbus_connection_send (connection, signal, NULL))
122         die ("No memory\n");
123       
124       return DBUS_HANDLER_RESULT_HANDLED;
125     }
126   else
127     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
128 }
129
130 static DBusObjectPathVTable
131 echo_vtable = {
132   path_unregistered_func,
133   path_message_func,
134   NULL,
135 };
136
137
138 static const char* echo_path = "/org/freedesktop/TestSuite" ;
139
140 static DBusHandlerResult
141 filter_func (DBusConnection     *connection,
142              DBusMessage        *message,
143              void               *user_data)
144 {
145   if (dbus_message_is_signal (message,
146                               DBUS_INTERFACE_LOCAL,
147                               "Disconnected"))
148     {
149       dbus_connection_disconnect (connection);
150       quit ();
151       return DBUS_HANDLER_RESULT_HANDLED;
152     }
153   else
154     {
155       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
156     }
157 }
158
159 int
160 main (int    argc,
161       char **argv)
162 {
163   DBusConnection *connection;
164   DBusError error;
165   int result;
166   
167   dbus_error_init (&error);
168   connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
169   if (connection == NULL)
170     {
171       fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
172                error.message);
173       dbus_error_free (&error);
174       return 1;
175     }
176
177   loop = _dbus_loop_new ();
178   if (loop == NULL)
179     die ("No memory\n");
180   
181   if (!test_connection_setup (loop, connection))
182     die ("No memory\n");
183
184   if (!dbus_connection_add_filter (connection,
185                                    filter_func, NULL, NULL))
186     die ("No memory");
187
188   if (!dbus_connection_register_object_path (connection,
189                                              echo_path,
190                                              &echo_vtable,
191                                              (void*) 0xdeadbeef))
192     die ("No memory");
193
194   {
195     void *d;
196     if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
197       die ("No memory");
198     if (d != (void*) 0xdeadbeef)
199       die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
200   }
201   
202   result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService",
203                                   0, &error);
204   if (dbus_error_is_set (&error))
205     {
206       fprintf (stderr, "Error %s\n", error.message);
207       _dbus_verbose ("*** Failed to acquire service: %s\n",
208                      error.message);
209       dbus_error_free (&error);
210       exit (1);
211     }
212
213   _dbus_verbose ("*** Test service entering main loop\n");
214   _dbus_loop_run (loop);
215
216   test_connection_shutdown (loop, connection);
217
218   dbus_connection_remove_filter (connection, filter_func, NULL);
219   
220   dbus_connection_unref (connection);
221
222   _dbus_loop_unref (loop);
223   loop = NULL;
224   
225   dbus_shutdown ();
226
227   _dbus_verbose ("*** Test service exiting\n");
228   
229   return 0;
230 }