scheduling
[oweals/gnunet.git] / src / experimentation / gnunet-daemon-experimentation_scheduler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file experimentation/gnunet-daemon-experimentation_scheduler.c
23  * @brief experimentation daemon: execute experiments
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  */
27 #include "platform.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet-daemon-experimentation.h"
33
34 struct ScheduledExperiment {
35         struct ScheduledExperiment *next;
36         struct ScheduledExperiment *prev;
37
38         struct Experiment *e;
39         GNUNET_SCHEDULER_TaskIdentifier task;
40 };
41
42 struct ScheduledExperiment *list_head;
43 struct ScheduledExperiment *list_tail;
44
45
46 static void run (void *cls,const struct GNUNET_SCHEDULER_TaskContext* tc)
47 {
48         struct ScheduledExperiment *se = cls;
49         //struct GNUNET_TIME_Relative end;
50         se->task = GNUNET_SCHEDULER_NO_TASK;
51
52         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running `%s'\n", se->e->name);
53
54 //      end = GNUNET_TIME_absolute_get_remaining(se->e->stop);
55 //      if (0 < end.rel_value)
56 //                      return; /* End of experiment is reached */
57
58 //GNUNET_break (0);
59         se->task = GNUNET_SCHEDULER_add_delayed (se->e->frequency, &run, se);
60 }
61
62 /**
63  * Start the scheduler component
64  */
65 void
66 GNUNET_EXPERIMENTATION_scheduler_add (struct Experiment *e)
67 {
68         struct ScheduledExperiment *se;
69         struct GNUNET_TIME_Relative start;
70         struct GNUNET_TIME_Relative end;
71
72         start = GNUNET_TIME_absolute_get_remaining(e->start);
73         end = GNUNET_TIME_absolute_get_remaining(e->stop);
74
75         if (0 == end.rel_value)
76                         return; /* End of experiment is reached */
77
78         se = GNUNET_malloc (sizeof (struct ScheduledExperiment));
79         se->e = e;
80         if (0 == start.rel_value)
81                         se->task = GNUNET_SCHEDULER_add_now (&run, se);
82         else
83                         se->task = GNUNET_SCHEDULER_add_delayed (start, &run, se);
84
85         GNUNET_CONTAINER_DLL_insert (list_head, list_tail, se);
86         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scheduled `%s'\n", e->name);
87 }
88
89 /**
90  * Start the scheduler component
91  */
92 void
93 GNUNET_EXPERIMENTATION_scheduler_start ()
94 {
95
96 }
97
98
99 /**
100  * Stop the scheduler component
101  */
102 void
103 GNUNET_EXPERIMENTATION_scheduler_stop ()
104 {
105         struct ScheduledExperiment *cur;
106         struct ScheduledExperiment *next;
107
108         next = list_head;
109         while (NULL != (cur = next))
110         {
111                         next = cur->next;
112                         GNUNET_CONTAINER_DLL_remove (list_head, list_tail, cur);
113                         if (GNUNET_SCHEDULER_NO_TASK != cur->task)
114                         {
115                                         GNUNET_SCHEDULER_cancel (cur->task);
116                                         cur->task = GNUNET_SCHEDULER_NO_TASK;
117                         }
118                         GNUNET_free (cur);
119         }
120 }
121
122 /* end of gnunet-daemon-experimentation_scheduler.c */