minor changes
[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 #define BIG_M_STRING "unlimited"
31
32 static struct Experiment *e;
33
34 static struct LoggingHandle *l;
35
36 static struct SolverHandle *sh;
37
38 static struct TestPeer *peer_head;
39 static struct TestPeer *peer_tail;
40
41 static double default_properties[GNUNET_ATS_PropertyCount];
42 static double default_preferences[GNUNET_ATS_PreferenceCount];
43
44 /**
45  * cmd option -e: experiment file
46  */
47 static char *opt_exp_file;
48
49 static char *opt_solver;
50
51 /**
52  * cmd option -l: enable logging
53  */
54 static int opt_log;
55
56 /**
57  * cmd option -p: enable plots
58  */
59 static int opt_save;
60
61 /**
62  * cmd option -v: verbose logs
63  */
64 static int opt_verbose;
65
66 /**
67  * cmd option -p: print logs
68  */
69 static int opt_print;
70
71 /**
72  * cmd option -d: disable normalization
73  */
74 static int opt_disable_normalization;
75
76 static int res;
77
78 static void
79 end_now ();
80
81
82 static char *
83 print_generator_type (enum GeneratorType g)
84 {
85   switch (g) {
86     case GNUNET_ATS_TEST_TG_CONSTANT:
87       return "CONSTANT";
88     case GNUNET_ATS_TEST_TG_LINEAR:
89       return "LINEAR";
90     case GNUNET_ATS_TEST_TG_RANDOM:
91       return "RANDOM";
92     case GNUNET_ATS_TEST_TG_SINUS:
93       return "SINUS";
94     default:
95       return "INVALID";
96       break;
97   }
98 }
99
100 struct AddressLookupCtx
101 {
102   struct ATS_Address *res;
103   char *plugin;
104   char *addr;
105 };
106
107
108 int find_address_it (void *cls,
109                      const struct GNUNET_PeerIdentity *key,
110                      void *value)
111 {
112   struct AddressLookupCtx *ctx = cls;
113   struct ATS_Address *addr = value;
114
115   if ( (0 == strcmp (ctx->plugin, addr->plugin)) &&
116        (0 == strcmp (ctx->addr, addr->addr)) )
117   {
118        ctx->res = addr;
119        return GNUNET_NO;
120   }
121   return GNUNET_YES;
122 }
123
124 static struct TestPeer *
125 find_peer_by_id (int id)
126 {
127   struct TestPeer *cur;
128   for (cur = peer_head; NULL != cur; cur = cur->next)
129     if (cur->id == id)
130       return cur;
131   return NULL;
132 }
133
134 static struct TestPeer *
135 find_peer_by_pid (const struct GNUNET_PeerIdentity *pid)
136 {
137   struct TestPeer *cur;
138   for (cur = peer_head; NULL != cur; cur = cur->next)
139     if (0 == memcmp (&cur->peer_id, pid, sizeof (struct GNUNET_PeerIdentity)))
140       return cur;
141   return NULL;
142 }
143
144 static struct TestAddress *
145 find_address_by_id (struct TestPeer *peer, int aid)
146 {
147   struct TestAddress *cur;
148   for (cur = peer->addr_head; NULL != cur; cur = cur->next)
149     if (cur->aid == aid)
150       return cur;
151   return NULL;
152 }
153
154
155 static struct TestAddress *
156 find_address_by_ats_address (struct TestPeer *p, const struct ATS_Address *addr)
157 {
158   struct TestAddress *cur;
159   for (cur = p->addr_head; NULL != cur; cur = cur->next)
160     if ((0 == strcmp(cur->ats_addr->plugin, addr->plugin)) &&
161          (cur->ats_addr->addr_len == addr->addr_len) &&
162         (0 == memcmp (cur->ats_addr->addr, addr->addr, addr->addr_len)))
163       return cur;
164   return NULL;
165 }
166
167
168 /**
169  * Logging
170  */
171
172 void
173 GNUNET_ATS_solver_logging_now (struct LoggingHandle *l)
174 {
175   struct LoggingTimeStep *lts;
176   struct TestPeer *cur;
177   struct TestAddress *cur_addr;
178   struct LoggingPeer *log_p;
179   struct LoggingAddress *log_a;
180   int c;
181
182   lts = GNUNET_new (struct LoggingTimeStep);
183   GNUNET_CONTAINER_DLL_insert_tail(l->head, l->tail, lts);
184   lts->timestamp = GNUNET_TIME_absolute_get();
185   if (NULL == lts->prev)
186     lts->delta = GNUNET_TIME_UNIT_ZERO;
187   else
188     lts->delta = GNUNET_TIME_absolute_get_duration(lts->prev->timestamp);
189
190   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging %llu, delta %llu\n",
191       lts->timestamp.abs_value_us, lts->delta.rel_value_us);
192
193
194   /* Store logging data here */
195   for (cur = peer_head; NULL != cur; cur = cur->next)
196   {
197     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging peer id %u\n", cur->peer_id);
198
199     log_p = GNUNET_new (struct LoggingPeer);
200     log_p->id = cur->id;
201     log_p->peer_id = cur->peer_id;
202     for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
203     {
204       log_p->pref_abs[c] = cur->pref_abs[c];
205       log_p->pref_norm[c] = cur->pref_norm[c];
206       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t %s = %.2f %.2f [abs/rel]\n",
207           GNUNET_ATS_print_preference_type(c),
208           log_p->pref_abs[c], log_p->pref_norm[c]);
209     }
210     GNUNET_CONTAINER_DLL_insert_tail(lts->head, lts->tail, log_p);
211
212     for (cur_addr = cur->addr_head; NULL != cur_addr; cur_addr = cur_addr->next)
213     {
214       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging peer id %u address %u\n", cur->peer_id, cur_addr->aid);
215       log_a = GNUNET_new (struct LoggingAddress);
216       log_a->aid = cur_addr->aid;
217       log_a->active = cur_addr->ats_addr->active;
218       log_a->network = cur_addr->network;
219       log_a->used = cur_addr->ats_addr->used;
220       log_a->assigned_bw_in = cur_addr->ats_addr->assigned_bw_in;
221       log_a->assigned_bw_out = cur_addr->ats_addr->assigned_bw_out;
222       for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
223       {
224         log_a->prop_abs[c] = cur_addr->prop_abs[c];
225         log_a->prop_norm[c] = cur_addr->prop_norm[c];
226         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t %s = %.2f %.2f [abs/rel]\n",
227             GNUNET_ATS_print_property_type(c),
228             log_a->prop_abs[c], log_a->prop_norm[c]);
229       }
230       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t Active = %i\n", log_a->active);
231       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t BW in = %llu\n", ntohl(log_a->assigned_bw_in.value__));
232       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t BW out = %llu\n", ntohl(log_a->assigned_bw_out.value__));
233
234       GNUNET_CONTAINER_DLL_insert_tail(log_p->addr_head, log_p->addr_tail, log_a);
235     }
236   }
237 }
238
239 static void
240 logging_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
241 {
242   struct LoggingHandle *l = cls;
243   l->logging_task = GNUNET_SCHEDULER_NO_TASK;
244
245   GNUNET_ATS_solver_logging_now (l);
246
247   l->logging_task = GNUNET_SCHEDULER_add_delayed (l->log_freq, &logging_task, l);
248
249 }
250
251 struct LoggingHandle *
252 GNUNET_ATS_solver_logging_start (struct GNUNET_TIME_Relative freq)
253 {
254   struct LoggingHandle *l;
255   l = GNUNET_new (struct LoggingHandle);
256
257   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start logging every  %s\n",
258       GNUNET_STRINGS_relative_time_to_string(freq, GNUNET_NO));
259   l->log_freq = freq;
260   l->logging_task = GNUNET_SCHEDULER_add_now (&logging_task, l);
261   return l;
262 }
263
264 void
265 GNUNET_ATS_solver_logging_stop (struct LoggingHandle *l)
266 {
267   if (GNUNET_SCHEDULER_NO_TASK != l->logging_task)
268     GNUNET_SCHEDULER_cancel (l->logging_task);
269
270   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stop logging\n");
271
272   l->logging_task = GNUNET_SCHEDULER_NO_TASK;
273 }
274
275 static struct LoggingFileHandle *
276 find_logging_file_handle (struct LoggingFileHandle *lf_head,
277     struct LoggingFileHandle *lf_tail,
278     int peer_id, int address_id)
279 {
280   struct LoggingFileHandle *res;
281
282   for (res = lf_head; NULL != res; res = res->next)
283     if ((res->pid == peer_id) && (res->pid == address_id))
284       return res;
285   return NULL;
286
287 }
288
289 void
290 GNUNET_ATS_solver_logging_write_to_disk (struct LoggingHandle *l, int add_time_stamp,
291     char *output_dir)
292 {
293   struct LoggingTimeStep *lts;
294   struct LoggingPeer *log_p;
295   struct LoggingAddress *log_a;
296   struct LoggingFileHandle *lf_head;
297   struct LoggingFileHandle *lf_tail;
298   struct LoggingFileHandle *cur;
299   struct LoggingFileHandle *next;
300   char * filename;
301   char * datastring;
302   char * propstring;
303   char * propstring_tmp;
304   char * prefstring;
305   char * prefstring_tmp;
306   int c;
307   int use_dir;
308
309   use_dir = GNUNET_NO;
310   if (NULL != output_dir)
311   {
312     if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (output_dir))
313     {
314       fprintf (stderr, "Failed to create directory `%s'\n", output_dir);
315       return;
316     }
317     else
318     {
319       fprintf (stderr, "Created directory `%s'\n", output_dir);
320       use_dir = GNUNET_YES;
321     }
322   }
323
324   lf_head = NULL;
325   lf_tail = NULL;
326
327   for (lts = l->head; NULL != lts; lts = lts->next)
328   {
329
330     fprintf (stderr, "Writing log step %llu\n",
331         (long long unsigned int) lts->timestamp.abs_value_us);
332
333     for (log_p = lts->head; NULL != log_p; log_p = log_p->next)
334     {
335       for (log_a = log_p->addr_head; NULL != log_a; log_a = log_a->next)
336       {
337
338         cur = find_logging_file_handle (lf_head, lf_tail, log_p->id,
339             log_a->aid);
340         if (NULL == cur)
341         {
342           cur = GNUNET_new (struct LoggingFileHandle);
343           cur->aid = log_a->aid;
344           cur->pid = log_p->id;
345
346           if (GNUNET_YES == add_time_stamp)
347             GNUNET_asprintf (&filename, "%s%s%s_%s_%u_%u_%llu.log",
348                 (GNUNET_YES == use_dir) ? output_dir : "",
349                 (GNUNET_YES == use_dir) ? DIR_SEPARATOR_STR : "",
350                 e->log_prefix,
351                 opt_solver,
352                 cur->aid,
353                 cur->pid,
354                 l->head->timestamp.abs_value_us);
355           else
356             GNUNET_asprintf (&filename, "%s%s%s_%s_%u_%u.log",
357                 (GNUNET_YES == use_dir) ? output_dir : "",
358                 (GNUNET_YES == use_dir) ? DIR_SEPARATOR_STR : "",
359                 e->log_prefix,
360                 opt_solver,
361                 cur->aid,
362                 cur->pid);
363
364           fprintf (stderr, "Add writing log data for %i %i to file `%s'\n",
365               cur->pid, cur->aid, filename);
366
367
368           cur->f_hd = GNUNET_DISK_file_open (filename,
369               GNUNET_DISK_OPEN_READWRITE |
370               GNUNET_DISK_OPEN_CREATE |
371               GNUNET_DISK_OPEN_TRUNCATE,
372               GNUNET_DISK_PERM_USER_READ |
373               GNUNET_DISK_PERM_USER_WRITE |
374               GNUNET_DISK_PERM_GROUP_READ |
375               GNUNET_DISK_PERM_OTHER_READ);
376           if (NULL == cur->f_hd)
377           {
378             fprintf (stderr, "Cannot open `%s' to write log data!\n", filename);
379             GNUNET_free (filename);
380             goto cleanup;
381           }
382           GNUNET_free (filename);
383           GNUNET_CONTAINER_DLL_insert (lf_head, lf_tail, cur);
384
385           GNUNET_asprintf(&datastring,"#timestamp_abs; ; addr net; addr_active; bw in; bw out; " \
386               "UTILIZATION_UP [abs/rel]; UTILIZATION_UP; UTILIZATION_DOWN; UTILIZATION_DOWN; " \
387               "UTILIZATION_PAYLOAD_UP; UTILIZATION_PAYLOAD_UP; UTILIZATION_PAYLOAD_DOWN; UTILIZATION_PAYLOAD_DOWN;"\
388               "DELAY; DELAY; " \
389               "DISTANCE ;DISTANCE ; COST_WAN; COST_WAN; COST_LAN; COST_LAN; " \
390               "COST_WLAN; COST_WLAN; PREF BW abs; PREF BW rel; PREF LATENCY abs; PREF LATENCY rel;\n");
391           GNUNET_DISK_file_write (cur->f_hd, datastring, strlen(datastring));
392           GNUNET_free (datastring);
393
394         }
395
396         prefstring = GNUNET_strdup("");
397         for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
398         {
399           /*
400           fprintf(stderr,"\t %s = %.2f %.2f [abs/rel]\n",
401               GNUNET_ATS_print_preference_type(c),
402               log_p->pref_abs[c], log_p->pref_norm[c]);
403            */
404           GNUNET_asprintf(&prefstring_tmp,"%s;%.3f;%.3f",
405               prefstring, log_p->pref_abs[c], log_p->pref_norm[c]);
406
407
408           GNUNET_free (prefstring);
409           prefstring = GNUNET_strdup(prefstring_tmp);
410           GNUNET_free (prefstring_tmp);
411         }
412
413
414         propstring = GNUNET_strdup("");
415         for (c = 1; c < GNUNET_ATS_PropertyCount; c++)
416         {
417           if (GNUNET_ATS_NETWORK_TYPE == c)
418             continue;
419           /*
420           fprintf(stderr, "\t %s = %.2f %.2f [abs/rel]\n",
421               GNUNET_ATS_print_property_type(c),
422               log_a->prop_abs[c], log_a->prop_norm[c]);*/
423           GNUNET_asprintf(&propstring_tmp,"%s;%.3f;%.3f",
424               propstring, log_a->prop_abs[c], log_a->prop_norm[c]);
425           GNUNET_free (propstring);
426           propstring = GNUNET_strdup(propstring_tmp);
427           GNUNET_free (propstring_tmp);
428         }
429
430         GNUNET_asprintf(&datastring,"%llu;%u;%i;%u;%u;%s;%s\n",
431             GNUNET_TIME_absolute_get_difference(l->head->timestamp, lts->timestamp).rel_value_us / 1000,
432             log_a->network,
433             log_a->active,
434             ntohl (log_a->assigned_bw_in.value__),
435             ntohl (log_a->assigned_bw_out.value__),
436             propstring, prefstring);
437
438         GNUNET_DISK_file_write (cur->f_hd, datastring, strlen(datastring));
439         GNUNET_free (datastring);
440         GNUNET_free (prefstring);
441         GNUNET_free (propstring);
442       }
443     }
444   }
445
446 cleanup:
447   next = lf_head;
448   for (cur = next; NULL != cur; cur = next)
449   {
450     next = cur->next;
451     GNUNET_CONTAINER_DLL_remove (lf_head, lf_tail, cur);
452     if (NULL != cur->f_hd)
453       GNUNET_DISK_file_close (cur->f_hd);
454     GNUNET_free (cur);
455   }
456
457 }
458
459 void
460 GNUNET_ATS_solver_logging_eval (struct LoggingHandle *l)
461 {
462   struct LoggingTimeStep *lts;
463   struct LoggingPeer *log_p;
464   struct LoggingAddress *log_a;
465   int c;
466
467   for (lts = l->head; NULL != lts; lts = lts->next)
468   {
469     fprintf (stderr, "Log step %llu %llu: \n",
470         (long long unsigned int) lts->timestamp.abs_value_us,
471         (long long unsigned int) lts->delta.rel_value_us);
472
473     for (log_p = lts->head; NULL != log_p; log_p = log_p->next)
474     {
475       fprintf (stderr,"\tLogging peer pid %u\n", log_p->id);
476       for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
477       {
478         fprintf(stderr,"\t %s = %.2f %.2f [abs/rel]\n",
479             GNUNET_ATS_print_preference_type(c),
480             log_p->pref_abs[c], log_p->pref_norm[c]);
481       }
482
483       for (log_a = log_p->addr_head; NULL != log_a; log_a = log_a->next)
484       {
485         fprintf (stderr, "\tPeer pid %u address %u: %u %u %u\n",
486             log_p->id, log_a->aid, log_a->active,
487             ntohl(log_a->assigned_bw_in.value__),
488             ntohl(log_a->assigned_bw_out.value__));
489
490         for (c = 1; c < GNUNET_ATS_PropertyCount; c++)
491         {
492           if (GNUNET_ATS_NETWORK_TYPE == c)
493             continue;
494           fprintf(stderr, "\t %s = %.2f %.2f [abs/rel]\n",
495               GNUNET_ATS_print_property_type(c),
496               log_a->prop_abs[c], log_a->prop_norm[c]);
497         }
498       }
499     }
500   }
501 }
502
503 void
504 GNUNET_ATS_solver_logging_free (struct LoggingHandle *l)
505 {
506   struct LoggingTimeStep *lts_cur;
507   struct LoggingTimeStep *lts_next;
508   struct LoggingPeer *log_p_cur;
509   struct LoggingPeer *log_p_next;
510   struct LoggingAddress *log_a_cur;
511   struct LoggingAddress *log_a_next;
512
513   if (GNUNET_SCHEDULER_NO_TASK != l->logging_task)
514     GNUNET_SCHEDULER_cancel (l->logging_task);
515   l->logging_task = GNUNET_SCHEDULER_NO_TASK;
516
517   lts_next = l->head;
518   while (NULL != (lts_cur = lts_next))
519   {
520     lts_next = lts_cur->next;
521
522     log_p_next = lts_cur->head;
523     while (NULL != (log_p_cur = log_p_next))
524     {
525       log_p_next = log_p_cur->next;
526
527       log_a_next = log_p_cur->addr_head;
528       while (NULL != (log_a_cur = log_a_next))
529       {
530         log_a_next = log_a_cur->next;
531
532         GNUNET_CONTAINER_DLL_remove (log_p_cur->addr_head, log_p_cur->addr_tail, log_a_cur);
533         GNUNET_free (log_a_cur);
534       }
535
536       GNUNET_CONTAINER_DLL_remove (lts_cur->head, lts_cur->tail, log_p_cur);
537       GNUNET_free (log_p_cur);
538     }
539
540     GNUNET_CONTAINER_DLL_remove (l->head, l->tail, lts_cur);
541     GNUNET_free (lts_cur);
542   }
543
544   GNUNET_free (l);
545 }
546
547 /**
548  * Property Generators
549  */
550
551 static struct PropertyGenerator *prop_gen_head;
552 static struct PropertyGenerator *prop_gen_tail;
553
554 static double
555 get_property (struct PropertyGenerator *pg)
556 {
557   struct GNUNET_TIME_Relative time_delta;
558   double delta_value;
559   double pref_value;
560
561   /* Calculate the current preference value */
562   switch (pg->type) {
563     case GNUNET_ATS_TEST_TG_CONSTANT:
564       pref_value = pg->base_value;
565       break;
566     case GNUNET_ATS_TEST_TG_LINEAR:
567       time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
568       /* Calculate point of time in the current period */
569       time_delta.rel_value_us = time_delta.rel_value_us %
570           pg->duration_period.rel_value_us;
571       delta_value = ((double) time_delta.rel_value_us  /
572           pg->duration_period.rel_value_us) * (pg->max_value - pg->base_value);
573       if ((pg->max_value < pg->base_value) &&
574           ((pg->max_value - pg->base_value) > pg->base_value))
575       {
576         /* This will cause an underflow */
577         GNUNET_break (0);
578       }
579       pref_value = pg->base_value + delta_value;
580       break;
581     case GNUNET_ATS_TEST_TG_RANDOM:
582       delta_value =  (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
583           10000 * (pg->max_value - pg->base_value)) / 10000;
584       pref_value = pg->base_value + delta_value;
585       break;
586     case GNUNET_ATS_TEST_TG_SINUS:
587       time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
588       /* Calculate point of time in the current period */
589       time_delta.rel_value_us = time_delta.rel_value_us %
590           pg->duration_period.rel_value_us;
591       if ((pg->max_value - pg->base_value) > pg->base_value)
592       {
593         /* This will cause an underflow for second half of sinus period,
594          * will be detected in general when experiments are loaded */
595         GNUNET_break (0);
596       }
597       delta_value = (pg->max_value - pg->base_value) *
598           sin ( (2 * M_PI) / ((double) pg->duration_period.rel_value_us) *
599               time_delta.rel_value_us);
600       pref_value = pg->base_value + delta_value;
601       break;
602     default:
603       pref_value = 0.0;
604       break;
605   }
606   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Current property value is %f\n",
607       pref_value);
608   return pref_value;
609 }
610
611
612 static void
613 set_prop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
614 {
615   struct PropertyGenerator *pg = cls;
616   struct TestPeer *p;
617   struct TestAddress *a;
618   double prop_value;
619   struct GNUNET_ATS_Information atsi;
620
621   pg->set_task = GNUNET_SCHEDULER_NO_TASK;
622
623   if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains_value (sh->addresses,
624       &pg->test_peer->peer_id, pg->test_address->ats_addr))
625   {
626     GNUNET_break (0);
627     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
628         "Setting property generation for unknown address [%u:%u]\n",
629         pg->peer, pg->address_id);
630     return;
631   }
632   if (NULL == (p = find_peer_by_id (pg->peer)))
633   {
634     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
635         "Setting property generation for unknown peer %u\n",
636         pg->peer);
637   }
638   if (NULL == (a = find_address_by_id (p, pg->address_id)))
639   {
640     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
641         "Setting property generation for unknown peer %u\n",
642         pg->peer);
643   }
644
645   prop_value = get_property (pg);
646   a->prop_abs[pg->ats_property] = prop_value;
647
648   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
649       "Setting property for peer [%u] address [%u] for %s to %f\n",
650       pg->peer, pg->address_id,
651       GNUNET_ATS_print_property_type (pg->ats_property), prop_value);
652
653   atsi.type = htonl (pg->ats_property);
654   atsi.value = htonl ((uint32_t) prop_value);
655
656   /* set performance here! */
657   sh->env.sf.s_bulk_start (sh->solver);
658   if (GNUNET_YES == opt_disable_normalization)
659   {
660     a->prop_abs[pg->ats_property] = prop_value;
661     a->prop_norm[pg->ats_property] = prop_value;
662     sh->env.sf.s_address_update_property (sh->solver, a->ats_addr,
663         pg->ats_property, prop_value, prop_value);
664   }
665   else
666     GAS_normalization_normalize_property (sh->addresses,
667       pg->test_address->ats_addr, &atsi, 1);
668   sh->env.sf.s_bulk_stop (sh->solver);
669
670   pg->set_task = GNUNET_SCHEDULER_add_delayed (pg->frequency,
671       &set_prop_task, pg);
672
673 }
674
675 /**
676  * Set ats_property to 0 to find all pgs
677  */
678
679 static struct PropertyGenerator *
680 find_prop_gen (unsigned int peer, unsigned int address,
681     uint32_t ats_property)
682 {
683   struct PropertyGenerator *cur;
684   for (cur = prop_gen_head; NULL != cur; cur = cur->next)
685     if ((cur->peer == peer) && (cur->address_id == address))
686     {
687       if ((cur->ats_property == ats_property) || (0 == ats_property))
688         return cur;
689     }
690   return NULL;
691 }
692
693 void
694 GNUNET_ATS_solver_generate_property_stop (struct PropertyGenerator *pg)
695 {
696   GNUNET_CONTAINER_DLL_remove (prop_gen_head, prop_gen_tail, pg);
697
698   if (GNUNET_SCHEDULER_NO_TASK != pg->set_task)
699   {
700     GNUNET_SCHEDULER_cancel (pg->set_task);
701     pg->set_task = GNUNET_SCHEDULER_NO_TASK;
702   }
703   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
704       "Removing old up preference generator peer [%u] address [%u] `%s'\n",
705       pg->peer, pg->address_id,
706       GNUNET_ATS_print_property_type(pg->ats_property));
707
708   GNUNET_free (pg);
709 }
710
711
712 /**
713  * Generate between the source master and the partner and set property with a
714  * value depending on the generator.
715  *
716  * @param peer source
717  * @param address_id partner
718  * @param test_peer the peer
719  * @param test_address the address
720  * @param type type of generator
721  * @param base_value base value
722  * @param value_rate maximum value
723  * @param period duration of a period of generation (~ 1/frequency)
724  * @param frequency how long to generate property
725  * @param ats_property ATS property to generate
726  * @return the property generator
727  */
728 struct PropertyGenerator *
729 GNUNET_ATS_solver_generate_property_start (unsigned int peer,
730     unsigned int address_id,
731     struct TestPeer *test_peer,
732     struct TestAddress *test_address,
733     enum GeneratorType type,
734     long int base_value,
735     long int value_rate,
736     struct GNUNET_TIME_Relative period,
737     struct GNUNET_TIME_Relative frequency,
738     uint32_t ats_property)
739 {
740   struct PropertyGenerator *pg;
741
742   pg = GNUNET_new (struct PropertyGenerator);
743   GNUNET_CONTAINER_DLL_insert (prop_gen_head, prop_gen_tail, pg);
744   pg->type = type;
745   pg->peer = peer;
746   pg->test_address = test_address;
747   pg->test_peer = test_peer;
748   pg->address_id = address_id;
749   pg->ats_property = ats_property;
750   pg->base_value = base_value;
751   pg->max_value = value_rate;
752   pg->duration_period = period;
753   pg->frequency = frequency;
754   pg->time_start = GNUNET_TIME_absolute_get();
755
756   switch (type) {
757     case GNUNET_ATS_TEST_TG_CONSTANT:
758       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
759           "Setting up %s property generator peer [%u] address [%u] `%s'"\
760           "max %u Bips\n",
761           print_generator_type(type), pg->peer, pg->address_id,
762           GNUNET_ATS_print_property_type (ats_property),
763           base_value);
764       break;
765     case GNUNET_ATS_TEST_TG_LINEAR:
766       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
767           "Setting up %s property generator peer [%u] address [%u] `%s' " \
768           "min %u Bips max %u Bips\n",
769           print_generator_type(type), pg->peer, pg->address_id,
770           GNUNET_ATS_print_property_type(ats_property),
771           base_value, value_rate);
772       break;
773     case GNUNET_ATS_TEST_TG_SINUS:
774       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
775           "Setting up %s property generator peer [%u] address [%u] `%s' "\
776           "baserate %u Bips, amplitude %u Bps\n",
777           print_generator_type(type), pg->peer, pg->address_id,
778           GNUNET_ATS_print_property_type(ats_property),
779           base_value, value_rate);
780       break;
781     case GNUNET_ATS_TEST_TG_RANDOM:
782       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
783           "Setting up %s property generator peer [%u] address [%u] `%s' "\
784           "min %u Bips max %u Bps\n",
785           print_generator_type(type), pg->peer, pg->address_id,
786           GNUNET_ATS_print_property_type(ats_property),
787           base_value, value_rate);
788       break;
789     default:
790       break;
791   }
792
793   pg->set_task = GNUNET_SCHEDULER_add_now (&set_prop_task, pg);
794   return pg;
795 }
796
797
798
799 /**
800  * Stop all preferences generators
801  */
802 void
803 GNUNET_ATS_solver_generate_property_stop_all ()
804 {
805   struct PropertyGenerator *cur;
806   struct PropertyGenerator *next;
807   next = prop_gen_head;
808   for (cur = next; NULL != cur; cur = next)
809   {
810       next = cur->next;
811       GNUNET_ATS_solver_generate_property_stop (cur);
812   }
813 }
814
815
816 /**
817  * Preference Generators
818  */
819
820 static struct PreferenceGenerator *pref_gen_head;
821 static struct PreferenceGenerator *pref_gen_tail;
822
823 static double
824 get_preference (struct PreferenceGenerator *pg)
825 {
826   struct GNUNET_TIME_Relative time_delta;
827   double delta_value;
828   double pref_value;
829
830   /* Calculate the current preference value */
831   switch (pg->type) {
832     case GNUNET_ATS_TEST_TG_CONSTANT:
833       pref_value = pg->base_value;
834       break;
835     case GNUNET_ATS_TEST_TG_LINEAR:
836       time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
837       /* Calculate point of time in the current period */
838       time_delta.rel_value_us = time_delta.rel_value_us %
839           pg->duration_period.rel_value_us;
840       delta_value = ((double) time_delta.rel_value_us  /
841           pg->duration_period.rel_value_us) * (pg->max_value - pg->base_value);
842       if ((pg->max_value < pg->base_value) &&
843           ((pg->max_value - pg->base_value) > pg->base_value))
844       {
845         /* This will cause an underflow */
846         GNUNET_break (0);
847       }
848       pref_value = pg->base_value + delta_value;
849       break;
850     case GNUNET_ATS_TEST_TG_RANDOM:
851       delta_value =  (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
852           10000 * (pg->max_value - pg->base_value)) / 10000;
853       pref_value = pg->base_value + delta_value;
854       break;
855     case GNUNET_ATS_TEST_TG_SINUS:
856       time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
857       /* Calculate point of time in the current period */
858       time_delta.rel_value_us = time_delta.rel_value_us %
859           pg->duration_period.rel_value_us;
860       if ((pg->max_value - pg->base_value) > pg->base_value)
861       {
862         /* This will cause an underflow for second half of sinus period,
863          * will be detected in general when experiments are loaded */
864         GNUNET_break (0);
865       }
866       delta_value = (pg->max_value - pg->base_value) *
867           sin ( (2 * M_PI) / ((double) pg->duration_period.rel_value_us) *
868               time_delta.rel_value_us);
869       pref_value = pg->base_value + delta_value;
870       break;
871     default:
872       pref_value = 0.0;
873       break;
874   }
875   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Current preference value is %f\n",
876       pref_value);
877   return pref_value;
878 }
879
880
881 static void
882 set_pref_task (void *cls,
883                     const struct GNUNET_SCHEDULER_TaskContext *tc)
884 {
885   struct PreferenceGenerator *pg = cls;
886   struct TestPeer *p;
887   double pref_value;
888   pg->set_task = GNUNET_SCHEDULER_NO_TASK;
889
890   if (NULL == (p = find_peer_by_id (pg->peer)))
891   {
892     GNUNET_break (0);
893     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
894         "Setting preference for unknown peer %u\n", pg->peer);
895     return;
896   }
897
898   pref_value = get_preference (pg);
899   p->pref_abs[pg->kind] = pref_value;
900
901   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
902       "Setting preference for peer [%u] for client %p pref %s to %f\n",
903       pg->peer, NULL + (pg->client_id),
904       GNUNET_ATS_print_preference_type (pg->kind), pref_value);
905
906   sh->env.sf.s_bulk_start (sh->solver);
907   if (GNUNET_YES == opt_disable_normalization)
908   {
909     p->pref_abs[pg->kind] = pref_value;
910     p->pref_norm[pg->kind] = pref_value;
911     sh->env.sf.s_pref (sh->solver, &p->peer_id, pg->kind, pref_value);
912   }
913   else
914     GAS_normalization_normalize_preference (NULL + (pg->client_id),
915         &p->peer_id, pg->kind, pref_value);
916   sh->env.sf.s_bulk_stop (sh->solver);
917
918   switch (pg->kind) {
919     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
920       //p->pref_bandwidth = pref_value;
921       break;
922     case GNUNET_ATS_PREFERENCE_LATENCY:
923       //p->pref_delay = pref_value;
924       break;
925     default:
926       break;
927   }
928
929   pg->set_task = GNUNET_SCHEDULER_add_delayed (pg->frequency,
930       set_pref_task, pg);
931
932 }
933
934 static struct PreferenceGenerator *
935 find_pref_gen (unsigned int peer, enum GNUNET_ATS_PreferenceKind kind)
936 {
937   struct PreferenceGenerator *cur;
938   for (cur = pref_gen_head; NULL != cur; cur = cur->next)
939     if (cur->peer == peer)
940     {
941       if ((cur->kind == kind) || (GNUNET_ATS_PREFERENCE_END == kind))
942         return cur;
943     }
944   return NULL;
945 }
946
947 void
948 GNUNET_ATS_solver_generate_preferences_stop (struct PreferenceGenerator *pg)
949 {
950   GNUNET_CONTAINER_DLL_remove (pref_gen_head, pref_gen_tail, pg);
951
952   if (GNUNET_SCHEDULER_NO_TASK != pg->set_task)
953   {
954     GNUNET_SCHEDULER_cancel (pg->set_task);
955     pg->set_task = GNUNET_SCHEDULER_NO_TASK;
956   }
957   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
958       "Removing old up preference generator peer [%u] `%s'\n",
959       pg->peer, GNUNET_ATS_print_preference_type(pg->kind));
960
961   GNUNET_free (pg);
962 }
963
964
965 /**
966  * Generate between the source master and the partner and set property with a
967  * value depending on the generator.
968  *
969  * @param peer source
970  * @param address_id partner
971  * @param client_id the client
972  * @param type type of generator
973  * @param base_value base value
974  * @param value_rate maximum value
975  * @param period duration of a period of generation (~ 1/frequency)
976  * @param frequency how long to generate property
977  * @param kind ATS preference to generate
978  * @return the preference generator
979  */
980 struct PreferenceGenerator *
981 GNUNET_ATS_solver_generate_preferences_start (unsigned int peer,
982     unsigned int address_id,
983     unsigned int client_id,
984     enum GeneratorType type,
985     long int base_value,
986     long int value_rate,
987     struct GNUNET_TIME_Relative period,
988     struct GNUNET_TIME_Relative frequency,
989     enum GNUNET_ATS_PreferenceKind kind)
990 {
991   struct PreferenceGenerator *pg;
992
993   pg = GNUNET_new (struct PreferenceGenerator);
994   GNUNET_CONTAINER_DLL_insert (pref_gen_head, pref_gen_tail, pg);
995   pg->type = type;
996   pg->peer = peer;
997   pg->client_id = client_id;
998   pg->kind = kind;
999   pg->base_value = base_value;
1000   pg->max_value = value_rate;
1001   pg->duration_period = period;
1002   pg->frequency = frequency;
1003   pg->time_start = GNUNET_TIME_absolute_get();
1004
1005   switch (type) {
1006     case GNUNET_ATS_TEST_TG_CONSTANT:
1007       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1008           "Setting up %s preference generator peer [%u] `%s' max %u Bips\n",
1009           print_generator_type (type), pg->peer,
1010           GNUNET_ATS_print_preference_type(kind),
1011           base_value);
1012       break;
1013     case GNUNET_ATS_TEST_TG_LINEAR:
1014       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1015           "Setting up %s preference generator peer [%u] `%s' min %u Bips max %u Bips\n",
1016           print_generator_type (type), pg->peer, GNUNET_ATS_print_preference_type(kind),
1017           base_value, value_rate);
1018       break;
1019     case GNUNET_ATS_TEST_TG_SINUS:
1020       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1021           "Setting up %s preference generator peer [%u] `%s' baserate %u Bips, amplitude %u Bps\n",
1022           print_generator_type (type), pg->peer, GNUNET_ATS_print_preference_type(kind),
1023           base_value, value_rate);
1024       break;
1025     case GNUNET_ATS_TEST_TG_RANDOM:
1026       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1027           "Setting up %s preference generator peer [%u] `%s' min %u Bips max %u Bps\n",
1028           print_generator_type (type), pg->peer, GNUNET_ATS_print_preference_type(kind),
1029           base_value, value_rate);
1030       break;
1031     default:
1032       break;
1033   }
1034
1035   pg->set_task = GNUNET_SCHEDULER_add_now (&set_pref_task, pg);
1036   return pg;
1037 }
1038
1039
1040
1041 /**
1042  * Stop all preferences generators
1043  */
1044 void
1045 GNUNET_ATS_solver_generate_preferences_stop_all ()
1046 {
1047   struct PreferenceGenerator *cur;
1048   struct PreferenceGenerator *next;
1049   next = pref_gen_head;
1050   for (cur = next; NULL != cur; cur = next)
1051   {
1052       next = cur->next;
1053       GNUNET_ATS_solver_generate_preferences_stop(cur);
1054   }
1055 }
1056
1057
1058
1059 /**
1060  * Experiments
1061  */
1062
1063 const char *
1064 print_op (enum OperationType op)
1065 {
1066   switch (op) {
1067     case SOLVER_OP_ADD_ADDRESS:
1068       return "ADD_ADDRESS";
1069     case SOLVER_OP_DEL_ADDRESS:
1070       return "DEL_ADDRESS";
1071     case SOLVER_OP_START_SET_PREFERENCE:
1072       return "START_SET_PREFERENCE";
1073     case SOLVER_OP_STOP_SET_PREFERENCE:
1074       return "STOP_STOP_PREFERENCE";
1075     case SOLVER_OP_START_SET_PROPERTY:
1076       return "START_SET_PROPERTY";
1077     case SOLVER_OP_STOP_SET_PROPERTY:
1078       return "STOP_SET_PROPERTY";
1079     case SOLVER_OP_START_REQUEST:
1080       return "START_REQUEST";
1081     case SOLVER_OP_STOP_REQUEST:
1082       return "STOP_REQUEST";
1083     default:
1084       break;
1085   }
1086   return "";
1087 }
1088
1089 static struct Experiment *
1090 create_experiment ()
1091 {
1092   struct Experiment *e;
1093   e = GNUNET_new (struct Experiment);
1094   e->name = NULL;
1095   e->start = NULL;
1096   e->total_duration = GNUNET_TIME_UNIT_ZERO;
1097   return e;
1098 }
1099
1100 static void
1101 free_experiment (struct Experiment *e)
1102 {
1103   struct Episode *cur;
1104   struct Episode *next;
1105   struct GNUNET_ATS_TEST_Operation *cur_o;
1106   struct GNUNET_ATS_TEST_Operation *next_o;
1107
1108   next = e->start;
1109   for (cur = next; NULL != cur; cur = next)
1110   {
1111     next = cur->next;
1112
1113     next_o = cur->head;
1114     for (cur_o = next_o; NULL != cur_o; cur_o = next_o)
1115     {
1116       next_o = cur_o->next;
1117       GNUNET_free_non_null (cur_o->address);
1118       GNUNET_free_non_null (cur_o->plugin);
1119       GNUNET_free (cur_o);
1120     }
1121     GNUNET_free (cur);
1122   }
1123
1124   GNUNET_free_non_null (e->name);
1125   GNUNET_free_non_null (e->log_prefix);
1126   GNUNET_free_non_null (e->log_output_dir);
1127   GNUNET_free_non_null (e->cfg_file);
1128   GNUNET_free (e);
1129 }
1130
1131
1132 static int
1133 load_op_add_address (struct GNUNET_ATS_TEST_Operation *o,
1134     struct Episode *e,
1135     int op_counter,
1136     char *sec_name,
1137     const struct GNUNET_CONFIGURATION_Handle *cfg)
1138 {
1139   char *op_name;
1140   char *op_network;
1141
1142   /* peer pid */
1143   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1144   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1145       sec_name, op_name, &o->peer_id))
1146   {
1147     fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
1148         op_counter, "ADD_ADDRESS", op_name);
1149     GNUNET_free (op_name);
1150     return GNUNET_SYSERR;
1151   }
1152   GNUNET_free (op_name);
1153
1154   /* address pid */
1155   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1156   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1157       sec_name, op_name, &o->address_id))
1158   {
1159     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1160         op_counter, "ADD_ADDRESS", op_name);
1161     GNUNET_free (op_name);
1162     return GNUNET_SYSERR;
1163   }
1164   GNUNET_free (op_name);
1165
1166   /* plugin */
1167   GNUNET_asprintf(&op_name, "op-%u-plugin", op_counter);
1168   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1169       sec_name, op_name, &o->plugin))
1170   {
1171     fprintf (stderr, "Missing plugin in operation %u `%s' in episode `%s'\n",
1172         op_counter, "ADD_ADDRESS", op_name);
1173     GNUNET_free (op_name);
1174     return GNUNET_SYSERR;
1175   }
1176   GNUNET_free (op_name);
1177
1178   /* address  */
1179   GNUNET_asprintf(&op_name, "op-%u-address", op_counter);
1180   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1181       sec_name, op_name, &o->address))
1182   {
1183     fprintf (stderr, "Missing address in operation %u `%s' in episode `%s'\n",
1184         op_counter, "ADD_ADDRESS", op_name);
1185     GNUNET_free (op_name);
1186     return GNUNET_SYSERR;
1187   }
1188   GNUNET_free (op_name);
1189
1190   /* session */
1191   GNUNET_asprintf(&op_name, "op-%u-address-session", op_counter);
1192   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1193       sec_name, op_name, &o->address_session))
1194   {
1195     fprintf (stderr, "Missing address-session in operation %u `%s' in episode `%s'\n",
1196         op_counter, "ADD_ADDRESS", op_name);
1197     GNUNET_free (op_name);
1198     return GNUNET_SYSERR;
1199   }
1200   GNUNET_free (op_name);
1201
1202   /* network */
1203   GNUNET_asprintf(&op_name, "op-%u-address-network", op_counter);
1204   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1205       sec_name, op_name, &op_network))
1206   {
1207     fprintf (stderr, "Missing address-network in operation %u `%s' in episode `%s'\n",
1208         op_counter, "ADD_ADDRESS", op_name);
1209     GNUNET_free (op_name);
1210     return GNUNET_SYSERR;
1211   }
1212   else
1213   {
1214     GNUNET_STRINGS_utf8_toupper (op_network,op_network);
1215     if (0 == strcmp(op_network, "UNSPECIFIED"))
1216     {
1217       o->address_network = GNUNET_ATS_NET_UNSPECIFIED;
1218     }
1219     else if (0 == strcmp(op_network, "LOOPBACK"))
1220     {
1221       o->address_network = GNUNET_ATS_NET_LOOPBACK;
1222     }
1223     else if (0 == strcmp(op_network, "LAN"))
1224     {
1225       o->address_network = GNUNET_ATS_NET_LAN;
1226     }
1227     else if (0 == strcmp(op_network, "WAN"))
1228     {
1229       o->address_network = GNUNET_ATS_NET_WAN;
1230     }
1231     else if (0 == strcmp(op_network, "WLAN"))
1232     {
1233       o->address_network = GNUNET_ATS_NET_WLAN;
1234     }
1235     else if (0 == strcmp(op_network, "BT"))
1236     {
1237       o->address_network = GNUNET_ATS_NET_BT;
1238     }
1239     else
1240     {
1241       fprintf (stderr, "Invalid address-network in operation %u `%s' in episode `%s': `%s'\n",
1242           op_counter, "ADD_ADDRESS", op_name, op_network);
1243       GNUNET_free (op_network);
1244       GNUNET_free (op_name);
1245       return GNUNET_SYSERR;
1246     }
1247   }
1248   GNUNET_free (op_network);
1249   GNUNET_free (op_name);
1250
1251   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1252       "Found operation %s: [%llu:%llu] address `%s' plugin `%s' \n",
1253       "ADD_ADDRESS", o->peer_id, o->address_id, o->address, o->plugin);
1254
1255   return GNUNET_OK;
1256 }
1257
1258 static int
1259 load_op_del_address (struct GNUNET_ATS_TEST_Operation *o,
1260     struct Episode *e,
1261     int op_counter,
1262     char *sec_name,
1263     const struct GNUNET_CONFIGURATION_Handle *cfg)
1264 {
1265   char *op_name;
1266   char *op_network;
1267
1268   /* peer pid */
1269   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1270   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1271       sec_name, op_name, &o->peer_id))
1272   {
1273     fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
1274         op_counter, "DEL_ADDRESS", op_name);
1275     GNUNET_free (op_name);
1276     return GNUNET_SYSERR;
1277   }
1278   GNUNET_free (op_name);
1279
1280   /* address pid */
1281   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1282   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1283       sec_name, op_name, &o->address_id))
1284   {
1285     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1286         op_counter, "DEL_ADDRESS", op_name);
1287     GNUNET_free (op_name);
1288     return GNUNET_SYSERR;
1289   }
1290   GNUNET_free (op_name);
1291
1292   /* plugin */
1293   GNUNET_asprintf(&op_name, "op-%u-plugin", op_counter);
1294   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1295       sec_name, op_name, &o->plugin))
1296   {
1297     fprintf (stderr, "Missing plugin in operation %u `%s' in episode `%s'\n",
1298         op_counter, "DEL_ADDRESS", op_name);
1299     GNUNET_free (op_name);
1300     return GNUNET_SYSERR;
1301   }
1302   GNUNET_free (op_name);
1303
1304   /* address  */
1305   GNUNET_asprintf(&op_name, "op-%u-address", op_counter);
1306   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1307       sec_name, op_name, &o->address))
1308   {
1309     fprintf (stderr, "Missing address in operation %u `%s' in episode `%s'\n",
1310         op_counter, "DEL_ADDRESS", op_name);
1311     GNUNET_free (op_name);
1312     return GNUNET_SYSERR;
1313   }
1314   GNUNET_free (op_name);
1315
1316   /* session */
1317   GNUNET_asprintf(&op_name, "op-%u-address-session", op_counter);
1318   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1319       sec_name, op_name, &o->address_session))
1320   {
1321     fprintf (stderr, "Missing address-session in operation %u `%s' in episode `%s'\n",
1322         op_counter, "DEL_ADDRESS", op_name);
1323     GNUNET_free (op_name);
1324     return GNUNET_SYSERR;
1325   }
1326   GNUNET_free (op_name);
1327
1328   /* network */
1329   GNUNET_asprintf(&op_name, "op-%u-address-network", op_counter);
1330   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1331       sec_name, op_name, &op_network))
1332   {
1333     fprintf (stderr, "Missing address-network in operation %u `%s' in episode `%s'\n",
1334         op_counter, "ADD_ADDRESS", op_name);
1335     GNUNET_free (op_name);
1336     return GNUNET_SYSERR;
1337   }
1338   else
1339   {
1340     GNUNET_STRINGS_utf8_toupper (op_network,op_network);
1341     if (0 == strcmp(op_network, "UNSPECIFIED"))
1342     {
1343       o->address_network = GNUNET_ATS_NET_UNSPECIFIED;
1344     }
1345     else if (0 == strcmp(op_network, "LOOPBACK"))
1346     {
1347       o->address_network = GNUNET_ATS_NET_LOOPBACK;
1348     }
1349     else if (0 == strcmp(op_network, "LAN"))
1350     {
1351       o->address_network = GNUNET_ATS_NET_LAN;
1352     }
1353     else if (0 == strcmp(op_network, "WAN"))
1354     {
1355       o->address_network = GNUNET_ATS_NET_WAN;
1356     }
1357     else if (0 == strcmp(op_network, "WLAN"))
1358     {
1359       o->address_network = GNUNET_ATS_NET_WLAN;
1360     }
1361     else if (0 == strcmp(op_network, "BT"))
1362     {
1363       o->address_network = GNUNET_ATS_NET_BT;
1364     }
1365     else
1366     {
1367       fprintf (stderr, "Invalid address-network in operation %u `%s' in episode `%s': `%s'\n",
1368           op_counter, "ADD_ADDRESS", op_name, op_network);
1369       GNUNET_free (op_network);
1370       GNUNET_free (op_name);
1371       return GNUNET_SYSERR;
1372     }
1373   }
1374   GNUNET_free (op_network);
1375   GNUNET_free (op_name);
1376
1377   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1378       "Found operation %s: [%llu:%llu] address `%s' plugin `%s' \n",
1379       "DEL_ADDRESS", o->peer_id, o->address_id, o->address, o->plugin);
1380
1381   return GNUNET_OK;
1382 }
1383
1384 static enum GNUNET_ATS_Property
1385 parse_preference_string (const char * str)
1386 {
1387   int c = 0;
1388   char *props[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1389
1390   for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1391     if (0 == strcmp(str, props[c]))
1392       return c;
1393   return 0;
1394 };
1395
1396 static int
1397 load_op_start_set_preference (struct GNUNET_ATS_TEST_Operation *o,
1398     struct Episode *e,
1399     int op_counter,
1400     char *sec_name,
1401     const struct GNUNET_CONFIGURATION_Handle *cfg)
1402 {
1403   char *op_name;
1404   char *type;
1405   char *pref;
1406
1407   /* peer pid */
1408   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1409   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1410       sec_name, op_name, &o->peer_id))
1411   {
1412     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1413         op_counter, "START_SET_PREFERENCE", op_name);
1414     GNUNET_free (op_name);
1415     return GNUNET_SYSERR;
1416   }
1417   GNUNET_free (op_name);
1418
1419   /* address pid */
1420   GNUNET_asprintf(&op_name, "op-%u-client-id", op_counter);
1421   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1422       sec_name, op_name, &o->client_id))
1423   {
1424     fprintf (stderr, "Missing client-id in operation %u `%s' in episode `%s'\n",
1425         op_counter, "START_SET_PREFERENCE", op_name);
1426     GNUNET_free (op_name);
1427     return GNUNET_SYSERR;
1428   }
1429   GNUNET_free (op_name);
1430
1431   /* generator */
1432   GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
1433   if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
1434           sec_name, op_name, &type)) )
1435   {
1436     fprintf (stderr, "Missing type in operation %u `%s' in episode `%s'\n",
1437         op_counter, "START_SET_PREFERENCE", op_name);
1438     GNUNET_free (op_name);
1439     return GNUNET_SYSERR;
1440   }
1441
1442   /* Load arguments for set_rate, start_send, set_preference */
1443   if (0 == strcmp (type, "constant"))
1444   {
1445     o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
1446   }
1447   else if (0 == strcmp (type, "linear"))
1448   {
1449     o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
1450   }
1451   else if (0 == strcmp (type, "sinus"))
1452   {
1453     o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
1454   }
1455   else if (0 == strcmp (type, "random"))
1456   {
1457     o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
1458   }
1459   else
1460   {
1461     fprintf (stderr, "Invalid generator type %u `%s' in episode %u\n",
1462         op_counter, op_name, e->id);
1463     GNUNET_free (type);
1464     GNUNET_free (op_name);
1465     return GNUNET_SYSERR;
1466   }
1467   GNUNET_free (type);
1468   GNUNET_free (op_name);
1469
1470
1471   /* Get base rate */
1472   GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
1473   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1474       sec_name, op_name, &o->base_rate))
1475   {
1476     fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
1477         op_counter, op_name, e->id);
1478     GNUNET_free (op_name);
1479     return GNUNET_SYSERR;
1480   }
1481   GNUNET_free (op_name);
1482
1483
1484   /* Get max rate */
1485   GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
1486   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1487       sec_name, op_name, &o->max_rate))
1488   {
1489     if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
1490         (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
1491         (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
1492     {
1493       fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
1494           op_counter, op_name, e->id);
1495       GNUNET_free (op_name);
1496       return GNUNET_SYSERR;
1497     }
1498   }
1499   GNUNET_free (op_name);
1500
1501   /* Get period */
1502   GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
1503   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1504       sec_name, op_name, &o->period))
1505   {
1506     o->period = e->duration;
1507   }
1508   GNUNET_free (op_name);
1509
1510   /* Get frequency */
1511   GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
1512   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1513       sec_name, op_name, &o->frequency))
1514   {
1515       fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
1516           op_counter, op_name, e->id);
1517       GNUNET_free (op_name);
1518       return GNUNET_SYSERR;
1519   }
1520   GNUNET_free (op_name);
1521
1522   /* Get preference */
1523   GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
1524   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1525       sec_name, op_name, &pref))
1526   {
1527       fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
1528           op_counter, op_name, e->id);
1529       GNUNET_free (op_name);
1530       return GNUNET_SYSERR;
1531   }
1532
1533   if (0 == (o->pref_type = parse_preference_string(pref)))
1534   {
1535       fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
1536           op_counter, op_name, e->id);
1537       GNUNET_free (op_name);
1538       GNUNET_free (pref);
1539       return GNUNET_SYSERR;
1540   }
1541   GNUNET_free (pref);
1542   GNUNET_free (op_name);
1543
1544   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1545       "Found operation %s: [%llu:%llu]: %s = %llu\n",
1546       "START_SET_PREFERENCE", o->peer_id, o->address_id,
1547       GNUNET_ATS_print_preference_type(o->pref_type), o->base_rate);
1548
1549   return GNUNET_OK;
1550 }
1551
1552 static int
1553 load_op_stop_set_preference (struct GNUNET_ATS_TEST_Operation *o,
1554     struct Episode *e,
1555     int op_counter,
1556     char *sec_name,
1557     const struct GNUNET_CONFIGURATION_Handle *cfg)
1558 {
1559   char *op_name;
1560   char *pref;
1561
1562   /* peer pid */
1563   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1564   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1565       sec_name, op_name, &o->peer_id))
1566   {
1567     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1568         op_counter, "STOP_SET_PREFERENCE", op_name);
1569     GNUNET_free (op_name);
1570     return GNUNET_SYSERR;
1571   }
1572   GNUNET_free (op_name);
1573
1574   /* address pid */
1575   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1576   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1577       sec_name, op_name, &o->address_id))
1578   {
1579     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1580         op_counter, "STOP_SET_PREFERENCE", op_name);
1581     GNUNET_free (op_name);
1582     return GNUNET_SYSERR;
1583   }
1584   GNUNET_free (op_name);
1585
1586   /* Get preference */
1587   GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
1588   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1589       sec_name, op_name, &pref))
1590   {
1591     fprintf (stderr, "Missing preference in operation %u `%s' in episode `%s'\n",
1592         op_counter, "STOP_SET_PREFERENCE", op_name);
1593       GNUNET_free (op_name);
1594       return GNUNET_SYSERR;
1595   }
1596
1597   if (0 == (o->pref_type = parse_preference_string(pref)))
1598   {
1599       fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
1600           op_counter, op_name, e->id);
1601       GNUNET_free (op_name);
1602       GNUNET_free (pref);
1603       return GNUNET_SYSERR;
1604   }
1605   GNUNET_free (pref);
1606   GNUNET_free (op_name);
1607
1608   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1609       "Found operation %s: [%llu:%llu]: %s\n",
1610       "STOP_SET_PREFERENCE", o->peer_id, o->address_id,
1611       GNUNET_ATS_print_preference_type(o->pref_type));
1612   return GNUNET_OK;
1613 }
1614
1615 static enum GNUNET_ATS_Property
1616 parse_property_string (const char * str)
1617 {
1618   int c = 0;
1619   char *props[GNUNET_ATS_PropertyCount] = GNUNET_ATS_PropertyStrings;
1620
1621   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
1622     if (0 == strcmp(str, props[c]))
1623       return c;
1624   return 0;
1625 };
1626
1627 static int
1628 load_op_start_set_property(struct GNUNET_ATS_TEST_Operation *o,
1629     struct Episode *e,
1630     int op_counter,
1631     char *sec_name,
1632     const struct GNUNET_CONFIGURATION_Handle *cfg)
1633 {
1634   char *op_name;
1635   char *type;
1636   char *prop;
1637
1638   /* peer pid */
1639   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1640   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1641       sec_name, op_name, &o->peer_id))
1642   {
1643     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1644         op_counter, "START_SET_PROPERTY", op_name);
1645     GNUNET_free (op_name);
1646     return GNUNET_SYSERR;
1647   }
1648   GNUNET_free (op_name);
1649
1650   /* address pid */
1651   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1652   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1653       sec_name, op_name, &o->address_id))
1654   {
1655     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1656         op_counter, "START_SET_PROPERTY", op_name);
1657     GNUNET_free (op_name);
1658     return GNUNET_SYSERR;
1659   }
1660   GNUNET_free (op_name);
1661
1662   /* generator */
1663   GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
1664   if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
1665           sec_name, op_name, &type)) )
1666   {
1667     fprintf (stderr, "Missing type in operation %u `%s' in episode `%s'\n",
1668         op_counter, "START_SET_PROPERTY", op_name);
1669     GNUNET_free (op_name);
1670     return GNUNET_SYSERR;
1671   }
1672
1673   /* Load arguments for set_rate, start_send, set_preference */
1674   if (0 == strcmp (type, "constant"))
1675   {
1676     o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
1677   }
1678   else if (0 == strcmp (type, "linear"))
1679   {
1680     o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
1681   }
1682   else if (0 == strcmp (type, "sinus"))
1683   {
1684     o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
1685   }
1686   else if (0 == strcmp (type, "random"))
1687   {
1688     o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
1689   }
1690   else
1691   {
1692     fprintf (stderr, "Invalid generator type %u `%s' in episode %u\n",
1693         op_counter, op_name, e->id);
1694     GNUNET_free (type);
1695     GNUNET_free (op_name);
1696     return GNUNET_SYSERR;
1697   }
1698   GNUNET_free (type);
1699   GNUNET_free (op_name);
1700
1701
1702   /* Get base rate */
1703   GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
1704   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1705       sec_name, op_name, &o->base_rate))
1706   {
1707     fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
1708         op_counter, op_name, e->id);
1709     GNUNET_free (op_name);
1710     return GNUNET_SYSERR;
1711   }
1712   GNUNET_free (op_name);
1713
1714
1715   /* Get max rate */
1716   GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
1717   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1718       sec_name, op_name, &o->max_rate))
1719   {
1720     if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
1721         (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
1722         (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
1723     {
1724       fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
1725           op_counter, op_name, e->id);
1726       GNUNET_free (op_name);
1727       return GNUNET_SYSERR;
1728     }
1729   }
1730   GNUNET_free (op_name);
1731
1732   /* Get period */
1733   GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
1734   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1735       sec_name, op_name, &o->period))
1736   {
1737     o->period = e->duration;
1738   }
1739   GNUNET_free (op_name);
1740
1741   /* Get frequency */
1742   GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
1743   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1744       sec_name, op_name, &o->frequency))
1745   {
1746       fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
1747           op_counter, op_name, e->id);
1748       GNUNET_free (op_name);
1749       return GNUNET_SYSERR;
1750   }
1751   GNUNET_free (op_name);
1752
1753   /* Get preference */
1754   GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
1755   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1756       sec_name, op_name, &prop))
1757   {
1758       fprintf (stderr, "Missing property in operation %u `%s' in episode %u\n",
1759           op_counter, op_name, e->id);
1760       GNUNET_free (op_name);
1761       GNUNET_free_non_null (prop);
1762       return GNUNET_SYSERR;
1763   }
1764
1765   if (0 == (o->prop_type = parse_property_string(prop)))
1766   {
1767       fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
1768           op_counter, op_name, e->id);
1769       GNUNET_free (op_name);
1770       GNUNET_free (prop);
1771       return GNUNET_SYSERR;
1772   }
1773
1774   GNUNET_free (prop);
1775   GNUNET_free (op_name);
1776
1777   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1778       "Found operation %s: [%llu:%llu] %s = %llu\n",
1779       "START_SET_PROPERTY", o->peer_id, o->address_id,
1780       GNUNET_ATS_print_property_type (o->prop_type), o->base_rate);
1781
1782   return GNUNET_OK;
1783 }
1784
1785 static int
1786 load_op_stop_set_property (struct GNUNET_ATS_TEST_Operation *o,
1787     struct Episode *e,
1788     int op_counter,
1789     char *sec_name,
1790     const struct GNUNET_CONFIGURATION_Handle *cfg)
1791 {
1792   char *op_name;
1793   char *pref;
1794
1795   /* peer pid */
1796   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1797   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1798       sec_name, op_name, &o->peer_id))
1799   {
1800     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1801         op_counter, "STOP_SET_PROPERTY", op_name);
1802     GNUNET_free (op_name);
1803     return GNUNET_SYSERR;
1804   }
1805   GNUNET_free (op_name);
1806
1807   /* address pid */
1808   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1809   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1810       sec_name, op_name, &o->address_id))
1811   {
1812     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1813         op_counter, "STOP_SET_PROPERTY", op_name);
1814     GNUNET_free (op_name);
1815     return GNUNET_SYSERR;
1816   }
1817   GNUNET_free (op_name);
1818
1819   /* Get property */
1820   GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
1821   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1822       sec_name, op_name, &pref))
1823   {
1824     fprintf (stderr, "Missing property in operation %u `%s' in episode `%s'\n",
1825         op_counter, "STOP_SET_PROPERTY", op_name);
1826       GNUNET_free (op_name);
1827       GNUNET_free_non_null (pref);
1828       return GNUNET_SYSERR;
1829   }
1830
1831   if (0 == (o->prop_type = parse_property_string(pref)))
1832   {
1833       fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
1834           op_counter, op_name, e->id);
1835       GNUNET_free (op_name);
1836       GNUNET_free_non_null (pref);
1837       return GNUNET_SYSERR;
1838   }
1839
1840   GNUNET_free (pref);
1841   GNUNET_free (op_name);
1842
1843   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1844       "Found operation %s: [%llu:%llu] %s\n",
1845       "STOP_SET_PROPERTY", o->peer_id, o->address_id,
1846       GNUNET_ATS_print_property_type (o->prop_type));
1847
1848   return GNUNET_OK;
1849 }
1850
1851
1852 static int
1853 load_op_start_request (struct GNUNET_ATS_TEST_Operation *o,
1854     struct Episode *e,
1855     int op_counter,
1856     char *sec_name,
1857     const struct GNUNET_CONFIGURATION_Handle *cfg)
1858 {
1859   char *op_name;
1860
1861   /* peer pid */
1862   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1863   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1864       sec_name, op_name, &o->peer_id))
1865   {
1866     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1867         op_counter, "START_REQUEST", op_name);
1868     GNUNET_free (op_name);
1869     return GNUNET_SYSERR;
1870   }
1871   GNUNET_free (op_name);
1872   return GNUNET_OK;
1873 }
1874
1875 static int
1876 load_op_stop_request (struct GNUNET_ATS_TEST_Operation *o,
1877     struct Episode *e,
1878     int op_counter,
1879     char *sec_name,
1880     const struct GNUNET_CONFIGURATION_Handle *cfg)
1881 {
1882   char *op_name;
1883
1884   /* peer pid */
1885   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1886   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1887       sec_name, op_name, &o->peer_id))
1888   {
1889     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1890         op_counter, "STOP_REQUEST", op_name);
1891     GNUNET_free (op_name);
1892     return GNUNET_SYSERR;
1893   }
1894   GNUNET_free (op_name);
1895   return GNUNET_OK;
1896 }
1897
1898
1899 static int
1900 load_episode (struct Experiment *e, struct Episode *cur,
1901     struct GNUNET_CONFIGURATION_Handle *cfg)
1902 {
1903   struct GNUNET_ATS_TEST_Operation *o;
1904   char *sec_name;
1905   char *op_name;
1906   char *op;
1907   int op_counter = 0;
1908   int res;
1909   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Parsing episode %u\n",cur->id);
1910   GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
1911
1912   while (1)
1913   {
1914     /* Load operation */
1915     GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
1916     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
1917         sec_name, op_name, &op))
1918     {
1919       GNUNET_free (op_name);
1920       break;
1921     }
1922     o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
1923     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "==== Parsing operation %u: `%s'\n",
1924         cur->id, op_name);
1925
1926     /* operations = set_rate, start_send, stop_send, set_preference */
1927     if (0 == strcmp (op, "address_add"))
1928     {
1929       o->type = SOLVER_OP_ADD_ADDRESS;
1930       res = load_op_add_address (o, cur,
1931           op_counter, sec_name, cfg);
1932     }
1933     else if (0 == strcmp (op, "address_del"))
1934     {
1935       o->type = SOLVER_OP_DEL_ADDRESS;
1936       res = load_op_del_address (o, cur,
1937           op_counter, sec_name, cfg);
1938     }
1939     else if (0 == strcmp (op, "start_set_property"))
1940     {
1941       o->type = SOLVER_OP_START_SET_PROPERTY;
1942       res = load_op_start_set_property (o, cur,
1943           op_counter, sec_name, cfg);
1944     }
1945     else if (0 == strcmp (op, "stop_set_property"))
1946     {
1947       o->type = SOLVER_OP_STOP_SET_PROPERTY;
1948       res = load_op_stop_set_property (o, cur,
1949           op_counter, sec_name, cfg);
1950     }
1951     else if (0 == strcmp (op, "start_set_preference"))
1952     {
1953       o->type = SOLVER_OP_START_SET_PREFERENCE;
1954       res =  load_op_start_set_preference (o, cur,
1955           op_counter, sec_name, cfg);
1956     }
1957     else if (0 == strcmp (op, "stop_set_preference"))
1958     {
1959       o->type = SOLVER_OP_STOP_SET_PREFERENCE;
1960       res =  load_op_stop_set_preference (o, cur,
1961           op_counter, sec_name, cfg);
1962     }
1963     else if (0 == strcmp (op, "start_request"))
1964     {
1965       o->type = SOLVER_OP_START_REQUEST;
1966       res = load_op_start_request (o, cur,
1967           op_counter, sec_name, cfg);
1968     }
1969     else if (0 == strcmp (op, "stop_request"))
1970     {
1971       o->type = SOLVER_OP_STOP_REQUEST;
1972       res = load_op_stop_request(o, cur,
1973           op_counter, sec_name, cfg);
1974     }
1975     else
1976     {
1977       fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
1978           op_counter, op, cur->id);
1979       res = GNUNET_SYSERR;
1980     }
1981
1982     GNUNET_free (op);
1983     GNUNET_free (op_name);
1984
1985     if (GNUNET_SYSERR == res)
1986     {
1987       GNUNET_free (o);
1988       GNUNET_free (sec_name);
1989       return GNUNET_SYSERR;
1990     }
1991
1992     GNUNET_CONTAINER_DLL_insert_tail (cur->head,cur->tail, o);
1993     op_counter++;
1994   }
1995   GNUNET_free (sec_name);
1996   return GNUNET_OK;
1997 }
1998
1999 static int
2000 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
2001 {
2002   int e_counter = 0;
2003   char *sec_name;
2004   struct GNUNET_TIME_Relative e_duration;
2005   struct Episode *cur;
2006   struct Episode *last;
2007
2008   e_counter = 0;
2009   last = NULL;
2010   while (1)
2011   {
2012     GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
2013     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
2014         sec_name, "duration", &e_duration))
2015     {
2016       GNUNET_free (sec_name);
2017       break;
2018     }
2019
2020     cur = GNUNET_new (struct Episode);
2021     cur->duration = e_duration;
2022     cur->id = e_counter;
2023
2024     if (GNUNET_OK != load_episode (e, cur, cfg))
2025     {
2026       GNUNET_free (sec_name);
2027       GNUNET_free (cur);
2028       return GNUNET_SYSERR;
2029     }
2030
2031     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Found episode %u with duration %s \n",
2032         e_counter,
2033         GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
2034
2035     /* Update experiment */
2036     e->num_episodes ++;
2037     e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
2038     /* Put in linked list */
2039     if (NULL == last)
2040       e->start = cur;
2041     else
2042       last->next = cur;
2043
2044     GNUNET_free (sec_name);
2045     e_counter ++;
2046     last = cur;
2047   }
2048   return e_counter;
2049 }
2050
2051 static void
2052 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
2053 {
2054   struct Experiment *e = cls;
2055   e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2056   fprintf (stderr, "Experiment timeout!\n");
2057
2058   if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
2059   {
2060     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
2061     e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2062   }
2063
2064   e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
2065       GNUNET_SYSERR);
2066 }
2067
2068 struct ATS_Address *
2069 create_ats_address (const struct GNUNET_PeerIdentity *peer,
2070                 const char *plugin_name,
2071                 const void *plugin_addr, size_t plugin_addr_len,
2072                 uint32_t session_id)
2073 {
2074   struct ATS_Address *aa = NULL;
2075
2076   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len + strlen (plugin_name) + 1);
2077   aa->peer = *peer;
2078   aa->addr_len = plugin_addr_len;
2079   aa->addr = &aa[1];
2080   aa->plugin = (char *) &aa[1] + plugin_addr_len;
2081   memcpy (&aa[1], plugin_addr, plugin_addr_len);
2082   memcpy (aa->plugin, plugin_name, strlen (plugin_name) + 1);
2083   aa->session_id = session_id;
2084   aa->active = GNUNET_NO;
2085   aa->used = GNUNET_NO;
2086   aa->solver_information = NULL;
2087   aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
2088   aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
2089   return aa;
2090 }
2091
2092
2093
2094 static void
2095 enforce_add_address (struct GNUNET_ATS_TEST_Operation *op)
2096 {
2097   struct TestPeer *p;
2098   struct TestAddress *a;
2099   int c;
2100
2101   if (NULL == (p = find_peer_by_id (op->peer_id)))
2102   {
2103     p = GNUNET_new (struct TestPeer);
2104     p->id = op->peer_id;
2105     memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
2106     for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
2107     {
2108       p->pref_abs[c] = DEFAULT_ABS_PREFERENCE;
2109       p->pref_norm[c] = DEFAULT_REL_PREFERENCE;
2110     }
2111
2112     GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, p);
2113   }
2114
2115   if (NULL != (find_address_by_id (p, op->address_id)))
2116   {
2117     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Duplicate address %u for peer %u\n",
2118         op->address_id, op->peer_id);
2119     return;
2120   }
2121
2122   a = GNUNET_new (struct TestAddress);
2123   a->aid = op->address_id;
2124   a->network = op->address_network;
2125   a->ats_addr = create_ats_address (&p->peer_id, op->plugin, op->address,
2126       strlen (op->address) + 1, op->address_session);
2127   memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
2128   GNUNET_CONTAINER_DLL_insert (p->addr_head, p->addr_tail, a);
2129
2130   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
2131     a->prop_norm[c] = DEFAULT_REL_QUALITY;
2132
2133   GNUNET_CONTAINER_multipeermap_put (sh->addresses, &p->peer_id, a->ats_addr,
2134     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2135
2136   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Adding address %u for peer %u in network `%s'\n",
2137     op->address_id, op->peer_id, GNUNET_ATS_print_network_type(op->address_network));
2138
2139   sh->env.sf.s_add (sh->solver, a->ats_addr, op->address_network);
2140
2141 }
2142
2143
2144 static void
2145 enforce_del_address (struct GNUNET_ATS_TEST_Operation *op)
2146 {
2147   struct TestPeer *p;
2148   struct TestAddress *a;
2149   struct AddressLookupCtx ctx;
2150   struct PropertyGenerator *pg;
2151
2152   if (NULL == (p = find_peer_by_id (op->peer_id)))
2153   {
2154     GNUNET_break (0);
2155     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2156         "Deleting address for unknown peer %u\n", op->peer_id);
2157     return;
2158   }
2159
2160   ctx.plugin = op->plugin;
2161   ctx.addr = op->address;
2162   ctx.res = NULL;
2163   GNUNET_CONTAINER_multipeermap_get_multiple (sh->addresses, &p->peer_id,
2164       find_address_it, &ctx);
2165   if (NULL == ctx.res)
2166   {
2167     GNUNET_break (0);
2168     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2169         "Deleting unknown address for peer %u\n", op->peer_id);
2170     return;
2171   }
2172
2173   if (NULL == (a =find_address_by_id (p, op->address_id)))
2174   {
2175     GNUNET_break (0);
2176     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2177         "Deleting address for unknown peer %u\n", op->peer_id);
2178     return;
2179   }
2180
2181   while (NULL != (pg = find_prop_gen (p->id, a->aid, 0)))
2182   {
2183     GNUNET_ATS_solver_generate_property_stop (pg);
2184   }
2185
2186   GNUNET_CONTAINER_DLL_remove(p->addr_head, p->addr_tail, a);
2187   GNUNET_free (a);
2188
2189   GNUNET_CONTAINER_multipeermap_remove (sh->addresses, &p->peer_id, ctx.res);
2190
2191   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Removing address %u for peer %u\n",
2192       op->address_id, op->peer_id);
2193
2194   sh->env.sf.s_del (sh->solver, ctx.res, GNUNET_NO);
2195   GNUNET_free (ctx.res);
2196
2197 }
2198
2199 static void
2200 enforce_start_property (struct GNUNET_ATS_TEST_Operation *op)
2201 {
2202   struct PropertyGenerator *pg;
2203   struct TestPeer *p;
2204   struct TestAddress *a;
2205
2206   if (NULL != (pg = find_prop_gen (op->peer_id, op->address_id, op->prop_type)))
2207   {
2208     GNUNET_ATS_solver_generate_property_stop (pg);
2209     GNUNET_free (pg);
2210   }
2211
2212   if (NULL == (p = find_peer_by_id (op->peer_id)))
2213   {
2214     GNUNET_break (0);
2215     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2216         "Starting property generation for unknown peer %u\n", op->peer_id);
2217     return;
2218   }
2219
2220   if (NULL == (a = find_address_by_id (p, op->address_id)))
2221   {
2222     GNUNET_break (0);
2223     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2224         "Setting property for unknown address %u\n", op->peer_id);
2225     return;
2226   }
2227
2228   GNUNET_ATS_solver_generate_property_start (op->peer_id,
2229     op->address_id,
2230     p, a,
2231     op->gen_type,
2232     op->base_rate,
2233     op->max_rate,
2234     op->period,
2235     op->frequency,
2236     op->prop_type);
2237 }
2238
2239 static void
2240 enforce_stop_property (struct GNUNET_ATS_TEST_Operation *op)
2241 {
2242   struct PropertyGenerator *pg = find_prop_gen(op->peer_id, op->address_id,
2243       op->prop_type);
2244   if (NULL != pg)
2245       GNUNET_ATS_solver_generate_property_stop (pg);
2246 }
2247
2248 static void
2249 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
2250 {
2251   struct PreferenceGenerator *pg;
2252   if (NULL != (pg = find_pref_gen (op->peer_id, op->pref_type)))
2253   {
2254     GNUNET_ATS_solver_generate_preferences_stop (pg);
2255     GNUNET_free (pg);
2256   }
2257
2258   if (NULL == (find_peer_by_id (op->peer_id)))
2259   {
2260     GNUNET_break (0);
2261     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2262         "Starting preference generation for unknown peer %u\n", op->peer_id);
2263     return;
2264   }
2265
2266   GNUNET_ATS_solver_generate_preferences_start (op->peer_id,
2267     op->address_id,
2268     op->client_id,
2269     op->gen_type,
2270     op->base_rate,
2271     op->max_rate,
2272     op->period,
2273     op->frequency,
2274     op->pref_type);
2275 }
2276
2277 static void
2278 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
2279 {
2280   struct PreferenceGenerator *pg = find_pref_gen(op->peer_id,
2281       op->pref_type);
2282   if (NULL != pg)
2283       GNUNET_ATS_solver_generate_preferences_stop (pg);
2284 }
2285
2286
2287 static void
2288 enforce_start_request (struct GNUNET_ATS_TEST_Operation *op)
2289 {
2290   struct TestPeer *p;
2291   const struct ATS_Address *res;
2292
2293   if (NULL == (p = find_peer_by_id (op->peer_id)))
2294   {
2295     GNUNET_break (0);
2296     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2297         "Requesting address for unknown peer %u\n", op->peer_id);
2298     return;
2299   }
2300
2301   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Requesting address for peer %u\n",
2302       op->peer_id);
2303
2304   res = sh->env.sf.s_get (sh->solver, &p->peer_id);
2305   if (NULL != res)
2306   {
2307
2308   }
2309 }
2310
2311 static void
2312 enforce_stop_request (struct GNUNET_ATS_TEST_Operation *op)
2313 {
2314   struct TestPeer *p;
2315
2316   if (NULL == (p = find_peer_by_id (op->peer_id)))
2317   {
2318     GNUNET_break (0);
2319     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2320         "Requesting address for unknown peer %u\n", op->peer_id);
2321     return;
2322   }
2323
2324   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stop requesting address for peer %u\n",
2325       op->peer_id);
2326
2327   sh->env.sf.s_get_stop (sh->solver, &p->peer_id);
2328 }
2329
2330 static void enforce_episode (struct Episode *ep)
2331 {
2332   struct GNUNET_ATS_TEST_Operation *cur;
2333   for (cur = ep->head; NULL != cur; cur = cur->next)
2334   {
2335     switch (cur->type) {
2336       case SOLVER_OP_ADD_ADDRESS:
2337         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
2338             print_op (cur->type), cur->peer_id, cur->address_id);
2339         enforce_add_address (cur);
2340         break;
2341       case SOLVER_OP_DEL_ADDRESS:
2342         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
2343             print_op (cur->type), cur->peer_id, cur->address_id);
2344         enforce_del_address (cur);
2345         break;
2346       case SOLVER_OP_START_SET_PROPERTY:
2347         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2348             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2349         enforce_start_property (cur);
2350         break;
2351       case SOLVER_OP_STOP_SET_PROPERTY:
2352         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2353             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2354         enforce_stop_property (cur);
2355         break;
2356       case SOLVER_OP_START_SET_PREFERENCE:
2357         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2358             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2359         enforce_start_preference (cur);
2360         break;
2361       case SOLVER_OP_STOP_SET_PREFERENCE:
2362         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2363             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2364         enforce_stop_preference (cur);
2365         break;
2366       case SOLVER_OP_START_REQUEST:
2367         fprintf (stderr, "Enforcing operation: %s [%llu]\n",
2368             print_op (cur->type), cur->peer_id);
2369         enforce_start_request (cur);
2370         break;
2371       case SOLVER_OP_STOP_REQUEST:
2372         fprintf (stderr, "Enforcing operation: %s [%llu]\n",
2373             print_op (cur->type), cur->peer_id);
2374         enforce_stop_request (cur);
2375         break;
2376       default:
2377         break;
2378     }
2379   }
2380 }
2381
2382 static void
2383 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
2384 {
2385   struct Experiment *e = cls;
2386   e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2387   if (NULL != e->ep_done_cb)
2388     e->ep_done_cb (e->cur);
2389
2390   /* Scheduling next */
2391   e->cur = e->cur->next;
2392   if (NULL == e->cur)
2393   {
2394     /* done */
2395     fprintf (stderr, "Last episode done!\n");
2396     if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
2397     {
2398       GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
2399       e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2400     }
2401     e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
2402     return;
2403   }
2404
2405   fprintf (stderr, "Running episode %u with timeout %s\n",
2406       e->cur->id,
2407       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
2408   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
2409       &timeout_episode, e);
2410   enforce_episode(e->cur);
2411
2412
2413 }
2414
2415
2416 void
2417 GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
2418     GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
2419     GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
2420 {
2421   fprintf (stderr, "Running experiment `%s'  with timeout %s\n", e->name,
2422       GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
2423   e->e_done_cb = e_done_cb;
2424   e->ep_done_cb = ep_done_cb;
2425   e->start_time = GNUNET_TIME_absolute_get();
2426
2427   /* Start total time out */
2428   e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
2429       &timeout_experiment, e);
2430
2431   /* Start */
2432   if (NULL == e->start)
2433   {
2434     GNUNET_break (0);
2435     return;
2436   }
2437
2438   e->cur = e->start;
2439   fprintf (stderr, "Running episode %u with timeout %s\n",
2440       e->cur->id,
2441       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
2442   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
2443       &timeout_episode, e);
2444   enforce_episode(e->cur);
2445
2446 }
2447
2448 void
2449 GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
2450 {
2451   if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
2452   {
2453     GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
2454     e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2455   }
2456   if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
2457   {
2458     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
2459     e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2460   }
2461   if (NULL != e->cfg)
2462   {
2463     GNUNET_CONFIGURATION_destroy(e->cfg);
2464     e->cfg = NULL;
2465   }
2466   free_experiment (e);
2467 }
2468
2469
2470 struct Experiment *
2471 GNUNET_ATS_solvers_experimentation_load (char *filename)
2472 {
2473   struct Experiment *e;
2474   struct GNUNET_CONFIGURATION_Handle *cfg;
2475   e = NULL;
2476
2477   cfg = GNUNET_CONFIGURATION_create();
2478   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
2479   {
2480     fprintf (stderr, "Failed to load `%s'\n", filename);
2481     GNUNET_CONFIGURATION_destroy (cfg);
2482     return NULL;
2483   }
2484
2485   e = create_experiment ();
2486
2487   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
2488       "name", &e->name))
2489   {
2490     fprintf (stderr, "Invalid %s", "name");
2491     free_experiment (e);
2492     return NULL;
2493   }
2494   else
2495     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment name: `%s'\n", e->name);
2496
2497   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
2498       "log_prefix", &e->log_prefix))
2499   {
2500     fprintf (stderr, "Invalid %s", "name");
2501     free_experiment (e);
2502     return NULL;
2503   }
2504   else
2505     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging prefix: `%s'\n",
2506         e->log_prefix);
2507
2508   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
2509       "log_output_dir", &e->log_output_dir))
2510   {
2511     e->log_output_dir = NULL;
2512   }
2513   else
2514     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging output directory: `%s'\n",
2515         e->log_output_dir);
2516
2517
2518   if (GNUNET_SYSERR == (e->log_append_time_stamp = GNUNET_CONFIGURATION_get_value_yesno(cfg,
2519       "experiment", "log_append_time_stamp")))
2520     e->log_append_time_stamp = GNUNET_YES;
2521   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging append timestamp: `%s'\n",
2522       (GNUNET_YES == e->log_append_time_stamp) ? "yes" : "no");
2523
2524
2525   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
2526       "cfg_file", &e->cfg_file))
2527   {
2528     fprintf (stderr, "Invalid %s", "cfg_file");
2529     free_experiment (e);
2530     return NULL;
2531   }
2532   else
2533   {
2534     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment configuration: `%s'\n", e->cfg_file);
2535     e->cfg = GNUNET_CONFIGURATION_create();
2536     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (e->cfg, e->cfg_file))
2537     {
2538       fprintf (stderr, "Invalid configuration %s", "cfg_file");
2539       free_experiment (e);
2540       return NULL;
2541     }
2542
2543   }
2544
2545   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
2546       "log_freq", &e->log_freq))
2547   {
2548     fprintf (stderr, "Invalid %s", "log_freq");
2549     free_experiment (e);
2550     return NULL;
2551   }
2552   else
2553     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging frequency: `%s'\n",
2554         GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
2555
2556   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
2557       "max_duration", &e->max_duration))
2558   {
2559     fprintf (stderr, "Invalid %s", "max_duration");
2560     free_experiment (e);
2561     return NULL;
2562   }
2563   else
2564     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment duration: `%s'\n",
2565         GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
2566
2567   if (GNUNET_SYSERR == load_episodes (e, cfg))
2568   {
2569     GNUNET_ATS_solvers_experimentation_stop (e);
2570     GNUNET_CONFIGURATION_destroy (cfg);
2571     e = NULL;
2572     fprintf (stderr, "Failed to load experiment\n");
2573     return NULL;
2574   }
2575   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loaded %u episodes with total duration %s\n",
2576       e->num_episodes,
2577       GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
2578
2579   GNUNET_CONFIGURATION_destroy (cfg);
2580   return e;
2581 }
2582
2583
2584
2585 /**
2586  * Solver
2587  */
2588
2589 static int
2590 free_all_it (void *cls,
2591     const struct GNUNET_PeerIdentity *key,
2592     void *value)
2593 {
2594   struct ATS_Address *address = value;
2595   GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (sh->env.addresses,
2596       key, value));
2597   GNUNET_free (address);
2598
2599   return GNUNET_OK;
2600 }
2601
2602 void
2603 GNUNET_ATS_solvers_solver_stop (struct SolverHandle *sh)
2604 {
2605  GNUNET_STATISTICS_destroy ((struct GNUNET_STATISTICS_Handle *) sh->env.stats,
2606      GNUNET_NO);
2607  GNUNET_PLUGIN_unload (sh->plugin, sh->solver);
2608
2609  GAS_normalization_stop();
2610
2611  GNUNET_CONTAINER_multipeermap_iterate (sh->addresses, &free_all_it, NULL);
2612  GNUNET_CONTAINER_multipeermap_destroy(sh->addresses);
2613  GNUNET_free (sh->plugin);
2614  GNUNET_free (sh);
2615 }
2616
2617 /**
2618  * Load quotas for networks from configuration
2619  *
2620  * @param cfg configuration handle
2621  * @param out_dest where to write outbound quotas
2622  * @param in_dest where to write inbound quotas
2623  * @param dest_length length of inbound and outbound arrays
2624  * @return number of networks loaded
2625  */
2626 unsigned int
2627 GNUNET_ATS_solvers_load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
2628                                                  unsigned long long *out_dest,
2629                                                  unsigned long long *in_dest,
2630                                                  int dest_length)
2631 {
2632   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
2633   char * entry_in = NULL;
2634   char * entry_out = NULL;
2635   char * quota_out_str;
2636   char * quota_in_str;
2637   int c;
2638   int res;
2639
2640   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
2641   {
2642     in_dest[c] = 0;
2643     out_dest[c] = 0;
2644     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
2645     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
2646
2647     /* quota out */
2648     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
2649     {
2650       res = GNUNET_NO;
2651       if (0 == strcmp(quota_out_str, BIG_M_STRING))
2652       {
2653         out_dest[c] = GNUNET_ATS_MaxBandwidth;
2654         res = GNUNET_YES;
2655       }
2656       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
2657         res = GNUNET_YES;
2658       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,  &out_dest[c])))
2659          res = GNUNET_YES;
2660
2661       if (GNUNET_NO == res)
2662       {
2663           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
2664               network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
2665           out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2666       }
2667       else
2668       {
2669           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Outbound quota configure for network `%s' is %llu\n"),
2670               network_str[c], out_dest[c]);
2671       }
2672       GNUNET_free (quota_out_str);
2673     }
2674     else
2675     {
2676       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
2677           network_str[c], GNUNET_ATS_DefaultBandwidth);
2678       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2679     }
2680
2681     /* quota in */
2682     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
2683     {
2684       res = GNUNET_NO;
2685       if (0 == strcmp(quota_in_str, BIG_M_STRING))
2686       {
2687         in_dest[c] = GNUNET_ATS_MaxBandwidth;
2688         res = GNUNET_YES;
2689       }
2690       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
2691         res = GNUNET_YES;
2692       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,  &in_dest[c])))
2693          res = GNUNET_YES;
2694
2695       if (GNUNET_NO == res)
2696       {
2697           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
2698               network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
2699           in_dest[c] = GNUNET_ATS_DefaultBandwidth;
2700       }
2701       else
2702       {
2703           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Inbound quota configured for network `%s' is %llu\n"),
2704               network_str[c], in_dest[c]);
2705       }
2706       GNUNET_free (quota_in_str);
2707     }
2708     else
2709     {
2710       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
2711           network_str[c], GNUNET_ATS_DefaultBandwidth);
2712       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2713     }
2714     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c], in_dest[c], out_dest[c]);
2715     GNUNET_free (entry_out);
2716     GNUNET_free (entry_in);
2717   }
2718   return GNUNET_ATS_NetworkTypeCount;
2719 }
2720
2721 /**
2722  * Information callback for the solver
2723  *
2724  * @param cls the closure
2725  * @param op the solver operation
2726  * @param stat status of the solver operation
2727  * @param add additional solver information
2728  */
2729 static void
2730 solver_info_cb (void *cls,
2731     enum GAS_Solver_Operation op,
2732     enum GAS_Solver_Status stat,
2733     enum GAS_Solver_Additional_Information add)
2734 {
2735   char *add_info;
2736   switch (add) {
2737     case GAS_INFO_NONE:
2738       add_info = "GAS_INFO_NONE";
2739       break;
2740     case GAS_INFO_FULL:
2741       add_info = "GAS_INFO_MLP_FULL";
2742       break;
2743     case GAS_INFO_UPDATED:
2744       add_info = "GAS_INFO_MLP_UPDATED";
2745       break;
2746     case GAS_INFO_PROP_ALL:
2747       add_info = "GAS_INFO_PROP_ALL";
2748       break;
2749     case GAS_INFO_PROP_SINGLE:
2750       add_info = "GAS_INFO_PROP_SINGLE";
2751       break;
2752     default:
2753       add_info = "INVALID";
2754       break;
2755   }
2756
2757   switch (op)
2758   {
2759     case GAS_OP_SOLVE_START:
2760       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2761           "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
2762           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
2763       return;
2764     case GAS_OP_SOLVE_STOP:
2765       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2766           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
2767           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
2768       return;
2769
2770     case GAS_OP_SOLVE_SETUP_START:
2771       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2772           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
2773           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2774       return;
2775
2776     case GAS_OP_SOLVE_SETUP_STOP:
2777       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2778           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
2779           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2780       return;
2781
2782     case GAS_OP_SOLVE_MLP_LP_START:
2783       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2784           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
2785           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2786       return;
2787     case GAS_OP_SOLVE_MLP_LP_STOP:
2788       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2789           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
2790           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2791       return;
2792
2793     case GAS_OP_SOLVE_MLP_MLP_START:
2794       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2795           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
2796           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2797       return;
2798     case GAS_OP_SOLVE_MLP_MLP_STOP:
2799       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2800           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
2801           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2802       return;
2803     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
2804       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2805           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
2806           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2807       return;
2808     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
2809       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2810           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
2811           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2812       return;
2813     default:
2814       break;
2815     }
2816 }
2817
2818 static void
2819 solver_bandwidth_changed_cb (void *cls, struct ATS_Address *address)
2820 {
2821   if ( (0 == ntohl (address->assigned_bw_out.value__)) &&
2822        (0 == ntohl (address->assigned_bw_in.value__)) )
2823   {
2824     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2825                 "Solver notified to disconnect peer `%s'\n",
2826                 GNUNET_i2s (&address->peer));
2827     return;
2828   }
2829
2830   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2831               "Bandwidth changed addresses %s %p to %u Bps out / %u Bps in\n",
2832               GNUNET_i2s (&address->peer),
2833               address,
2834               (unsigned int) ntohl (address->assigned_bw_out.value__),
2835               (unsigned int) ntohl (address->assigned_bw_in.value__));
2836   /*if (GNUNET_YES == ph.bulk_running)
2837     GNUNET_break (0);*/
2838   return;
2839 }
2840
2841 const double *
2842 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
2843 {
2844   struct TestPeer *p;
2845   if (GNUNET_YES == opt_disable_normalization)
2846   {
2847     if (NULL == (p = find_peer_by_pid (id)))
2848       return NULL;
2849     return p->pref_abs;
2850   }
2851   else
2852     return GAS_normalization_get_preferences_by_peer (id);
2853 }
2854
2855
2856 const double *
2857 get_property_cb (void *cls, const struct ATS_Address *address)
2858 {
2859   struct TestPeer *p;
2860   struct TestAddress *a;
2861
2862   if (GNUNET_YES == opt_disable_normalization)
2863   {
2864     p = find_peer_by_pid (&address->peer);
2865     a = find_address_by_ats_address (p, address);
2866     return a->prop_abs;
2867   }
2868   else
2869     return GAS_normalization_get_properties ((struct ATS_Address *) address);
2870 }
2871
2872 static void
2873 set_updated_property ( struct ATS_Address *address, uint32_t type, double prop_rel)
2874 {
2875   struct TestPeer *p;
2876   struct TestAddress *a;
2877
2878   if (NULL == (p = find_peer_by_pid (&address->peer)))
2879   {
2880     GNUNET_break (0);
2881     return;
2882   }
2883
2884   if (NULL == (a = find_address_by_ats_address (p, address)))
2885   {
2886     GNUNET_break (0);
2887     return;
2888   }
2889   a->prop_norm[type] = prop_rel;
2890   sh->env.sf.s_address_update_property (sh->solver, address, type, a->prop_abs [type], prop_rel);
2891 }
2892
2893
2894 static void
2895 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
2896     uint32_t type, double prop_rel)
2897 {
2898   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2899       "Normalized property %s for peer `%s' changed to %.3f \n",
2900       GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2901       prop_rel);
2902
2903   set_updated_property (address, type, prop_rel);
2904 }
2905
2906 static void
2907 set_updated_preference (const struct GNUNET_PeerIdentity *peer,
2908     enum GNUNET_ATS_PreferenceKind kind,
2909     double pref_rel)
2910 {
2911   struct TestPeer *p;
2912
2913   if (NULL == (p = find_peer_by_pid (peer)))
2914   {
2915     GNUNET_break (0);
2916     return;
2917   }
2918
2919   p->pref_norm[kind] = pref_rel;
2920   sh->env.sf.s_pref (sh->solver, peer, kind, pref_rel);
2921 }
2922
2923
2924 static void
2925 normalized_preference_changed_cb (void *cls,
2926     const struct GNUNET_PeerIdentity *peer,
2927     enum GNUNET_ATS_PreferenceKind kind,
2928     double pref_rel)
2929 {
2930   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2931       "Normalized preference %s for peer `%s' changed to %.3f \n",
2932       GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer),
2933       pref_rel);
2934
2935   set_updated_preference(peer, kind, pref_rel);
2936 }
2937
2938
2939 struct SolverHandle *
2940 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
2941 {
2942   struct SolverHandle *sh;
2943   char * solver_str;
2944   int c;
2945
2946   switch (type) {
2947     case GNUNET_ATS_SOLVER_PROPORTIONAL:
2948       solver_str = "proportional";
2949       break;
2950     case GNUNET_ATS_SOLVER_MLP:
2951       solver_str = "mlp";
2952       break;
2953     case GNUNET_ATS_SOLVER_RIL:
2954       solver_str = "ril";
2955       break;
2956     default:
2957       GNUNET_break (0);
2958       return NULL;
2959       break;
2960   }
2961
2962   sh = GNUNET_new (struct SolverHandle);
2963   GNUNET_asprintf (&sh->plugin, "libgnunet_plugin_ats_%s", solver_str);
2964
2965   sh->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
2966   /* setup environment */
2967   sh->env.cfg = e->cfg;
2968   sh->env.stats = GNUNET_STATISTICS_create ("ats", e->cfg);
2969   sh->env.addresses = sh->addresses;
2970   sh->env.bandwidth_changed_cb = &solver_bandwidth_changed_cb;
2971   sh->env.get_preferences = &get_preferences_cb;
2972   sh->env.get_property = &get_property_cb;
2973   sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
2974   sh->env.info_cb = &solver_info_cb;
2975   sh->env.info_cb_cls = NULL;
2976   sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
2977   int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
2978   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
2979     sh->env.networks[c] = networks[c];
2980
2981
2982   /* start normalization */
2983   GAS_normalization_start (&normalized_preference_changed_cb, NULL,
2984       &normalized_property_changed_cb, NULL );
2985
2986   /* load quotas */
2987   if (GNUNET_ATS_NetworkTypeCount != GNUNET_ATS_solvers_load_quotas (e->cfg,
2988       sh->env.out_quota, sh->env.in_quota, GNUNET_ATS_NetworkTypeCount))
2989   {
2990     GNUNET_break(0);
2991     GNUNET_free (sh->plugin);
2992     GNUNET_free (sh);
2993     end_now ();
2994     return NULL;
2995   }
2996
2997   sh->solver = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
2998   if (NULL == sh->solver)
2999   {
3000     fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
3001     GNUNET_break(0);
3002     GNUNET_free (sh->plugin);
3003     GNUNET_free (sh);
3004     end_now ();
3005     return NULL;
3006   }
3007   return sh;
3008 }
3009
3010 static void
3011 done ()
3012 {
3013   struct TestPeer *cur;
3014   struct TestPeer *next;
3015
3016   struct TestAddress *cur_a;
3017   struct TestAddress *next_a;
3018
3019   /* Stop logging */
3020   GNUNET_ATS_solver_logging_stop (l);
3021
3022   /* Stop all preference generation */
3023   GNUNET_ATS_solver_generate_preferences_stop_all ();
3024
3025   /* Stop all property generation */
3026   GNUNET_ATS_solver_generate_property_stop_all ();
3027
3028   if (opt_print)
3029   {
3030     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Printing log information \n");
3031     GNUNET_ATS_solver_logging_eval (l);
3032   }
3033   if (opt_save)
3034   {
3035     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Saving log information \n");
3036     GNUNET_ATS_solver_logging_write_to_disk (l, e->log_append_time_stamp,
3037         e->log_output_dir);
3038   }
3039
3040   if (NULL != l)
3041   {
3042     GNUNET_ATS_solver_logging_free (l);
3043     l = NULL;
3044   }
3045
3046   /* Clean up experiment */
3047   if (NULL != e)
3048   {
3049     GNUNET_ATS_solvers_experimentation_stop (e);
3050     e = NULL;
3051   }
3052
3053   next = peer_head;
3054   while  (NULL != (cur = next))
3055   {
3056     next = cur->next;
3057     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, cur);
3058     next_a = cur->addr_head;
3059     while  (NULL != (cur_a = next_a))
3060     {
3061       next_a = cur_a->next;
3062       GNUNET_CONTAINER_DLL_remove (cur->addr_head, cur->addr_tail, cur_a);
3063       GNUNET_free (cur_a);
3064     }
3065     GNUNET_free (cur);
3066   }
3067   if (NULL != sh)
3068   {
3069     GNUNET_ATS_solvers_solver_stop (sh);
3070     sh = NULL;
3071   }
3072
3073   /* Shutdown */
3074   end_now();
3075 }
3076
3077 static void
3078 experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,int success)
3079 {
3080   if (GNUNET_OK == success)
3081     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment done successful in %s\n",
3082         GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
3083   else
3084     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
3085
3086   GNUNET_SCHEDULER_add_now (&done, NULL);
3087 }
3088
3089 static void
3090 episode_done_cb (struct Episode *ep)
3091 {
3092   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
3093 }
3094
3095
3096
3097 /**
3098  * Do shutdown
3099  */
3100 static void
3101 end_now ()
3102 {
3103   if (NULL != e)
3104   {
3105     GNUNET_ATS_solvers_experimentation_stop (e);
3106     e = NULL;
3107   }
3108   if (NULL != sh)
3109   {
3110     GNUNET_ATS_solvers_solver_stop (sh);
3111     sh = NULL;
3112   }
3113 }
3114
3115 static void
3116 run (void *cls, char * const *args, const char *cfgfile,
3117     const struct GNUNET_CONFIGURATION_Handle *cfg)
3118 {
3119   enum GNUNET_ATS_Solvers solver;
3120   int c;
3121
3122   if (NULL == opt_exp_file)
3123   {
3124     fprintf (stderr, "No experiment given ...\n");
3125     res = 1;
3126     end_now ();
3127     return;
3128   }
3129
3130   if (NULL == opt_solver)
3131   {
3132     fprintf (stderr, "No solver given ...\n");
3133     res = 1;
3134     end_now ();
3135     return;
3136   }
3137
3138   if (0 == strcmp(opt_solver, "mlp"))
3139   {
3140     solver = GNUNET_ATS_SOLVER_MLP;
3141   }
3142   else if (0 == strcmp(opt_solver, "proportional"))
3143   {
3144     solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
3145   }
3146   else if (0 == strcmp(opt_solver, "ril"))
3147   {
3148     solver = GNUNET_ATS_SOLVER_RIL;
3149   }
3150   else
3151   {
3152     fprintf (stderr, "No solver given ...");
3153     res = 1;
3154     end_now ();
3155     return;
3156   }
3157
3158   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
3159     default_properties[c] = DEFAULT_REL_QUALITY;
3160
3161   for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
3162     default_preferences[c] = DEFAULT_REL_PREFERENCE;
3163
3164   /* load experiment */
3165   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading experiment\n");
3166   e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
3167   if (NULL == e)
3168   {
3169     fprintf (stderr, "Failed to load experiment ...\n");
3170     res = 1;
3171     end_now ();
3172     return;
3173   }
3174
3175   /* load solver */
3176   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading solver\n");
3177   sh = GNUNET_ATS_solvers_solver_start (solver);
3178   if (NULL == sh)
3179   {
3180     fprintf (stderr, "Failed to start solver ...\n");
3181     end_now ();
3182     res = 1;
3183     return;
3184   }
3185
3186   /* start logging */
3187   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Start logging \n");
3188   l = GNUNET_ATS_solver_logging_start (e->log_freq);
3189
3190   /* run experiment */
3191   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Running experiment \n");
3192   GNUNET_ATS_solvers_experimentation_run (e, episode_done_cb,
3193       experiment_done_cb);
3194
3195   /* WAIT */
3196 }
3197
3198
3199 /**
3200  * Main function of the benchmark
3201  *
3202  * @param argc argument count
3203  * @param argv argument values
3204  */
3205 int
3206 main (int argc, char *argv[])
3207 {
3208   opt_exp_file = NULL;
3209   opt_solver = NULL;
3210   opt_log = GNUNET_NO;
3211   opt_save = GNUNET_NO;
3212
3213   res = 0;
3214
3215   static struct GNUNET_GETOPT_CommandLineOption options[] =
3216   {
3217     { 's', "solver", NULL,
3218         gettext_noop ("solver to use"),
3219         1, &GNUNET_GETOPT_set_string, &opt_solver},
3220     {  'e', "experiment", NULL,
3221       gettext_noop ("experiment to use"),
3222       1, &GNUNET_GETOPT_set_string, &opt_exp_file},
3223     {  'V', "verbose", NULL,
3224       gettext_noop ("be verbose"),
3225       0, &GNUNET_GETOPT_set_one, &opt_verbose},
3226     {  'p', "print", NULL,
3227       gettext_noop ("print logging"),
3228       0, &GNUNET_GETOPT_set_one, &opt_print},
3229     {  'f', "file", NULL,
3230         gettext_noop ("save logging to disk"),
3231         0, &GNUNET_GETOPT_set_one, &opt_save},
3232     {  'd', "dn", NULL,
3233         gettext_noop ("disable normalization"),
3234         0, &GNUNET_GETOPT_set_one, &opt_disable_normalization},
3235     GNUNET_GETOPT_OPTION_END
3236   };
3237
3238   GNUNET_PROGRAM_run (argc, argv, "gnunet-ats-solver-eval",
3239       NULL, options, &run, argv[0]);
3240
3241   return res;
3242 }
3243 /* end of file ats-testing-experiment.c*/
3244