cvsimport
[beagle.git] / libbeagle / beagle / beagle-daemon-information-response.c
blob300384a571e6c0bb1ef3840095aae1fa786313b0
1 /*
2 * beagle-daemon-information-response.c
4 * Copyright (C) 2005 Novell, Inc.
6 */
8 /*
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
33 #include "beagle-scheduler-information.h"
34 #include "beagle-queryable-status.h"
35 #include "beagle-daemon-information-response.h"
36 #include "beagle-private.h"
38 typedef struct {
39 char *version; /* Version. */
40 gboolean is_indexing; /* Currently indexing ? */
41 BeagleSchedulerInformation *scheduler_information; /* Current task information. */
42 GSList *index_status; /* List of BeagleQueryableStatus. */
43 } BeagleDaemonInformationResponsePrivate;
45 #define BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BEAGLE_TYPE_DAEMON_INFORMATION_RESPONSE, BeagleDaemonInformationResponsePrivate))
47 static GObjectClass *parent_class = NULL;
49 G_DEFINE_TYPE (BeagleDaemonInformationResponse, beagle_daemon_information_response, BEAGLE_TYPE_RESPONSE)
51 static void
52 beagle_daemon_information_response_finalize (GObject *obj)
54 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (obj);
56 g_free (priv->version);
58 if (priv->scheduler_information)
59 beagle_scheduler_information_unref (priv->scheduler_information);
61 if (priv->index_status) {
62 g_slist_foreach (priv->index_status, (GFunc) beagle_queryable_status_unref, NULL);
63 g_slist_free (priv->index_status);
66 if (G_OBJECT_CLASS (parent_class)->finalize)
67 G_OBJECT_CLASS (parent_class)->finalize (obj);
70 static void
71 end_version (BeagleParserContext *ctx)
73 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
74 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
76 priv->version = _beagle_parser_context_get_text_buffer (ctx);
79 static void
80 end_is_indexing (BeagleParserContext *ctx)
82 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
83 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
84 char *buf;
86 buf = _beagle_parser_context_get_text_buffer (ctx);
88 priv->is_indexing = (strcmp (buf, "true") == 0);
90 g_free (buf);
93 static void
94 start_scheduler_information (BeagleParserContext *ctx, const char **attrs)
96 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
97 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
98 int i;
99 BeagleSchedulerInformation *scheduler_information;
101 scheduler_information = _beagle_scheduler_information_new ();
103 for (i = 0; attrs[i] != NULL; i += 2) {
104 if (strcmp (attrs[i], "TotalTaskCount") == 0)
105 scheduler_information->total_task_count = (int) g_ascii_strtod (attrs[i + 1], NULL);
106 else if (strcmp (attrs[i], "StatusString") == 0)
107 scheduler_information->status_string = g_strdup (attrs[i + 1]);
108 else
109 g_warning ("unknown attribute \"%s\" with value \"%s\"", attrs[i], attrs[i + 1]);
112 priv->scheduler_information = scheduler_information;
115 static void
116 end_scheduler_information (BeagleParserContext *ctx)
118 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
119 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
122 static void
123 end_pending_task (BeagleParserContext *ctx)
125 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
126 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
127 char *buf;
129 buf = _beagle_parser_context_get_text_buffer (ctx);
131 priv->scheduler_information->pending_task = g_slist_prepend (priv->scheduler_information->pending_task, buf);
134 static void
135 end_pending_tasks (BeagleParserContext *ctx)
137 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
138 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
140 // Fix the order of tasks
141 priv->scheduler_information->pending_task = g_slist_reverse (priv->scheduler_information->pending_task);
144 static void
145 end_future_task (BeagleParserContext *ctx)
147 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
148 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
149 char *buf;
151 buf = _beagle_parser_context_get_text_buffer (ctx);
153 priv->scheduler_information->future_task = g_slist_prepend (priv->scheduler_information->future_task, buf);
156 static void
157 end_future_tasks (BeagleParserContext *ctx)
159 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
160 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
162 // Fix the order of tasks
163 priv->scheduler_information->future_task = g_slist_reverse (priv->scheduler_information->future_task);
166 static void
167 end_blocked_task (BeagleParserContext *ctx)
169 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
170 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
171 char *buf;
173 buf = _beagle_parser_context_get_text_buffer (ctx);
175 priv->scheduler_information->blocked_task = g_slist_prepend (priv->scheduler_information->blocked_task, buf);
178 static void
179 end_blocked_tasks (BeagleParserContext *ctx)
181 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
182 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
184 // Fix the order of tasks
185 priv->scheduler_information->blocked_task = g_slist_reverse (priv->scheduler_information->blocked_task);
188 static void
189 end_index_status (BeagleParserContext *ctx)
191 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
192 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
194 // Reverse the list to maintain the same order as returned by the daemon
195 priv->index_status = g_slist_reverse (priv->index_status);
198 static void
199 start_queryable_status (BeagleParserContext *ctx, const char **attrs)
201 BeagleDaemonInformationResponse *response = BEAGLE_DAEMON_INFORMATION_RESPONSE (_beagle_parser_context_get_response (ctx));
202 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
204 int i;
205 BeagleQueryableStatus *queryable_status = _beagle_queryable_status_new ();
207 for (i = 0; attrs[i] != NULL; i += 2) {
208 if (strcmp (attrs[i], "Name") == 0)
209 queryable_status->name = g_strdup (attrs[i + 1]);
210 else if (strcmp (attrs[i], "ItemCount") == 0)
211 queryable_status->item_count = (int) g_ascii_strtod (attrs[i + 1], NULL);
212 else if (strcmp (attrs[i], "ProgressPercent") == 0)
213 queryable_status->progress_percent = (int) g_ascii_strtod (attrs[i + 1], NULL);
214 else if (strcmp (attrs[i], "IsIndexing") == 0)
215 queryable_status->is_indexing = strcmp (attrs[i + 1], "true") == 0;
216 else
217 g_warning ("could not handle %s", attrs[i]);
220 priv->index_status = g_slist_prepend (priv->index_status, queryable_status);
223 enum {
224 PARSER_STATE_DAEMON_INFORMATION_VERSION,
225 PARSER_STATE_DAEMON_INFORMATION_IS_INDEXING,
226 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION,
227 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_PENDING_TASKS,
228 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_FUTURE_TASKS,
229 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_BLOCKED_TASKS,
230 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_TASK,
231 PARSER_STATE_DAEMON_INFORMATION_INDEX_STATUS,
232 PARSER_STATE_DAEMON_INFORMATION_QUERYABLE_STATUS,
235 static BeagleParserHandler parser_handlers[] = {
236 { "Version",
238 PARSER_STATE_DAEMON_INFORMATION_VERSION,
239 NULL,
240 end_version },
242 { "IsIndexing",
244 PARSER_STATE_DAEMON_INFORMATION_IS_INDEXING,
245 NULL,
246 end_is_indexing },
248 { "SchedulerInformation",
250 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION,
251 start_scheduler_information,
252 NULL},
254 { "PendingTasks",
255 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION,
256 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_PENDING_TASKS,
257 NULL,
258 end_pending_tasks },
260 { "PendingTask",
261 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_PENDING_TASKS,
262 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_TASK,
263 NULL,
264 end_pending_task },
266 { "FutureTasks",
267 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION,
268 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_FUTURE_TASKS,
269 NULL,
270 end_future_tasks },
272 { "FutureTask",
273 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_FUTURE_TASKS,
274 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_TASK,
275 NULL,
276 end_future_task },
278 { "BlockedTasks",
279 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION,
280 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_BLOCKED_TASKS,
281 NULL,
282 end_blocked_tasks },
284 { "BlockedTask",
285 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_BLOCKED_TASKS,
286 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_TASK,
287 NULL,
288 end_blocked_task },
290 { "IndexStatus",
292 PARSER_STATE_DAEMON_INFORMATION_INDEX_STATUS,
293 NULL,
294 end_index_status },
296 { "QueryableStatus",
297 PARSER_STATE_DAEMON_INFORMATION_INDEX_STATUS,
298 PARSER_STATE_DAEMON_INFORMATION_QUERYABLE_STATUS,
299 start_queryable_status,
300 NULL },
301 { 0 }
304 static void
305 beagle_daemon_information_response_class_init (BeagleDaemonInformationResponseClass *klass)
307 GObjectClass *obj_class = G_OBJECT_CLASS (klass);
308 BeagleResponseClass *response_class = BEAGLE_RESPONSE_CLASS (klass);
310 parent_class = g_type_class_peek_parent (klass);
312 obj_class->finalize = beagle_daemon_information_response_finalize;
314 _beagle_response_class_set_parser_handlers (response_class,
315 parser_handlers);
317 g_type_class_add_private (klass, sizeof (BeagleDaemonInformationResponsePrivate));
320 static void
321 beagle_daemon_information_response_init (BeagleDaemonInformationResponse *response)
323 BeagleDaemonInformationResponsePrivate *priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
325 priv->version = NULL;
326 priv->scheduler_information = NULL;
327 priv->index_status = NULL;
328 priv->is_indexing = FALSE;
332 * beagle_daemon_information_response_get_version:
333 * @response: a #BeagleDaemonInformationResponse
335 * Fetches the version string of the given #BeagleDaemonInformationResponse.
337 * Return value: the version string of the #BeagleDaemonInformationResponse.
339 G_CONST_RETURN char *
340 beagle_daemon_information_response_get_version (BeagleDaemonInformationResponse *response)
342 BeagleDaemonInformationResponsePrivate *priv;
344 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response), NULL);
346 priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
347 g_return_val_if_fail (priv->version, NULL);
349 return priv->version;
353 * beagle_daemon_information_response_get_scheduler_information:
354 * @response: a #BeagleDaemonInformationResponse
356 * Fetches the current scheduler information from the given #BeagleDaemonInformationResponse.
358 * Return value: the current scheduler information from the #BeagleDaemonInformationResponse.
360 BeagleSchedulerInformation *
361 beagle_daemon_information_response_get_scheduler_information (BeagleDaemonInformationResponse *response)
363 BeagleDaemonInformationResponsePrivate *priv;
365 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response), NULL);
367 priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
368 g_return_val_if_fail (priv->scheduler_information, NULL);
370 return priv->scheduler_information;
374 * beagle_daemon_information_response_get_human_readable_status:
375 * @response: a #BeagleDaemonInformationResponse
377 * Fetches the status string of the given #BeagleDaemonInformationResponse.
379 * Return value: the status of the #BeagleDaemonInformationResponse.
381 G_CONST_RETURN char *
382 beagle_daemon_information_response_get_human_readable_status (BeagleDaemonInformationResponse *response)
384 BeagleDaemonInformationResponsePrivate *priv;
385 BeagleSchedulerInformation *process_info;
387 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response), NULL);
389 priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
390 g_return_val_if_fail (priv->scheduler_information != NULL, NULL);
392 process_info = priv->scheduler_information;
394 return beagle_scheduler_information_to_human_readable_string (process_info);
398 * beagle_daemon_information_response_get_index_status:
399 * @response: a #BeagleDaemonInformationResponse
401 * Fetches the list of #QueryableStatus from each of the currently running backends.
403 * Return value: the index information of the #BeagleDaemonInformationResponse.
405 GSList *
406 beagle_daemon_information_response_get_index_status (BeagleDaemonInformationResponse *response)
408 BeagleDaemonInformationResponsePrivate *priv;
410 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response), NULL);
412 priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
413 g_return_val_if_fail (priv->index_status, NULL);
415 return priv->index_status;
419 * beagle_daemon_information_response_get_index_information:
420 * @response: a #BeagleDaemonInformationResponse
422 * Fetches a human-readable string describing the index information
423 * of the given #BeagleDaemonInformationResponse.
425 * Return value: string describing the index information of the #BeagleDaemonInformationResponse.
427 G_CONST_RETURN char *
428 beagle_daemon_information_response_get_index_information (BeagleDaemonInformationResponse *response)
430 BeagleDaemonInformationResponsePrivate *priv;
431 BeagleQueryableStatus *status;
432 GString *tmp;
433 GSList *iter;
435 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response), NULL);
437 priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
438 g_return_val_if_fail (priv->index_status != NULL, NULL);
440 tmp = g_string_new ("\n");
442 for (iter = priv->index_status; iter != NULL; iter = iter->next) {
443 status = iter->data;
444 g_string_append_printf (tmp, "Name: %s\nCount: %d\nIndexing: %s\n\n",
445 status->name,
446 status->item_count,
447 (status->is_indexing ? "Yes" : "No"));
450 return g_string_free (tmp, FALSE);
454 * beagle_daemon_information_response_is_indexing:
455 * @response: a #BeagleDaemonInformationResponse
457 * Returns whether the daemon is in the process of indexing data.
459 * Return value: a boolean indicating whether the daemon is indexing.
461 gboolean
462 beagle_daemon_information_response_is_indexing (BeagleDaemonInformationResponse *response)
464 BeagleDaemonInformationResponsePrivate *priv;
466 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response), FALSE);
468 priv = BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response);
470 return priv->is_indexing;