simplify definition of address delete: only peer id and address id is required
[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 #if 0
1293   /* plugin */
1294   GNUNET_asprintf(&op_name, "op-%u-plugin", op_counter);
1295   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1296       sec_name, op_name, &o->plugin))
1297   {
1298     fprintf (stderr, "Missing plugin in operation %u `%s' in episode `%s'\n",
1299         op_counter, "DEL_ADDRESS", op_name);
1300     GNUNET_free (op_name);
1301     return GNUNET_SYSERR;
1302   }
1303   GNUNET_free (op_name);
1304
1305   /* address  */
1306   GNUNET_asprintf(&op_name, "op-%u-address", op_counter);
1307   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1308       sec_name, op_name, &o->address))
1309   {
1310     fprintf (stderr, "Missing address in operation %u `%s' in episode `%s'\n",
1311         op_counter, "DEL_ADDRESS", op_name);
1312     GNUNET_free (op_name);
1313     return GNUNET_SYSERR;
1314   }
1315   GNUNET_free (op_name);
1316
1317   /* session */
1318   GNUNET_asprintf(&op_name, "op-%u-address-session", op_counter);
1319   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1320       sec_name, op_name, &o->address_session))
1321   {
1322     fprintf (stderr, "Missing address-session in operation %u `%s' in episode `%s'\n",
1323         op_counter, "DEL_ADDRESS", op_name);
1324     GNUNET_free (op_name);
1325     return GNUNET_SYSERR;
1326   }
1327   GNUNET_free (op_name);
1328 #endif
1329
1330   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1331       "Found operation %s: [%llu:%llu] address `%s' plugin `%s' \n",
1332       "DEL_ADDRESS", o->peer_id, o->address_id, o->address, o->plugin);
1333
1334   return GNUNET_OK;
1335 }
1336
1337 static enum GNUNET_ATS_Property
1338 parse_preference_string (const char * str)
1339 {
1340   int c = 0;
1341   char *props[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1342
1343   for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1344     if (0 == strcmp(str, props[c]))
1345       return c;
1346   return 0;
1347 };
1348
1349 static int
1350 load_op_start_set_preference (struct GNUNET_ATS_TEST_Operation *o,
1351     struct Episode *e,
1352     int op_counter,
1353     char *sec_name,
1354     const struct GNUNET_CONFIGURATION_Handle *cfg)
1355 {
1356   char *op_name;
1357   char *type;
1358   char *pref;
1359
1360   /* peer pid */
1361   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1362   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1363       sec_name, op_name, &o->peer_id))
1364   {
1365     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1366         op_counter, "START_SET_PREFERENCE", op_name);
1367     GNUNET_free (op_name);
1368     return GNUNET_SYSERR;
1369   }
1370   GNUNET_free (op_name);
1371
1372   /* address pid */
1373   GNUNET_asprintf(&op_name, "op-%u-client-id", op_counter);
1374   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1375       sec_name, op_name, &o->client_id))
1376   {
1377     fprintf (stderr, "Missing client-id in operation %u `%s' in episode `%s'\n",
1378         op_counter, "START_SET_PREFERENCE", op_name);
1379     GNUNET_free (op_name);
1380     return GNUNET_SYSERR;
1381   }
1382   GNUNET_free (op_name);
1383
1384   /* generator */
1385   GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
1386   if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
1387           sec_name, op_name, &type)) )
1388   {
1389     fprintf (stderr, "Missing type in operation %u `%s' in episode `%s'\n",
1390         op_counter, "START_SET_PREFERENCE", op_name);
1391     GNUNET_free (op_name);
1392     return GNUNET_SYSERR;
1393   }
1394
1395   /* Load arguments for set_rate, start_send, set_preference */
1396   if (0 == strcmp (type, "constant"))
1397   {
1398     o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
1399   }
1400   else if (0 == strcmp (type, "linear"))
1401   {
1402     o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
1403   }
1404   else if (0 == strcmp (type, "sinus"))
1405   {
1406     o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
1407   }
1408   else if (0 == strcmp (type, "random"))
1409   {
1410     o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
1411   }
1412   else
1413   {
1414     fprintf (stderr, "Invalid generator type %u `%s' in episode %u\n",
1415         op_counter, op_name, e->id);
1416     GNUNET_free (type);
1417     GNUNET_free (op_name);
1418     return GNUNET_SYSERR;
1419   }
1420   GNUNET_free (type);
1421   GNUNET_free (op_name);
1422
1423
1424   /* Get base rate */
1425   GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
1426   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1427       sec_name, op_name, &o->base_rate))
1428   {
1429     fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
1430         op_counter, op_name, e->id);
1431     GNUNET_free (op_name);
1432     return GNUNET_SYSERR;
1433   }
1434   GNUNET_free (op_name);
1435
1436
1437   /* Get max rate */
1438   GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
1439   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1440       sec_name, op_name, &o->max_rate))
1441   {
1442     if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
1443         (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
1444         (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
1445     {
1446       fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
1447           op_counter, op_name, e->id);
1448       GNUNET_free (op_name);
1449       return GNUNET_SYSERR;
1450     }
1451   }
1452   GNUNET_free (op_name);
1453
1454   /* Get period */
1455   GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
1456   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1457       sec_name, op_name, &o->period))
1458   {
1459     o->period = e->duration;
1460   }
1461   GNUNET_free (op_name);
1462
1463   /* Get frequency */
1464   GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
1465   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1466       sec_name, op_name, &o->frequency))
1467   {
1468       fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
1469           op_counter, op_name, e->id);
1470       GNUNET_free (op_name);
1471       return GNUNET_SYSERR;
1472   }
1473   GNUNET_free (op_name);
1474
1475   /* Get preference */
1476   GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
1477   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1478       sec_name, op_name, &pref))
1479   {
1480       fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
1481           op_counter, op_name, e->id);
1482       GNUNET_free (op_name);
1483       return GNUNET_SYSERR;
1484   }
1485
1486   if (0 == (o->pref_type = parse_preference_string(pref)))
1487   {
1488       fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
1489           op_counter, op_name, e->id);
1490       GNUNET_free (op_name);
1491       GNUNET_free (pref);
1492       return GNUNET_SYSERR;
1493   }
1494   GNUNET_free (pref);
1495   GNUNET_free (op_name);
1496
1497   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1498       "Found operation %s: [%llu:%llu]: %s = %llu\n",
1499       "START_SET_PREFERENCE", o->peer_id, o->address_id,
1500       GNUNET_ATS_print_preference_type(o->pref_type), o->base_rate);
1501
1502   return GNUNET_OK;
1503 }
1504
1505 static int
1506 load_op_stop_set_preference (struct GNUNET_ATS_TEST_Operation *o,
1507     struct Episode *e,
1508     int op_counter,
1509     char *sec_name,
1510     const struct GNUNET_CONFIGURATION_Handle *cfg)
1511 {
1512   char *op_name;
1513   char *pref;
1514
1515   /* peer pid */
1516   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1517   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1518       sec_name, op_name, &o->peer_id))
1519   {
1520     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1521         op_counter, "STOP_SET_PREFERENCE", op_name);
1522     GNUNET_free (op_name);
1523     return GNUNET_SYSERR;
1524   }
1525   GNUNET_free (op_name);
1526
1527   /* address pid */
1528   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1529   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1530       sec_name, op_name, &o->address_id))
1531   {
1532     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1533         op_counter, "STOP_SET_PREFERENCE", op_name);
1534     GNUNET_free (op_name);
1535     return GNUNET_SYSERR;
1536   }
1537   GNUNET_free (op_name);
1538
1539   /* Get preference */
1540   GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
1541   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1542       sec_name, op_name, &pref))
1543   {
1544     fprintf (stderr, "Missing preference in operation %u `%s' in episode `%s'\n",
1545         op_counter, "STOP_SET_PREFERENCE", op_name);
1546       GNUNET_free (op_name);
1547       return GNUNET_SYSERR;
1548   }
1549
1550   if (0 == (o->pref_type = parse_preference_string(pref)))
1551   {
1552       fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
1553           op_counter, op_name, e->id);
1554       GNUNET_free (op_name);
1555       GNUNET_free (pref);
1556       return GNUNET_SYSERR;
1557   }
1558   GNUNET_free (pref);
1559   GNUNET_free (op_name);
1560
1561   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1562       "Found operation %s: [%llu:%llu]: %s\n",
1563       "STOP_SET_PREFERENCE", o->peer_id, o->address_id,
1564       GNUNET_ATS_print_preference_type(o->pref_type));
1565   return GNUNET_OK;
1566 }
1567
1568 static enum GNUNET_ATS_Property
1569 parse_property_string (const char * str)
1570 {
1571   int c = 0;
1572   char *props[GNUNET_ATS_PropertyCount] = GNUNET_ATS_PropertyStrings;
1573
1574   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
1575     if (0 == strcmp(str, props[c]))
1576       return c;
1577   return 0;
1578 };
1579
1580 static int
1581 load_op_start_set_property(struct GNUNET_ATS_TEST_Operation *o,
1582     struct Episode *e,
1583     int op_counter,
1584     char *sec_name,
1585     const struct GNUNET_CONFIGURATION_Handle *cfg)
1586 {
1587   char *op_name;
1588   char *type;
1589   char *prop;
1590
1591   /* peer pid */
1592   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1593   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1594       sec_name, op_name, &o->peer_id))
1595   {
1596     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1597         op_counter, "START_SET_PROPERTY", op_name);
1598     GNUNET_free (op_name);
1599     return GNUNET_SYSERR;
1600   }
1601   GNUNET_free (op_name);
1602
1603   /* address pid */
1604   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1605   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1606       sec_name, op_name, &o->address_id))
1607   {
1608     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1609         op_counter, "START_SET_PROPERTY", op_name);
1610     GNUNET_free (op_name);
1611     return GNUNET_SYSERR;
1612   }
1613   GNUNET_free (op_name);
1614
1615   /* generator */
1616   GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
1617   if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
1618           sec_name, op_name, &type)) )
1619   {
1620     fprintf (stderr, "Missing type in operation %u `%s' in episode `%s'\n",
1621         op_counter, "START_SET_PROPERTY", op_name);
1622     GNUNET_free (op_name);
1623     return GNUNET_SYSERR;
1624   }
1625
1626   /* Load arguments for set_rate, start_send, set_preference */
1627   if (0 == strcmp (type, "constant"))
1628   {
1629     o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
1630   }
1631   else if (0 == strcmp (type, "linear"))
1632   {
1633     o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
1634   }
1635   else if (0 == strcmp (type, "sinus"))
1636   {
1637     o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
1638   }
1639   else if (0 == strcmp (type, "random"))
1640   {
1641     o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
1642   }
1643   else
1644   {
1645     fprintf (stderr, "Invalid generator type %u `%s' in episode %u\n",
1646         op_counter, op_name, e->id);
1647     GNUNET_free (type);
1648     GNUNET_free (op_name);
1649     return GNUNET_SYSERR;
1650   }
1651   GNUNET_free (type);
1652   GNUNET_free (op_name);
1653
1654
1655   /* Get base rate */
1656   GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
1657   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1658       sec_name, op_name, &o->base_rate))
1659   {
1660     fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
1661         op_counter, op_name, e->id);
1662     GNUNET_free (op_name);
1663     return GNUNET_SYSERR;
1664   }
1665   GNUNET_free (op_name);
1666
1667
1668   /* Get max rate */
1669   GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
1670   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1671       sec_name, op_name, &o->max_rate))
1672   {
1673     if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
1674         (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
1675         (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
1676     {
1677       fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
1678           op_counter, op_name, e->id);
1679       GNUNET_free (op_name);
1680       return GNUNET_SYSERR;
1681     }
1682   }
1683   GNUNET_free (op_name);
1684
1685   /* Get period */
1686   GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
1687   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1688       sec_name, op_name, &o->period))
1689   {
1690     o->period = e->duration;
1691   }
1692   GNUNET_free (op_name);
1693
1694   /* Get frequency */
1695   GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
1696   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1697       sec_name, op_name, &o->frequency))
1698   {
1699       fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
1700           op_counter, op_name, e->id);
1701       GNUNET_free (op_name);
1702       return GNUNET_SYSERR;
1703   }
1704   GNUNET_free (op_name);
1705
1706   /* Get preference */
1707   GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
1708   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1709       sec_name, op_name, &prop))
1710   {
1711       fprintf (stderr, "Missing property in operation %u `%s' in episode %u\n",
1712           op_counter, op_name, e->id);
1713       GNUNET_free (op_name);
1714       GNUNET_free_non_null (prop);
1715       return GNUNET_SYSERR;
1716   }
1717
1718   if (0 == (o->prop_type = parse_property_string(prop)))
1719   {
1720       fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
1721           op_counter, op_name, e->id);
1722       GNUNET_free (op_name);
1723       GNUNET_free (prop);
1724       return GNUNET_SYSERR;
1725   }
1726
1727   GNUNET_free (prop);
1728   GNUNET_free (op_name);
1729
1730   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1731       "Found operation %s: [%llu:%llu] %s = %llu\n",
1732       "START_SET_PROPERTY", o->peer_id, o->address_id,
1733       GNUNET_ATS_print_property_type (o->prop_type), o->base_rate);
1734
1735   return GNUNET_OK;
1736 }
1737
1738 static int
1739 load_op_stop_set_property (struct GNUNET_ATS_TEST_Operation *o,
1740     struct Episode *e,
1741     int op_counter,
1742     char *sec_name,
1743     const struct GNUNET_CONFIGURATION_Handle *cfg)
1744 {
1745   char *op_name;
1746   char *pref;
1747
1748   /* peer pid */
1749   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1750   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1751       sec_name, op_name, &o->peer_id))
1752   {
1753     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1754         op_counter, "STOP_SET_PROPERTY", op_name);
1755     GNUNET_free (op_name);
1756     return GNUNET_SYSERR;
1757   }
1758   GNUNET_free (op_name);
1759
1760   /* address pid */
1761   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1762   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1763       sec_name, op_name, &o->address_id))
1764   {
1765     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1766         op_counter, "STOP_SET_PROPERTY", op_name);
1767     GNUNET_free (op_name);
1768     return GNUNET_SYSERR;
1769   }
1770   GNUNET_free (op_name);
1771
1772   /* Get property */
1773   GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
1774   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1775       sec_name, op_name, &pref))
1776   {
1777     fprintf (stderr, "Missing property in operation %u `%s' in episode `%s'\n",
1778         op_counter, "STOP_SET_PROPERTY", op_name);
1779       GNUNET_free (op_name);
1780       GNUNET_free_non_null (pref);
1781       return GNUNET_SYSERR;
1782   }
1783
1784   if (0 == (o->prop_type = parse_property_string(pref)))
1785   {
1786       fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
1787           op_counter, op_name, e->id);
1788       GNUNET_free (op_name);
1789       GNUNET_free_non_null (pref);
1790       return GNUNET_SYSERR;
1791   }
1792
1793   GNUNET_free (pref);
1794   GNUNET_free (op_name);
1795
1796   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1797       "Found operation %s: [%llu:%llu] %s\n",
1798       "STOP_SET_PROPERTY", o->peer_id, o->address_id,
1799       GNUNET_ATS_print_property_type (o->prop_type));
1800
1801   return GNUNET_OK;
1802 }
1803
1804
1805 static int
1806 load_op_start_request (struct GNUNET_ATS_TEST_Operation *o,
1807     struct Episode *e,
1808     int op_counter,
1809     char *sec_name,
1810     const struct GNUNET_CONFIGURATION_Handle *cfg)
1811 {
1812   char *op_name;
1813
1814   /* peer pid */
1815   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1816   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1817       sec_name, op_name, &o->peer_id))
1818   {
1819     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1820         op_counter, "START_REQUEST", op_name);
1821     GNUNET_free (op_name);
1822     return GNUNET_SYSERR;
1823   }
1824   GNUNET_free (op_name);
1825   return GNUNET_OK;
1826 }
1827
1828 static int
1829 load_op_stop_request (struct GNUNET_ATS_TEST_Operation *o,
1830     struct Episode *e,
1831     int op_counter,
1832     char *sec_name,
1833     const struct GNUNET_CONFIGURATION_Handle *cfg)
1834 {
1835   char *op_name;
1836
1837   /* peer pid */
1838   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1839   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1840       sec_name, op_name, &o->peer_id))
1841   {
1842     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1843         op_counter, "STOP_REQUEST", op_name);
1844     GNUNET_free (op_name);
1845     return GNUNET_SYSERR;
1846   }
1847   GNUNET_free (op_name);
1848   return GNUNET_OK;
1849 }
1850
1851
1852 static int
1853 load_episode (struct Experiment *e, struct Episode *cur,
1854     struct GNUNET_CONFIGURATION_Handle *cfg)
1855 {
1856   struct GNUNET_ATS_TEST_Operation *o;
1857   char *sec_name;
1858   char *op_name;
1859   char *op;
1860   int op_counter = 0;
1861   int res;
1862   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Parsing episode %u\n",cur->id);
1863   GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
1864
1865   while (1)
1866   {
1867     /* Load operation */
1868     GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
1869     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
1870         sec_name, op_name, &op))
1871     {
1872       GNUNET_free (op_name);
1873       break;
1874     }
1875     o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
1876     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "==== Parsing operation %u: `%s'\n",
1877         cur->id, op_name);
1878
1879     /* operations = set_rate, start_send, stop_send, set_preference */
1880     if (0 == strcmp (op, "address_add"))
1881     {
1882       o->type = SOLVER_OP_ADD_ADDRESS;
1883       res = load_op_add_address (o, cur,
1884           op_counter, sec_name, cfg);
1885     }
1886     else if (0 == strcmp (op, "address_del"))
1887     {
1888       o->type = SOLVER_OP_DEL_ADDRESS;
1889       res = load_op_del_address (o, cur,
1890           op_counter, sec_name, cfg);
1891     }
1892     else if (0 == strcmp (op, "start_set_property"))
1893     {
1894       o->type = SOLVER_OP_START_SET_PROPERTY;
1895       res = load_op_start_set_property (o, cur,
1896           op_counter, sec_name, cfg);
1897     }
1898     else if (0 == strcmp (op, "stop_set_property"))
1899     {
1900       o->type = SOLVER_OP_STOP_SET_PROPERTY;
1901       res = load_op_stop_set_property (o, cur,
1902           op_counter, sec_name, cfg);
1903     }
1904     else if (0 == strcmp (op, "start_set_preference"))
1905     {
1906       o->type = SOLVER_OP_START_SET_PREFERENCE;
1907       res =  load_op_start_set_preference (o, cur,
1908           op_counter, sec_name, cfg);
1909     }
1910     else if (0 == strcmp (op, "stop_set_preference"))
1911     {
1912       o->type = SOLVER_OP_STOP_SET_PREFERENCE;
1913       res =  load_op_stop_set_preference (o, cur,
1914           op_counter, sec_name, cfg);
1915     }
1916     else if (0 == strcmp (op, "start_request"))
1917     {
1918       o->type = SOLVER_OP_START_REQUEST;
1919       res = load_op_start_request (o, cur,
1920           op_counter, sec_name, cfg);
1921     }
1922     else if (0 == strcmp (op, "stop_request"))
1923     {
1924       o->type = SOLVER_OP_STOP_REQUEST;
1925       res = load_op_stop_request(o, cur,
1926           op_counter, sec_name, cfg);
1927     }
1928     else
1929     {
1930       fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
1931           op_counter, op, cur->id);
1932       res = GNUNET_SYSERR;
1933     }
1934
1935     GNUNET_free (op);
1936     GNUNET_free (op_name);
1937
1938     if (GNUNET_SYSERR == res)
1939     {
1940       GNUNET_free (o);
1941       GNUNET_free (sec_name);
1942       return GNUNET_SYSERR;
1943     }
1944
1945     GNUNET_CONTAINER_DLL_insert_tail (cur->head,cur->tail, o);
1946     op_counter++;
1947   }
1948   GNUNET_free (sec_name);
1949   return GNUNET_OK;
1950 }
1951
1952 static int
1953 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
1954 {
1955   int e_counter = 0;
1956   char *sec_name;
1957   struct GNUNET_TIME_Relative e_duration;
1958   struct Episode *cur;
1959   struct Episode *last;
1960
1961   e_counter = 0;
1962   last = NULL;
1963   while (1)
1964   {
1965     GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
1966     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
1967         sec_name, "duration", &e_duration))
1968     {
1969       GNUNET_free (sec_name);
1970       break;
1971     }
1972
1973     cur = GNUNET_new (struct Episode);
1974     cur->duration = e_duration;
1975     cur->id = e_counter;
1976
1977     if (GNUNET_OK != load_episode (e, cur, cfg))
1978     {
1979       GNUNET_free (sec_name);
1980       GNUNET_free (cur);
1981       return GNUNET_SYSERR;
1982     }
1983
1984     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Found episode %u with duration %s \n",
1985         e_counter,
1986         GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
1987
1988     /* Update experiment */
1989     e->num_episodes ++;
1990     e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
1991     /* Put in linked list */
1992     if (NULL == last)
1993       e->start = cur;
1994     else
1995       last->next = cur;
1996
1997     GNUNET_free (sec_name);
1998     e_counter ++;
1999     last = cur;
2000   }
2001   return e_counter;
2002 }
2003
2004 static void
2005 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
2006 {
2007   struct Experiment *e = cls;
2008   e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2009   fprintf (stderr, "Experiment timeout!\n");
2010
2011   if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
2012   {
2013     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
2014     e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2015   }
2016
2017   e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
2018       GNUNET_SYSERR);
2019 }
2020
2021 struct ATS_Address *
2022 create_ats_address (const struct GNUNET_PeerIdentity *peer,
2023                 const char *plugin_name,
2024                 const void *plugin_addr, size_t plugin_addr_len,
2025                 uint32_t session_id)
2026 {
2027   struct ATS_Address *aa = NULL;
2028
2029   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len + strlen (plugin_name) + 1);
2030   aa->peer = *peer;
2031   aa->addr_len = plugin_addr_len;
2032   aa->addr = &aa[1];
2033   aa->plugin = (char *) &aa[1] + plugin_addr_len;
2034   memcpy (&aa[1], plugin_addr, plugin_addr_len);
2035   memcpy (aa->plugin, plugin_name, strlen (plugin_name) + 1);
2036   aa->session_id = session_id;
2037   aa->active = GNUNET_NO;
2038   aa->used = GNUNET_NO;
2039   aa->solver_information = NULL;
2040   aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
2041   aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
2042   return aa;
2043 }
2044
2045
2046
2047 static void
2048 enforce_add_address (struct GNUNET_ATS_TEST_Operation *op)
2049 {
2050   struct TestPeer *p;
2051   struct TestAddress *a;
2052   int c;
2053
2054   if (NULL == (p = find_peer_by_id (op->peer_id)))
2055   {
2056     p = GNUNET_new (struct TestPeer);
2057     p->id = op->peer_id;
2058     memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
2059     for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
2060     {
2061       p->pref_abs[c] = DEFAULT_ABS_PREFERENCE;
2062       p->pref_norm[c] = DEFAULT_REL_PREFERENCE;
2063     }
2064
2065     GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, p);
2066   }
2067
2068   if (NULL != (find_address_by_id (p, op->address_id)))
2069   {
2070     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Duplicate address %u for peer %u\n",
2071         op->address_id, op->peer_id);
2072     return;
2073   }
2074
2075   a = GNUNET_new (struct TestAddress);
2076   a->aid = op->address_id;
2077   a->network = op->address_network;
2078   a->ats_addr = create_ats_address (&p->peer_id, op->plugin, op->address,
2079       strlen (op->address) + 1, op->address_session);
2080   memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
2081   GNUNET_CONTAINER_DLL_insert (p->addr_head, p->addr_tail, a);
2082
2083   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
2084     a->prop_norm[c] = DEFAULT_REL_QUALITY;
2085
2086   GNUNET_CONTAINER_multipeermap_put (sh->addresses, &p->peer_id, a->ats_addr,
2087     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2088
2089   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Adding address %u for peer %u in network `%s'\n",
2090     op->address_id, op->peer_id, GNUNET_ATS_print_network_type(op->address_network));
2091
2092   sh->env.sf.s_add (sh->solver, a->ats_addr, op->address_network);
2093
2094 }
2095
2096
2097 static void
2098 enforce_del_address (struct GNUNET_ATS_TEST_Operation *op)
2099 {
2100   struct TestPeer *p;
2101   struct TestAddress *a;
2102   struct PropertyGenerator *pg;
2103
2104   if (NULL == (p = find_peer_by_id (op->peer_id)))
2105   {
2106     GNUNET_break (0);
2107     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2108         "Deleting address for unknown peer %u\n", op->peer_id);
2109     return;
2110   }
2111
2112   if (NULL == (a =find_address_by_id (p, op->address_id)))
2113   {
2114     GNUNET_break (0);
2115     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2116         "Deleting address for unknown peer %u\n", op->peer_id);
2117     return;
2118   }
2119
2120   while (NULL != (pg = find_prop_gen (p->id, a->aid, 0)))
2121   {
2122     GNUNET_ATS_solver_generate_property_stop (pg);
2123   }
2124
2125   GNUNET_CONTAINER_DLL_remove(p->addr_head, p->addr_tail, a);
2126   GNUNET_CONTAINER_multipeermap_remove (sh->addresses, &p->peer_id, a->ats_addr);
2127
2128   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Removing address %u for peer %u\n",
2129       op->address_id, op->peer_id);
2130
2131   sh->env.sf.s_del (sh->solver, a->ats_addr, GNUNET_NO);
2132   GNUNET_free (a->ats_addr);
2133   GNUNET_free (a);
2134
2135 }
2136
2137 static void
2138 enforce_start_property (struct GNUNET_ATS_TEST_Operation *op)
2139 {
2140   struct PropertyGenerator *pg;
2141   struct TestPeer *p;
2142   struct TestAddress *a;
2143
2144   if (NULL != (pg = find_prop_gen (op->peer_id, op->address_id, op->prop_type)))
2145   {
2146     GNUNET_ATS_solver_generate_property_stop (pg);
2147     GNUNET_free (pg);
2148   }
2149
2150   if (NULL == (p = find_peer_by_id (op->peer_id)))
2151   {
2152     GNUNET_break (0);
2153     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2154         "Starting property generation for unknown peer %u\n", op->peer_id);
2155     return;
2156   }
2157
2158   if (NULL == (a = find_address_by_id (p, op->address_id)))
2159   {
2160     GNUNET_break (0);
2161     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2162         "Setting property for unknown address %u\n", op->peer_id);
2163     return;
2164   }
2165
2166   GNUNET_ATS_solver_generate_property_start (op->peer_id,
2167     op->address_id,
2168     p, a,
2169     op->gen_type,
2170     op->base_rate,
2171     op->max_rate,
2172     op->period,
2173     op->frequency,
2174     op->prop_type);
2175 }
2176
2177 static void
2178 enforce_stop_property (struct GNUNET_ATS_TEST_Operation *op)
2179 {
2180   struct PropertyGenerator *pg = find_prop_gen(op->peer_id, op->address_id,
2181       op->prop_type);
2182   if (NULL != pg)
2183       GNUNET_ATS_solver_generate_property_stop (pg);
2184 }
2185
2186 static void
2187 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
2188 {
2189   struct PreferenceGenerator *pg;
2190   if (NULL != (pg = find_pref_gen (op->peer_id, op->pref_type)))
2191   {
2192     GNUNET_ATS_solver_generate_preferences_stop (pg);
2193     GNUNET_free (pg);
2194   }
2195
2196   if (NULL == (find_peer_by_id (op->peer_id)))
2197   {
2198     GNUNET_break (0);
2199     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2200         "Starting preference generation for unknown peer %u\n", op->peer_id);
2201     return;
2202   }
2203
2204   GNUNET_ATS_solver_generate_preferences_start (op->peer_id,
2205     op->address_id,
2206     op->client_id,
2207     op->gen_type,
2208     op->base_rate,
2209     op->max_rate,
2210     op->period,
2211     op->frequency,
2212     op->pref_type);
2213 }
2214
2215 static void
2216 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
2217 {
2218   struct PreferenceGenerator *pg = find_pref_gen(op->peer_id,
2219       op->pref_type);
2220   if (NULL != pg)
2221       GNUNET_ATS_solver_generate_preferences_stop (pg);
2222 }
2223
2224
2225 static void
2226 enforce_start_request (struct GNUNET_ATS_TEST_Operation *op)
2227 {
2228   struct TestPeer *p;
2229   const struct ATS_Address *res;
2230
2231   if (NULL == (p = find_peer_by_id (op->peer_id)))
2232   {
2233     GNUNET_break (0);
2234     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2235         "Requesting address for unknown peer %u\n", op->peer_id);
2236     return;
2237   }
2238
2239   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Requesting address for peer %u\n",
2240       op->peer_id);
2241
2242   res = sh->env.sf.s_get (sh->solver, &p->peer_id);
2243   if (NULL != res)
2244   {
2245
2246   }
2247 }
2248
2249 static void
2250 enforce_stop_request (struct GNUNET_ATS_TEST_Operation *op)
2251 {
2252   struct TestPeer *p;
2253
2254   if (NULL == (p = find_peer_by_id (op->peer_id)))
2255   {
2256     GNUNET_break (0);
2257     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2258         "Requesting address for unknown peer %u\n", op->peer_id);
2259     return;
2260   }
2261
2262   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stop requesting address for peer %u\n",
2263       op->peer_id);
2264
2265   sh->env.sf.s_get_stop (sh->solver, &p->peer_id);
2266 }
2267
2268 static void enforce_episode (struct Episode *ep)
2269 {
2270   struct GNUNET_ATS_TEST_Operation *cur;
2271   for (cur = ep->head; NULL != cur; cur = cur->next)
2272   {
2273     switch (cur->type) {
2274       case SOLVER_OP_ADD_ADDRESS:
2275         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
2276             print_op (cur->type), cur->peer_id, cur->address_id);
2277         enforce_add_address (cur);
2278         break;
2279       case SOLVER_OP_DEL_ADDRESS:
2280         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
2281             print_op (cur->type), cur->peer_id, cur->address_id);
2282         enforce_del_address (cur);
2283         break;
2284       case SOLVER_OP_START_SET_PROPERTY:
2285         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2286             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2287         enforce_start_property (cur);
2288         break;
2289       case SOLVER_OP_STOP_SET_PROPERTY:
2290         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2291             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2292         enforce_stop_property (cur);
2293         break;
2294       case SOLVER_OP_START_SET_PREFERENCE:
2295         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2296             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2297         enforce_start_preference (cur);
2298         break;
2299       case SOLVER_OP_STOP_SET_PREFERENCE:
2300         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2301             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2302         enforce_stop_preference (cur);
2303         break;
2304       case SOLVER_OP_START_REQUEST:
2305         fprintf (stderr, "Enforcing operation: %s [%llu]\n",
2306             print_op (cur->type), cur->peer_id);
2307         enforce_start_request (cur);
2308         break;
2309       case SOLVER_OP_STOP_REQUEST:
2310         fprintf (stderr, "Enforcing operation: %s [%llu]\n",
2311             print_op (cur->type), cur->peer_id);
2312         enforce_stop_request (cur);
2313         break;
2314       default:
2315         break;
2316     }
2317   }
2318 }
2319
2320 static void
2321 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
2322 {
2323   struct Experiment *e = cls;
2324   e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2325   if (NULL != e->ep_done_cb)
2326     e->ep_done_cb (e->cur);
2327
2328   /* Scheduling next */
2329   e->cur = e->cur->next;
2330   if (NULL == e->cur)
2331   {
2332     /* done */
2333     fprintf (stderr, "Last episode done!\n");
2334     if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
2335     {
2336       GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
2337       e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2338     }
2339     e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
2340     return;
2341   }
2342
2343   fprintf (stderr, "Running episode %u with timeout %s\n",
2344       e->cur->id,
2345       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
2346   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
2347       &timeout_episode, e);
2348   enforce_episode(e->cur);
2349
2350
2351 }
2352
2353
2354 void
2355 GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
2356     GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
2357     GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
2358 {
2359   fprintf (stderr, "Running experiment `%s'  with timeout %s\n", e->name,
2360       GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
2361   e->e_done_cb = e_done_cb;
2362   e->ep_done_cb = ep_done_cb;
2363   e->start_time = GNUNET_TIME_absolute_get();
2364
2365   /* Start total time out */
2366   e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
2367       &timeout_experiment, e);
2368
2369   /* Start */
2370   if (NULL == e->start)
2371   {
2372     GNUNET_break (0);
2373     return;
2374   }
2375
2376   e->cur = e->start;
2377   fprintf (stderr, "Running episode %u with timeout %s\n",
2378       e->cur->id,
2379       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
2380   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
2381       &timeout_episode, e);
2382   enforce_episode(e->cur);
2383
2384 }
2385
2386 void
2387 GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
2388 {
2389   if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
2390   {
2391     GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
2392     e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2393   }
2394   if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
2395   {
2396     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
2397     e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2398   }
2399   if (NULL != e->cfg)
2400   {
2401     GNUNET_CONFIGURATION_destroy(e->cfg);
2402     e->cfg = NULL;
2403   }
2404   free_experiment (e);
2405 }
2406
2407
2408 struct Experiment *
2409 GNUNET_ATS_solvers_experimentation_load (char *filename)
2410 {
2411   struct Experiment *e;
2412   struct GNUNET_CONFIGURATION_Handle *cfg;
2413   e = NULL;
2414
2415   cfg = GNUNET_CONFIGURATION_create();
2416   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
2417   {
2418     fprintf (stderr, "Failed to load `%s'\n", filename);
2419     GNUNET_CONFIGURATION_destroy (cfg);
2420     return NULL;
2421   }
2422
2423   e = create_experiment ();
2424
2425   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
2426       "name", &e->name))
2427   {
2428     fprintf (stderr, "Invalid %s", "name");
2429     free_experiment (e);
2430     return NULL;
2431   }
2432   else
2433     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment name: `%s'\n", e->name);
2434
2435   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
2436       "log_prefix", &e->log_prefix))
2437   {
2438     fprintf (stderr, "Invalid %s", "name");
2439     free_experiment (e);
2440     return NULL;
2441   }
2442   else
2443     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging prefix: `%s'\n",
2444         e->log_prefix);
2445
2446   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
2447       "log_output_dir", &e->log_output_dir))
2448   {
2449     e->log_output_dir = NULL;
2450   }
2451   else
2452     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging output directory: `%s'\n",
2453         e->log_output_dir);
2454
2455
2456   if (GNUNET_SYSERR == (e->log_append_time_stamp = GNUNET_CONFIGURATION_get_value_yesno(cfg,
2457       "experiment", "log_append_time_stamp")))
2458     e->log_append_time_stamp = GNUNET_YES;
2459   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging append timestamp: `%s'\n",
2460       (GNUNET_YES == e->log_append_time_stamp) ? "yes" : "no");
2461
2462
2463   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
2464       "cfg_file", &e->cfg_file))
2465   {
2466     fprintf (stderr, "Invalid %s", "cfg_file");
2467     free_experiment (e);
2468     return NULL;
2469   }
2470   else
2471   {
2472     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment configuration: `%s'\n", e->cfg_file);
2473     e->cfg = GNUNET_CONFIGURATION_create();
2474     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (e->cfg, e->cfg_file))
2475     {
2476       fprintf (stderr, "Invalid configuration %s", "cfg_file");
2477       free_experiment (e);
2478       return NULL;
2479     }
2480
2481   }
2482
2483   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
2484       "log_freq", &e->log_freq))
2485   {
2486     fprintf (stderr, "Invalid %s", "log_freq");
2487     free_experiment (e);
2488     return NULL;
2489   }
2490   else
2491     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging frequency: `%s'\n",
2492         GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
2493
2494   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
2495       "max_duration", &e->max_duration))
2496   {
2497     fprintf (stderr, "Invalid %s", "max_duration");
2498     free_experiment (e);
2499     return NULL;
2500   }
2501   else
2502     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment duration: `%s'\n",
2503         GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
2504
2505   if (GNUNET_SYSERR == load_episodes (e, cfg))
2506   {
2507     GNUNET_ATS_solvers_experimentation_stop (e);
2508     GNUNET_CONFIGURATION_destroy (cfg);
2509     e = NULL;
2510     fprintf (stderr, "Failed to load experiment\n");
2511     return NULL;
2512   }
2513   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loaded %u episodes with total duration %s\n",
2514       e->num_episodes,
2515       GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
2516
2517   GNUNET_CONFIGURATION_destroy (cfg);
2518   return e;
2519 }
2520
2521
2522
2523 /**
2524  * Solver
2525  */
2526
2527 static int
2528 free_all_it (void *cls,
2529     const struct GNUNET_PeerIdentity *key,
2530     void *value)
2531 {
2532   struct ATS_Address *address = value;
2533   GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (sh->env.addresses,
2534       key, value));
2535   GNUNET_free (address);
2536
2537   return GNUNET_OK;
2538 }
2539
2540 void
2541 GNUNET_ATS_solvers_solver_stop (struct SolverHandle *sh)
2542 {
2543  GNUNET_STATISTICS_destroy ((struct GNUNET_STATISTICS_Handle *) sh->env.stats,
2544      GNUNET_NO);
2545  GNUNET_PLUGIN_unload (sh->plugin, sh->solver);
2546
2547  GAS_normalization_stop();
2548
2549  GNUNET_CONTAINER_multipeermap_iterate (sh->addresses, &free_all_it, NULL);
2550  GNUNET_CONTAINER_multipeermap_destroy(sh->addresses);
2551  GNUNET_free (sh->plugin);
2552  GNUNET_free (sh);
2553 }
2554
2555 /**
2556  * Load quotas for networks from configuration
2557  *
2558  * @param cfg configuration handle
2559  * @param out_dest where to write outbound quotas
2560  * @param in_dest where to write inbound quotas
2561  * @param dest_length length of inbound and outbound arrays
2562  * @return number of networks loaded
2563  */
2564 unsigned int
2565 GNUNET_ATS_solvers_load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
2566                                                  unsigned long long *out_dest,
2567                                                  unsigned long long *in_dest,
2568                                                  int dest_length)
2569 {
2570   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
2571   char * entry_in = NULL;
2572   char * entry_out = NULL;
2573   char * quota_out_str;
2574   char * quota_in_str;
2575   int c;
2576   int res;
2577
2578   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
2579   {
2580     in_dest[c] = 0;
2581     out_dest[c] = 0;
2582     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
2583     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
2584
2585     /* quota out */
2586     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
2587     {
2588       res = GNUNET_NO;
2589       if (0 == strcmp(quota_out_str, BIG_M_STRING))
2590       {
2591         out_dest[c] = GNUNET_ATS_MaxBandwidth;
2592         res = GNUNET_YES;
2593       }
2594       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
2595         res = GNUNET_YES;
2596       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,  &out_dest[c])))
2597          res = GNUNET_YES;
2598
2599       if (GNUNET_NO == res)
2600       {
2601           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
2602               network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
2603           out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2604       }
2605       else
2606       {
2607           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Outbound quota configure for network `%s' is %llu\n"),
2608               network_str[c], out_dest[c]);
2609       }
2610       GNUNET_free (quota_out_str);
2611     }
2612     else
2613     {
2614       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
2615           network_str[c], GNUNET_ATS_DefaultBandwidth);
2616       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2617     }
2618
2619     /* quota in */
2620     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
2621     {
2622       res = GNUNET_NO;
2623       if (0 == strcmp(quota_in_str, BIG_M_STRING))
2624       {
2625         in_dest[c] = GNUNET_ATS_MaxBandwidth;
2626         res = GNUNET_YES;
2627       }
2628       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
2629         res = GNUNET_YES;
2630       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,  &in_dest[c])))
2631          res = GNUNET_YES;
2632
2633       if (GNUNET_NO == res)
2634       {
2635           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
2636               network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
2637           in_dest[c] = GNUNET_ATS_DefaultBandwidth;
2638       }
2639       else
2640       {
2641           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Inbound quota configured for network `%s' is %llu\n"),
2642               network_str[c], in_dest[c]);
2643       }
2644       GNUNET_free (quota_in_str);
2645     }
2646     else
2647     {
2648       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
2649           network_str[c], GNUNET_ATS_DefaultBandwidth);
2650       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2651     }
2652     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]);
2653     GNUNET_free (entry_out);
2654     GNUNET_free (entry_in);
2655   }
2656   return GNUNET_ATS_NetworkTypeCount;
2657 }
2658
2659 /**
2660  * Information callback for the solver
2661  *
2662  * @param cls the closure
2663  * @param op the solver operation
2664  * @param stat status of the solver operation
2665  * @param add additional solver information
2666  */
2667 static void
2668 solver_info_cb (void *cls,
2669     enum GAS_Solver_Operation op,
2670     enum GAS_Solver_Status stat,
2671     enum GAS_Solver_Additional_Information add)
2672 {
2673   char *add_info;
2674   switch (add) {
2675     case GAS_INFO_NONE:
2676       add_info = "GAS_INFO_NONE";
2677       break;
2678     case GAS_INFO_FULL:
2679       add_info = "GAS_INFO_MLP_FULL";
2680       break;
2681     case GAS_INFO_UPDATED:
2682       add_info = "GAS_INFO_MLP_UPDATED";
2683       break;
2684     case GAS_INFO_PROP_ALL:
2685       add_info = "GAS_INFO_PROP_ALL";
2686       break;
2687     case GAS_INFO_PROP_SINGLE:
2688       add_info = "GAS_INFO_PROP_SINGLE";
2689       break;
2690     default:
2691       add_info = "INVALID";
2692       break;
2693   }
2694
2695   switch (op)
2696   {
2697     case GAS_OP_SOLVE_START:
2698       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2699           "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
2700           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
2701       return;
2702     case GAS_OP_SOLVE_STOP:
2703       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2704           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
2705           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
2706       return;
2707
2708     case GAS_OP_SOLVE_SETUP_START:
2709       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2710           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
2711           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2712       return;
2713
2714     case GAS_OP_SOLVE_SETUP_STOP:
2715       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2716           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
2717           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2718       return;
2719
2720     case GAS_OP_SOLVE_MLP_LP_START:
2721       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2722           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
2723           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2724       return;
2725     case GAS_OP_SOLVE_MLP_LP_STOP:
2726       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2727           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
2728           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2729       return;
2730
2731     case GAS_OP_SOLVE_MLP_MLP_START:
2732       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2733           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
2734           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2735       return;
2736     case GAS_OP_SOLVE_MLP_MLP_STOP:
2737       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2738           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
2739           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2740       return;
2741     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
2742       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2743           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
2744           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2745       return;
2746     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
2747       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2748           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
2749           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2750       return;
2751     default:
2752       break;
2753     }
2754 }
2755
2756 static void
2757 solver_bandwidth_changed_cb (void *cls, struct ATS_Address *address)
2758 {
2759   if ( (0 == ntohl (address->assigned_bw_out.value__)) &&
2760        (0 == ntohl (address->assigned_bw_in.value__)) )
2761   {
2762     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2763                 "Solver notified to disconnect peer `%s'\n",
2764                 GNUNET_i2s (&address->peer));
2765     return;
2766   }
2767
2768   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2769               "Bandwidth changed addresses %s %p to %u Bps out / %u Bps in\n",
2770               GNUNET_i2s (&address->peer),
2771               address,
2772               (unsigned int) ntohl (address->assigned_bw_out.value__),
2773               (unsigned int) ntohl (address->assigned_bw_in.value__));
2774   /*if (GNUNET_YES == ph.bulk_running)
2775     GNUNET_break (0);*/
2776   return;
2777 }
2778
2779 const double *
2780 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
2781 {
2782   struct TestPeer *p;
2783   if (GNUNET_YES == opt_disable_normalization)
2784   {
2785     if (NULL == (p = find_peer_by_pid (id)))
2786       return NULL;
2787     return p->pref_abs;
2788   }
2789   else
2790     return GAS_normalization_get_preferences_by_peer (id);
2791 }
2792
2793
2794 const double *
2795 get_property_cb (void *cls, const struct ATS_Address *address)
2796 {
2797   struct TestPeer *p;
2798   struct TestAddress *a;
2799
2800   if (GNUNET_YES == opt_disable_normalization)
2801   {
2802     p = find_peer_by_pid (&address->peer);
2803     a = find_address_by_ats_address (p, address);
2804     return a->prop_abs;
2805   }
2806   else
2807     return GAS_normalization_get_properties ((struct ATS_Address *) address);
2808 }
2809
2810 static void
2811 set_updated_property ( struct ATS_Address *address, uint32_t type, double prop_rel)
2812 {
2813   struct TestPeer *p;
2814   struct TestAddress *a;
2815
2816   if (NULL == (p = find_peer_by_pid (&address->peer)))
2817   {
2818     GNUNET_break (0);
2819     return;
2820   }
2821
2822   if (NULL == (a = find_address_by_ats_address (p, address)))
2823   {
2824     GNUNET_break (0);
2825     return;
2826   }
2827   a->prop_norm[type] = prop_rel;
2828   sh->env.sf.s_address_update_property (sh->solver, address, type, a->prop_abs [type], prop_rel);
2829 }
2830
2831
2832 static void
2833 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
2834     uint32_t type, double prop_rel)
2835 {
2836   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2837       "Normalized property %s for peer `%s' changed to %.3f \n",
2838       GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2839       prop_rel);
2840
2841   set_updated_property (address, type, prop_rel);
2842 }
2843
2844 static void
2845 set_updated_preference (const struct GNUNET_PeerIdentity *peer,
2846     enum GNUNET_ATS_PreferenceKind kind,
2847     double pref_rel)
2848 {
2849   struct TestPeer *p;
2850
2851   if (NULL == (p = find_peer_by_pid (peer)))
2852   {
2853     GNUNET_break (0);
2854     return;
2855   }
2856
2857   p->pref_norm[kind] = pref_rel;
2858   sh->env.sf.s_pref (sh->solver, peer, kind, pref_rel);
2859 }
2860
2861
2862 static void
2863 normalized_preference_changed_cb (void *cls,
2864     const struct GNUNET_PeerIdentity *peer,
2865     enum GNUNET_ATS_PreferenceKind kind,
2866     double pref_rel)
2867 {
2868   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2869       "Normalized preference %s for peer `%s' changed to %.3f \n",
2870       GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer),
2871       pref_rel);
2872
2873   set_updated_preference(peer, kind, pref_rel);
2874 }
2875
2876
2877 struct SolverHandle *
2878 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
2879 {
2880   struct SolverHandle *sh;
2881   char * solver_str;
2882   int c;
2883
2884   switch (type) {
2885     case GNUNET_ATS_SOLVER_PROPORTIONAL:
2886       solver_str = "proportional";
2887       break;
2888     case GNUNET_ATS_SOLVER_MLP:
2889       solver_str = "mlp";
2890       break;
2891     case GNUNET_ATS_SOLVER_RIL:
2892       solver_str = "ril";
2893       break;
2894     default:
2895       GNUNET_break (0);
2896       return NULL;
2897       break;
2898   }
2899
2900   sh = GNUNET_new (struct SolverHandle);
2901   GNUNET_asprintf (&sh->plugin, "libgnunet_plugin_ats_%s", solver_str);
2902
2903   sh->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
2904   /* setup environment */
2905   sh->env.cfg = e->cfg;
2906   sh->env.stats = GNUNET_STATISTICS_create ("ats", e->cfg);
2907   sh->env.addresses = sh->addresses;
2908   sh->env.bandwidth_changed_cb = &solver_bandwidth_changed_cb;
2909   sh->env.get_preferences = &get_preferences_cb;
2910   sh->env.get_property = &get_property_cb;
2911   sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
2912   sh->env.info_cb = &solver_info_cb;
2913   sh->env.info_cb_cls = NULL;
2914   sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
2915   int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
2916   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
2917     sh->env.networks[c] = networks[c];
2918
2919
2920   /* start normalization */
2921   GAS_normalization_start (&normalized_preference_changed_cb, NULL,
2922       &normalized_property_changed_cb, NULL );
2923
2924   /* load quotas */
2925   if (GNUNET_ATS_NetworkTypeCount != GNUNET_ATS_solvers_load_quotas (e->cfg,
2926       sh->env.out_quota, sh->env.in_quota, GNUNET_ATS_NetworkTypeCount))
2927   {
2928     GNUNET_break(0);
2929     GNUNET_free (sh->plugin);
2930     GNUNET_free (sh);
2931     end_now ();
2932     return NULL;
2933   }
2934
2935   sh->solver = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
2936   if (NULL == sh->solver)
2937   {
2938     fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
2939     GNUNET_break(0);
2940     GNUNET_free (sh->plugin);
2941     GNUNET_free (sh);
2942     end_now ();
2943     return NULL;
2944   }
2945   return sh;
2946 }
2947
2948 static void
2949 done ()
2950 {
2951   struct TestPeer *cur;
2952   struct TestPeer *next;
2953
2954   struct TestAddress *cur_a;
2955   struct TestAddress *next_a;
2956
2957   /* Stop logging */
2958   GNUNET_ATS_solver_logging_stop (l);
2959
2960   /* Stop all preference generation */
2961   GNUNET_ATS_solver_generate_preferences_stop_all ();
2962
2963   /* Stop all property generation */
2964   GNUNET_ATS_solver_generate_property_stop_all ();
2965
2966   if (opt_print)
2967   {
2968     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Printing log information \n");
2969     GNUNET_ATS_solver_logging_eval (l);
2970   }
2971   if (opt_save)
2972   {
2973     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Saving log information \n");
2974     GNUNET_ATS_solver_logging_write_to_disk (l, e->log_append_time_stamp,
2975         e->log_output_dir);
2976   }
2977
2978   if (NULL != l)
2979   {
2980     GNUNET_ATS_solver_logging_free (l);
2981     l = NULL;
2982   }
2983
2984   /* Clean up experiment */
2985   if (NULL != e)
2986   {
2987     GNUNET_ATS_solvers_experimentation_stop (e);
2988     e = NULL;
2989   }
2990
2991   next = peer_head;
2992   while  (NULL != (cur = next))
2993   {
2994     next = cur->next;
2995     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, cur);
2996     next_a = cur->addr_head;
2997     while  (NULL != (cur_a = next_a))
2998     {
2999       next_a = cur_a->next;
3000       GNUNET_CONTAINER_DLL_remove (cur->addr_head, cur->addr_tail, cur_a);
3001       GNUNET_free (cur_a);
3002     }
3003     GNUNET_free (cur);
3004   }
3005   if (NULL != sh)
3006   {
3007     GNUNET_ATS_solvers_solver_stop (sh);
3008     sh = NULL;
3009   }
3010
3011   /* Shutdown */
3012   end_now();
3013 }
3014
3015 static void
3016 experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,int success)
3017 {
3018   if (GNUNET_OK == success)
3019     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment done successful in %s\n",
3020         GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
3021   else
3022     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
3023
3024   GNUNET_SCHEDULER_add_now (&done, NULL);
3025 }
3026
3027 static void
3028 episode_done_cb (struct Episode *ep)
3029 {
3030   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
3031 }
3032
3033
3034
3035 /**
3036  * Do shutdown
3037  */
3038 static void
3039 end_now ()
3040 {
3041   if (NULL != e)
3042   {
3043     GNUNET_ATS_solvers_experimentation_stop (e);
3044     e = NULL;
3045   }
3046   if (NULL != sh)
3047   {
3048     GNUNET_ATS_solvers_solver_stop (sh);
3049     sh = NULL;
3050   }
3051 }
3052
3053 static void
3054 run (void *cls, char * const *args, const char *cfgfile,
3055     const struct GNUNET_CONFIGURATION_Handle *cfg)
3056 {
3057   enum GNUNET_ATS_Solvers solver;
3058   int c;
3059
3060   if (NULL == opt_exp_file)
3061   {
3062     fprintf (stderr, "No experiment given ...\n");
3063     res = 1;
3064     end_now ();
3065     return;
3066   }
3067
3068   if (NULL == opt_solver)
3069   {
3070     fprintf (stderr, "No solver given ...\n");
3071     res = 1;
3072     end_now ();
3073     return;
3074   }
3075
3076   if (0 == strcmp(opt_solver, "mlp"))
3077   {
3078     solver = GNUNET_ATS_SOLVER_MLP;
3079   }
3080   else if (0 == strcmp(opt_solver, "proportional"))
3081   {
3082     solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
3083   }
3084   else if (0 == strcmp(opt_solver, "ril"))
3085   {
3086     solver = GNUNET_ATS_SOLVER_RIL;
3087   }
3088   else
3089   {
3090     fprintf (stderr, "No solver given ...");
3091     res = 1;
3092     end_now ();
3093     return;
3094   }
3095
3096   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
3097     default_properties[c] = DEFAULT_REL_QUALITY;
3098
3099   for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
3100     default_preferences[c] = DEFAULT_REL_PREFERENCE;
3101
3102   /* load experiment */
3103   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading experiment\n");
3104   e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
3105   if (NULL == e)
3106   {
3107     fprintf (stderr, "Failed to load experiment ...\n");
3108     res = 1;
3109     end_now ();
3110     return;
3111   }
3112
3113   /* load solver */
3114   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading solver\n");
3115   sh = GNUNET_ATS_solvers_solver_start (solver);
3116   if (NULL == sh)
3117   {
3118     fprintf (stderr, "Failed to start solver ...\n");
3119     end_now ();
3120     res = 1;
3121     return;
3122   }
3123
3124   /* start logging */
3125   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Start logging \n");
3126   l = GNUNET_ATS_solver_logging_start (e->log_freq);
3127
3128   /* run experiment */
3129   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Running experiment \n");
3130   GNUNET_ATS_solvers_experimentation_run (e, episode_done_cb,
3131       experiment_done_cb);
3132
3133   /* WAIT */
3134 }
3135
3136
3137 /**
3138  * Main function of the benchmark
3139  *
3140  * @param argc argument count
3141  * @param argv argument values
3142  */
3143 int
3144 main (int argc, char *argv[])
3145 {
3146   opt_exp_file = NULL;
3147   opt_solver = NULL;
3148   opt_log = GNUNET_NO;
3149   opt_save = GNUNET_NO;
3150
3151   res = 0;
3152
3153   static struct GNUNET_GETOPT_CommandLineOption options[] =
3154   {
3155     { 's', "solver", NULL,
3156         gettext_noop ("solver to use"),
3157         1, &GNUNET_GETOPT_set_string, &opt_solver},
3158     {  'e', "experiment", NULL,
3159       gettext_noop ("experiment to use"),
3160       1, &GNUNET_GETOPT_set_string, &opt_exp_file},
3161     {  'V', "verbose", NULL,
3162       gettext_noop ("be verbose"),
3163       0, &GNUNET_GETOPT_set_one, &opt_verbose},
3164     {  'p', "print", NULL,
3165       gettext_noop ("print logging"),
3166       0, &GNUNET_GETOPT_set_one, &opt_print},
3167     {  'f', "file", NULL,
3168         gettext_noop ("save logging to disk"),
3169         0, &GNUNET_GETOPT_set_one, &opt_save},
3170     {  'd', "dn", NULL,
3171         gettext_noop ("disable normalization"),
3172         0, &GNUNET_GETOPT_set_one, &opt_disable_normalization},
3173     GNUNET_GETOPT_OPTION_END
3174   };
3175
3176   GNUNET_PROGRAM_run (argc, argv, "gnunet-ats-solver-eval",
3177       NULL, options, &run, argv[0]);
3178
3179   return res;
3180 }
3181 /* end of file ats-testing-experiment.c*/
3182