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