|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "test-utils.h" |
|
19 |
|
20 typedef struct |
|
21 { |
|
22 DBusLoop *loop; |
|
23 DBusConnection *connection; |
|
24 |
|
25 } CData; |
|
26 |
|
27 static dbus_bool_t |
|
28 connection_watch_callback (DBusWatch *watch, |
|
29 unsigned int condition, |
|
30 void *data) |
|
31 { |
|
32 return dbus_watch_handle (watch, condition); |
|
33 } |
|
34 |
|
35 static dbus_bool_t |
|
36 add_watch (DBusWatch *watch, |
|
37 void *data) |
|
38 { |
|
39 CData *cd = data; |
|
40 |
|
41 return _dbus_loop_add_watch (cd->loop, |
|
42 watch, |
|
43 connection_watch_callback, |
|
44 cd, NULL); |
|
45 } |
|
46 |
|
47 static void |
|
48 remove_watch (DBusWatch *watch, |
|
49 void *data) |
|
50 { |
|
51 CData *cd = data; |
|
52 |
|
53 _dbus_loop_remove_watch (cd->loop, |
|
54 watch, connection_watch_callback, cd); |
|
55 } |
|
56 |
|
57 static void |
|
58 connection_timeout_callback (DBusTimeout *timeout, |
|
59 void *data) |
|
60 { |
|
61 /* Can return FALSE on OOM but we just let it fire again later */ |
|
62 dbus_timeout_handle (timeout); |
|
63 } |
|
64 |
|
65 static dbus_bool_t |
|
66 add_timeout (DBusTimeout *timeout, |
|
67 void *data) |
|
68 { |
|
69 CData *cd = data; |
|
70 |
|
71 return _dbus_loop_add_timeout (cd->loop, |
|
72 timeout, connection_timeout_callback, cd, NULL); |
|
73 } |
|
74 |
|
75 static void |
|
76 remove_timeout (DBusTimeout *timeout, |
|
77 void *data) |
|
78 { |
|
79 CData *cd = data; |
|
80 |
|
81 _dbus_loop_remove_timeout (cd->loop, |
|
82 timeout, connection_timeout_callback, cd); |
|
83 } |
|
84 |
|
85 static void |
|
86 dispatch_status_function (DBusConnection *connection, |
|
87 DBusDispatchStatus new_status, |
|
88 void *data) |
|
89 { |
|
90 DBusLoop *loop = data; |
|
91 |
|
92 if (new_status != DBUS_DISPATCH_COMPLETE) |
|
93 { |
|
94 while (!_dbus_loop_queue_dispatch (loop, connection)) |
|
95 _dbus_wait_for_memory (); |
|
96 } |
|
97 } |
|
98 |
|
99 static void |
|
100 cdata_free (void *data) |
|
101 { |
|
102 CData *cd = data; |
|
103 |
|
104 dbus_connection_unref (cd->connection); |
|
105 _dbus_loop_unref (cd->loop); |
|
106 |
|
107 dbus_free (cd); |
|
108 } |
|
109 |
|
110 static CData* |
|
111 cdata_new (DBusLoop *loop, |
|
112 DBusConnection *connection) |
|
113 { |
|
114 CData *cd; |
|
115 |
|
116 cd = dbus_new0 (CData, 1); |
|
117 if (cd == NULL) |
|
118 return NULL; |
|
119 |
|
120 cd->loop = loop; |
|
121 cd->connection = connection; |
|
122 |
|
123 dbus_connection_ref (cd->connection); |
|
124 _dbus_loop_ref (cd->loop); |
|
125 |
|
126 return cd; |
|
127 } |
|
128 |
|
129 dbus_bool_t |
|
130 test_connection_setup (DBusLoop *loop, |
|
131 DBusConnection *connection) |
|
132 { |
|
133 CData *cd; |
|
134 |
|
135 cd = NULL; |
|
136 |
|
137 dbus_connection_set_dispatch_status_function (connection, dispatch_status_function, |
|
138 loop, NULL); |
|
139 |
|
140 cd = cdata_new (loop, connection); |
|
141 if (cd == NULL) |
|
142 goto nomem; |
|
143 |
|
144 /* Because dbus-mainloop.c checks dbus_timeout_get_enabled(), |
|
145 * dbus_watch_get_enabled() directly, we don't have to provide |
|
146 * "toggled" callbacks. |
|
147 */ |
|
148 |
|
149 if (!dbus_connection_set_watch_functions (connection, |
|
150 add_watch, |
|
151 remove_watch, |
|
152 NULL, |
|
153 cd, cdata_free)) |
|
154 goto nomem; |
|
155 |
|
156 |
|
157 cd = cdata_new (loop, connection); |
|
158 if (cd == NULL) |
|
159 goto nomem; |
|
160 |
|
161 if (!dbus_connection_set_timeout_functions (connection, |
|
162 add_timeout, |
|
163 remove_timeout, |
|
164 NULL, |
|
165 cd, cdata_free)) |
|
166 goto nomem; |
|
167 |
|
168 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE) |
|
169 { |
|
170 if (!_dbus_loop_queue_dispatch (loop, connection)) |
|
171 goto nomem; |
|
172 } |
|
173 |
|
174 return TRUE; |
|
175 |
|
176 nomem: |
|
177 if (cd) |
|
178 cdata_free (cd); |
|
179 |
|
180 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL); |
|
181 dbus_connection_set_watch_functions (connection, NULL, NULL, NULL, NULL, NULL); |
|
182 dbus_connection_set_timeout_functions (connection, NULL, NULL, NULL, NULL, NULL); |
|
183 |
|
184 return FALSE; |
|
185 } |
|
186 |
|
187 void |
|
188 test_connection_shutdown (DBusLoop *loop, |
|
189 DBusConnection *connection) |
|
190 { |
|
191 if (!dbus_connection_set_watch_functions (connection, |
|
192 NULL, |
|
193 NULL, |
|
194 NULL, |
|
195 NULL, NULL)) |
|
196 _dbus_assert_not_reached ("setting watch functions to NULL failed"); |
|
197 |
|
198 if (!dbus_connection_set_timeout_functions (connection, |
|
199 NULL, |
|
200 NULL, |
|
201 NULL, |
|
202 NULL, NULL)) |
|
203 _dbus_assert_not_reached ("setting timeout functions to NULL failed"); |
|
204 |
|
205 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL); |
|
206 } |