blob: 57c161235acfa1f7ab0b22c547788e231ac2c769 [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001#include <config.h>
2
3#include "test-utils.h"
4
5static DBusLoop *loop;
6static dbus_bool_t already_quit = FALSE;
7static const char* echo_path = "/org/freedesktop/TestSuite";
8
9typedef struct
10{
11 int argc;
12 char **argv;
13} EchoData;
14
15static void
16quit (void)
17{
18 if (!already_quit)
19 {
20 _dbus_loop_quit (loop);
21 already_quit = TRUE;
22 }
23}
24
25static void
26die (const char *message)
27{
28 fprintf (stderr, "*** test-service: %s", message);
29 exit (1);
30}
31
32static DBusHandlerResult
33handle_echo (DBusConnection *connection,
34 DBusMessage *message)
35{
36 DBusError error;
37 DBusMessage *reply;
38 DBusMessageIter iter;
39 int i;
40 EchoData *d;
41
42 _dbus_verbose ("sending reply to Echo method\n");
43
44 if (!dbus_connection_get_object_path_data (connection, echo_path, (void **)&d))
45 die ("No memory");
46
47
48 dbus_error_init (&error);
49
50 reply = dbus_message_new_method_return (message);
51 if (reply == NULL)
52 die ("No memory\n");
53
54 dbus_message_iter_init_append (reply, &iter);
55 for (i = 0; i < d->argc; ++i)
56 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &(d->argv[i])))
57 die ("No memory\n");
58
59 if (!dbus_connection_send (connection, reply, NULL))
60 die ("No memory\n");
61
62 fprintf (stderr, "Shell echo service echoed the command line\n");
63
64 dbus_message_unref (reply);
65
66 return DBUS_HANDLER_RESULT_HANDLED;
67}
68
69static void
70path_unregistered_func (DBusConnection *connection,
71 void *user_data)
72{
73 /* connection was finalized */
74}
75
76static DBusHandlerResult
77path_message_func (DBusConnection *connection,
78 DBusMessage *message,
79 void *user_data)
80{
81 if (dbus_message_is_method_call (message,
82 "org.freedesktop.TestSuite",
83 "Echo"))
84 return handle_echo (connection, message);
85 else if (dbus_message_is_method_call (message,
86 "org.freedesktop.TestSuite",
87 "Exit"))
88 {
89 quit ();
90 return DBUS_HANDLER_RESULT_HANDLED;
91 }
92 else
93 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
94}
95
96static DBusObjectPathVTable
97echo_vtable = {
98 path_unregistered_func,
99 path_message_func,
100 NULL,
101};
102
103static DBusHandlerResult
104filter_func (DBusConnection *connection,
105 DBusMessage *message,
106 void *user_data)
107{
108 if (dbus_message_is_signal (message,
109 DBUS_INTERFACE_LOCAL,
110 "Disconnected"))
111 {
112 quit ();
113 return DBUS_HANDLER_RESULT_HANDLED;
114 }
115 else
116 {
117 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
118 }
119}
120
121int
122main (int argc,
123 char **argv)
124{
125 DBusConnection *connection;
126 DBusError error;
127 EchoData echo_data;
128 int result;
129
130 echo_data.argc = argc;
131 echo_data.argv = argv;
132
133 dbus_error_init (&error);
134 connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
135 if (connection == NULL)
136 {
137 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
138 error.message);
139 dbus_error_free (&error);
140 return 1;
141 }
142
143 loop = _dbus_loop_new ();
144 if (loop == NULL)
145 die ("No memory\n");
146
147 if (!test_connection_setup (loop, connection))
148 die ("No memory\n");
149
150 if (!dbus_connection_add_filter (connection,
151 filter_func, NULL, NULL))
152 die ("No memory");
153
154 if (!dbus_connection_register_object_path (connection,
155 echo_path,
156 &echo_vtable,
157 (void*) &echo_data))
158 die ("No memory");
159
160 {
161 void *d;
162 if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
163 die ("No memory");
164 if (d != (void*) &echo_data)
165 die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
166 }
167
168 result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess",
169 0, &error);
170 if (dbus_error_is_set (&error))
171 {
172 fprintf (stderr, "Error %s\n", error.message);
173 _dbus_verbose ("*** Failed to acquire service: %s\n",
174 error.message);
175 dbus_error_free (&error);
176 exit (1);
177 }
178
179 _dbus_verbose ("*** Test service entering main loop\n");
180 _dbus_loop_run (loop);
181
182 test_connection_shutdown (loop, connection);
183
184 dbus_connection_remove_filter (connection, filter_func, NULL);
185
186 dbus_connection_unref (connection);
187
188 _dbus_loop_unref (loop);
189 loop = NULL;
190
191 dbus_shutdown ();
192
193 _dbus_verbose ("*** Test service exiting\n");
194
195 return 0;
196}