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