fixes for the logging
[oweals/gnunet.git] / src / ats / gnunet-ats-solver-eval.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010-2013 Christian Grothoff (and other contributing authors)
4
5  GNUnet is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published
7  by the Free Software Foundation; either version 3, or (at your
8  option) any later version.
9
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with GNUnet; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  Boston, MA 02111-1307, USA.
19  */
20 /**
21  * @file ats-tests/ats-testing-experiment.c
22  * @brief ats benchmark: controlled experiment execution
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet-ats-solver-eval.h"
29
30 #define BIG_M_STRING "unlimited"
31
32 static struct Experiment *e;
33
34 static struct LoggingHandle *l;
35
36 static struct SolverHandle *sh;
37
38 static struct TestPeer *peer_head;
39 static struct TestPeer *peer_tail;
40
41 static double default_properties[GNUNET_ATS_PropertyCount];
42 static double default_preferences[GNUNET_ATS_PreferenceCount];
43
44 /**
45  * cmd option -e: experiment file
46  */
47 static char *opt_exp_file;
48
49 static char *opt_solver;
50
51 /**
52  * cmd option -l: enable logging
53  */
54 static int opt_log;
55
56 /**
57  * cmd option -p: enable plots
58  */
59 static int opt_save;
60
61 /**
62  * cmd option -v: verbose logs
63  */
64 static int opt_verbose;
65
66 /**
67  * cmd option -p: print logs
68  */
69 static int opt_print;
70
71 /**
72  * cmd option -d: disable normalization
73  */
74 static int opt_disable_normalization;
75
76 static int res;
77
78 static void
79 end_now ();
80
81
82 static char *
83 print_generator_type (enum GeneratorType g)
84 {
85   switch (g) {
86     case GNUNET_ATS_TEST_TG_CONSTANT:
87       return "CONSTANT";
88     case GNUNET_ATS_TEST_TG_LINEAR:
89       return "LINEAR";
90     case GNUNET_ATS_TEST_TG_RANDOM:
91       return "RANDOM";
92     case GNUNET_ATS_TEST_TG_SINUS:
93       return "SINUS";
94     default:
95       return "INVALID";
96       break;
97   }
98 }
99
100 struct AddressLookupCtx
101 {
102   struct ATS_Address *res;
103   char *plugin;
104   char *addr;
105 };
106
107
108 int find_address_it (void *cls,
109                      const struct GNUNET_PeerIdentity *key,
110                      void *value)
111 {
112   struct AddressLookupCtx *ctx = cls;
113   struct ATS_Address *addr = value;
114
115   if ( (0 == strcmp (ctx->plugin, addr->plugin)) &&
116        (0 == strcmp (ctx->addr, addr->addr)) )
117   {
118        ctx->res = addr;
119        return GNUNET_NO;
120   }
121   return GNUNET_YES;
122 }
123
124 static struct TestPeer *
125 find_peer_by_id (int id)
126 {
127   struct TestPeer *cur;
128   for (cur = peer_head; NULL != cur; cur = cur->next)
129     if (cur->id == id)
130       return cur;
131   return NULL;
132 }
133
134 static struct TestPeer *
135 find_peer_by_pid (const struct GNUNET_PeerIdentity *pid)
136 {
137   struct TestPeer *cur;
138   for (cur = peer_head; NULL != cur; cur = cur->next)
139     if (0 == memcmp (&cur->peer_id, pid, sizeof (struct GNUNET_PeerIdentity)))
140       return cur;
141   return NULL;
142 }
143
144 static struct TestAddress *
145 find_address_by_id (struct TestPeer *peer, int aid)
146 {
147   struct TestAddress *cur;
148   for (cur = peer->addr_head; NULL != cur; cur = cur->next)
149     if (cur->aid == aid)
150       return cur;
151   return NULL;
152 }
153
154
155 static struct TestAddress *
156 find_address_by_ats_address (struct TestPeer *p, const struct ATS_Address *addr)
157 {
158   struct TestAddress *cur;
159   for (cur = p->addr_head; NULL != cur; cur = cur->next)
160     if ((0 == strcmp(cur->ats_addr->plugin, addr->plugin)) &&
161          (cur->ats_addr->addr_len == addr->addr_len) &&
162         (0 == memcmp (cur->ats_addr->addr, addr->addr, addr->addr_len)))
163       return cur;
164   return NULL;
165 }
166
167
168 /**
169  * Logging
170  */
171
172 void
173 GNUNET_ATS_solver_logging_now (struct LoggingHandle *l)
174 {
175   struct LoggingTimeStep *lts;
176   struct TestPeer *cur;
177   struct TestAddress *cur_addr;
178   struct LoggingPeer *log_p;
179   struct LoggingAddress *log_a;
180   int c;
181
182   lts = GNUNET_new (struct LoggingTimeStep);
183   GNUNET_CONTAINER_DLL_insert_tail(l->head, l->tail, lts);
184   lts->timestamp = GNUNET_TIME_absolute_get();
185   if (NULL == lts->prev)
186     lts->delta = GNUNET_TIME_UNIT_ZERO;
187   else
188     lts->delta = GNUNET_TIME_absolute_get_duration(lts->prev->timestamp);
189
190   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging %llu, delta %llu\n",
191       lts->timestamp.abs_value_us, lts->delta.rel_value_us);
192
193
194   /* Store logging data here */
195   for (cur = peer_head; NULL != cur; cur = cur->next)
196   {
197     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging peer id %llu\n", cur->id);
198
199     log_p = GNUNET_new (struct LoggingPeer);
200     log_p->id = cur->id;
201     log_p->peer_id = cur->peer_id;
202     for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
203     {
204       log_p->pref_abs[c] = cur->pref_abs[c];
205       log_p->pref_norm[c] = cur->pref_norm[c];
206       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t %s = %.2f %.2f [abs/rel]\n",
207           GNUNET_ATS_print_preference_type(c),
208           log_p->pref_abs[c], log_p->pref_norm[c]);
209     }
210     GNUNET_CONTAINER_DLL_insert_tail(lts->head, lts->tail, log_p);
211
212     for (cur_addr = cur->addr_head; NULL != cur_addr; cur_addr = cur_addr->next)
213     {
214       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging peer id %llu address %llu \n",
215           cur->id, cur_addr->aid);
216       log_a = GNUNET_new (struct LoggingAddress);
217       log_a->aid = cur_addr->aid;
218       log_a->active = cur_addr->ats_addr->active;
219       log_a->network = cur_addr->network;
220       log_a->used = cur_addr->ats_addr->used;
221       log_a->assigned_bw_in = cur_addr->ats_addr->assigned_bw_in;
222       log_a->assigned_bw_out = cur_addr->ats_addr->assigned_bw_out;
223       for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
224       {
225         log_a->prop_abs[c] = cur_addr->prop_abs[c];
226         log_a->prop_norm[c] = cur_addr->prop_norm[c];
227         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t %s = %.2f %.2f [abs/rel]\n",
228             GNUNET_ATS_print_property_type(c),
229             log_a->prop_abs[c], log_a->prop_norm[c]);
230       }
231       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t Active = %i\n", log_a->active);
232       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t BW in = %llu\n", ntohl(log_a->assigned_bw_in.value__));
233       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "\t BW out = %llu\n", ntohl(log_a->assigned_bw_out.value__));
234
235       GNUNET_CONTAINER_DLL_insert_tail (log_p->addr_head, log_p->addr_tail, log_a);
236     }
237   }
238 }
239
240 static void
241 logging_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
242 {
243   struct LoggingHandle *l = cls;
244   l->logging_task = GNUNET_SCHEDULER_NO_TASK;
245
246   GNUNET_ATS_solver_logging_now (l);
247
248   l->logging_task = GNUNET_SCHEDULER_add_delayed (l->log_freq, &logging_task, l);
249
250 }
251
252 struct LoggingHandle *
253 GNUNET_ATS_solver_logging_start (struct GNUNET_TIME_Relative freq)
254 {
255   struct LoggingHandle *l;
256   l = GNUNET_new (struct LoggingHandle);
257
258   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start logging every  %s\n",
259       GNUNET_STRINGS_relative_time_to_string(freq, GNUNET_NO));
260   l->log_freq = freq;
261   l->logging_task = GNUNET_SCHEDULER_add_now (&logging_task, l);
262   return l;
263 }
264
265 void
266 GNUNET_ATS_solver_logging_stop (struct LoggingHandle *l)
267 {
268   if (GNUNET_SCHEDULER_NO_TASK != l->logging_task)
269     GNUNET_SCHEDULER_cancel (l->logging_task);
270
271   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stop logging\n");
272
273   l->logging_task = GNUNET_SCHEDULER_NO_TASK;
274 }
275
276 static struct LoggingFileHandle *
277 find_logging_file_handle (struct LoggingFileHandle *lf_head,
278     struct LoggingFileHandle *lf_tail,
279     int peer_id, int address_id)
280 {
281   struct LoggingFileHandle *res;
282
283   for (res = lf_head; NULL != res; res = res->next)
284     if ((res->pid == peer_id) && (res->aid == address_id))
285       return res;
286   return NULL;
287
288 }
289
290 void
291 GNUNET_ATS_solver_logging_write_to_disk (struct LoggingHandle *l, int add_time_stamp,
292     char *output_dir)
293 {
294   struct LoggingTimeStep *lts;
295   struct LoggingPeer *log_p;
296   struct LoggingAddress *log_a;
297   struct LoggingFileHandle *lf_head;
298   struct LoggingFileHandle *lf_tail;
299   struct LoggingFileHandle *cur;
300   struct LoggingFileHandle *next;
301   char * filename;
302   char * datastring;
303   char * propstring;
304   char * propstring_tmp;
305   char * prefstring;
306   char * prefstring_tmp;
307   int c;
308   int use_dir;
309
310   use_dir = GNUNET_NO;
311   if (NULL != output_dir)
312   {
313     if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (output_dir))
314     {
315       fprintf (stderr, "Failed to create directory `%s'\n", output_dir);
316       return;
317     }
318     else
319     {
320       fprintf (stderr, "Created directory `%s'\n", output_dir);
321       use_dir = GNUNET_YES;
322     }
323   }
324
325   lf_head = NULL;
326   lf_tail = NULL;
327
328   for (lts = l->head; NULL != lts; lts = lts->next)
329   {
330
331     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Writing log step %llu\n",
332         (long long unsigned int) lts->timestamp.abs_value_us);
333
334     for (log_p = lts->head; NULL != log_p; log_p = log_p->next)
335     {
336       for (log_a = log_p->addr_head; NULL != log_a; log_a = log_a->next)
337       {
338
339         cur = find_logging_file_handle (lf_head, lf_tail, log_p->id,
340             log_a->aid);
341         if (NULL == cur)
342         {
343           cur = GNUNET_new (struct LoggingFileHandle);
344           cur->aid = log_a->aid;
345           cur->pid = log_p->id;
346
347           if (GNUNET_YES == add_time_stamp)
348             GNUNET_asprintf (&filename, "%s%s%s_%s_p%u_a%u_%llu.log",
349                 (GNUNET_YES == use_dir) ? output_dir : "",
350                 (GNUNET_YES == use_dir) ? DIR_SEPARATOR_STR : "",
351                 e->log_prefix,
352                 opt_solver,
353                 cur->pid,
354                 cur->aid,
355                 l->head->timestamp.abs_value_us);
356           else
357             GNUNET_asprintf (&filename, "%s%s%s_%s_p%u_a%u.log",
358                 (GNUNET_YES == use_dir) ? output_dir : "",
359                 (GNUNET_YES == use_dir) ? DIR_SEPARATOR_STR : "",
360                 e->log_prefix,
361                 opt_solver,
362                 cur->pid,
363                 cur->aid);
364
365           fprintf (stderr, "Add writing log data for peer %llu address %llu to file `%s'\n",
366               cur->pid, cur->aid, filename);
367
368
369           cur->f_hd = GNUNET_DISK_file_open (filename,
370               GNUNET_DISK_OPEN_READWRITE |
371               GNUNET_DISK_OPEN_CREATE |
372               GNUNET_DISK_OPEN_TRUNCATE,
373               GNUNET_DISK_PERM_USER_READ |
374               GNUNET_DISK_PERM_USER_WRITE |
375               GNUNET_DISK_PERM_GROUP_READ |
376               GNUNET_DISK_PERM_OTHER_READ);
377           if (NULL == cur->f_hd)
378           {
379             fprintf (stderr, "Cannot open `%s' to write log data!\n", filename);
380             GNUNET_free (filename);
381             goto cleanup;
382           }
383           GNUNET_free (filename);
384           GNUNET_CONTAINER_DLL_insert (lf_head, lf_tail, cur);
385
386           GNUNET_asprintf(&datastring,"#time delta; 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,
2026                 size_t plugin_addr_len,
2027                 uint32_t session_id,
2028                 uint32_t network)
2029 {
2030   struct ATS_Address *aa = NULL;
2031
2032   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len + strlen (plugin_name) + 1);
2033   aa->atsi = GNUNET_new (struct GNUNET_ATS_Information);
2034   aa->atsi[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
2035   aa->atsi[0].value = htonl (network);
2036   aa->atsi_count = 1;
2037
2038   aa->peer = *peer;
2039   aa->addr_len = plugin_addr_len;
2040   aa->addr = &aa[1];
2041   aa->plugin = (char *) &aa[1] + plugin_addr_len;
2042   memcpy (&aa[1], plugin_addr, plugin_addr_len);
2043   memcpy (aa->plugin, plugin_name, strlen (plugin_name) + 1);
2044   aa->session_id = session_id;
2045   aa->active = GNUNET_NO;
2046   aa->used = GNUNET_NO;
2047   aa->solver_information = NULL;
2048   aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
2049   aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
2050
2051   return aa;
2052 }
2053
2054
2055
2056 static void
2057 enforce_add_address (struct GNUNET_ATS_TEST_Operation *op)
2058 {
2059   struct TestPeer *p;
2060   struct TestAddress *a;
2061   int c;
2062
2063   if (NULL == (p = find_peer_by_id (op->peer_id)))
2064   {
2065     p = GNUNET_new (struct TestPeer);
2066     p->id = op->peer_id;
2067     memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
2068     for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
2069     {
2070       p->pref_abs[c] = DEFAULT_ABS_PREFERENCE;
2071       p->pref_norm[c] = DEFAULT_REL_PREFERENCE;
2072     }
2073
2074     GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, p);
2075   }
2076
2077   if (NULL != (find_address_by_id (p, op->address_id)))
2078   {
2079     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Duplicate address %u for peer %u\n",
2080         op->address_id, op->peer_id);
2081     return;
2082   }
2083
2084   a = GNUNET_new (struct TestAddress);
2085   a->aid = op->address_id;
2086   a->network = op->address_network;
2087   a->ats_addr = create_ats_address (&p->peer_id, op->plugin, op->address,
2088       strlen (op->address) + 1, op->address_session, op->address_network);
2089   memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
2090   GNUNET_CONTAINER_DLL_insert_tail (p->addr_head, p->addr_tail, a);
2091
2092   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
2093     a->prop_norm[c] = DEFAULT_REL_QUALITY;
2094
2095   GNUNET_CONTAINER_multipeermap_put (sh->addresses, &p->peer_id, a->ats_addr,
2096     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2097
2098   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Adding address %u for peer %u in network `%s'\n",
2099     op->address_id, op->peer_id, GNUNET_ATS_print_network_type(a->network));
2100
2101   sh->env.sf.s_add (sh->solver, a->ats_addr, op->address_network);
2102
2103 }
2104
2105
2106 static void
2107 enforce_del_address (struct GNUNET_ATS_TEST_Operation *op)
2108 {
2109   struct TestPeer *p;
2110   struct TestAddress *a;
2111   struct PropertyGenerator *pg;
2112
2113   if (NULL == (p = find_peer_by_id (op->peer_id)))
2114   {
2115     GNUNET_break (0);
2116     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2117         "Deleting address for unknown peer %u\n", op->peer_id);
2118     return;
2119   }
2120
2121   if (NULL == (a =find_address_by_id (p, op->address_id)))
2122   {
2123     GNUNET_break (0);
2124     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2125         "Deleting address for unknown peer %u\n", op->peer_id);
2126     return;
2127   }
2128
2129   while (NULL != (pg = find_prop_gen (p->id, a->aid, 0)))
2130   {
2131     GNUNET_ATS_solver_generate_property_stop (pg);
2132   }
2133
2134   GNUNET_CONTAINER_multipeermap_remove (sh->addresses, &p->peer_id, a->ats_addr);
2135
2136   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Removing address %u for peer %u\n",
2137       op->address_id, op->peer_id);
2138
2139   sh->env.sf.s_del (sh->solver, a->ats_addr, GNUNET_NO);
2140
2141   if (NULL != l)
2142   {
2143     GNUNET_ATS_solver_logging_now (l);
2144   }
2145   GNUNET_CONTAINER_DLL_remove(p->addr_head, p->addr_tail, a);
2146
2147   GNUNET_free_non_null(a->ats_addr->atsi);
2148   GNUNET_free (a->ats_addr);
2149   GNUNET_free (a);
2150
2151 }
2152
2153 static void
2154 enforce_start_property (struct GNUNET_ATS_TEST_Operation *op)
2155 {
2156   struct PropertyGenerator *pg;
2157   struct TestPeer *p;
2158   struct TestAddress *a;
2159
2160   if (NULL != (pg = find_prop_gen (op->peer_id, op->address_id, op->prop_type)))
2161   {
2162     GNUNET_ATS_solver_generate_property_stop (pg);
2163     GNUNET_free (pg);
2164   }
2165
2166   if (NULL == (p = find_peer_by_id (op->peer_id)))
2167   {
2168     GNUNET_break (0);
2169     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2170         "Starting property generation for unknown peer %u\n", op->peer_id);
2171     return;
2172   }
2173
2174   if (NULL == (a = find_address_by_id (p, op->address_id)))
2175   {
2176     GNUNET_break (0);
2177     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2178         "Setting property for unknown address %u\n", op->peer_id);
2179     return;
2180   }
2181
2182   GNUNET_ATS_solver_generate_property_start (op->peer_id,
2183     op->address_id,
2184     p, a,
2185     op->gen_type,
2186     op->base_rate,
2187     op->max_rate,
2188     op->period,
2189     op->frequency,
2190     op->prop_type);
2191 }
2192
2193 static void
2194 enforce_stop_property (struct GNUNET_ATS_TEST_Operation *op)
2195 {
2196   struct PropertyGenerator *pg = find_prop_gen(op->peer_id, op->address_id,
2197       op->prop_type);
2198   if (NULL != pg)
2199   {
2200     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2201         "Stopping preference generation for peer %u address %u\n", op->peer_id,
2202         op->address_id);
2203     GNUNET_ATS_solver_generate_property_stop (pg);
2204   }
2205   else
2206   {
2207     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2208         "Cannot find preference generator for peer %u address %u\n",
2209         op->peer_id, op->address_id);
2210     GNUNET_break (0);
2211   }
2212 }
2213
2214 static void
2215 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
2216 {
2217   struct PreferenceGenerator *pg;
2218   if (NULL != (pg = find_pref_gen (op->peer_id, op->pref_type)))
2219   {
2220     GNUNET_ATS_solver_generate_preferences_stop (pg);
2221     GNUNET_free (pg);
2222   }
2223
2224   if (NULL == (find_peer_by_id (op->peer_id)))
2225   {
2226     GNUNET_break (0);
2227     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2228         "Starting preference generation for unknown peer %u\n", op->peer_id);
2229     return;
2230   }
2231
2232   GNUNET_ATS_solver_generate_preferences_start (op->peer_id,
2233     op->address_id,
2234     op->client_id,
2235     op->gen_type,
2236     op->base_rate,
2237     op->max_rate,
2238     op->period,
2239     op->frequency,
2240     op->pref_type);
2241 }
2242
2243 static void
2244 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
2245 {
2246   struct PreferenceGenerator *pg = find_pref_gen(op->peer_id,
2247       op->pref_type);
2248   if (NULL != pg)
2249   {
2250     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2251         "Stopping property generation for peer %u address %u\n", op->peer_id,
2252         op->address_id);
2253     GNUNET_ATS_solver_generate_preferences_stop (pg);
2254   }
2255   else
2256   {
2257     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2258         "Cannot find preference generator for peer %u address %u\n",
2259         op->peer_id, op->address_id);
2260     GNUNET_break (0);
2261   }
2262 }
2263
2264
2265 static void
2266 enforce_start_request (struct GNUNET_ATS_TEST_Operation *op)
2267 {
2268   struct TestPeer *p;
2269   const struct ATS_Address *res;
2270
2271   if (NULL == (p = find_peer_by_id (op->peer_id)))
2272   {
2273     GNUNET_break (0);
2274     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2275         "Requesting address for unknown peer %u\n", op->peer_id);
2276     return;
2277   }
2278
2279   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Requesting address for peer %u\n",
2280       op->peer_id);
2281
2282   res = sh->env.sf.s_get (sh->solver, &p->peer_id);
2283   if (NULL != res)
2284   {
2285     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Suggested address for peer %u: %llu %llu\n",
2286         op->peer_id,
2287         ntohl(res->assigned_bw_in.value__),
2288         ntohl(res->assigned_bw_out.value__));
2289     if (NULL != l)
2290       GNUNET_ATS_solver_logging_now (l);
2291   }
2292 }
2293
2294 static void
2295 enforce_stop_request (struct GNUNET_ATS_TEST_Operation *op)
2296 {
2297   struct TestPeer *p;
2298
2299   if (NULL == (p = find_peer_by_id (op->peer_id)))
2300   {
2301     GNUNET_break (0);
2302     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2303         "Requesting address for unknown peer %u\n", op->peer_id);
2304     return;
2305   }
2306
2307   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stop requesting address for peer %u\n",
2308       op->peer_id);
2309
2310   sh->env.sf.s_get_stop (sh->solver, &p->peer_id);
2311
2312   if (NULL != l)
2313   {
2314     GNUNET_ATS_solver_logging_now (l);
2315   }
2316
2317 }
2318
2319 static void enforce_episode (struct Episode *ep)
2320 {
2321   struct GNUNET_ATS_TEST_Operation *cur;
2322   for (cur = ep->head; NULL != cur; cur = cur->next)
2323   {
2324     switch (cur->type) {
2325       case SOLVER_OP_ADD_ADDRESS:
2326         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
2327             print_op (cur->type), cur->peer_id, cur->address_id);
2328         enforce_add_address (cur);
2329         break;
2330       case SOLVER_OP_DEL_ADDRESS:
2331         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
2332             print_op (cur->type), cur->peer_id, cur->address_id);
2333         enforce_del_address (cur);
2334         break;
2335       case SOLVER_OP_START_SET_PROPERTY:
2336         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2337             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2338         enforce_start_property (cur);
2339         break;
2340       case SOLVER_OP_STOP_SET_PROPERTY:
2341         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2342             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2343         enforce_stop_property (cur);
2344         break;
2345       case SOLVER_OP_START_SET_PREFERENCE:
2346         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2347             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2348         enforce_start_preference (cur);
2349         break;
2350       case SOLVER_OP_STOP_SET_PREFERENCE:
2351         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2352             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2353         enforce_stop_preference (cur);
2354         break;
2355       case SOLVER_OP_START_REQUEST:
2356         fprintf (stderr, "Enforcing operation: %s [%llu]\n",
2357             print_op (cur->type), cur->peer_id);
2358         enforce_start_request (cur);
2359         break;
2360       case SOLVER_OP_STOP_REQUEST:
2361         fprintf (stderr, "Enforcing operation: %s [%llu]\n",
2362             print_op (cur->type), cur->peer_id);
2363         enforce_stop_request (cur);
2364         break;
2365       default:
2366         break;
2367     }
2368   }
2369 }
2370
2371 static void
2372 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
2373 {
2374   struct Experiment *e = cls;
2375   e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2376   if (NULL != e->ep_done_cb)
2377     e->ep_done_cb (e->cur);
2378
2379   /* Scheduling next */
2380   e->cur = e->cur->next;
2381   if (NULL == e->cur)
2382   {
2383     /* done */
2384     fprintf (stderr, "Last episode done!\n");
2385     if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
2386     {
2387       GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
2388       e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2389     }
2390     e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
2391     return;
2392   }
2393
2394   fprintf (stderr, "Running episode %u with timeout %s\n",
2395       e->cur->id,
2396       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
2397   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
2398       &timeout_episode, e);
2399   enforce_episode(e->cur);
2400
2401
2402 }
2403
2404
2405 void
2406 GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
2407     GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
2408     GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
2409 {
2410   fprintf (stderr, "Running experiment `%s'  with timeout %s\n", e->name,
2411       GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
2412   e->e_done_cb = e_done_cb;
2413   e->ep_done_cb = ep_done_cb;
2414   e->start_time = GNUNET_TIME_absolute_get();
2415
2416   /* Start total time out */
2417   e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
2418       &timeout_experiment, e);
2419
2420   /* Start */
2421   if (NULL == e->start)
2422   {
2423     GNUNET_break (0);
2424     return;
2425   }
2426
2427   e->cur = e->start;
2428   fprintf (stderr, "Running episode %u with timeout %s\n",
2429       e->cur->id,
2430       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
2431   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
2432       &timeout_episode, e);
2433   enforce_episode(e->cur);
2434
2435 }
2436
2437 void
2438 GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
2439 {
2440   if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
2441   {
2442     GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
2443     e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2444   }
2445   if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
2446   {
2447     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
2448     e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2449   }
2450   if (NULL != e->cfg)
2451   {
2452     GNUNET_CONFIGURATION_destroy(e->cfg);
2453     e->cfg = NULL;
2454   }
2455   free_experiment (e);
2456 }
2457
2458
2459 struct Experiment *
2460 GNUNET_ATS_solvers_experimentation_load (char *filename)
2461 {
2462   struct Experiment *e;
2463   struct GNUNET_CONFIGURATION_Handle *cfg;
2464   e = NULL;
2465
2466   cfg = GNUNET_CONFIGURATION_create();
2467   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
2468   {
2469     fprintf (stderr, "Failed to load `%s'\n", filename);
2470     GNUNET_CONFIGURATION_destroy (cfg);
2471     return NULL;
2472   }
2473
2474   e = create_experiment ();
2475
2476   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
2477       "name", &e->name))
2478   {
2479     fprintf (stderr, "Invalid %s \n", "name");
2480     free_experiment (e);
2481     return NULL;
2482   }
2483   else
2484     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment name: `%s'\n", e->name);
2485
2486   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
2487       "log_prefix", &e->log_prefix))
2488   {
2489     fprintf (stderr, "Invalid %s \n", "log_prefix");
2490     free_experiment (e);
2491     return NULL;
2492   }
2493   else
2494     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging prefix: `%s'\n",
2495         e->log_prefix);
2496
2497   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
2498       "log_output_dir", &e->log_output_dir))
2499   {
2500     e->log_output_dir = NULL;
2501   }
2502   else
2503     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging output directory: `%s'\n",
2504         e->log_output_dir);
2505
2506
2507   if (GNUNET_SYSERR == (e->log_append_time_stamp = GNUNET_CONFIGURATION_get_value_yesno(cfg,
2508       "experiment", "log_append_time_stamp")))
2509     e->log_append_time_stamp = GNUNET_YES;
2510   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging append timestamp: `%s'\n",
2511       (GNUNET_YES == e->log_append_time_stamp) ? "yes" : "no");
2512
2513
2514   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
2515       "cfg_file", &e->cfg_file))
2516   {
2517     fprintf (stderr, "Invalid %s \n", "cfg_file");
2518     free_experiment (e);
2519     return NULL;
2520   }
2521   else
2522   {
2523     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment configuration: `%s'\n", e->cfg_file);
2524     e->cfg = GNUNET_CONFIGURATION_create();
2525     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (e->cfg, e->cfg_file))
2526     {
2527       fprintf (stderr, "Invalid configuration %s \n", "cfg_file");
2528       free_experiment (e);
2529       return NULL;
2530     }
2531
2532   }
2533
2534   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
2535       "log_freq", &e->log_freq))
2536   {
2537     fprintf (stderr, "Invalid %s \n", "log_freq");
2538     free_experiment (e);
2539     return NULL;
2540   }
2541   else
2542     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging frequency: `%s'\n",
2543         GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
2544
2545   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
2546       "max_duration", &e->max_duration))
2547   {
2548     fprintf (stderr, "Invalid %s", "max_duration");
2549     free_experiment (e);
2550     return NULL;
2551   }
2552   else
2553     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment duration: `%s'\n",
2554         GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
2555
2556   if (GNUNET_SYSERR == load_episodes (e, cfg))
2557   {
2558     GNUNET_ATS_solvers_experimentation_stop (e);
2559     GNUNET_CONFIGURATION_destroy (cfg);
2560     e = NULL;
2561     fprintf (stderr, "Failed to load experiment\n");
2562     return NULL;
2563   }
2564   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loaded %u episodes with total duration %s\n",
2565       e->num_episodes,
2566       GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
2567
2568   GNUNET_CONFIGURATION_destroy (cfg);
2569   return e;
2570 }
2571
2572
2573
2574 /**
2575  * Solver
2576  */
2577
2578 static int
2579 free_all_it (void *cls,
2580     const struct GNUNET_PeerIdentity *key,
2581     void *value)
2582 {
2583   struct ATS_Address *address = value;
2584   GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (sh->env.addresses,
2585       key, value));
2586   GNUNET_free (address);
2587
2588   return GNUNET_OK;
2589 }
2590
2591 void
2592 GNUNET_ATS_solvers_solver_stop (struct SolverHandle *sh)
2593 {
2594  GNUNET_STATISTICS_destroy ((struct GNUNET_STATISTICS_Handle *) sh->env.stats,
2595      GNUNET_NO);
2596  GNUNET_PLUGIN_unload (sh->plugin, sh->solver);
2597
2598  GAS_normalization_stop();
2599
2600  GNUNET_CONTAINER_multipeermap_iterate (sh->addresses, &free_all_it, NULL);
2601  GNUNET_CONTAINER_multipeermap_destroy(sh->addresses);
2602  GNUNET_free (sh->plugin);
2603  GNUNET_free (sh);
2604 }
2605
2606 /**
2607  * Load quotas for networks from configuration
2608  *
2609  * @param cfg configuration handle
2610  * @param out_dest where to write outbound quotas
2611  * @param in_dest where to write inbound quotas
2612  * @param dest_length length of inbound and outbound arrays
2613  * @return number of networks loaded
2614  */
2615 unsigned int
2616 GNUNET_ATS_solvers_load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
2617                                                  unsigned long long *out_dest,
2618                                                  unsigned long long *in_dest,
2619                                                  int dest_length)
2620 {
2621   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
2622   char * entry_in = NULL;
2623   char * entry_out = NULL;
2624   char * quota_out_str;
2625   char * quota_in_str;
2626   int c;
2627   int res;
2628
2629   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
2630   {
2631     in_dest[c] = 0;
2632     out_dest[c] = 0;
2633     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
2634     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
2635
2636     /* quota out */
2637     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
2638     {
2639       res = GNUNET_NO;
2640       if (0 == strcmp(quota_out_str, BIG_M_STRING))
2641       {
2642         out_dest[c] = GNUNET_ATS_MaxBandwidth;
2643         res = GNUNET_YES;
2644       }
2645       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
2646         res = GNUNET_YES;
2647       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,  &out_dest[c])))
2648          res = GNUNET_YES;
2649
2650       if (GNUNET_NO == res)
2651       {
2652           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
2653               network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
2654           out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2655       }
2656       else
2657       {
2658           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Outbound quota configure for network `%s' is %llu\n"),
2659               network_str[c], out_dest[c]);
2660       }
2661       GNUNET_free (quota_out_str);
2662     }
2663     else
2664     {
2665       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
2666           network_str[c], GNUNET_ATS_DefaultBandwidth);
2667       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2668     }
2669
2670     /* quota in */
2671     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
2672     {
2673       res = GNUNET_NO;
2674       if (0 == strcmp(quota_in_str, BIG_M_STRING))
2675       {
2676         in_dest[c] = GNUNET_ATS_MaxBandwidth;
2677         res = GNUNET_YES;
2678       }
2679       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
2680         res = GNUNET_YES;
2681       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,  &in_dest[c])))
2682          res = GNUNET_YES;
2683
2684       if (GNUNET_NO == res)
2685       {
2686           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
2687               network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
2688           in_dest[c] = GNUNET_ATS_DefaultBandwidth;
2689       }
2690       else
2691       {
2692           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Inbound quota configured for network `%s' is %llu\n"),
2693               network_str[c], in_dest[c]);
2694       }
2695       GNUNET_free (quota_in_str);
2696     }
2697     else
2698     {
2699       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
2700           network_str[c], GNUNET_ATS_DefaultBandwidth);
2701       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2702     }
2703     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]);
2704     GNUNET_free (entry_out);
2705     GNUNET_free (entry_in);
2706   }
2707   return GNUNET_ATS_NetworkTypeCount;
2708 }
2709
2710 /**
2711  * Information callback for the solver
2712  *
2713  * @param cls the closure
2714  * @param op the solver operation
2715  * @param stat status of the solver operation
2716  * @param add additional solver information
2717  */
2718 static void
2719 solver_info_cb (void *cls,
2720     enum GAS_Solver_Operation op,
2721     enum GAS_Solver_Status stat,
2722     enum GAS_Solver_Additional_Information add)
2723 {
2724   char *add_info;
2725   switch (add) {
2726     case GAS_INFO_NONE:
2727       add_info = "GAS_INFO_NONE";
2728       break;
2729     case GAS_INFO_FULL:
2730       add_info = "GAS_INFO_MLP_FULL";
2731       break;
2732     case GAS_INFO_UPDATED:
2733       add_info = "GAS_INFO_MLP_UPDATED";
2734       break;
2735     case GAS_INFO_PROP_ALL:
2736       add_info = "GAS_INFO_PROP_ALL";
2737       break;
2738     case GAS_INFO_PROP_SINGLE:
2739       add_info = "GAS_INFO_PROP_SINGLE";
2740       break;
2741     default:
2742       add_info = "INVALID";
2743       break;
2744   }
2745
2746   switch (op)
2747   {
2748     case GAS_OP_SOLVE_START:
2749       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2750           "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
2751           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
2752       return;
2753     case GAS_OP_SOLVE_STOP:
2754       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2755           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
2756           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
2757       return;
2758
2759     case GAS_OP_SOLVE_SETUP_START:
2760       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2761           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
2762           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2763       return;
2764
2765     case GAS_OP_SOLVE_SETUP_STOP:
2766       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2767           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
2768           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2769       return;
2770
2771     case GAS_OP_SOLVE_MLP_LP_START:
2772       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2773           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
2774           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2775       return;
2776     case GAS_OP_SOLVE_MLP_LP_STOP:
2777       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2778           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
2779           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2780       return;
2781
2782     case GAS_OP_SOLVE_MLP_MLP_START:
2783       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2784           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
2785           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2786       return;
2787     case GAS_OP_SOLVE_MLP_MLP_STOP:
2788       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2789           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
2790           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2791       return;
2792     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
2793       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2794           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
2795           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2796       return;
2797     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
2798       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2799           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
2800           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2801       return;
2802     default:
2803       break;
2804     }
2805 }
2806
2807 static void
2808 solver_bandwidth_changed_cb (void *cls, struct ATS_Address *address)
2809 {
2810   if ( (0 == ntohl (address->assigned_bw_out.value__)) &&
2811        (0 == ntohl (address->assigned_bw_in.value__)) )
2812   {
2813     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2814                 "Solver notified to disconnect peer `%s'\n",
2815                 GNUNET_i2s (&address->peer));
2816     return;
2817   }
2818
2819   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2820               "Bandwidth changed addresses %s %p to %u Bps out / %u Bps in\n",
2821               GNUNET_i2s (&address->peer),
2822               address,
2823               (unsigned int) ntohl  (address->assigned_bw_out.value__),
2824               (unsigned int) ntohl (address->assigned_bw_in.value__));
2825   /*if (GNUNET_YES == ph.bulk_running)
2826     GNUNET_break (0);*/
2827   if (NULL != l)
2828     GNUNET_ATS_solver_logging_now (l);
2829
2830   return;
2831 }
2832
2833 const double *
2834 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
2835 {
2836   struct TestPeer *p;
2837   if (GNUNET_YES == opt_disable_normalization)
2838   {
2839     if (NULL == (p = find_peer_by_pid (id)))
2840       return NULL;
2841     return p->pref_abs;
2842   }
2843   else
2844     return GAS_normalization_get_preferences_by_peer (id);
2845 }
2846
2847
2848 const double *
2849 get_property_cb (void *cls, const struct ATS_Address *address)
2850 {
2851   struct TestPeer *p;
2852   struct TestAddress *a;
2853
2854   if (GNUNET_YES == opt_disable_normalization)
2855   {
2856     p = find_peer_by_pid (&address->peer);
2857     a = find_address_by_ats_address (p, address);
2858     return a->prop_abs;
2859   }
2860   else
2861     return GAS_normalization_get_properties ((struct ATS_Address *) address);
2862 }
2863
2864 static void
2865 set_updated_property ( struct ATS_Address *address, uint32_t type, double prop_rel)
2866 {
2867   struct TestPeer *p;
2868   struct TestAddress *a;
2869
2870   if (NULL == (p = find_peer_by_pid (&address->peer)))
2871   {
2872     GNUNET_break (0);
2873     return;
2874   }
2875
2876   if (NULL == (a = find_address_by_ats_address (p, address)))
2877   {
2878     GNUNET_break (0);
2879     return;
2880   }
2881   a->prop_norm[type] = prop_rel;
2882   sh->env.sf.s_address_update_property (sh->solver, address, type, a->prop_abs [type], prop_rel);
2883 }
2884
2885
2886 static void
2887 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
2888     uint32_t type, double prop_rel)
2889 {
2890   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2891       "Normalized property %s for peer `%s' changed to %.3f \n",
2892       GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2893       prop_rel);
2894
2895   set_updated_property (address, type, prop_rel);
2896 }
2897
2898 static void
2899 set_updated_preference (const struct GNUNET_PeerIdentity *peer,
2900     enum GNUNET_ATS_PreferenceKind kind,
2901     double pref_rel)
2902 {
2903   struct TestPeer *p;
2904
2905   if (NULL == (p = find_peer_by_pid (peer)))
2906   {
2907     GNUNET_break (0);
2908     return;
2909   }
2910
2911   p->pref_norm[kind] = pref_rel;
2912   sh->env.sf.s_pref (sh->solver, peer, kind, pref_rel);
2913 }
2914
2915
2916 static void
2917 normalized_preference_changed_cb (void *cls,
2918     const struct GNUNET_PeerIdentity *peer,
2919     enum GNUNET_ATS_PreferenceKind kind,
2920     double pref_rel)
2921 {
2922   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2923       "Normalized preference %s for peer `%s' changed to %.3f \n",
2924       GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer),
2925       pref_rel);
2926
2927   set_updated_preference(peer, kind, pref_rel);
2928 }
2929
2930
2931 struct SolverHandle *
2932 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
2933 {
2934   struct SolverHandle *sh;
2935   char * solver_str;
2936   int c;
2937
2938   switch (type) {
2939     case GNUNET_ATS_SOLVER_PROPORTIONAL:
2940       solver_str = "proportional";
2941       break;
2942     case GNUNET_ATS_SOLVER_MLP:
2943       solver_str = "mlp";
2944       break;
2945     case GNUNET_ATS_SOLVER_RIL:
2946       solver_str = "ril";
2947       break;
2948     default:
2949       GNUNET_break (0);
2950       return NULL;
2951       break;
2952   }
2953
2954   sh = GNUNET_new (struct SolverHandle);
2955   GNUNET_asprintf (&sh->plugin, "libgnunet_plugin_ats_%s", solver_str);
2956
2957   sh->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
2958   /* setup environment */
2959   sh->env.cfg = e->cfg;
2960   sh->env.stats = GNUNET_STATISTICS_create ("ats", e->cfg);
2961   sh->env.addresses = sh->addresses;
2962   sh->env.bandwidth_changed_cb = &solver_bandwidth_changed_cb;
2963   sh->env.get_preferences = &get_preferences_cb;
2964   sh->env.get_property = &get_property_cb;
2965   sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
2966   sh->env.info_cb = &solver_info_cb;
2967   sh->env.info_cb_cls = NULL;
2968   sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
2969   int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
2970   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
2971     sh->env.networks[c] = networks[c];
2972
2973
2974   /* start normalization */
2975   GAS_normalization_start (&normalized_preference_changed_cb, NULL,
2976       &normalized_property_changed_cb, NULL );
2977
2978   /* load quotas */
2979   if (GNUNET_ATS_NetworkTypeCount != GNUNET_ATS_solvers_load_quotas (e->cfg,
2980       sh->env.out_quota, sh->env.in_quota, GNUNET_ATS_NetworkTypeCount))
2981   {
2982     GNUNET_break(0);
2983     GNUNET_free (sh->plugin);
2984     GNUNET_free (sh);
2985     end_now ();
2986     return NULL;
2987   }
2988
2989   sh->solver = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
2990   if (NULL == sh->solver)
2991   {
2992     fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
2993     GNUNET_break(0);
2994     GNUNET_free (sh->plugin);
2995     GNUNET_free (sh);
2996     end_now ();
2997     return NULL;
2998   }
2999   return sh;
3000 }
3001
3002 static void
3003 done ()
3004 {
3005   struct TestPeer *cur;
3006   struct TestPeer *next;
3007
3008   struct TestAddress *cur_a;
3009   struct TestAddress *next_a;
3010
3011   /* Stop logging */
3012   GNUNET_ATS_solver_logging_stop (l);
3013
3014   /* Stop all preference generation */
3015   GNUNET_ATS_solver_generate_preferences_stop_all ();
3016
3017   /* Stop all property generation */
3018   GNUNET_ATS_solver_generate_property_stop_all ();
3019
3020   if (opt_print)
3021   {
3022     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Printing log information \n");
3023     GNUNET_ATS_solver_logging_eval (l);
3024   }
3025   if (opt_save)
3026   {
3027     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Saving log information \n");
3028     GNUNET_ATS_solver_logging_write_to_disk (l, e->log_append_time_stamp,
3029         e->log_output_dir);
3030   }
3031
3032   if (NULL != l)
3033   {
3034     GNUNET_ATS_solver_logging_free (l);
3035     l = NULL;
3036   }
3037
3038   /* Clean up experiment */
3039   if (NULL != e)
3040   {
3041     GNUNET_ATS_solvers_experimentation_stop (e);
3042     e = NULL;
3043   }
3044
3045   next = peer_head;
3046   while  (NULL != (cur = next))
3047   {
3048     next = cur->next;
3049     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, cur);
3050     next_a = cur->addr_head;
3051     while  (NULL != (cur_a = next_a))
3052     {
3053       next_a = cur_a->next;
3054       GNUNET_CONTAINER_DLL_remove (cur->addr_head, cur->addr_tail, cur_a);
3055       GNUNET_free (cur_a);
3056     }
3057     GNUNET_free (cur);
3058   }
3059   if (NULL != sh)
3060   {
3061     GNUNET_ATS_solvers_solver_stop (sh);
3062     sh = NULL;
3063   }
3064
3065   /* Shutdown */
3066   end_now();
3067 }
3068
3069 static void
3070 experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,int success)
3071 {
3072   if (GNUNET_OK == success)
3073     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment done successful in %s\n",
3074         GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
3075   else
3076     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
3077
3078   GNUNET_SCHEDULER_add_now (&done, NULL);
3079 }
3080
3081 static void
3082 episode_done_cb (struct Episode *ep)
3083 {
3084   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
3085 }
3086
3087
3088
3089 /**
3090  * Do shutdown
3091  */
3092 static void
3093 end_now ()
3094 {
3095   if (NULL != e)
3096   {
3097     GNUNET_ATS_solvers_experimentation_stop (e);
3098     e = NULL;
3099   }
3100   if (NULL != sh)
3101   {
3102     GNUNET_ATS_solvers_solver_stop (sh);
3103     sh = NULL;
3104   }
3105 }
3106
3107 static void
3108 run (void *cls, char * const *args, const char *cfgfile,
3109     const struct GNUNET_CONFIGURATION_Handle *cfg)
3110 {
3111   enum GNUNET_ATS_Solvers solver;
3112   int c;
3113
3114   if (NULL == opt_exp_file)
3115   {
3116     fprintf (stderr, "No experiment given ...\n");
3117     res = 1;
3118     end_now ();
3119     return;
3120   }
3121
3122   if (NULL == opt_solver)
3123   {
3124     fprintf (stderr, "No solver given ...\n");
3125     res = 1;
3126     end_now ();
3127     return;
3128   }
3129
3130   if (0 == strcmp(opt_solver, "mlp"))
3131   {
3132     solver = GNUNET_ATS_SOLVER_MLP;
3133   }
3134   else if (0 == strcmp(opt_solver, "proportional"))
3135   {
3136     solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
3137   }
3138   else if (0 == strcmp(opt_solver, "ril"))
3139   {
3140     solver = GNUNET_ATS_SOLVER_RIL;
3141   }
3142   else
3143   {
3144     fprintf (stderr, "No solver given ...");
3145     res = 1;
3146     end_now ();
3147     return;
3148   }
3149
3150   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
3151     default_properties[c] = DEFAULT_REL_QUALITY;
3152
3153   for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
3154     default_preferences[c] = DEFAULT_REL_PREFERENCE;
3155
3156   /* load experiment */
3157   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading experiment\n");
3158   e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
3159   if (NULL == e)
3160   {
3161     fprintf (stderr, "Failed to load experiment ...\n");
3162     res = 1;
3163     end_now ();
3164     return;
3165   }
3166
3167   /* load solver */
3168   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading solver\n");
3169   sh = GNUNET_ATS_solvers_solver_start (solver);
3170   if (NULL == sh)
3171   {
3172     fprintf (stderr, "Failed to start solver ...\n");
3173     end_now ();
3174     res = 1;
3175     return;
3176   }
3177
3178   /* start logging */
3179   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Start logging \n");
3180   l = GNUNET_ATS_solver_logging_start (e->log_freq);
3181
3182   /* run experiment */
3183   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Running experiment \n");
3184   GNUNET_ATS_solvers_experimentation_run (e, episode_done_cb,
3185       experiment_done_cb);
3186
3187   /* WAIT */
3188 }
3189
3190
3191 /**
3192  * Main function of the benchmark
3193  *
3194  * @param argc argument count
3195  * @param argv argument values
3196  */
3197 int
3198 main (int argc, char *argv[])
3199 {
3200   opt_exp_file = NULL;
3201   opt_solver = NULL;
3202   opt_log = GNUNET_NO;
3203   opt_save = GNUNET_NO;
3204
3205   res = 0;
3206
3207   static struct GNUNET_GETOPT_CommandLineOption options[] =
3208   {
3209     { 's', "solver", NULL,
3210         gettext_noop ("solver to use"),
3211         1, &GNUNET_GETOPT_set_string, &opt_solver},
3212     {  'e', "experiment", NULL,
3213       gettext_noop ("experiment to use"),
3214       1, &GNUNET_GETOPT_set_string, &opt_exp_file},
3215     {  'V', "verbose", NULL,
3216       gettext_noop ("be verbose"),
3217       0, &GNUNET_GETOPT_set_one, &opt_verbose},
3218     {  'p', "print", NULL,
3219       gettext_noop ("print logging"),
3220       0, &GNUNET_GETOPT_set_one, &opt_print},
3221     {  'f', "file", NULL,
3222         gettext_noop ("save logging to disk"),
3223         0, &GNUNET_GETOPT_set_one, &opt_save},
3224     {  'd', "dn", NULL,
3225         gettext_noop ("disable normalization"),
3226         0, &GNUNET_GETOPT_set_one, &opt_disable_normalization},
3227     GNUNET_GETOPT_OPTION_END
3228   };
3229
3230   GNUNET_PROGRAM_run (argc, argv, "gnunet-ats-solver-eval",
3231       NULL, options, &run, argv[0]);
3232
3233   return res;
3234 }
3235 /* end of file ats-testing-experiment.c*/
3236