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