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