1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-transport-debug.c In-proc debug subclass of DBusTransport
4 * Copyright (C) 2003 CodeFactory AB
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-connection-internal.h"
26 #include "dbus-transport-protected.h"
27 #include "dbus-transport-debug.h"
28 #include "dbus-server-debug.h"
29 #include "dbus-list.h"
31 #ifdef DBUS_BUILD_TESTS
34 * @defgroup DBusTransportDebug DBusTransportDebug
35 * @ingroup DBusInternals
36 * @brief In-process debug transport used in unit tests.
38 * Types and functions related to DBusTransportDebug.
39 * This is used for unit testing.
45 * Default timeout interval when reading or writing.
47 #define DEFAULT_INTERVAL 10
50 * Opaque object representing a debug transport.
53 typedef struct DBusTransportDebug DBusTransportDebug;
56 * Implementation details of DBusTransportDebug. All members are private.
58 struct DBusTransportDebug
60 DBusTransport base; /**< Parent instance */
62 DBusTimeout *write_timeout; /**< Timeout for reading. */
63 DBusTimeout *read_timeout; /**< Timeout for writing. */
65 DBusTransport *other_end; /**< The transport that this transport is connected to. */
69 debug_finalize (DBusTransport *transport)
71 _dbus_transport_finalize_base (transport);
73 dbus_free (transport);
77 do_reading (DBusTransport *transport)
79 DBusTransportDebug *debug_transport = (DBusTransportDebug*) transport;
81 if (transport->disconnected)
84 /* Now dispatch the messages */
85 if (dbus_connection_dispatch_message (transport->connection))
87 debug_transport->read_timeout =
88 _dbus_timeout_new (DEFAULT_INTERVAL, (DBusTimeoutHandler)do_reading,
90 if (!_dbus_connection_add_timeout (transport->connection,
91 debug_transport->read_timeout))
93 _dbus_timeout_unref (debug_transport->read_timeout);
94 debug_transport->read_timeout = NULL;
100 check_read_timeout (DBusTransport *transport)
102 DBusTransportDebug *debug_transport = (DBusTransportDebug*) transport;
103 dbus_bool_t need_read_timeout;
105 if (transport->connection == NULL)
108 _dbus_transport_ref (transport);
110 need_read_timeout = dbus_connection_get_n_messages (transport->connection) > 0;
112 if (transport->disconnected)
113 need_read_timeout = FALSE;
115 if (need_read_timeout &&
116 debug_transport->read_timeout == NULL)
118 debug_transport->read_timeout =
119 _dbus_timeout_new (DEFAULT_INTERVAL, (DBusTimeoutHandler)do_reading,
122 if (debug_transport->read_timeout == NULL)
125 if (!_dbus_connection_add_timeout (transport->connection,
126 debug_transport->read_timeout))
128 _dbus_timeout_unref (debug_transport->read_timeout);
129 debug_transport->read_timeout = NULL;
134 else if (!need_read_timeout &&
135 debug_transport->read_timeout != NULL)
137 _dbus_connection_remove_timeout (transport->connection,
138 debug_transport->read_timeout);
139 _dbus_timeout_unref (debug_transport->read_timeout);
140 debug_transport->read_timeout = NULL;
144 _dbus_transport_unref (transport);
148 do_writing (DBusTransport *transport)
150 if (transport->disconnected)
153 while (!transport->disconnected &&
154 _dbus_connection_have_messages_to_send (transport->connection))
156 DBusMessage *message, *copy;
158 message = _dbus_connection_get_message_to_send (transport->connection);
159 _dbus_message_lock (message);
161 copy = dbus_message_new_from_message (message);
163 _dbus_connection_message_sent (transport->connection,
166 _dbus_connection_queue_received_message (((DBusTransportDebug *)transport)->other_end->connection,
168 dbus_message_unref (copy);
171 check_read_timeout (((DBusTransportDebug *)transport)->other_end);
175 check_write_timeout (DBusTransport *transport)
177 DBusTransportDebug *debug_transport = (DBusTransportDebug *)transport;
178 dbus_bool_t need_write_timeout;
180 if (transport->connection == NULL)
183 _dbus_transport_ref (transport);
185 need_write_timeout = transport->messages_need_sending;
187 if (transport->disconnected)
188 need_write_timeout = FALSE;
190 if (need_write_timeout &&
191 debug_transport->write_timeout == NULL)
193 debug_transport->write_timeout =
194 _dbus_timeout_new (DEFAULT_INTERVAL, (DBusTimeoutHandler)do_writing,
197 if (debug_transport->write_timeout == NULL)
200 if (!_dbus_connection_add_timeout (transport->connection,
201 debug_transport->write_timeout))
203 _dbus_timeout_unref (debug_transport->write_timeout);
204 debug_transport->write_timeout = NULL;
207 else if (!need_write_timeout &&
208 debug_transport->write_timeout != NULL)
210 _dbus_connection_remove_timeout (transport->connection,
211 debug_transport->write_timeout);
212 _dbus_timeout_unref (debug_transport->write_timeout);
213 debug_transport->write_timeout = NULL;
217 _dbus_transport_unref (transport);
221 debug_handle_watch (DBusTransport *transport,
228 debug_disconnect (DBusTransport *transport)
233 debug_connection_set (DBusTransport *transport)
238 debug_messages_pending (DBusTransport *transport,
239 int messages_pending)
241 check_write_timeout (transport);
245 debug_do_iteration (DBusTransport *transport,
247 int timeout_milliseconds)
252 debug_live_messages_changed (DBusTransport *transport)
256 static DBusTransportVTable debug_vtable = {
260 debug_connection_set,
261 debug_messages_pending,
263 debug_live_messages_changed
267 * Creates a new debug server transport.
269 * @param client the client transport that the server transport
271 * @returns a new debug transport
274 _dbus_transport_debug_server_new (DBusTransport *client)
276 DBusTransportDebug *debug_transport;
278 debug_transport = dbus_new0 (DBusTransportDebug, 1);
280 if (debug_transport == NULL)
283 if (!_dbus_transport_init_base (&debug_transport->base,
287 dbus_free (debug_transport);
291 debug_transport->base.authenticated = TRUE;
293 /* Connect the two transports */
294 debug_transport->other_end = client;
295 ((DBusTransportDebug *)client)->other_end = (DBusTransport *)debug_transport;
297 return (DBusTransport *)debug_transport;
301 * Creates a new debug client transport.
303 * @param server_name name of the server transport that
304 * the client should try to connect to.
305 * @param result address where a result code can be returned.
306 * @returns a new transport, or #NULL on failure.
309 _dbus_transport_debug_client_new (const char *server_name,
310 DBusResultCode *result)
312 DBusServer *debug_server;
313 DBusTransportDebug *debug_transport;
315 debug_server = _dbus_server_debug_lookup (server_name);
319 dbus_set_result (result, DBUS_RESULT_NO_SERVER);
323 debug_transport = dbus_new0 (DBusTransportDebug, 1);
324 if (debug_transport == NULL)
326 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
330 if (!_dbus_transport_init_base (&debug_transport->base,
334 dbus_free (debug_transport);
335 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
339 if (!_dbus_server_debug_accept_transport (debug_server, (DBusTransport *)debug_transport))
341 _dbus_transport_finalize_base (&debug_transport->base);
343 dbus_free (debug_transport);
344 dbus_set_result (result, DBUS_RESULT_IO_ERROR);
349 /* FIXME: Prolly wrong to do this. */
350 debug_transport->base.authenticated = TRUE;
352 return (DBusTransport *)debug_transport;
357 #endif /* DBUS_BUILD_TESTS */