2003-04-06 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / test / test-service.c
1
2 #include "test-utils.h"
3
4 static DBusLoop *loop;
5
6 static void
7 die (const char *message)
8 {
9   fprintf (stderr, "%s", message);
10   exit (1);
11 }
12
13 static DBusHandlerResult
14 handle_echo (DBusConnection     *connection,
15              DBusMessage        *message)
16 {
17   DBusError error;
18   DBusMessage *reply;
19   char *s;
20   
21   dbus_error_init (&error);
22   
23   if (!dbus_message_get_args (message,
24                               &error,
25                               DBUS_TYPE_STRING, &s,
26                               DBUS_TYPE_INVALID))
27     {
28       reply = dbus_message_new_error_reply (message,
29                                             error.name,
30                                             error.message);
31
32       if (reply == NULL)
33         die ("No memory\n");
34
35       if (!dbus_connection_send (connection, reply, NULL))
36         die ("No memory\n");
37
38       dbus_message_unref (reply);
39
40       return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
41     }
42
43   reply = dbus_message_new_reply (message);
44   if (reply == NULL)
45     die ("No memory\n");
46
47   if (!dbus_message_append_string (reply, s))
48     die ("No memory");
49
50   if (!dbus_connection_send (connection, reply, NULL))
51     die ("No memory\n");
52   
53   dbus_free (s);
54   
55   dbus_message_unref (reply);
56     
57   return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
58 }
59
60 static DBusHandlerResult
61 filter_func (DBusMessageHandler *handler,
62              DBusConnection     *connection,
63              DBusMessage        *message,
64              void               *user_data)
65 {
66   if (dbus_message_name_is (message, "org.freedesktop.DBus.TestSuiteEcho"))
67     return handle_echo (connection, message);
68   else if (dbus_message_name_is (message, DBUS_MESSAGE_LOCAL_DISCONNECT))
69     {
70       _dbus_loop_quit (loop);
71       return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
72     }
73   else
74     {
75       return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
76     }
77 }
78
79 int
80 main (int    argc,
81       char **argv)
82 {
83   DBusConnection *connection;
84   DBusError error;
85   DBusMessageHandler *handler;
86   const char *to_handle[] = {
87     "org.freedesktop.DBus.TestSuiteEcho",
88     DBUS_MESSAGE_LOCAL_DISCONNECT,
89   };
90   int result;
91   
92   dbus_error_init (&error);
93   connection = dbus_bus_get (DBUS_BUS_ACTIVATION, &error);
94   if (connection == NULL)
95     {
96       fprintf (stderr, "Failed to open connection to activating message bus: %s\n",
97                error.message);
98       dbus_error_free (&error);
99       return 1;
100     }
101
102   loop = _dbus_loop_new ();
103   if (loop == NULL)
104     die ("No memory\n");
105   
106   if (!test_connection_setup (loop, connection))
107     die ("No memory\n");
108
109   handler = dbus_message_handler_new (filter_func, NULL, NULL);
110   if (handler == NULL)
111     die ("No memory");
112   
113   if (!dbus_connection_register_handler (connection, handler, to_handle,
114                                          _DBUS_N_ELEMENTS (to_handle)))
115     die ("No memory");
116
117   result = dbus_bus_acquire_service (connection, "org.freedesktop.DBus.TestSuiteEchoService",
118                                      0, &error);
119   if (dbus_error_is_set (&error))
120     {
121       fprintf (stderr, "Failed to acquire service: %s\n",
122                error.message);
123       dbus_error_free (&error);
124       return 1;
125     }
126   
127   _dbus_loop_run (loop);
128
129   dbus_connection_unref (connection);
130   
131   dbus_message_handler_unref (handler);
132
133   _dbus_loop_unref (loop);
134   loop = NULL;
135   
136   dbus_shutdown ();
137   
138   return 0;
139 }