2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file experimentation/gnunet-daemon-experimentation_scheduler.c
23 * @brief experimentation daemon: execute experiments
24 * @author Christian Grothoff
25 * @author Matthias Wachs
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"
42 struct ScheduledExperiment {
43 struct ScheduledExperiment *next;
44 struct ScheduledExperiment *prev;
49 GNUNET_SCHEDULER_TaskIdentifier task;
52 struct ScheduledExperiment *list_head;
53 struct ScheduledExperiment *list_tail;
57 request_timeout (void *cls,const struct GNUNET_SCHEDULER_TaskContext* tc)
59 struct ScheduledExperiment *se = cls;
60 se->task = GNUNET_SCHEDULER_NO_TASK;
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);
65 GNUNET_CONTAINER_DLL_remove (list_head, list_tail, se);
68 /* Remove experiment */
71 static void run (void *cls,const struct GNUNET_SCHEDULER_TaskContext* tc)
73 struct ScheduledExperiment *se = cls;
74 struct GNUNET_TIME_Relative end;
75 se->task = GNUNET_SCHEDULER_NO_TASK;
77 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Executing `%s'\n", se->e->name);
80 if (NOT_RUNNING == se->state)
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);
88 else if (REQUESTED == se->state)
90 /* Already requested */
93 else if (STARTED == se->state)
95 /* Experiment is running */
100 end = GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_add (se->e->stop, se->e->frequency));
101 if (0 == end.rel_value)
104 return; /* End of experiment is reached */
106 se->task = GNUNET_SCHEDULER_add_delayed (se->e->frequency, &run, se);
109 else if (STOPPED == se->state)
111 /* Experiment expired */
116 * Start the scheduler component
119 GNUNET_EXPERIMENTATION_scheduler_add (struct Node *n, struct Experiment *e)
121 struct ScheduledExperiment *se;
122 struct GNUNET_TIME_Relative start;
123 struct GNUNET_TIME_Relative end;
125 start = GNUNET_TIME_absolute_get_remaining(e->start);
126 end = GNUNET_TIME_absolute_get_remaining(e->stop);
128 /* Add additional checks here if required */
130 if (0 == end.rel_value)
131 return; /* End of experiment is reached */
133 se = GNUNET_malloc (sizeof (struct ScheduledExperiment));
134 se->state = NOT_RUNNING;
137 if (0 == start.rel_value)
138 se->task = GNUNET_SCHEDULER_add_now (&run, se);
140 se->task = GNUNET_SCHEDULER_add_delayed (start, &run, se);
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));
148 * Start the scheduler component
151 GNUNET_EXPERIMENTATION_scheduler_start ()
158 * Stop the scheduler component
161 GNUNET_EXPERIMENTATION_scheduler_stop ()
163 struct ScheduledExperiment *cur;
164 struct ScheduledExperiment *next;
167 while (NULL != (cur = next))
170 GNUNET_CONTAINER_DLL_remove (list_head, list_tail, cur);
171 if (GNUNET_SCHEDULER_NO_TASK != cur->task)
173 GNUNET_SCHEDULER_cancel (cur->task);
174 cur->task = GNUNET_SCHEDULER_NO_TASK;
180 /* end of gnunet-daemon-experimentation_scheduler.c */