2 * beagle-daemon-information-response.c
4 * Copyright (C) 2005 Novell, Inc.
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.
29 #include <sys/types.h>
30 #include <sys/socket.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"
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
)
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
);
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
);
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
);
86 buf
= _beagle_parser_context_get_text_buffer (ctx
);
88 priv
->is_indexing
= (strcmp (buf
, "true") == 0);
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
);
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]);
109 g_warning ("unknown attribute \"%s\" with value \"%s\"", attrs
[i
], attrs
[i
+ 1]);
112 priv
->scheduler_information
= scheduler_information
;
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
);
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
);
129 buf
= _beagle_parser_context_get_text_buffer (ctx
);
131 priv
->scheduler_information
->pending_task
= g_slist_prepend (priv
->scheduler_information
->pending_task
, buf
);
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
);
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
);
151 buf
= _beagle_parser_context_get_text_buffer (ctx
);
153 priv
->scheduler_information
->future_task
= g_slist_prepend (priv
->scheduler_information
->future_task
, buf
);
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
);
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
);
173 buf
= _beagle_parser_context_get_text_buffer (ctx
);
175 priv
->scheduler_information
->blocked_task
= g_slist_prepend (priv
->scheduler_information
->blocked_task
, buf
);
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
);
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
);
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
);
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 if (strcmp (attrs
[i
], "State") == 0) {
217 if (strcmp (attrs
[i
+ 1], "NotApplicable") == 0)
218 queryable_status
->state
= BEAGLE_QUERYABLE_STATE_NA
;
219 else if (strcmp (attrs
[i
+ 1], "Idle") == 0)
220 queryable_status
->state
= BEAGLE_QUERYABLE_STATE_IDLE
;
221 else if (strcmp (attrs
[i
+ 1], "Crawling") == 0)
222 queryable_status
->state
= BEAGLE_QUERYABLE_STATE_CRAWLING
;
223 else if (strcmp (attrs
[i
+ 1], "Indexing") == 0)
224 queryable_status
->state
= BEAGLE_QUERYABLE_STATE_INDEXING
;
225 else if (strcmp (attrs
[i
+ 1], "Flushing") == 0)
226 queryable_status
->state
= BEAGLE_QUERYABLE_STATE_FLUSHING
;
228 g_warning ("could not handle %s", attrs
[i
]);
231 priv
->index_status
= g_slist_prepend (priv
->index_status
, queryable_status
);
235 PARSER_STATE_DAEMON_INFORMATION_VERSION
,
236 PARSER_STATE_DAEMON_INFORMATION_IS_INDEXING
,
237 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION
,
238 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_PENDING_TASKS
,
239 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_FUTURE_TASKS
,
240 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_BLOCKED_TASKS
,
241 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_TASK
,
242 PARSER_STATE_DAEMON_INFORMATION_INDEX_STATUS
,
243 PARSER_STATE_DAEMON_INFORMATION_QUERYABLE_STATUS
,
246 static BeagleParserHandler parser_handlers
[] = {
249 PARSER_STATE_DAEMON_INFORMATION_VERSION
,
255 PARSER_STATE_DAEMON_INFORMATION_IS_INDEXING
,
259 { "SchedulerInformation",
261 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION
,
262 start_scheduler_information
,
266 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION
,
267 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_PENDING_TASKS
,
272 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_PENDING_TASKS
,
273 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_TASK
,
278 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION
,
279 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_FUTURE_TASKS
,
284 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_FUTURE_TASKS
,
285 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_TASK
,
290 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION
,
291 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_BLOCKED_TASKS
,
296 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_BLOCKED_TASKS
,
297 PARSER_STATE_DAEMON_INFORMATION_SCHEDULER_INFORMATION_TASK
,
303 PARSER_STATE_DAEMON_INFORMATION_INDEX_STATUS
,
308 PARSER_STATE_DAEMON_INFORMATION_INDEX_STATUS
,
309 PARSER_STATE_DAEMON_INFORMATION_QUERYABLE_STATUS
,
310 start_queryable_status
,
316 beagle_daemon_information_response_class_init (BeagleDaemonInformationResponseClass
*klass
)
318 GObjectClass
*obj_class
= G_OBJECT_CLASS (klass
);
319 BeagleResponseClass
*response_class
= BEAGLE_RESPONSE_CLASS (klass
);
321 parent_class
= g_type_class_peek_parent (klass
);
323 obj_class
->finalize
= beagle_daemon_information_response_finalize
;
325 _beagle_response_class_set_parser_handlers (response_class
,
328 g_type_class_add_private (klass
, sizeof (BeagleDaemonInformationResponsePrivate
));
332 beagle_daemon_information_response_init (BeagleDaemonInformationResponse
*response
)
334 BeagleDaemonInformationResponsePrivate
*priv
= BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response
);
336 priv
->version
= NULL
;
337 priv
->scheduler_information
= NULL
;
338 priv
->index_status
= NULL
;
339 priv
->is_indexing
= FALSE
;
343 * beagle_daemon_information_response_get_version:
344 * @response: a #BeagleDaemonInformationResponse
346 * Fetches the version string of the given #BeagleDaemonInformationResponse.
348 * Return value: the version string of the #BeagleDaemonInformationResponse.
350 G_CONST_RETURN
char *
351 beagle_daemon_information_response_get_version (BeagleDaemonInformationResponse
*response
)
353 BeagleDaemonInformationResponsePrivate
*priv
;
355 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response
), NULL
);
357 priv
= BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response
);
358 g_return_val_if_fail (priv
->version
, NULL
);
360 return priv
->version
;
364 * beagle_daemon_information_response_get_scheduler_information:
365 * @response: a #BeagleDaemonInformationResponse
367 * Fetches the current scheduler information from the given #BeagleDaemonInformationResponse.
369 * Return value: the current scheduler information from the #BeagleDaemonInformationResponse.
371 BeagleSchedulerInformation
*
372 beagle_daemon_information_response_get_scheduler_information (BeagleDaemonInformationResponse
*response
)
374 BeagleDaemonInformationResponsePrivate
*priv
;
376 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response
), NULL
);
378 priv
= BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response
);
379 g_return_val_if_fail (priv
->scheduler_information
, NULL
);
381 return priv
->scheduler_information
;
385 * beagle_daemon_information_response_get_human_readable_status:
386 * @response: a #BeagleDaemonInformationResponse
388 * Fetches the status string of the given #BeagleDaemonInformationResponse.
390 * Return value: the status of the #BeagleDaemonInformationResponse.
392 G_CONST_RETURN
char *
393 beagle_daemon_information_response_get_human_readable_status (BeagleDaemonInformationResponse
*response
)
395 BeagleDaemonInformationResponsePrivate
*priv
;
396 BeagleSchedulerInformation
*process_info
;
398 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response
), NULL
);
400 priv
= BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response
);
401 g_return_val_if_fail (priv
->scheduler_information
!= NULL
, NULL
);
403 process_info
= priv
->scheduler_information
;
405 return beagle_scheduler_information_to_human_readable_string (process_info
);
409 * beagle_daemon_information_response_get_index_status:
410 * @response: a #BeagleDaemonInformationResponse
412 * Fetches the list of #QueryableStatus from each of the currently running backends.
414 * Return value: the index information of the #BeagleDaemonInformationResponse.
417 beagle_daemon_information_response_get_index_status (BeagleDaemonInformationResponse
*response
)
419 BeagleDaemonInformationResponsePrivate
*priv
;
421 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response
), NULL
);
423 priv
= BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response
);
424 g_return_val_if_fail (priv
->index_status
, NULL
);
426 return priv
->index_status
;
430 * beagle_daemon_information_response_get_index_information:
431 * @response: a #BeagleDaemonInformationResponse
433 * Fetches a human-readable string describing the index information
434 * of the given #BeagleDaemonInformationResponse.
436 * Return value: string describing the index information of the #BeagleDaemonInformationResponse.
438 G_CONST_RETURN
char *
439 beagle_daemon_information_response_get_index_information (BeagleDaemonInformationResponse
*response
)
441 BeagleDaemonInformationResponsePrivate
*priv
;
442 BeagleQueryableStatus
*status
;
446 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response
), NULL
);
448 priv
= BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response
);
449 g_return_val_if_fail (priv
->index_status
!= NULL
, NULL
);
451 tmp
= g_string_new ("\n");
453 for (iter
= priv
->index_status
; iter
!= NULL
; iter
= iter
->next
) {
455 g_string_append_printf (tmp
, "Name: %s\nCount: %d\nIndexing: %s\n\n",
458 (status
->is_indexing
? "Yes" : "No"));
461 return g_string_free (tmp
, FALSE
);
465 * beagle_daemon_information_response_is_indexing:
466 * @response: a #BeagleDaemonInformationResponse
468 * Returns whether the daemon is in the process of indexing data.
470 * Return value: a boolean indicating whether the daemon is indexing.
473 beagle_daemon_information_response_is_indexing (BeagleDaemonInformationResponse
*response
)
475 BeagleDaemonInformationResponsePrivate
*priv
;
477 g_return_val_if_fail (BEAGLE_IS_DAEMON_INFORMATION_RESPONSE (response
), FALSE
);
479 priv
= BEAGLE_DAEMON_INFORMATION_RESPONSE_GET_PRIVATE (response
);
481 return priv
->is_indexing
;