1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server-debug.h In-proc debug server implementation
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-server-debug.h"
26 #include "dbus-transport-debug.h"
27 #include "dbus-connection-internal.h"
28 #include "dbus-hash.h"
30 #ifdef DBUS_BUILD_TESTS
33 * @defgroup DBusServerDebug DBusServerDebug
34 * @ingroup DBusInternals
35 * @brief In-process debug server used in unit tests.
37 * Types and functions related to DBusServerDebug.
38 * This is used for unit testing.
44 * Default timeout interval when reading or writing.
46 #define DEFAULT_INTERVAL 1
49 * Opaque object representing a debug server implementation.
51 typedef struct DBusServerDebug DBusServerDebug;
54 * Implementation details of DBusServerDebug. All members
57 struct DBusServerDebug
59 DBusServer base; /**< Parent class members. */
61 char *name; /**< Server name. */
64 /* Not thread safe, but OK since we don't use
67 static DBusHashTable *server_hash;
70 debug_finalize (DBusServer *server)
75 debug_handle_watch (DBusServer *server,
83 debug_disconnect (DBusServer *server)
87 static DBusServerVTable debug_vtable = {
94 * Looks up a server by its name.
96 * @param server_name the server name.
97 * @returns the server, or #NULL if none exists.
100 _dbus_server_debug_lookup (const char *server_name)
105 return _dbus_hash_table_lookup_string (server_hash, server_name);
109 * Creates a new debug server.
111 * @param server_name the name of the server.
112 * @param result address where a result code can be returned.
113 * @returns a new server, or #NULL on failure.
116 _dbus_server_debug_new (const char *server_name,
117 DBusResultCode *result)
119 DBusServerDebug *debug_server;
123 server_hash = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, NULL);
127 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
132 if (_dbus_hash_table_lookup_string (server_hash, server_name) != NULL)
134 dbus_set_result (result, DBUS_RESULT_ADDRESS_IN_USE);
138 debug_server = dbus_new0 (DBusServerDebug, 1);
140 if (debug_server == NULL)
143 debug_server->name = _dbus_strdup (server_name);
144 if (debug_server->name == NULL)
146 dbus_free (debug_server->name);
147 dbus_free (debug_server);
149 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
152 if (!_dbus_server_init_base (&debug_server->base,
155 dbus_free (debug_server->name);
156 dbus_free (debug_server);
158 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
163 if (!_dbus_hash_table_insert_string (server_hash,
167 _dbus_server_finalize_base (&debug_server->base);
168 dbus_free (debug_server->name);
169 dbus_free (debug_server);
171 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
176 dbus_set_result (result, DBUS_RESULT_SUCCESS);
178 return (DBusServer *)debug_server;
184 DBusTransport *transport;
185 DBusTimeout *timeout;
186 } ServerAndTransport;
189 handle_new_client (void *data)
191 ServerAndTransport *st = data;
192 DBusTransport *transport;
193 DBusConnection *connection;
195 _dbus_verbose (" new debug client transport %p connecting to server\n",
198 transport = _dbus_transport_debug_server_new (st->transport);
199 if (transport == NULL)
202 connection = _dbus_connection_new_for_transport (transport);
203 _dbus_transport_unref (transport);
205 if (connection == NULL)
208 /* See if someone wants to handle this new connection,
209 * self-referencing for paranoia
211 if (st->server->new_connection_function)
213 dbus_server_ref (st->server);
215 (* st->server->new_connection_function) (st->server, connection,
216 st->server->new_connection_data);
217 dbus_server_unref (st->server);
220 _dbus_server_remove_timeout (st->server, st->timeout);
222 /* If no one grabbed a reference, the connection will die. */
223 dbus_connection_unref (connection);
225 /* killing timeout frees both "st" and "timeout" */
226 _dbus_timeout_unref (st->timeout);
232 * Tells the server to accept a transport so the transport
233 * can send messages to it.
235 * @param server the server
236 * @param transport the transport
237 * @returns #TRUE on success.
240 _dbus_server_debug_accept_transport (DBusServer *server,
241 DBusTransport *transport)
243 ServerAndTransport *st = NULL;
245 st = dbus_new (ServerAndTransport, 1);
249 st->transport = transport;
252 st->timeout = _dbus_timeout_new (DEFAULT_INTERVAL, handle_new_client, st,
255 if (st->timeout == NULL)
258 if (!_dbus_server_add_timeout (server, st->timeout))
265 _dbus_timeout_unref (st->timeout);
272 #endif /* DBUS_BUILD_TESTS */