preference generation implemented
[oweals/gnunet.git] / src / ats / gnunet-ats-solver-eval.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010-2013 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  * @file ats-tests/ats-testing-experiment.c
22  * @brief ats benchmark: controlled experiment execution
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet-ats-solver-eval.h"
29
30
31 #define BIG_M_STRING "unlimited"
32
33 static struct Experiment *e;
34
35 static struct GNUNET_ATS_TESTING_SolverHandle *sh;
36
37 /**
38  * cmd option -e: experiment file
39  */
40 static char *opt_exp_file;
41
42 static char *opt_solver;
43
44 /**
45  * cmd option -l: enable logging
46  */
47 static int opt_log;
48
49 /**
50  * cmd option -p: enable plots
51  */
52 static int opt_plot;
53
54 /**
55  * cmd option -v: verbose logs
56  */
57 static int opt_verbose;
58
59 static int res;
60
61 static void
62 end_now ();
63
64
65 /**
66  * Preference Generators
67  */
68
69 static struct PreferenceGenerator *pg_head;
70 static struct PreferenceGenerator *pg_tail;
71
72 static double
73 get_preference (struct PreferenceGenerator *pg)
74 {
75   struct GNUNET_TIME_Relative time_delta;
76   double delta_value;
77   double pref_value;
78
79   /* Calculate the current preference value */
80   switch (pg->type) {
81     case GNUNET_ATS_TEST_TG_CONSTANT:
82       pref_value = pg->base_value;
83       break;
84     case GNUNET_ATS_TEST_TG_LINEAR:
85       time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
86       /* Calculate point of time in the current period */
87       time_delta.rel_value_us = time_delta.rel_value_us %
88           pg->duration_period.rel_value_us;
89       delta_value = ((double) time_delta.rel_value_us  /
90           pg->duration_period.rel_value_us) * (pg->max_value - pg->base_value);
91       if ((pg->max_value < pg->base_value) &&
92           ((pg->max_value - pg->base_value) > pg->base_value))
93       {
94         /* This will cause an underflow */
95         GNUNET_break (0);
96       }
97       pref_value = pg->base_value + delta_value;
98       break;
99     case GNUNET_ATS_TEST_TG_RANDOM:
100       delta_value =  (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
101           10000 * (pg->max_value - pg->base_value)) / 10000;
102       pref_value = pg->base_value + delta_value;
103       break;
104     case GNUNET_ATS_TEST_TG_SINUS:
105       time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
106       /* Calculate point of time in the current period */
107       time_delta.rel_value_us = time_delta.rel_value_us %
108           pg->duration_period.rel_value_us;
109       if ((pg->max_value - pg->base_value) > pg->base_value)
110       {
111         /* This will cause an underflow for second half of sinus period,
112          * will be detected in general when experiments are loaded */
113         GNUNET_break (0);
114       }
115       delta_value = (pg->max_value - pg->base_value) *
116           sin ( (2 * M_PI) / ((double) pg->duration_period.rel_value_us) *
117               time_delta.rel_value_us);
118       pref_value = pg->base_value + delta_value;
119       break;
120     default:
121       pref_value = 0.0;
122       break;
123   }
124   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Current preference value is %f\n",
125       pref_value);
126   return pref_value;
127 }
128
129
130 static void
131 set_pref_task (void *cls,
132                     const struct GNUNET_SCHEDULER_TaskContext *tc)
133 {
134   struct PreferenceGenerator *pg = cls;
135   double pref_value;
136   pg->set_task = GNUNET_SCHEDULER_NO_TASK;
137
138   pref_value = get_preference (pg);
139
140   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
141       "Setting preference for peer [%u] address [%u] for %s to %f\n",
142       pg->peer, pg->address_id,
143       GNUNET_ATS_print_preference_type (pg->kind), pref_value);
144
145   /* set performance here!
146   GNUNET_ATS_performance_change_preference(p->me->ats_perf_handle,
147       &p->dest->id, p->pg->kind, pref_value, GNUNET_ATS_PREFERENCE_END);
148 */
149
150   switch (pg->kind) {
151     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
152       //p->pref_bandwidth = pref_value;
153       break;
154     case GNUNET_ATS_PREFERENCE_LATENCY:
155       //p->pref_delay = pref_value;
156       break;
157     default:
158       break;
159   }
160
161   pg->set_task = GNUNET_SCHEDULER_add_delayed (pg->frequency,
162       set_pref_task, pg);
163
164 }
165
166 static struct PreferenceGenerator *
167 find_pref_gen (unsigned int peer, unsigned int address,
168     enum GNUNET_ATS_PreferenceKind kind)
169 {
170   struct PreferenceGenerator *cur;
171   for (cur = pg_head; NULL != cur; cur = cur->next)
172     if ((cur->peer == peer) && (cur->address_id == address) && (cur->kind == kind))
173       return cur;
174   return NULL;
175 }
176
177 void
178 GNUNET_ATS_solver_generate_preferences_stop (struct PreferenceGenerator *pg)
179 {
180   GNUNET_CONTAINER_DLL_remove (pg_head, pg_tail, pg);
181
182   if (GNUNET_SCHEDULER_NO_TASK != pg->set_task)
183   {
184     GNUNET_SCHEDULER_cancel (pg->set_task);
185     pg->set_task = GNUNET_SCHEDULER_NO_TASK;
186   }
187   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
188       "Removing old up preference generator peer [%u] address [%u] `%s'\n",
189       pg->peer, pg->address_id, GNUNET_ATS_print_preference_type(pg->kind));
190
191   GNUNET_free (pg);
192 }
193
194
195 /**
196  * Generate between the source master and the partner and set preferences with a
197  * value depending on the generator.
198  *
199  * @param src source
200  * @param dest partner
201  * @param type type of preferences to generate
202  * @param base_rate traffic base rate to send data with
203  * @param max_rate  traffic maximum rate to send data with
204  * @param period duration of a period of traffic generation (~ 1/frequency)
205  * @param duration how long to generate traffic
206  * @return the traffic generator
207  */
208 struct PreferenceGenerator *
209 GNUNET_ATS_solver_generate_preferences_start (unsigned int peer,
210     unsigned int address_id,
211     enum GeneratorType type,
212     long int base_value,
213     long int value_rate,
214     struct GNUNET_TIME_Relative period,
215     struct GNUNET_TIME_Relative frequency,
216     enum GNUNET_ATS_PreferenceKind kind)
217 {
218   struct PreferenceGenerator *pg;
219
220   pg = GNUNET_new (struct PreferenceGenerator);
221   GNUNET_CONTAINER_DLL_insert (pg_head, pg_tail, pg);
222   pg->type = type;
223   pg->peer = peer;
224   pg->address_id = address_id;
225   pg->kind = kind;
226   pg->base_value = base_value;
227   pg->max_value = value_rate;
228   pg->duration_period = period;
229   pg->frequency = frequency;
230   pg->time_start = GNUNET_TIME_absolute_get();
231
232   switch (type) {
233     case GNUNET_ATS_TEST_TG_CONSTANT:
234       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235           "Setting up constant preference generator peer [%u] address [%u] `%s' max %u Bips\n",
236           pg->peer, pg->address_id,  GNUNET_ATS_print_preference_type(kind),
237           base_value);
238       break;
239     case GNUNET_ATS_TEST_TG_LINEAR:
240       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
241           "Setting up linear preference generator peer [%u] address [%u] `%s' min %u Bips max %u Bips\n",
242           pg->peer, pg->address_id, GNUNET_ATS_print_preference_type(kind),
243           base_value, value_rate);
244       break;
245     case GNUNET_ATS_TEST_TG_SINUS:
246       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
247           "Setting up sinus preference generator peer [%u] address [%u] `%s' baserate %u Bips, amplitude %u Bps\n",
248           pg->peer, pg->address_id, GNUNET_ATS_print_preference_type(kind),
249           base_value, value_rate);
250
251       break;
252     case GNUNET_ATS_TEST_TG_RANDOM:
253       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
254           "Setting up random preference generator peer [%u] address [%u] `%s' min %u Bips max %u Bps\n",
255           pg->peer, pg->address_id, GNUNET_ATS_print_preference_type(kind),
256           base_value, value_rate);
257       break;
258     default:
259       break;
260   }
261
262   pg->set_task = GNUNET_SCHEDULER_add_now (&set_pref_task, pg);
263   return pg;
264 }
265
266
267
268 /**
269  * Stop all preferences generators
270  */
271 void
272 GNUNET_ATS_solver_generate_preferences_stop_all ()
273 {
274   struct PreferenceGenerator *cur;
275   struct PreferenceGenerator *next;
276   next = pg_head;
277   for (cur = next; NULL != cur; cur = next)
278   {
279       next = cur->next;
280       GNUNET_ATS_solver_generate_preferences_stop(cur);
281   }
282 }
283
284
285
286 /**
287  * Experiments
288  */
289
290 const char *
291 print_op (enum OperationType op)
292 {
293   switch (op) {
294     case SOLVER_OP_ADD_ADDRESS:
295       return "ADD_ADDRESS";
296     case SOLVER_OP_DEL_ADDRESS:
297       return "DEL_ADDRESS";
298     case SOLVER_OP_START_SET_PREFERENCE:
299       return "START_SET_PREFERENCE";
300     case SOLVER_OP_STOP_SET_PREFERENCE:
301       return "STOP_STOP_PREFERENCE";
302     case SOLVER_OP_START_SET_PROPERTY:
303           return "START_SET_PROPERTY";
304     case SOLVER_OP_STOP_SET_PROPERTY:
305       return "STOP_SET_PROPERTY";
306     default:
307       break;
308   }
309   return "";
310 }
311
312 static struct Experiment *
313 create_experiment ()
314 {
315   struct Experiment *e;
316   e = GNUNET_new (struct Experiment);
317   e->name = NULL;
318   e->num_masters = 0;
319   e->num_slaves = 0;
320   e->start = NULL;
321   e->total_duration = GNUNET_TIME_UNIT_ZERO;
322   return e;
323 }
324
325 static void
326 free_experiment (struct Experiment *e)
327 {
328   struct Episode *cur;
329   struct Episode *next;
330   struct GNUNET_ATS_TEST_Operation *cur_o;
331   struct GNUNET_ATS_TEST_Operation *next_o;
332
333   next = e->start;
334   for (cur = next; NULL != cur; cur = next)
335   {
336     next = cur->next;
337
338     next_o = cur->head;
339     for (cur_o = next_o; NULL != cur_o; cur_o = next_o)
340     {
341       next_o = cur_o->next;
342       GNUNET_free_non_null (cur_o->address);
343       GNUNET_free_non_null (cur_o->plugin);
344       GNUNET_free (cur_o);
345     }
346     GNUNET_free (cur);
347   }
348
349   GNUNET_free_non_null (e->name);
350   GNUNET_free_non_null (e->cfg_file);
351   GNUNET_free (e);
352 }
353
354
355 static int
356 load_op_add_address (struct GNUNET_ATS_TEST_Operation *o,
357     struct Episode *e,
358     int op_counter,
359     char *sec_name,
360     const struct GNUNET_CONFIGURATION_Handle *cfg)
361 {
362   char *op_name;
363
364   /* peer id */
365   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
366   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
367       sec_name, op_name, &o->peer_id))
368   {
369     fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
370         op_counter, "ADD_ADDRESS", op_name);
371     GNUNET_free (op_name);
372     return GNUNET_SYSERR;
373   }
374   GNUNET_free (op_name);
375
376   /* address id */
377   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
378   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
379       sec_name, op_name, &o->address_id))
380   {
381     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
382         op_counter, "ADD_ADDRESS", op_name);
383     GNUNET_free (op_name);
384     return GNUNET_SYSERR;
385   }
386   GNUNET_free (op_name);
387
388   /* plugin */
389   GNUNET_asprintf(&op_name, "op-%u-plugin", op_counter);
390   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
391       sec_name, op_name, &o->plugin))
392   {
393     fprintf (stderr, "Missing plugin in operation %u `%s' in episode `%s'\n",
394         op_counter, "ADD_ADDRESS", op_name);
395     GNUNET_free (op_name);
396     return GNUNET_SYSERR;
397   }
398   GNUNET_free (op_name);
399
400   /* address  */
401   GNUNET_asprintf(&op_name, "op-%u-address", op_counter);
402   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
403       sec_name, op_name, &o->address))
404   {
405     fprintf (stderr, "Missing address in operation %u `%s' in episode `%s'\n",
406         op_counter, "ADD_ADDRESS", op_name);
407     GNUNET_free (op_name);
408     return GNUNET_SYSERR;
409   }
410   GNUNET_free (op_name);
411
412   /* session */
413   GNUNET_asprintf(&op_name, "op-%u-address-session", op_counter);
414   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
415       sec_name, op_name, &o->address_session))
416   {
417     fprintf (stderr, "Missing address-session in operation %u `%s' in episode `%s'\n",
418         op_counter, "ADD_ADDRESS", op_name);
419     GNUNET_free (op_name);
420     return GNUNET_SYSERR;
421   }
422   GNUNET_free (op_name);
423
424   /* network */
425   GNUNET_asprintf(&op_name, "op-%u-address-network", op_counter);
426   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
427       sec_name, op_name, &o->address_network))
428   {
429     fprintf (stderr, "Missing address-network in operation %u `%s' in episode `%s'\n",
430         op_counter, "ADD_ADDRESS", op_name);
431     GNUNET_free (op_name);
432     return GNUNET_SYSERR;
433   }
434   GNUNET_free (op_name);
435
436   fprintf (stderr,
437       "Found operation %s: [%llu:%llu] address `%s' plugin `%s' \n",
438       "ADD_ADDRESS", o->peer_id, o->address_id, o->address, o->plugin);
439
440   return GNUNET_OK;
441 }
442
443 static int
444 load_op_del_address (struct GNUNET_ATS_TEST_Operation *o,
445     struct Episode *e,
446     int op_counter,
447     char *sec_name,
448     const struct GNUNET_CONFIGURATION_Handle *cfg)
449 {
450   char *op_name;
451
452   /* peer id */
453   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
454   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
455       sec_name, op_name, &o->peer_id))
456   {
457     fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
458         op_counter, "DEL_ADDRESS", op_name);
459     GNUNET_free (op_name);
460     return GNUNET_SYSERR;
461   }
462   GNUNET_free (op_name);
463
464   /* address id */
465   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
466   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
467       sec_name, op_name, &o->address_id))
468   {
469     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
470         op_counter, "DEL_ADDRESS", op_name);
471     GNUNET_free (op_name);
472     return GNUNET_SYSERR;
473   }
474   GNUNET_free (op_name);
475
476   /* plugin */
477   GNUNET_asprintf(&op_name, "op-%u-plugin", op_counter);
478   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
479       sec_name, op_name, &o->plugin))
480   {
481     fprintf (stderr, "Missing plugin in operation %u `%s' in episode `%s'\n",
482         op_counter, "DEL_ADDRESS", op_name);
483     GNUNET_free (op_name);
484     return GNUNET_SYSERR;
485   }
486   GNUNET_free (op_name);
487
488   /* address  */
489   GNUNET_asprintf(&op_name, "op-%u-address", op_counter);
490   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
491       sec_name, op_name, &o->address))
492   {
493     fprintf (stderr, "Missing address in operation %u `%s' in episode `%s'\n",
494         op_counter, "DEL_ADDRESS", op_name);
495     GNUNET_free (op_name);
496     return GNUNET_SYSERR;
497   }
498   GNUNET_free (op_name);
499
500   /* session */
501   GNUNET_asprintf(&op_name, "op-%u-address-session", op_counter);
502   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
503       sec_name, op_name, &o->address_session))
504   {
505     fprintf (stderr, "Missing address-session in operation %u `%s' in episode `%s'\n",
506         op_counter, "DEL_ADDRESS", op_name);
507     GNUNET_free (op_name);
508     return GNUNET_SYSERR;
509   }
510   GNUNET_free (op_name);
511
512   /* network */
513   GNUNET_asprintf(&op_name, "op-%u-address-network", op_counter);
514   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
515       sec_name, op_name, &o->address_network))
516   {
517     fprintf (stderr, "Missing address-network in operation %u `%s' in episode `%s'\n",
518         op_counter, "DEL_ADDRESS", op_name);
519     GNUNET_free (op_name);
520     return GNUNET_SYSERR;
521   }
522   GNUNET_free (op_name);
523
524   fprintf (stderr,
525       "Found operation %s: [%llu:%llu] address `%s' plugin `%s' \n",
526       "DEL_ADDRESS", o->peer_id, o->address_id, o->address, o->plugin);
527
528   return GNUNET_OK;
529 }
530
531 static enum GNUNET_ATS_Property
532 parse_preference_string (const char * str)
533 {
534   int c = 0;
535   char *props[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
536
537   for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
538     if (0 == strcmp(str, props[c]))
539       return c;
540   return 0;
541 };
542
543 static int
544 load_op_start_set_preference (struct GNUNET_ATS_TEST_Operation *o,
545     struct Episode *e,
546     int op_counter,
547     char *sec_name,
548     const struct GNUNET_CONFIGURATION_Handle *cfg)
549 {
550   char *op_name;
551   char *type;
552   char *pref;
553
554   /* peer id */
555   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
556   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
557       sec_name, op_name, &o->peer_id))
558   {
559     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
560         op_counter, "START_SET_PREFERENCE", op_name);
561     GNUNET_free (op_name);
562     return GNUNET_SYSERR;
563   }
564   GNUNET_free (op_name);
565
566   /* address id */
567   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
568   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
569       sec_name, op_name, &o->address_id))
570   {
571     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
572         op_counter, "START_SET_PREFERENCE", op_name);
573     GNUNET_free (op_name);
574     return GNUNET_SYSERR;
575   }
576   GNUNET_free (op_name);
577
578   /* generator */
579   GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
580   if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
581           sec_name, op_name, &type)) )
582   {
583     fprintf (stderr, "Missing type in operation %u `%s' in episode `%s'\n",
584         op_counter, "START_SET_PREFERENCE", op_name);
585     GNUNET_free (op_name);
586     return GNUNET_SYSERR;
587   }
588
589   /* Load arguments for set_rate, start_send, set_preference */
590   if (0 == strcmp (type, "constant"))
591   {
592     o->tg_type = GNUNET_ATS_TEST_TG_CONSTANT;
593   }
594   else if (0 == strcmp (type, "linear"))
595   {
596     o->tg_type = GNUNET_ATS_TEST_TG_LINEAR;
597   }
598   else if (0 == strcmp (type, "sinus"))
599   {
600     o->tg_type = GNUNET_ATS_TEST_TG_SINUS;
601   }
602   else if (0 == strcmp (type, "random"))
603   {
604     o->tg_type = GNUNET_ATS_TEST_TG_RANDOM;
605   }
606   else
607   {
608     fprintf (stderr, "Invalid generator type %u `%s' in episode %u\n",
609         op_counter, op_name, e->id);
610     GNUNET_free (type);
611     GNUNET_free (op_name);
612     return GNUNET_SYSERR;
613   }
614   GNUNET_free (type);
615   GNUNET_free (op_name);
616
617
618   /* Get base rate */
619   GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
620   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
621       sec_name, op_name, &o->base_rate))
622   {
623     fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
624         op_counter, op_name, e->id);
625     GNUNET_free (op_name);
626     return GNUNET_SYSERR;
627   }
628   GNUNET_free (op_name);
629
630
631   /* Get max rate */
632   GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
633   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
634       sec_name, op_name, &o->max_rate))
635   {
636     if ((GNUNET_ATS_TEST_TG_LINEAR == o->tg_type) ||
637         (GNUNET_ATS_TEST_TG_RANDOM == o->tg_type) ||
638         (GNUNET_ATS_TEST_TG_SINUS == o->tg_type))
639     {
640       fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
641           op_counter, op_name, e->id);
642       GNUNET_free (op_name);
643       return GNUNET_SYSERR;
644     }
645   }
646   GNUNET_free (op_name);
647
648   /* Get period */
649   GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
650   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
651       sec_name, op_name, &o->period))
652   {
653     o->period = e->duration;
654   }
655   GNUNET_free (op_name);
656
657   /* Get frequency */
658   GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
659   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
660       sec_name, op_name, &o->frequency))
661   {
662       fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
663           op_counter, op_name, e->id);
664       GNUNET_free (op_name);
665       return GNUNET_SYSERR;
666   }
667   GNUNET_free (op_name);
668
669   /* Get preference */
670   GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
671   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
672       sec_name, op_name, &pref))
673   {
674       fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
675           op_counter, op_name, e->id);
676       GNUNET_free (op_name);
677       return GNUNET_SYSERR;
678   }
679
680   if (0 == (o->pref_type = parse_preference_string(pref)))
681   {
682       fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
683           op_counter, op_name, e->id);
684       GNUNET_free (op_name);
685       GNUNET_free (pref);
686       return GNUNET_SYSERR;
687   }
688   GNUNET_free (pref);
689   GNUNET_free (op_name);
690
691   fprintf (stderr,
692       "Found operation %s: [%llu:%llu]: %s = %llu\n",
693       "START_SET_PREFERENCE", o->peer_id, o->address_id,
694       GNUNET_ATS_print_preference_type(o->pref_type), o->base_rate);
695
696   return GNUNET_OK;
697 }
698
699 static int
700 load_op_stop_set_preference (struct GNUNET_ATS_TEST_Operation *o,
701     struct Episode *e,
702     int op_counter,
703     char *sec_name,
704     const struct GNUNET_CONFIGURATION_Handle *cfg)
705 {
706   char *op_name;
707   char *pref;
708
709   /* peer id */
710   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
711   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
712       sec_name, op_name, &o->peer_id))
713   {
714     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
715         op_counter, "STOP_SET_PREFERENCE", op_name);
716     GNUNET_free (op_name);
717     return GNUNET_SYSERR;
718   }
719   GNUNET_free (op_name);
720
721   /* address id */
722   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
723   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
724       sec_name, op_name, &o->address_id))
725   {
726     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
727         op_counter, "STOP_SET_PREFERENCE", op_name);
728     GNUNET_free (op_name);
729     return GNUNET_SYSERR;
730   }
731   GNUNET_free (op_name);
732
733   /* Get preference */
734   GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
735   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
736       sec_name, op_name, &pref))
737   {
738     fprintf (stderr, "Missing preference in operation %u `%s' in episode `%s'\n",
739         op_counter, "STOP_SET_PREFERENCE", op_name);
740       GNUNET_free (op_name);
741       return GNUNET_SYSERR;
742   }
743
744   if (0 == (o->pref_type = parse_preference_string(pref)))
745   {
746       fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
747           op_counter, op_name, e->id);
748       GNUNET_free (op_name);
749       GNUNET_free (pref);
750       return GNUNET_SYSERR;
751   }
752   GNUNET_free (pref);
753   GNUNET_free (op_name);
754
755   fprintf (stderr,
756       "Found operation %s: [%llu:%llu]: %s\n",
757       "STOP_SET_PREFERENCE", o->peer_id, o->address_id,
758       GNUNET_ATS_print_preference_type(o->pref_type));
759   return GNUNET_OK;
760 }
761
762 static enum GNUNET_ATS_Property
763 parse_property_string (const char * str)
764 {
765   int c = 0;
766   char *props[GNUNET_ATS_PropertyCount] = GNUNET_ATS_PropertyStrings;
767
768   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
769     if (0 == strcmp(str, props[c]))
770       return c;
771   return 0;
772 };
773
774 static int
775 load_op_start_set_property(struct GNUNET_ATS_TEST_Operation *o,
776     struct Episode *e,
777     int op_counter,
778     char *sec_name,
779     const struct GNUNET_CONFIGURATION_Handle *cfg)
780 {
781   char *op_name;
782   char *type;
783   char *prop;
784
785   /* peer id */
786   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
787   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
788       sec_name, op_name, &o->peer_id))
789   {
790     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
791         op_counter, "START_SET_PROPERTY", op_name);
792     GNUNET_free (op_name);
793     return GNUNET_SYSERR;
794   }
795   GNUNET_free (op_name);
796
797   /* address id */
798   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
799   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
800       sec_name, op_name, &o->address_id))
801   {
802     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
803         op_counter, "START_SET_PROPERTY", op_name);
804     GNUNET_free (op_name);
805     return GNUNET_SYSERR;
806   }
807   GNUNET_free (op_name);
808
809   /* generator */
810   GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
811   if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
812           sec_name, op_name, &type)) )
813   {
814     fprintf (stderr, "Missing type in operation %u `%s' in episode `%s'\n",
815         op_counter, "START_SET_PROPERTY", op_name);
816     GNUNET_free (op_name);
817     return GNUNET_SYSERR;
818   }
819
820   /* Load arguments for set_rate, start_send, set_preference */
821   if (0 == strcmp (type, "constant"))
822   {
823     o->tg_type = GNUNET_ATS_TEST_TG_CONSTANT;
824   }
825   else if (0 == strcmp (type, "linear"))
826   {
827     o->tg_type = GNUNET_ATS_TEST_TG_LINEAR;
828   }
829   else if (0 == strcmp (type, "sinus"))
830   {
831     o->tg_type = GNUNET_ATS_TEST_TG_SINUS;
832   }
833   else if (0 == strcmp (type, "random"))
834   {
835     o->tg_type = GNUNET_ATS_TEST_TG_RANDOM;
836   }
837   else
838   {
839     fprintf (stderr, "Invalid generator type %u `%s' in episode %u\n",
840         op_counter, op_name, e->id);
841     GNUNET_free (type);
842     GNUNET_free (op_name);
843     return GNUNET_SYSERR;
844   }
845   GNUNET_free (type);
846   GNUNET_free (op_name);
847
848
849   /* Get base rate */
850   GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
851   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
852       sec_name, op_name, &o->base_rate))
853   {
854     fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
855         op_counter, op_name, e->id);
856     GNUNET_free (op_name);
857     return GNUNET_SYSERR;
858   }
859   GNUNET_free (op_name);
860
861
862   /* Get max rate */
863   GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
864   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
865       sec_name, op_name, &o->max_rate))
866   {
867     if ((GNUNET_ATS_TEST_TG_LINEAR == o->tg_type) ||
868         (GNUNET_ATS_TEST_TG_RANDOM == o->tg_type) ||
869         (GNUNET_ATS_TEST_TG_SINUS == o->tg_type))
870     {
871       fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
872           op_counter, op_name, e->id);
873       GNUNET_free (op_name);
874       return GNUNET_SYSERR;
875     }
876   }
877   GNUNET_free (op_name);
878
879   /* Get period */
880   GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
881   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
882       sec_name, op_name, &o->period))
883   {
884     o->period = e->duration;
885   }
886   GNUNET_free (op_name);
887
888   /* Get frequency */
889   GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
890   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
891       sec_name, op_name, &o->frequency))
892   {
893       fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
894           op_counter, op_name, e->id);
895       GNUNET_free (op_name);
896       return GNUNET_SYSERR;
897   }
898   GNUNET_free (op_name);
899
900   /* Get preference */
901   GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
902   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
903       sec_name, op_name, &prop))
904   {
905       fprintf (stderr, "Missing property in operation %u `%s' in episode %u\n",
906           op_counter, op_name, e->id);
907       GNUNET_free (op_name);
908       GNUNET_free_non_null (prop);
909       return GNUNET_SYSERR;
910   }
911
912   if (0 == (o->prop_type = parse_property_string(prop)))
913   {
914       fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
915           op_counter, op_name, e->id);
916       GNUNET_free (op_name);
917       GNUNET_free (prop);
918       return GNUNET_SYSERR;
919   }
920
921   GNUNET_free (prop);
922   GNUNET_free (op_name);
923
924   fprintf (stderr,
925       "Found operation %s: [%llu:%llu] %s = %llu\n",
926       "START_SET_PROPERTY", o->peer_id, o->address_id,
927       GNUNET_ATS_print_property_type (o->prop_type), o->base_rate);
928
929   return GNUNET_OK;
930 }
931
932 static int
933 load_op_stop_set_property (struct GNUNET_ATS_TEST_Operation *o,
934     struct Episode *e,
935     int op_counter,
936     char *sec_name,
937     const struct GNUNET_CONFIGURATION_Handle *cfg)
938 {
939   char *op_name;
940   char *pref;
941
942   /* peer id */
943   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
944   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
945       sec_name, op_name, &o->peer_id))
946   {
947     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
948         op_counter, "STOP_SET_PROPERTY", op_name);
949     GNUNET_free (op_name);
950     return GNUNET_SYSERR;
951   }
952   GNUNET_free (op_name);
953
954   /* address id */
955   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
956   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
957       sec_name, op_name, &o->address_id))
958   {
959     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
960         op_counter, "STOP_SET_PROPERTY", op_name);
961     GNUNET_free (op_name);
962     return GNUNET_SYSERR;
963   }
964   GNUNET_free (op_name);
965
966   /* Get property */
967   GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
968   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
969       sec_name, op_name, &pref))
970   {
971     fprintf (stderr, "Missing property in operation %u `%s' in episode `%s'\n",
972         op_counter, "STOP_SET_PROPERTY", op_name);
973       GNUNET_free (op_name);
974       GNUNET_free_non_null (pref);
975       return GNUNET_SYSERR;
976   }
977
978   if (0 == (o->prop_type = parse_property_string(pref)))
979   {
980       fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
981           op_counter, op_name, e->id);
982       GNUNET_free (op_name);
983       GNUNET_free (pref);
984       GNUNET_free_non_null (pref);
985       return GNUNET_SYSERR;
986   }
987
988   GNUNET_free (pref);
989   GNUNET_free (op_name);
990
991   fprintf (stderr,
992       "Found operation %s: [%llu:%llu] %s\n",
993       "STOP_SET_PROPERTY", o->peer_id, o->address_id,
994       GNUNET_ATS_print_property_type (o->prop_type));
995
996   return GNUNET_OK;
997 }
998
999 static int
1000 load_episode (struct Experiment *e, struct Episode *cur,
1001     struct GNUNET_CONFIGURATION_Handle *cfg)
1002 {
1003   struct GNUNET_ATS_TEST_Operation *o;
1004   char *sec_name;
1005   char *op_name;
1006   char *op;
1007   int op_counter = 0;
1008   fprintf (stderr, "Parsing episode %u\n",cur->id);
1009   GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
1010
1011   while (1)
1012   {
1013     /* Load operation */
1014     GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
1015     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
1016         sec_name, op_name, &op))
1017     {
1018       GNUNET_free (op_name);
1019       break;
1020     }
1021     o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
1022     /* operations = set_rate, start_send, stop_send, set_preference */
1023     if (0 == strcmp (op, "address_add"))
1024     {
1025       o->type = SOLVER_OP_ADD_ADDRESS;
1026       if (GNUNET_SYSERR == load_op_add_address (o, cur,
1027           op_counter, sec_name, cfg))
1028       {
1029         GNUNET_free (o);
1030         GNUNET_free (op);
1031         GNUNET_free (op_name);
1032         GNUNET_free (sec_name);
1033         return GNUNET_SYSERR;
1034       }
1035     }
1036     else if (0 == strcmp (op, "address_del"))
1037     {
1038       o->type = SOLVER_OP_DEL_ADDRESS;
1039       if (GNUNET_SYSERR == load_op_del_address (o, cur,
1040           op_counter, sec_name, cfg))
1041       {
1042         GNUNET_free (o);
1043         GNUNET_free (op);
1044         GNUNET_free (op_name);
1045         GNUNET_free (sec_name);
1046         return GNUNET_SYSERR;
1047       }
1048     }
1049     else if (0 == strcmp (op, "start_set_property"))
1050     {
1051       o->type = SOLVER_OP_START_SET_PROPERTY;
1052       if (GNUNET_SYSERR == load_op_start_set_property (o, cur,
1053           op_counter, sec_name, cfg))
1054       {
1055         GNUNET_free (o);
1056         GNUNET_free (op);
1057         GNUNET_free (op_name);
1058         GNUNET_free (sec_name);
1059         return GNUNET_SYSERR;
1060       }
1061     }
1062     else if (0 == strcmp (op, "stop_set_property"))
1063     {
1064       o->type = SOLVER_OP_STOP_SET_PROPERTY;
1065       if (GNUNET_SYSERR == load_op_stop_set_property (o, cur,
1066           op_counter, sec_name, cfg))
1067       {
1068         GNUNET_free (o);
1069         GNUNET_free (op);
1070         GNUNET_free (op_name);
1071         GNUNET_free (sec_name);
1072         return GNUNET_SYSERR;
1073       }
1074     }
1075     else if (0 == strcmp (op, "start_set_preference"))
1076     {
1077       o->type = SOLVER_OP_START_SET_PREFERENCE;
1078       if (GNUNET_SYSERR == load_op_start_set_preference (o, cur,
1079           op_counter, sec_name, cfg))
1080       {
1081         GNUNET_free (o);
1082         GNUNET_free (op);
1083         GNUNET_free (op_name);
1084         GNUNET_free (sec_name);
1085         return GNUNET_SYSERR;
1086       }
1087     }
1088     else if (0 == strcmp (op, "stop_set_preference"))
1089     {
1090       o->type = SOLVER_OP_STOP_SET_PREFERENCE;
1091       if (GNUNET_SYSERR == load_op_stop_set_preference (o, cur,
1092           op_counter, sec_name, cfg))
1093       {
1094         GNUNET_free (o);
1095         GNUNET_free (op);
1096         GNUNET_free (op_name);
1097         GNUNET_free (sec_name);
1098         return GNUNET_SYSERR;
1099       }
1100     }
1101     else
1102     {
1103       fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
1104           op_counter, op, cur->id);
1105       GNUNET_free (o);
1106       GNUNET_free (op);
1107       GNUNET_free (op_name);
1108       GNUNET_free (sec_name);
1109       return GNUNET_SYSERR;
1110     }
1111     GNUNET_free (op);
1112     GNUNET_free (op_name);
1113
1114     GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
1115     op_counter++;
1116   }
1117   GNUNET_free (sec_name);
1118
1119
1120 #if 0
1121     /* Get source */
1122     GNUNET_asprintf(&op_name, "op-%u-src", op_counter);
1123     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1124         sec_name, op_name, &o->src_id))
1125     {
1126       fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
1127           op_counter, op, cur->id);
1128       GNUNET_free (op);
1129       GNUNET_free (op_name);
1130       return GNUNET_SYSERR;
1131     }
1132     if (o->src_id > (e->num_masters - 1))
1133     {
1134       fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
1135           o->src_id, op_counter, op, cur->id);
1136       GNUNET_free (op);
1137       GNUNET_free (op_name);
1138       return GNUNET_SYSERR;
1139     }
1140     GNUNET_free (op_name);
1141
1142     /* Get destination */
1143     GNUNET_asprintf(&op_name, "op-%u-dest", op_counter);
1144     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1145         sec_name, op_name, &o->dest_id))
1146     {
1147       fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
1148           op_counter, op, cur->id);
1149       GNUNET_free (op);
1150       GNUNET_free (op_name);
1151       return GNUNET_SYSERR;
1152     }
1153     if (o->dest_id > (e->num_slaves - 1))
1154     {
1155       fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
1156           o->dest_id, op_counter, op, cur->id);
1157       GNUNET_free (op);
1158       GNUNET_free (op_name);
1159       return GNUNET_SYSERR;
1160     }
1161     GNUNET_free (op_name);
1162
1163     GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
1164     if ( (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_string(cfg,
1165             sec_name, op_name, &type)) &&
1166         ((STOP_SEND != o->type) || (STOP_PREFERENCE != o->type)))
1167     {
1168       /* Load arguments for set_rate, start_send, set_preference */
1169       if (0 == strcmp (type, "constant"))
1170       {
1171         o->tg_type = GNUNET_ATS_TEST_TG_CONSTANT;
1172       }
1173       else if (0 == strcmp (type, "linear"))
1174       {
1175         o->tg_type = GNUNET_ATS_TEST_TG_LINEAR;
1176       }
1177       else if (0 == strcmp (type, "sinus"))
1178       {
1179         o->tg_type = GNUNET_ATS_TEST_TG_SINUS;
1180       }
1181       else if (0 == strcmp (type, "random"))
1182       {
1183         o->tg_type = GNUNET_ATS_TEST_TG_RANDOM;
1184       }
1185       else
1186       {
1187         fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
1188             op_counter, op, cur->id);
1189         GNUNET_free (type);
1190         GNUNET_free (op);
1191         GNUNET_free (op_name);
1192         return GNUNET_SYSERR;
1193       }
1194       GNUNET_free (op_name);
1195
1196       /* Get base rate */
1197       GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
1198       if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1199           sec_name, op_name, &o->base_rate))
1200       {
1201         fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
1202             op_counter, op, cur->id);
1203         GNUNET_free (type);
1204         GNUNET_free (op);
1205         GNUNET_free (op_name);
1206         return GNUNET_SYSERR;
1207       }
1208       GNUNET_free (op_name);
1209
1210       /* Get max rate */
1211       GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
1212       if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1213           sec_name, op_name, &o->max_rate))
1214       {
1215         if ((GNUNET_ATS_TEST_TG_LINEAR == o->tg_type) ||
1216             (GNUNET_ATS_TEST_TG_RANDOM == o->tg_type) ||
1217             (GNUNET_ATS_TEST_TG_SINUS == o->tg_type))
1218         {
1219           fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
1220               op_counter, op, cur->id);
1221           GNUNET_free (type);
1222           GNUNET_free (op_name);
1223           GNUNET_free (op);
1224           return GNUNET_SYSERR;
1225         }
1226       }
1227       GNUNET_free (op_name);
1228
1229       /* Get period */
1230       GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
1231       if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1232           sec_name, op_name, &o->period))
1233       {
1234         o->period = cur->duration;
1235       }
1236       GNUNET_free (op_name);
1237
1238       if (START_PREFERENCE == o->type)
1239       {
1240           /* Get frequency */
1241           GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
1242           if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1243               sec_name, op_name, &o->frequency))
1244           {
1245               fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
1246                   op_counter, op, cur->id);
1247               GNUNET_free (type);
1248               GNUNET_free (op_name);
1249               GNUNET_free (op);
1250               return GNUNET_SYSERR;
1251           }
1252           GNUNET_free (op_name);
1253
1254           /* Get preference */
1255           GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
1256           if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1257               sec_name, op_name, &pref))
1258           {
1259               fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
1260                   op_counter, op, cur->id);
1261               GNUNET_free (type);
1262               GNUNET_free (op_name);
1263               GNUNET_free (op);
1264               GNUNET_free_non_null (pref);
1265               return GNUNET_SYSERR;
1266           }
1267
1268           if (0 == strcmp(pref, "bandwidth"))
1269             o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
1270           else if (0 == strcmp(pref, "latency"))
1271             o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
1272           else
1273           {
1274               fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
1275                   op_counter, op, cur->id);
1276               GNUNET_free (type);
1277               GNUNET_free (op_name);
1278               GNUNET_free (op);
1279               GNUNET_free (pref);
1280               GNUNET_free_non_null (pref);
1281               return GNUNET_SYSERR;
1282           }
1283           GNUNET_free (pref);
1284           GNUNET_free (op_name);
1285       }
1286     }
1287
1288     /* Safety checks */
1289     if ((GNUNET_ATS_TEST_TG_LINEAR == o->tg_type) ||
1290         (GNUNET_ATS_TEST_TG_SINUS == o->tg_type))
1291     {
1292       if ((o->max_rate - o->base_rate) > o->base_rate)
1293       {
1294         /* This will cause an underflow */
1295         GNUNET_break (0);
1296       }
1297       fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
1298     }
1299
1300     if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
1301       fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
1302         op_counter, cur->id, print_op (o->type), o->src_id,
1303         o->dest_id, (NULL != type) ? type : "",
1304         o->base_rate, o->max_rate,
1305         GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
1306     else
1307       fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
1308         op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
1309
1310     GNUNET_free_non_null (type);
1311     GNUNET_free (op);
1312 #endif
1313
1314   return GNUNET_OK;
1315 }
1316
1317 static int
1318 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
1319 {
1320   int e_counter = 0;
1321   char *sec_name;
1322   struct GNUNET_TIME_Relative e_duration;
1323   struct Episode *cur;
1324   struct Episode *last;
1325
1326   e_counter = 0;
1327   last = NULL;
1328   while (1)
1329   {
1330     GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
1331     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
1332         sec_name, "duration", &e_duration))
1333     {
1334       GNUNET_free (sec_name);
1335       break;
1336     }
1337
1338     cur = GNUNET_new (struct Episode);
1339     cur->duration = e_duration;
1340     cur->id = e_counter;
1341
1342     if (GNUNET_OK != load_episode (e, cur, cfg))
1343     {
1344       GNUNET_free (sec_name);
1345       GNUNET_free (cur);
1346       return GNUNET_SYSERR;
1347     }
1348
1349     fprintf (stderr, "Found episode %u with duration %s \n",
1350         e_counter,
1351         GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
1352
1353     /* Update experiment */
1354     e->num_episodes ++;
1355     e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
1356     /* Put in linked list */
1357     if (NULL == last)
1358       e->start = cur;
1359     else
1360     last->next = cur;
1361
1362     GNUNET_free (sec_name);
1363     e_counter ++;
1364     last = cur;
1365   }
1366   return e_counter;
1367 }
1368
1369 static void
1370 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
1371 {
1372   struct Experiment *e = cls;
1373   e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
1374   fprintf (stderr, "Experiment timeout!\n");
1375
1376   if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
1377   {
1378     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
1379     e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
1380   }
1381
1382   e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
1383       GNUNET_SYSERR);
1384 }
1385
1386 static void
1387 enforce_add_address (struct GNUNET_ATS_TEST_Operation *op)
1388 {
1389   /*
1390   struct BenchmarkPeer *peer;
1391   struct BenchmarkPartner *partner;
1392
1393   peer = GNUNET_ATS_TEST_get_peer (op->src_id);
1394   if (NULL == peer)
1395   {
1396     GNUNET_break (0);
1397     return;
1398   }
1399
1400   partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
1401   if (NULL == partner)
1402   {
1403     GNUNET_break (0);
1404     return;
1405   }
1406
1407   fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
1408
1409   if (NULL != partner->tg)
1410   {
1411     fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
1412     GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
1413     partner->tg = NULL;
1414   }
1415
1416   partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
1417       op->tg_type, op->base_rate, op->max_rate, op->period,
1418       GNUNET_TIME_UNIT_FOREVER_REL);
1419    */
1420 }
1421
1422 static void
1423 enforce_del_address (struct GNUNET_ATS_TEST_Operation *op)
1424 {
1425   /*
1426   struct BenchmarkPartner *p;
1427   p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
1428   if (NULL == p)
1429   {
1430     GNUNET_break (0);
1431     return;
1432   }
1433
1434   fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
1435
1436   if (NULL != p->tg)
1437   {
1438     fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
1439         op->src_id, op->dest_id);
1440     GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
1441     p->tg = NULL;
1442   }
1443   */
1444 }
1445
1446 static void
1447 enforce_start_property (struct GNUNET_ATS_TEST_Operation *op)
1448 {
1449
1450 }
1451
1452 static void
1453 enforce_stop_property (struct GNUNET_ATS_TEST_Operation *op)
1454 {
1455
1456 }
1457
1458 static void
1459 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
1460 {
1461   struct PreferenceGenerator *pg;
1462   if (NULL != (pg = find_pref_gen (op->peer_id, op->address_id, op->pref_type)))
1463   {
1464     GNUNET_ATS_solver_generate_preferences_stop (pg);
1465     GNUNET_free (pg);
1466   }
1467
1468   GNUNET_ATS_solver_generate_preferences_start (op->peer_id,
1469     op->address_id,
1470     op->type,
1471     op->base_rate,
1472     op->max_rate,
1473     op->period,
1474     op->frequency,
1475     op->pref_type);
1476 }
1477
1478 static void
1479 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
1480 {
1481   struct PreferenceGenerator *pg = find_pref_gen(op->peer_id, op->address_id,
1482       op->pref_type);
1483   if (NULL != pg)
1484       GNUNET_ATS_solver_generate_preferences_stop (pg);
1485 }
1486
1487 static void enforce_episode (struct Episode *ep)
1488 {
1489   struct GNUNET_ATS_TEST_Operation *cur;
1490   for (cur = ep->head; NULL != cur; cur = cur->next)
1491   {
1492     switch (cur->type) {
1493       case SOLVER_OP_ADD_ADDRESS:
1494         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
1495             print_op (cur->type), cur->peer_id, cur->address_id);
1496         enforce_add_address (cur);
1497         break;
1498       case SOLVER_OP_DEL_ADDRESS:
1499         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
1500             print_op (cur->type), cur->peer_id, cur->address_id);
1501         enforce_del_address (cur);
1502         break;
1503       case SOLVER_OP_START_SET_PROPERTY:
1504         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
1505             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
1506         enforce_start_property (cur);
1507         break;
1508       case SOLVER_OP_STOP_SET_PROPERTY:
1509         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
1510             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
1511         enforce_stop_property (cur);
1512         break;
1513       case SOLVER_OP_START_SET_PREFERENCE:
1514         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
1515             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
1516         enforce_start_preference (cur);
1517         break;
1518       case SOLVER_OP_STOP_SET_PREFERENCE:
1519         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
1520             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
1521         enforce_stop_preference (cur);
1522         break;
1523       default:
1524         break;
1525     }
1526   }
1527 }
1528
1529 static void
1530 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
1531 {
1532   struct Experiment *e = cls;
1533   e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
1534   if (NULL != e->ep_done_cb)
1535     e->ep_done_cb (e->cur);
1536
1537   /* Scheduling next */
1538   e->cur = e->cur->next;
1539   if (NULL == e->cur)
1540   {
1541     /* done */
1542     fprintf (stderr, "Last episode done!\n");
1543     if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
1544     {
1545       GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
1546       e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
1547     }
1548     e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
1549     return;
1550   }
1551
1552   fprintf (stderr, "Running episode %u with timeout %s\n",
1553       e->cur->id,
1554       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
1555   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
1556       &timeout_episode, e);
1557   enforce_episode(e->cur);
1558
1559
1560 }
1561
1562
1563 void
1564 GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
1565     GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
1566     GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
1567 {
1568   fprintf (stderr, "Running experiment `%s'  with timeout %s\n", e->name,
1569       GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
1570   e->e_done_cb = e_done_cb;
1571   e->ep_done_cb = ep_done_cb;
1572   e->start_time = GNUNET_TIME_absolute_get();
1573
1574   /* Start total time out */
1575   e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
1576       &timeout_experiment, e);
1577
1578
1579   /* Start */
1580   if (NULL == e->start)
1581   {
1582     GNUNET_break (0);
1583     return;
1584   }
1585   e->cur = e->start;
1586   fprintf (stderr, "Running episode %u with timeout %s\n",
1587       e->cur->id,
1588       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
1589   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
1590       &timeout_episode, e);
1591   enforce_episode(e->cur);
1592
1593 }
1594
1595 void
1596 GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
1597 {
1598   if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
1599   {
1600     GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
1601     e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
1602   }
1603   if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
1604   {
1605     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
1606     e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
1607   }
1608   if (NULL != e->cfg)
1609   {
1610     GNUNET_CONFIGURATION_destroy(e->cfg);
1611     e->cfg = NULL;
1612   }
1613   free_experiment (e);
1614 }
1615
1616
1617 struct Experiment *
1618 GNUNET_ATS_solvers_experimentation_load (char *filename)
1619 {
1620   struct Experiment *e;
1621   struct GNUNET_CONFIGURATION_Handle *cfg;
1622   e = NULL;
1623
1624   cfg = GNUNET_CONFIGURATION_create();
1625   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
1626   {
1627     fprintf (stderr, "Failed to load `%s'\n", filename);
1628     GNUNET_CONFIGURATION_destroy (cfg);
1629     return NULL;
1630   }
1631
1632   e = create_experiment ();
1633
1634   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
1635       "name", &e->name))
1636   {
1637     fprintf (stderr, "Invalid %s", "name");
1638     free_experiment (e);
1639     return NULL;
1640   }
1641   else
1642     fprintf (stderr, "Experiment name: `%s'\n", e->name);
1643
1644   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
1645       "cfg_file", &e->cfg_file))
1646   {
1647     fprintf (stderr, "Invalid %s", "cfg_file");
1648     free_experiment (e);
1649     return NULL;
1650   }
1651   else
1652   {
1653     fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
1654     e->cfg = GNUNET_CONFIGURATION_create();
1655     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (e->cfg, e->cfg_file))
1656     {
1657       fprintf (stderr, "Invalid configuration %s", "cfg_file");
1658       free_experiment (e);
1659       return NULL;
1660     }
1661
1662   }
1663
1664   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
1665       "masters", &e->num_masters))
1666   {
1667     fprintf (stderr, "Invalid %s", "masters");
1668     free_experiment (e);
1669     return NULL;
1670   }
1671   else
1672     fprintf (stderr, "Experiment masters: `%llu'\n",
1673         e->num_masters);
1674
1675   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
1676       "slaves", &e->num_slaves))
1677   {
1678     fprintf (stderr, "Invalid %s", "slaves");
1679     free_experiment (e);
1680     return NULL;
1681   }
1682   else
1683     fprintf (stderr, "Experiment slaves: `%llu'\n",
1684         e->num_slaves);
1685
1686   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
1687       "log_freq", &e->log_freq))
1688   {
1689     fprintf (stderr, "Invalid %s", "log_freq");
1690     free_experiment (e);
1691     return NULL;
1692   }
1693   else
1694     fprintf (stderr, "Experiment logging frequency: `%s'\n",
1695         GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
1696
1697   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
1698       "max_duration", &e->max_duration))
1699   {
1700     fprintf (stderr, "Invalid %s", "max_duration");
1701     free_experiment (e);
1702     return NULL;
1703   }
1704   else
1705     fprintf (stderr, "Experiment duration: `%s'\n",
1706         GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
1707
1708   if (GNUNET_SYSERR == load_episodes (e, cfg))
1709   {
1710     GNUNET_ATS_solvers_experimentation_stop (e);
1711     GNUNET_CONFIGURATION_destroy (cfg);
1712     e = NULL;
1713     fprintf (stderr, "Failed to load experiment\n");
1714     return NULL;
1715   }
1716   fprintf (stderr, "Loaded %u episodes with total duration %s\n",
1717       e->num_episodes,
1718       GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
1719
1720   GNUNET_CONFIGURATION_destroy (cfg);
1721   return e;
1722 }
1723
1724 /**
1725  * Logging
1726  */
1727
1728
1729 /**
1730  * Solver
1731  */
1732
1733 struct GNUNET_ATS_TESTING_SolverHandle
1734 {
1735   char * plugin;
1736   struct GNUNET_ATS_PluginEnvironment env;
1737   void *solver;
1738   struct GNUNET_CONTAINER_MultiPeerMap *addresses;
1739 };
1740
1741 enum GNUNET_ATS_Solvers
1742 {
1743   GNUNET_ATS_SOLVER_PROPORTIONAL,
1744   GNUNET_ATS_SOLVER_MLP,
1745   GNUNET_ATS_SOLVER_RIL,
1746 };
1747
1748 void
1749 GNUNET_ATS_solvers_solver_stop (struct GNUNET_ATS_TESTING_SolverHandle *sh)
1750 {
1751  GNUNET_STATISTICS_destroy ((struct GNUNET_STATISTICS_Handle *) sh->env.stats,
1752      GNUNET_NO);
1753  GNUNET_CONTAINER_multipeermap_destroy(sh->env.addresses);
1754  GNUNET_PLUGIN_unload (sh->plugin, sh->solver);
1755  GNUNET_CONTAINER_multipeermap_destroy(sh->addresses);
1756  GNUNET_free (sh->plugin);
1757  GNUNET_free (sh);
1758 }
1759
1760 /**
1761  * Load quotas for networks from configuration
1762  *
1763  * @param cfg configuration handle
1764  * @param out_dest where to write outbound quotas
1765  * @param in_dest where to write inbound quotas
1766  * @param dest_length length of inbound and outbound arrays
1767  * @return number of networks loaded
1768  */
1769 unsigned int
1770 GNUNET_ATS_solvers_load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1771                                                  unsigned long long *out_dest,
1772                                                  unsigned long long *in_dest,
1773                                                  int dest_length)
1774 {
1775   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1776   char * entry_in = NULL;
1777   char * entry_out = NULL;
1778   char * quota_out_str;
1779   char * quota_in_str;
1780   int c;
1781   int res;
1782
1783   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1784   {
1785     in_dest[c] = 0;
1786     out_dest[c] = 0;
1787     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1788     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1789
1790     /* quota out */
1791     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
1792     {
1793       res = GNUNET_NO;
1794       if (0 == strcmp(quota_out_str, BIG_M_STRING))
1795       {
1796         out_dest[c] = GNUNET_ATS_MaxBandwidth;
1797         res = GNUNET_YES;
1798       }
1799       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
1800         res = GNUNET_YES;
1801       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,  &out_dest[c])))
1802          res = GNUNET_YES;
1803
1804       if (GNUNET_NO == res)
1805       {
1806           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
1807               network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1808           out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1809       }
1810       else
1811       {
1812           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Outbound quota configure for network `%s' is %llu\n"),
1813               network_str[c], out_dest[c]);
1814       }
1815       GNUNET_free (quota_out_str);
1816     }
1817     else
1818     {
1819       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1820           network_str[c], GNUNET_ATS_DefaultBandwidth);
1821       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1822     }
1823
1824     /* quota in */
1825     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
1826     {
1827       res = GNUNET_NO;
1828       if (0 == strcmp(quota_in_str, BIG_M_STRING))
1829       {
1830         in_dest[c] = GNUNET_ATS_MaxBandwidth;
1831         res = GNUNET_YES;
1832       }
1833       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1834         res = GNUNET_YES;
1835       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,  &in_dest[c])))
1836          res = GNUNET_YES;
1837
1838       if (GNUNET_NO == res)
1839       {
1840           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
1841               network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1842           in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1843       }
1844       else
1845       {
1846           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Inbound quota configured for network `%s' is %llu\n"),
1847               network_str[c], in_dest[c]);
1848       }
1849       GNUNET_free (quota_in_str);
1850     }
1851     else
1852     {
1853       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1854           network_str[c], GNUNET_ATS_DefaultBandwidth);
1855       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1856     }
1857     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c], in_dest[c], out_dest[c]);
1858     GNUNET_free (entry_out);
1859     GNUNET_free (entry_in);
1860   }
1861   return GNUNET_ATS_NetworkTypeCount;
1862 }
1863
1864 /**
1865  * Information callback for the solver
1866  *
1867  * @param op the solver operation
1868  * @param stat status of the solver operation
1869  * @param add additional solver information
1870  */
1871 static void
1872 solver_info_cb (void *cls,
1873     enum GAS_Solver_Operation op,
1874     enum GAS_Solver_Status stat,
1875     enum GAS_Solver_Additional_Information add)
1876 {
1877   char *add_info;
1878   switch (add) {
1879     case GAS_INFO_NONE:
1880       add_info = "GAS_INFO_NONE";
1881       break;
1882     case GAS_INFO_FULL:
1883       add_info = "GAS_INFO_MLP_FULL";
1884       break;
1885     case GAS_INFO_UPDATED:
1886       add_info = "GAS_INFO_MLP_UPDATED";
1887       break;
1888     case GAS_INFO_PROP_ALL:
1889       add_info = "GAS_INFO_PROP_ALL";
1890       break;
1891     case GAS_INFO_PROP_SINGLE:
1892       add_info = "GAS_INFO_PROP_SINGLE";
1893       break;
1894     default:
1895       add_info = "INVALID";
1896       break;
1897   }
1898
1899   switch (op)
1900   {
1901     case GAS_OP_SOLVE_START:
1902       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1903           "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
1904           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1905       return;
1906     case GAS_OP_SOLVE_STOP:
1907       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1908           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
1909           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1910       return;
1911
1912     case GAS_OP_SOLVE_SETUP_START:
1913       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1914           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
1915           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1916       return;
1917
1918     case GAS_OP_SOLVE_SETUP_STOP:
1919       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1920           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
1921           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1922       return;
1923
1924     case GAS_OP_SOLVE_MLP_LP_START:
1925       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1926           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
1927           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1928       return;
1929     case GAS_OP_SOLVE_MLP_LP_STOP:
1930       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1931           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
1932           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1933       return;
1934
1935     case GAS_OP_SOLVE_MLP_MLP_START:
1936       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1937           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
1938           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1939       return;
1940     case GAS_OP_SOLVE_MLP_MLP_STOP:
1941       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1942           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
1943           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1944       return;
1945     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
1946       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1947           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
1948           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1949       return;
1950     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
1951       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1952           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
1953           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1954       return;
1955     default:
1956       break;
1957     }
1958 }
1959
1960 static void
1961 solver_bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1962 {
1963   if ( (0 == ntohl (address->assigned_bw_out.value__)) &&
1964        (0 == ntohl (address->assigned_bw_in.value__)) )
1965     return;
1966
1967   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1968               "Bandwidth changed addresses %s %p to %u Bps out / %u Bps in\n",
1969               GNUNET_i2s (&address->peer),
1970               address,
1971               (unsigned int) ntohl (address->assigned_bw_out.value__),
1972               (unsigned int) ntohl (address->assigned_bw_in.value__));
1973   /*if (GNUNET_YES == ph.bulk_running)
1974     GNUNET_break (0);*/
1975   return;
1976 }
1977
1978 const double *
1979 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
1980 {
1981   return GAS_normalization_get_preferences_by_peer (id);
1982 }
1983
1984
1985 const double *
1986 get_property_cb (void *cls, const struct ATS_Address *address)
1987 {
1988   return GAS_normalization_get_properties ((struct ATS_Address *) address);
1989 }
1990
1991 static void
1992 normalized_property_changed_cb (void *cls, struct ATS_Address *peer,
1993     uint32_t type, double prop_rel)
1994 {
1995   /* TODO */
1996 }
1997
1998
1999 struct GNUNET_ATS_TESTING_SolverHandle *
2000 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
2001 {
2002   struct GNUNET_ATS_TESTING_SolverHandle *sh;
2003   char * solver_str;
2004   unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
2005   unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
2006
2007   switch (type) {
2008     case GNUNET_ATS_SOLVER_PROPORTIONAL:
2009       solver_str = "proportional";
2010       break;
2011     case GNUNET_ATS_SOLVER_MLP:
2012       solver_str = "mlp";
2013       break;
2014     case GNUNET_ATS_SOLVER_RIL:
2015       solver_str = "ril";
2016       break;
2017     default:
2018       GNUNET_break (0);
2019       return NULL;
2020       break;
2021   }
2022
2023   sh = GNUNET_new (struct GNUNET_ATS_TESTING_SolverHandle);
2024   GNUNET_asprintf (&sh->plugin, "libgnunet_plugin_ats_%s", solver_str);
2025
2026   /* setup environment */
2027   sh->env.cfg = e->cfg;
2028   sh->env.stats = GNUNET_STATISTICS_create ("ats", e->cfg);
2029   sh->env.addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
2030   sh->env.bandwidth_changed_cb = &solver_bandwidth_changed_cb;
2031   sh->env.get_preferences = &get_preferences_cb;
2032   sh->env.get_property = &get_property_cb;
2033   sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
2034   sh->env.info_cb = &solver_info_cb;
2035   sh->env.info_cb_cls = NULL;
2036
2037   /* start normalization */
2038   GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL );
2039
2040   /* load quotas */
2041   if (GNUNET_ATS_NetworkTypeCount != GNUNET_ATS_solvers_load_quotas (e->cfg,
2042       quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount))
2043   {
2044     GNUNET_break(0);
2045     GNUNET_free (sh->plugin);
2046     GNUNET_free (sh);
2047     end_now ();
2048     return NULL;
2049   }
2050
2051   sh->solver = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
2052   if (NULL == sh->solver)
2053   {
2054     fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
2055     GNUNET_break(0);
2056     GNUNET_free (sh->plugin);
2057     GNUNET_free (sh);
2058     end_now ();
2059     return NULL;
2060   }
2061
2062   sh->addresses = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
2063
2064   return sh;
2065 }
2066
2067 static void
2068 done ()
2069 {
2070   /* Clean up experiment */
2071   GNUNET_ATS_solver_generate_preferences_stop_all ();
2072   GNUNET_ATS_solvers_experimentation_stop (e);
2073   e = NULL;
2074
2075   /* Shutdown */
2076   end_now();
2077
2078 }
2079
2080 static void
2081 experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,int success)
2082 {
2083   if (GNUNET_OK == success)
2084     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment done successful in %s\n",
2085         GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
2086   else
2087     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
2088
2089   /* Stop logging */
2090   // GNUNET_ATS_TEST_logging_stop (l);
2091
2092   /* Stop traffic generation */
2093   // GNUNET_ATS_TEST_generate_traffic_stop_all();
2094
2095   /* Stop all preference generations */
2096   GNUNET_ATS_solver_generate_preferences_stop_all ();
2097
2098   /*
2099   evaluate (duration);
2100   if (opt_log)
2101     GNUNET_ATS_TEST_logging_write_to_file(l, opt_exp_file, opt_plot);
2102
2103   if (NULL != l)
2104   {
2105     GNUNET_ATS_TEST_logging_stop (l);
2106     GNUNET_ATS_TEST_logging_clean_up (l);
2107     l = NULL;
2108   }
2109   */
2110   GNUNET_SCHEDULER_add_now (&done, NULL);
2111 }
2112
2113 static void
2114 episode_done_cb (struct Episode *ep)
2115 {
2116   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
2117 }
2118
2119
2120
2121 /**
2122  * Do shutdown
2123  */
2124 static void
2125 end_now ()
2126 {
2127   if (NULL != e)
2128   {
2129     GNUNET_ATS_solvers_experimentation_stop (e);
2130     e = NULL;
2131   }
2132   if (NULL != sh)
2133   {
2134     GNUNET_ATS_solvers_solver_stop (sh);
2135     sh = NULL;
2136   }
2137 }
2138
2139 static void
2140 run (void *cls, char * const *args, const char *cfgfile,
2141     const struct GNUNET_CONFIGURATION_Handle *cfg)
2142 {
2143   enum GNUNET_ATS_Solvers solver;
2144
2145   if (NULL == opt_exp_file)
2146   {
2147     fprintf (stderr, "No experiment given ...\n");
2148     res = 1;
2149     end_now ();
2150     return;
2151   }
2152
2153   if (NULL == opt_solver)
2154   {
2155     fprintf (stderr, "No solver given ...\n");
2156     res = 1;
2157     end_now ();
2158     return;
2159   }
2160
2161   if (0 == strcmp(opt_solver, "mlp"))
2162   {
2163     solver = GNUNET_ATS_SOLVER_MLP;
2164   }
2165   else if (0 == strcmp(opt_solver, "proportional"))
2166   {
2167     solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
2168   }
2169   else if (0 == strcmp(opt_solver, "ril"))
2170   {
2171     solver = GNUNET_ATS_SOLVER_RIL;
2172   }
2173   else
2174   {
2175     fprintf (stderr, "No solver given ...");
2176     res = 1;
2177     end_now ();
2178     return;
2179   }
2180
2181   /* load experiment */
2182   e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
2183   if (NULL == e)
2184   {
2185     fprintf (stderr, "Failed to load experiment ...\n");
2186     res = 1;
2187     end_now ();
2188     return;
2189   }
2190
2191
2192   /* load solver */
2193   sh = GNUNET_ATS_solvers_solver_start (solver);
2194   if (NULL == sh)
2195   {
2196     fprintf (stderr, "Failed to start solver ...\n");
2197     end_now ();
2198     res = 1;
2199     return;
2200   }
2201
2202   /* start logging */
2203
2204
2205   /* run experiment */
2206   GNUNET_ATS_solvers_experimentation_run (e, episode_done_cb,
2207       experiment_done_cb);
2208
2209   /* WAIT */
2210 }
2211
2212
2213 /**
2214  * Main function of the benchmark
2215  *
2216  * @param argc argument count
2217  * @param argv argument values
2218  */
2219 int
2220 main (int argc, char *argv[])
2221 {
2222   opt_exp_file = NULL;
2223   opt_solver = NULL;
2224   opt_log = GNUNET_NO;
2225   opt_plot = GNUNET_NO;
2226
2227   res = 0;
2228
2229   static struct GNUNET_GETOPT_CommandLineOption options[] =
2230   {
2231     { 's', "solver", NULL,
2232         gettext_noop ("solver to use"),
2233         1, &GNUNET_GETOPT_set_string, &opt_solver},
2234     {  'e', "experiment", NULL,
2235       gettext_noop ("experiment to use"),
2236       1, &GNUNET_GETOPT_set_string, &opt_exp_file},
2237     {  'e', "experiment", NULL,
2238       gettext_noop ("experiment to use"),
2239       1, &GNUNET_GETOPT_set_one, &opt_verbose},
2240     GNUNET_GETOPT_OPTION_END
2241   };
2242
2243   GNUNET_PROGRAM_run (argc, argv, argv[0], NULL, options, &run, argv[0]);
2244
2245   return res;
2246 }
2247 /* end of file ats-testing-experiment.c*/
2248