2 * beagle-scheduler-information.c
4 * Copyright (C) 2006 Debajyoti Bera <dbera.web@gmail.com>
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 "beagle-scheduler-information.h"
29 #include "beagle-private.h"
31 BeagleSchedulerInformation
*
32 _beagle_scheduler_information_new ()
34 BeagleSchedulerInformation
*sched_info
;
36 sched_info
= g_new0 (BeagleSchedulerInformation
, 1);
38 sched_info
->ref_count
= 1;
40 sched_info
->total_task_count
= -1;
41 sched_info
->status_string
= NULL
;
42 sched_info
->pending_task
= NULL
;
43 sched_info
->future_task
= NULL
;
44 sched_info
->blocked_task
= NULL
;
50 * beagle_scheduler_information_ref:
51 * @sched_info: a #BeagleSchedulerInformation
53 * Increases the reference count of the #BeagleSchedulerInformation.
55 * Return value: the #BeagleSchedulerInformation
57 BeagleSchedulerInformation
*
58 beagle_scheduler_information_ref (BeagleSchedulerInformation
*sched_info
)
60 g_return_if_fail (sched_info
!= NULL
);
62 sched_info
->ref_count
++;
68 * beagle_scheduler_information_unref:
69 * @sched_info: a #BeagleSchedulerInformation
71 * Decreases the reference count of the #BeagleSchedulerInformation. When the reference count drops to 0, it is freed.
74 beagle_scheduler_information_unref (BeagleSchedulerInformation
*sched_info
)
76 g_return_if_fail (sched_info
!= NULL
);
77 g_return_if_fail (sched_info
->ref_count
> 0);
79 sched_info
->ref_count
--;
81 if (sched_info
->ref_count
== 0) {
82 g_free (sched_info
->status_string
);
84 if (sched_info
->pending_task
) {
85 g_slist_foreach (sched_info
->pending_task
, (GFunc
) g_free
, NULL
);
86 g_slist_free (sched_info
->pending_task
);
89 if (sched_info
->future_task
) {
90 g_slist_foreach (sched_info
->future_task
, (GFunc
) g_free
, NULL
);
91 g_slist_free (sched_info
->future_task
);
94 if (sched_info
->blocked_task
) {
95 g_slist_foreach (sched_info
->blocked_task
, (GFunc
) g_free
, NULL
);
96 g_slist_free (sched_info
->blocked_task
);
104 * beagle_scheduler_information_get_total_task_count:
105 * @sched_info: a #BeagleSchedulerInformation
107 * Fetches the total number of tasks from the given #BeagleSchedulerInformation.
109 * Return value: the number of tasks from the #BeagleSchedulerInformation.
112 beagle_scheduler_information_get_total_task_count (BeagleSchedulerInformation
*sched_info
)
114 g_return_val_if_fail (sched_info
!= NULL
, -1);
116 return sched_info
->total_task_count
;
120 * beagle_scheduler_information_get_status_string:
121 * @sched_info: a #BeagleSchedulerInformation
123 * Fetches the status string from the given #BeagleSchedulerInformation.
125 * Return value: the status string from the #BeagleSchedulerInformation.
127 G_CONST_RETURN
char *
128 beagle_scheduler_information_get_status_string (BeagleSchedulerInformation
*sched_info
)
130 g_return_val_if_fail (sched_info
!= NULL
, NULL
);
132 return sched_info
->status_string
;
136 * beagle_scheduler_information_get_pending_tasks:
137 * @sched_info: a #BeagleSchedulerInformation
139 * Fetches the list of pending tasks as strings from the given #BeagleSchedulerInformation.
141 * Return value: the list of pending tasks from the #BeagleSchedulerInformation.
144 beagle_scheduler_information_get_pending_tasks (BeagleSchedulerInformation
*sched_info
)
146 g_return_val_if_fail (sched_info
!= NULL
, NULL
);
148 return sched_info
->pending_task
;
152 * beagle_scheduler_information_get_future_tasks:
153 * @sched_info: a #BeagleSchedulerInformation
155 * Fetches the list of future tasks as strings from the given #BeagleSchedulerInformation.
157 * Return value: the list of future tasks from the #BeagleSchedulerInformation.
160 beagle_scheduler_information_get_future_tasks (BeagleSchedulerInformation
*sched_info
)
162 g_return_val_if_fail (sched_info
!= NULL
, NULL
);
164 return sched_info
->future_task
;
168 * beagle_scheduler_information_get_blocked_tasks:
169 * @sched_info: a #BeagleSchedulerInformation
171 * Fetches the list of blocked tasks as strings from the given #BeagleSchedulerInformation.
173 * Return value: the list of blocked tasks from the #BeagleSchedulerInformation.
176 beagle_scheduler_information_get_blocked_tasks (BeagleSchedulerInformation
*sched_info
)
178 g_return_val_if_fail (sched_info
!= NULL
, NULL
);
180 return sched_info
->blocked_task
;
184 * beagle_scheduler_information_to_human_readable_string:
185 * @sched_info: a #BeagleSchedulerInformation
187 * Fetches a string version of the given #BeagleSchedulerInformation.
189 * Return value: a string version from the #BeagleSchedulerInformation.
191 G_CONST_RETURN
char *
192 beagle_scheduler_information_to_human_readable_string (BeagleSchedulerInformation
*sched_info
)
197 GString
*tmp
= g_string_new (NULL
);
199 g_string_append_printf (tmp
, "Scheduler:\nCount: %d\n", sched_info
->total_task_count
);
201 if (sched_info
->status_string
)
202 g_string_append_printf (tmp
, "Status: %s\n", sched_info
->status_string
);
206 g_string_append (tmp
, "\nPending Tasks:\n");
207 if (g_slist_length (sched_info
->pending_task
) > 0) {
208 iter
= sched_info
->pending_task
;
209 while (iter
!= NULL
) {
211 g_string_append_printf (tmp
, "%d %s\n", pos
, task
);
216 g_string_append (tmp
, "Scheduler queue is empty.\n");
218 if (g_slist_length (sched_info
->future_task
) > 0) {
219 g_string_append (tmp
, "\nFuture Tasks:\n");
220 iter
= sched_info
->future_task
;
221 while (iter
!= NULL
) {
223 g_string_append_printf (tmp
, "%s\n", task
);
228 if (g_slist_length (sched_info
->blocked_task
) > 0) {
229 g_string_append (tmp
, "\nBlocked Tasks:\n");
230 iter
= sched_info
->blocked_task
;
231 while (iter
!= NULL
) {
233 g_string_append_printf (tmp
, "%s\n", task
);
238 return g_string_free (tmp
, FALSE
);
241 void _task_to_xml (GSList
*task_list
, const char *list_name
, const char *list_item_name
, GString
*data
)
246 g_string_append_printf (data
, "<%s>", list_name
);
248 if (task_list
!= NULL
) {
250 while (iter
!= NULL
) {
251 task_data
= iter
->data
;
252 g_string_append_printf (data
, "<%s>%s</%s>", list_item_name
, task_data
, list_item_name
);
257 g_string_append_printf (data
, "</%s>", list_name
);
261 _beagle_scheduler_information_to_xml (BeagleSchedulerInformation
*sched_info
, GString
*data
)
266 g_string_append_printf (data
, "<SchedulerInformation");
268 g_string_append_printf (data
, " TotalTaskCount=\"%d\"", sched_info
->total_task_count
);
270 if (sched_info
->status_string
)
271 g_string_append_printf (data
, " StatusString=\"%s\"", sched_info
->status_string
);
273 g_string_append (data
, ">");
275 _task_to_xml (sched_info
->pending_task
, "PendingTasks", "PendingTask", data
);
276 _task_to_xml (sched_info
->future_task
, "FutureTasks", "FutureTask", data
);
277 _task_to_xml (sched_info
->blocked_task
, "BlockedTasks", "BlockedTask", data
);
279 g_string_append (data
, "</SchedulerInformation>");