changes
[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 enum ExperimentState
35 {
36         NOT_RUNNING,
37         REQUESTED,
38         STARTED,
39         STOPPED
40 };
41
42 struct ScheduledExperiment {
43         struct ScheduledExperiment *next;
44         struct ScheduledExperiment *prev;
45
46         struct Experiment *e;
47         struct Node *n;
48         int state;
49         GNUNET_SCHEDULER_TaskIdentifier task;
50 };
51
52 struct ScheduledExperiment *list_head;
53 struct ScheduledExperiment *list_tail;
54
55
56 static void
57 request_timeout (void *cls,const struct GNUNET_SCHEDULER_TaskContext* tc)
58 {
59         struct ScheduledExperiment *se = cls;
60         se->task = GNUNET_SCHEDULER_NO_TASK;
61
62         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer `%s' did not respond to request for experiment `%s'\n",
63                         GNUNET_i2s (&se->n->id), se->e->name);
64
65         GNUNET_CONTAINER_DLL_remove (list_head, list_tail, se);
66         GNUNET_free (se);
67
68         /* Remove experiment */
69 }
70
71 static void run (void *cls,const struct GNUNET_SCHEDULER_TaskContext* tc)
72 {
73         struct ScheduledExperiment *se = cls;
74         struct GNUNET_TIME_Relative end;
75         se->task = GNUNET_SCHEDULER_NO_TASK;
76
77         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Executing `%s'\n", se->e->name);
78
79
80         if (NOT_RUNNING == se->state)
81         {
82                         /* Send start message */
83                         //GNUNET_EXPERIMENT_nodes_request_start (se->e);
84                         se->state = REQUESTED;
85                         se->task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &request_timeout, se);
86                         return;
87         }
88         else if (REQUESTED == se->state)
89         {
90                         /* Already requested */
91                         return;
92         }
93         else if (STARTED == se->state)
94         {
95                         /* Experiment is running */
96
97                         /* do work here */
98
99                         /* Reschedule */
100                         end = GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_add (se->e->stop, se->e->frequency));
101                         if (0 == end.rel_value)
102                         {
103                                 se->state = STOPPED;
104                                 return; /* End of experiment is reached */
105                         }
106                 se->task = GNUNET_SCHEDULER_add_delayed (se->e->frequency, &run, se);
107         }
108
109         else if (STOPPED == se->state)
110         {
111                         /* Experiment expired */
112         }
113 }
114
115 /**
116  * Start the scheduler component
117  */
118 void
119 GNUNET_EXPERIMENTATION_scheduler_add (struct Node *n, struct Experiment *e)
120 {
121         struct ScheduledExperiment *se;
122         struct GNUNET_TIME_Relative start;
123         struct GNUNET_TIME_Relative end;
124
125         start = GNUNET_TIME_absolute_get_remaining(e->start);
126         end = GNUNET_TIME_absolute_get_remaining(e->stop);
127
128         /* Add additional checks here if required */
129
130         if (0 == end.rel_value)
131                         return; /* End of experiment is reached */
132
133         se = GNUNET_malloc (sizeof (struct ScheduledExperiment));
134         se->state = NOT_RUNNING;
135         se->e = e;
136         se->n = n;
137         if (0 == start.rel_value)
138                         se->task = GNUNET_SCHEDULER_add_now (&run, se);
139         else
140                         se->task = GNUNET_SCHEDULER_add_delayed (start, &run, se);
141
142         GNUNET_CONTAINER_DLL_insert (list_head, list_tail, se);
143         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Added experiment `%s' for node to be scheduled\n",
144                         e->name, GNUNET_i2s(&se->n->id));
145 }
146
147 /**
148  * Start the scheduler component
149  */
150 void
151 GNUNET_EXPERIMENTATION_scheduler_start ()
152 {
153
154 }
155
156
157 /**
158  * Stop the scheduler component
159  */
160 void
161 GNUNET_EXPERIMENTATION_scheduler_stop ()
162 {
163         struct ScheduledExperiment *cur;
164         struct ScheduledExperiment *next;
165
166         next = list_head;
167         while (NULL != (cur = next))
168         {
169                         next = cur->next;
170                         GNUNET_CONTAINER_DLL_remove (list_head, list_tail, cur);
171                         if (GNUNET_SCHEDULER_NO_TASK != cur->task)
172                         {
173                                         GNUNET_SCHEDULER_cancel (cur->task);
174                                         cur->task = GNUNET_SCHEDULER_NO_TASK;
175                         }
176                         GNUNET_free (cur);
177         }
178 }
179
180 /* end of gnunet-daemon-experimentation_scheduler.c */