summaryrefslogtreecommitdiff
path: root/src/gypsy-server.c
blob: e2e3c1c5e38bac61700f55bf3d3f6260d595efe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Gypsy
 *
 * A simple to use and understand GPSD replacement
 * that uses D-Bus, GLib and memory allocations
 *
 * Author: Iain Holmes <iain@gnome.org>
 * Copyright (C) 2007 Iain Holmes
 * Copyright (C) 2007 Openedhand, Ltd
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

/*
 * GypsyServer - The main control object that creates GPS connection objects.
 */
#include <glib.h>

#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-bindings.h>
#include <dbus/dbus-glib-lowlevel.h>

#include "gypsy-server.h"
#include "gypsy-debug.h"
#include "gypsy-client.h"

enum {
	TERMINATE,
	LAST_SIGNAL,
};

typedef struct _GypsyServerPrivate {
	DBusGConnection *connection;
	GHashTable *connections;
	int client_count; /* When client_count returns to 0, 
			     we quit the daemon after TERMINATE_TIMEOUT */
	guint32 terminate_id;
} GypsyServerPrivate;

static guint32 signals[LAST_SIGNAL] = {0, };

#define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE  ((obj), GYPSY_TYPE_SERVER, GypsyServerPrivate))

G_DEFINE_TYPE (GypsyServer, gypsy_server, G_TYPE_OBJECT);

#define GYPSY_GPS_PATH "/org/freedesktop/Gypsy/"
#define TERMINATE_TIMEOUT 10000 /* 10 second timeout */

static void gypsy_server_create (GypsyServer            *gps,
				 const char             *IN_device_path,
				 DBusGMethodInvocation *context);
static void gypsy_server_shutdown (GypsyServer           *gps,
				   const char            *IN_device_path,
				   DBusGMethodInvocation *context);
#include "gypsy-server-glue.h"

GQuark
gypsy_server_error_quark (void)
{
	static GQuark quark = 0;
	if (quark == 0) {
		quark = g_quark_from_static_string ("gypsy-server-error-quark");
	}

	return quark;
}

/* We don't want to terminate at the moment, as there is no way to be
   restarted until D-Bus 1.2 which has the System bus activation stuff
*/
static gboolean
gypsy_terminate (gpointer data)
{
	GypsyServer *server = data;

	g_signal_emit (server, signals[TERMINATE], 0);

	return FALSE;
}

/* IN_args contains that path to the GPS device we wish to open */
static void
gypsy_server_create (GypsyServer           *gps,
		     const char            *IN_device_path,
		     DBusGMethodInvocation *context)
{
	GypsyServerPrivate *priv;
	GypsyClient *client;
	char *path, *device_name, *sender;
	GList *list;

	priv = GET_PRIVATE (gps);

	/* We might be in the termination timeout when we receive a new
	   create request, so cancel that timeout */
	if (priv->terminate_id > 0) {
		g_source_remove (priv->terminate_id);
		priv->terminate_id = 0;
	}

	GYPSY_NOTE (SERVER, "Creating client for %s", IN_device_path);
	device_name = g_path_get_basename (IN_device_path);
	GYPSY_NOTE (SERVER, "Device name: %s", device_name);
	path = g_strdup_printf ("%s%s", GYPSY_GPS_PATH, 
				g_strdelimit (device_name, ":", '_'));
	g_free (device_name);

	client = (GypsyClient *) dbus_g_connection_lookup_g_object (priv->connection, path);
	if (client == NULL) {
		/* If there isn't already an object registered on that path
		   create and register it */
		client = g_object_new (GYPSY_TYPE_CLIENT, 
				       "device_path", IN_device_path,
				       NULL);
	
		dbus_g_connection_register_g_object (priv->connection, path,
						     G_OBJECT (client));
	} else {
		/* Ref the client so that when one client calls shutdown
		   we won't destroy another clients object */
		g_object_ref (client);
	}

	GYPSY_NOTE (SERVER, "Registered client on %s", path);

	/* Update the hash of open connnctions */
	sender = dbus_g_method_get_sender (context);
	list = g_hash_table_lookup (priv->connections, sender);
	list = g_list_prepend (list, client);
	g_hash_table_insert (priv->connections, sender, list);

	priv->client_count++;

	dbus_g_method_return (context, path);
	g_free (path);
}

static void
gypsy_server_shutdown (GypsyServer           *gps,
		       const char            *IN_device_path,
		       DBusGMethodInvocation *context)
{
	GypsyServerPrivate *priv;
	GypsyClient *client;
	GList *list, *owner;
	char *path, *device_name, *sender;

	priv = GET_PRIVATE (gps);

	GYPSY_NOTE (SERVER, "Shutting down %s", IN_device_path);
	device_name = g_path_get_basename (IN_device_path);
	path = g_strdup_printf ("%s%s", GYPSY_GPS_PATH, device_name);

	client = (GypsyClient *) dbus_g_connection_lookup_g_object (priv->connection, path);
	g_free (path);

	if (client == NULL) {
		dbus_g_method_return_error (context,
					    g_error_new (GYPSY_SERVER_ERROR,
							 GYPSY_SERVER_ERROR_NO_CLIENT,
							 "No such client: %s",
							 device_name));
	} else {
		if (--priv->client_count == 0) {
			if (priv->terminate_id == 0) {
				priv->terminate_id = g_timeout_add (TERMINATE_TIMEOUT,
								    gypsy_terminate,
								    gps);
			}
		}

		/* Update the hash of open connnctions */
		sender = dbus_g_method_get_sender (context);
		list = g_hash_table_lookup (priv->connections, sender);
		owner = g_list_find (list, client);
		if (owner) {
			g_object_unref (owner->data);
		}
		list = g_list_remove (list, client);
		g_hash_table_insert (priv->connections, sender, list);

		dbus_g_method_return (context);

	}

	g_free (device_name);
}

static void
finalize (GObject *object) 
{
 	GypsyServerPrivate *priv = GET_PRIVATE (object);

	g_hash_table_destroy (priv->connections);
	((GObjectClass *) gypsy_server_parent_class)->finalize (object);
}

static void
dispose (GObject *object)
{
	GypsyServerPrivate *priv = GET_PRIVATE (object);

	if (priv->connection) {
		dbus_g_connection_unref (priv->connection);
		priv->connection = NULL;
	}

	((GObjectClass *) gypsy_server_parent_class)->dispose (object);
}

static void
gypsy_server_class_init (GypsyServerClass *klass)
{
	GObjectClass *o_class = (GObjectClass *) klass;
	
	o_class->finalize = finalize;
	o_class->dispose = dispose;

	g_type_class_add_private (klass, sizeof (GypsyServerPrivate));
	dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (klass),
					 &dbus_glib_gypsy_server_object_info);

	signals[TERMINATE] = g_signal_new ("terminate",
					   G_TYPE_FROM_CLASS (klass),
					   G_SIGNAL_RUN_FIRST |
					   G_SIGNAL_NO_RECURSE,
					   G_STRUCT_OFFSET (GypsyServerClass,
							    terminate),
					   NULL, NULL,
					   g_cclosure_marshal_VOID__VOID,
					   G_TYPE_NONE, 0);
}

static void
gypsy_server_init (GypsyServer *gps)
{
	GypsyServerPrivate *priv = GET_PRIVATE (gps);
	GError *error = NULL;

	priv->connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
	if (priv->connection == NULL) {
		g_warning ("Error connecting to system bus:\n%s",
			   error->message);
		g_error_free (error);
		return;
	}
	dbus_g_connection_ref (priv->connection);

	priv->connections = g_hash_table_new_full (g_str_hash, g_str_equal,
						   g_free, NULL);

	priv->client_count = 0;
	priv->terminate_id = 0;
}

void
gypsy_server_remove_clients (GypsyServer *gps,
			     const char  *prev_owner)
{
	GypsyServerPrivate *priv = GET_PRIVATE (gps);
	char *key;
	GList *list = NULL;

	if (g_hash_table_lookup_extended (priv->connections, prev_owner,
					  (gpointer) &key, (gpointer) &list)) {
		GList *l;

		for (l = list; l; l = l->next) {
			g_object_unref (l->data);
			if (--priv->client_count == 0) {
				if (priv->terminate_id == 0) {
					priv->terminate_id = 
						g_timeout_add (TERMINATE_TIMEOUT,
							       gypsy_terminate,
							       gps);
				}
			}
		}
		g_list_free (list);
		g_hash_table_remove (priv->connections, prev_owner);
	}
}