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 if (transport->disconnected)
82 /* Now dispatch the messages */
83 while (dbus_connection_dispatch_message (transport->connection));
87 check_read_timeout (DBusTransport *transport)
89 DBusTransportDebug *debug_transport = (DBusTransportDebug*) transport;
90 dbus_bool_t need_read_timeout;
92 if (transport->connection == NULL)
95 _dbus_transport_ref (transport);
97 need_read_timeout = dbus_connection_get_n_messages (transport->connection) > 0;
99 if (transport->disconnected)
100 need_read_timeout = FALSE;
102 if (need_read_timeout &&
103 debug_transport->read_timeout == NULL)
105 debug_transport->read_timeout =
106 _dbus_timeout_new (DEFAULT_INTERVAL, (DBusTimeoutHandler)do_reading,
109 if (debug_transport->read_timeout == NULL)
112 if (!_dbus_connection_add_timeout (transport->connection,
113 debug_transport->read_timeout))
115 _dbus_timeout_unref (debug_transport->read_timeout);
116 debug_transport->read_timeout = NULL;
121 else if (!need_read_timeout &&
122 debug_transport->read_timeout != NULL)
124 _dbus_connection_remove_timeout (transport->connection,
125 debug_transport->read_timeout);
126 _dbus_timeout_unref (debug_transport->read_timeout);
127 debug_transport->read_timeout = NULL;
131 _dbus_transport_unref (transport);
135 do_writing (DBusTransport *transport)
137 if (transport->disconnected)
140 while (!transport->disconnected &&
141 _dbus_connection_have_messages_to_send (transport->connection))
143 DBusMessage *message, *copy;
145 message = _dbus_connection_get_message_to_send (transport->connection);
146 _dbus_message_lock (message);
148 copy = dbus_message_new_from_message (message);
150 _dbus_connection_message_sent (transport->connection,
153 _dbus_connection_queue_received_message (((DBusTransportDebug *)transport)->other_end->connection,
155 dbus_message_unref (copy);
158 check_read_timeout (((DBusTransportDebug *)transport)->other_end);
162 check_write_timeout (DBusTransport *transport)
164 DBusTransportDebug *debug_transport = (DBusTransportDebug *)transport;
165 dbus_bool_t need_write_timeout;
167 if (transport->connection == NULL)
170 _dbus_transport_ref (transport);
172 need_write_timeout = transport->messages_need_sending;
174 if (transport->disconnected)
175 need_write_timeout = FALSE;
177 if (need_write_timeout &&
178 debug_transport->write_timeout == NULL)
180 debug_transport->write_timeout =
181 _dbus_timeout_new (DEFAULT_INTERVAL, (DBusTimeoutHandler)do_writing,
184 if (debug_transport->write_timeout == NULL)
187 if (!_dbus_connection_add_timeout (transport->connection,
188 debug_transport->write_timeout))
190 _dbus_timeout_unref (debug_transport->write_timeout);
191 debug_transport->write_timeout = NULL;
194 else if (!need_write_timeout &&
195 debug_transport->write_timeout != NULL)
197 _dbus_connection_remove_timeout (transport->connection,
198 debug_transport->write_timeout);
199 _dbus_timeout_unref (debug_transport->write_timeout);
200 debug_transport->write_timeout = NULL;
204 _dbus_transport_unref (transport);
208 debug_handle_watch (DBusTransport *transport,
215 debug_disconnect (DBusTransport *transport)
220 debug_connection_set (DBusTransport *transport)
225 debug_messages_pending (DBusTransport *transport,
226 int messages_pending)
228 check_write_timeout (transport);
232 debug_do_iteration (DBusTransport *transport,
234 int timeout_milliseconds)
239 debug_live_messages_changed (DBusTransport *transport)
243 static DBusTransportVTable debug_vtable = {
247 debug_connection_set,
248 debug_messages_pending,
250 debug_live_messages_changed
254 * Creates a new debug server transport.
256 * @param client the client transport that the server transport
258 * @returns a new debug transport
261 _dbus_transport_debug_server_new (DBusTransport *client)
263 DBusTransportDebug *debug_transport;
265 debug_transport = dbus_new0 (DBusTransportDebug, 1);
267 if (debug_transport == NULL)
270 if (!_dbus_transport_init_base (&debug_transport->base,
274 dbus_free (debug_transport);
278 debug_transport->base.authenticated = TRUE;
280 /* Connect the two transports */
281 debug_transport->other_end = client;
282 ((DBusTransportDebug *)client)->other_end = (DBusTransport *)debug_transport;
284 return (DBusTransport *)debug_transport;
288 * Creates a new debug client transport.
290 * @param server_name name of the server transport that
291 * the client should try to connect to.
292 * @param result address where a result code can be returned.
293 * @returns a new transport, or #NULL on failure.
296 _dbus_transport_debug_client_new (const char *server_name,
297 DBusResultCode *result)
299 DBusServer *debug_server;
300 DBusTransportDebug *debug_transport;
302 debug_server = _dbus_server_debug_lookup (server_name);
306 dbus_set_result (result, DBUS_RESULT_NO_SERVER);
310 debug_transport = dbus_new0 (DBusTransportDebug, 1);
311 if (debug_transport == NULL)
313 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
317 if (!_dbus_transport_init_base (&debug_transport->base,
321 dbus_free (debug_transport);
322 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
326 if (!_dbus_server_debug_accept_transport (debug_server, (DBusTransport *)debug_transport))
328 _dbus_transport_finalize_base (&debug_transport->base);
330 dbus_free (debug_transport);
331 dbus_set_result (result, DBUS_RESULT_IO_ERROR);
336 /* FIXME: Prolly wrong to do this. */
337 debug_transport->base.authenticated = TRUE;
339 return (DBusTransport *)debug_transport;
344 #endif /* DBUS_BUILD_TESTS */