deduplicate code for quota parsing
[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.c"
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   if (GNUNET_YES == opt_disable_normalization)
984   {
985     p->pref_abs[pg->kind] = pref_value;
986     p->pref_norm[pg->kind] = pref_value;
987     sh->sf->s_pref (sh->sf->cls, &p->peer_id, pg->kind, pref_value);
988   }
989   else
990     normalize_preference (NULL + (pg->client_id),
991                           &p->peer_id,
992                           pg->kind,
993                           pref_value);
994
995   pg->set_task = GNUNET_SCHEDULER_add_delayed (pg->frequency,
996                                                &set_pref_task,
997                                                pg);
998 }
999
1000
1001 static struct PreferenceGenerator *
1002 find_pref_gen (unsigned int peer, enum GNUNET_ATS_PreferenceKind kind)
1003 {
1004   struct PreferenceGenerator *cur;
1005   for (cur = pref_gen_head; NULL != cur; cur = cur->next)
1006     if (cur->peer == peer)
1007     {
1008       if ((cur->kind == kind) || (GNUNET_ATS_PREFERENCE_END == kind))
1009         return cur;
1010     }
1011   return NULL;
1012 }
1013
1014 void
1015 GNUNET_ATS_solver_generate_preferences_stop (struct PreferenceGenerator *pg)
1016 {
1017   GNUNET_CONTAINER_DLL_remove (pref_gen_head, pref_gen_tail, pg);
1018
1019   if (NULL != pg->feedback_task)
1020   {
1021     GNUNET_SCHEDULER_cancel (pg->feedback_task);
1022     pg->feedback_task = NULL;
1023   }
1024
1025   if (NULL != pg->set_task)
1026   {
1027     GNUNET_SCHEDULER_cancel (pg->set_task);
1028     pg->set_task = NULL;
1029   }
1030   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1031       "Removing old up preference generator peer [%u] `%s'\n",
1032       pg->peer, GNUNET_ATS_print_preference_type(pg->kind));
1033
1034   GNUNET_free (pg);
1035 }
1036
1037 struct TestAddress*
1038 find_active_address (struct TestPeer *p)
1039 {
1040   struct TestAddress *cur;
1041   for (cur = p->addr_head; NULL != cur; cur = cur->next)
1042     if (GNUNET_YES == cur->ats_addr->active)
1043       return cur;
1044   return NULL;
1045 }
1046
1047 /**
1048  * Generate between the source master and the partner and set property with a
1049  * value depending on the generator.
1050  *
1051  * @param peer source
1052  * @param address_id partner
1053  * @param client_id the client
1054  * @param type type of generator
1055  * @param base_value base value
1056  * @param value_rate maximum value
1057  * @param period duration of a period of generation (~ 1/frequency)
1058  * @param frequency how long to generate property
1059  * @param kind ATS preference to generate
1060  * @param feedback_frequency how often to give feedback
1061  * @return the preference generator
1062  */
1063 struct PreferenceGenerator *
1064 GNUNET_ATS_solver_generate_preferences_start (unsigned int peer,
1065     unsigned int address_id,
1066     unsigned int client_id,
1067     enum GeneratorType type,
1068     long int base_value,
1069     long int value_rate,
1070     struct GNUNET_TIME_Relative period,
1071     struct GNUNET_TIME_Relative frequency,
1072     enum GNUNET_ATS_PreferenceKind kind,
1073     struct GNUNET_TIME_Relative feedback_frequency)
1074 {
1075   struct PreferenceGenerator *pg;
1076   struct TestPeer *p;
1077
1078   if (NULL == (p = find_peer_by_id (peer)))
1079   {
1080     GNUNET_break (0);
1081     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1082         "Starting preference for unknown peer %u\n", peer);
1083     return NULL;
1084   }
1085
1086   pg = GNUNET_new (struct PreferenceGenerator);
1087   GNUNET_CONTAINER_DLL_insert (pref_gen_head, pref_gen_tail, pg);
1088   pg->type = type;
1089   pg->peer = peer;
1090   pg->client_id = client_id;
1091   pg->kind = kind;
1092   pg->base_value = base_value;
1093   pg->max_value = value_rate;
1094   pg->duration_period = period;
1095   pg->frequency = frequency;
1096   pg->time_start = GNUNET_TIME_absolute_get();
1097   pg->feedback_frequency = feedback_frequency;
1098
1099   switch (type) {
1100     case GNUNET_ATS_TEST_TG_CONSTANT:
1101       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1102           "Setting up %s preference generator peer [%u] `%s' max %u Bips\n",
1103           print_generator_type (type), pg->peer,
1104           GNUNET_ATS_print_preference_type(kind),
1105           base_value);
1106       break;
1107     case GNUNET_ATS_TEST_TG_LINEAR:
1108       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1109           "Setting up %s preference generator peer [%u] `%s' min %u Bips max %u Bips\n",
1110           print_generator_type (type), pg->peer, GNUNET_ATS_print_preference_type(kind),
1111           base_value, value_rate);
1112       break;
1113     case GNUNET_ATS_TEST_TG_SINUS:
1114       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1115           "Setting up %s preference generator peer [%u] `%s' baserate %u Bips, amplitude %u Bps\n",
1116           print_generator_type (type), pg->peer, GNUNET_ATS_print_preference_type(kind),
1117           base_value, value_rate);
1118       break;
1119     case GNUNET_ATS_TEST_TG_RANDOM:
1120       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1121           "Setting up %s preference generator peer [%u] `%s' min %u Bips max %u Bps\n",
1122           print_generator_type (type), pg->peer, GNUNET_ATS_print_preference_type(kind),
1123           base_value, value_rate);
1124       break;
1125     default:
1126       break;
1127   }
1128
1129   pg->set_task = GNUNET_SCHEDULER_add_now (&set_pref_task, pg);
1130   if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != feedback_frequency.rel_value_us)
1131   {
1132     struct TestAddress * addr = find_active_address(p);
1133     const double *properties = get_property_cb (NULL, addr->ats_addr);
1134
1135     pg->last_assigned_bw_in = p->assigned_bw_in;
1136     pg->last_assigned_bw_out = p->assigned_bw_out;
1137     pg->feedback_bw_in_acc = 0;
1138     pg->feedback_bw_out_acc = 0;
1139
1140     pg->last_delay_value = properties[GNUNET_ATS_QUALITY_NET_DELAY];
1141     pg->feedback_delay_acc = 0;
1142
1143     pg->feedback_last_bw_update = GNUNET_TIME_absolute_get();
1144     pg->feedback_last_delay_update = GNUNET_TIME_absolute_get();
1145     pg->feedback_last = GNUNET_TIME_absolute_get();
1146     pg->feedback_task = GNUNET_SCHEDULER_add_delayed (feedback_frequency,
1147         &set_feedback_task, pg);
1148   }
1149
1150   return pg;
1151 }
1152
1153
1154
1155 /**
1156  * Stop all preferences generators
1157  */
1158 void
1159 GNUNET_ATS_solver_generate_preferences_stop_all ()
1160 {
1161   struct PreferenceGenerator *cur;
1162   struct PreferenceGenerator *next;
1163   next = pref_gen_head;
1164   for (cur = next; NULL != cur; cur = next)
1165   {
1166       next = cur->next;
1167       GNUNET_ATS_solver_generate_preferences_stop(cur);
1168   }
1169 }
1170
1171
1172
1173 /**
1174  * Experiments
1175  */
1176
1177 const char *
1178 print_op (enum OperationType op)
1179 {
1180   switch (op) {
1181     case SOLVER_OP_ADD_ADDRESS:
1182       return "ADD_ADDRESS";
1183     case SOLVER_OP_DEL_ADDRESS:
1184       return "DEL_ADDRESS";
1185     case SOLVER_OP_START_SET_PREFERENCE:
1186       return "START_SET_PREFERENCE";
1187     case SOLVER_OP_STOP_SET_PREFERENCE:
1188       return "STOP_STOP_PREFERENCE";
1189     case SOLVER_OP_START_SET_PROPERTY:
1190       return "START_SET_PROPERTY";
1191     case SOLVER_OP_STOP_SET_PROPERTY:
1192       return "STOP_SET_PROPERTY";
1193     case SOLVER_OP_START_REQUEST:
1194       return "START_REQUEST";
1195     case SOLVER_OP_STOP_REQUEST:
1196       return "STOP_REQUEST";
1197     default:
1198       break;
1199   }
1200   return "";
1201 }
1202
1203 static struct Experiment *
1204 create_experiment ()
1205 {
1206   struct Experiment *e;
1207   e = GNUNET_new (struct Experiment);
1208   e->name = NULL;
1209   e->start = NULL;
1210   e->total_duration = GNUNET_TIME_UNIT_ZERO;
1211   return e;
1212 }
1213
1214 static void
1215 free_experiment (struct Experiment *e)
1216 {
1217   struct Episode *cur;
1218   struct Episode *next;
1219   struct GNUNET_ATS_TEST_Operation *cur_o;
1220   struct GNUNET_ATS_TEST_Operation *next_o;
1221
1222   next = e->start;
1223   for (cur = next; NULL != cur; cur = next)
1224   {
1225     next = cur->next;
1226
1227     next_o = cur->head;
1228     for (cur_o = next_o; NULL != cur_o; cur_o = next_o)
1229     {
1230       next_o = cur_o->next;
1231       GNUNET_free_non_null (cur_o->address);
1232       GNUNET_free_non_null (cur_o->plugin);
1233       GNUNET_free (cur_o);
1234     }
1235     GNUNET_free (cur);
1236   }
1237
1238   GNUNET_free_non_null (e->name);
1239   GNUNET_free_non_null (e->log_prefix);
1240   GNUNET_free_non_null (e->log_output_dir);
1241   GNUNET_free_non_null (e->cfg_file);
1242   GNUNET_free (e);
1243 }
1244
1245
1246 static int
1247 load_op_add_address (struct GNUNET_ATS_TEST_Operation *o,
1248     struct Episode *e,
1249     int op_counter,
1250     char *sec_name,
1251     const struct GNUNET_CONFIGURATION_Handle *cfg)
1252 {
1253   char *op_name;
1254   char *op_network;
1255
1256   /* peer pid */
1257   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1258   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1259       sec_name, op_name, &o->peer_id))
1260   {
1261     fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
1262         op_counter, "ADD_ADDRESS", op_name);
1263     GNUNET_free (op_name);
1264     return GNUNET_SYSERR;
1265   }
1266   GNUNET_free (op_name);
1267
1268   /* address pid */
1269   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1270   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1271       sec_name, op_name, &o->address_id))
1272   {
1273     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1274         op_counter, "ADD_ADDRESS", op_name);
1275     GNUNET_free (op_name);
1276     return GNUNET_SYSERR;
1277   }
1278   GNUNET_free (op_name);
1279
1280   /* plugin */
1281   GNUNET_asprintf(&op_name, "op-%u-plugin", op_counter);
1282   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1283       sec_name, op_name, &o->plugin))
1284   {
1285     fprintf (stderr, "Missing plugin in operation %u `%s' in episode `%s'\n",
1286         op_counter, "ADD_ADDRESS", op_name);
1287     GNUNET_free (op_name);
1288     return GNUNET_SYSERR;
1289   }
1290   GNUNET_free (op_name);
1291
1292   /* address  */
1293   GNUNET_asprintf(&op_name, "op-%u-address", op_counter);
1294   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1295       sec_name, op_name, &o->address))
1296   {
1297     fprintf (stderr, "Missing address in operation %u `%s' in episode `%s'\n",
1298         op_counter, "ADD_ADDRESS", op_name);
1299     GNUNET_free (op_name);
1300     return GNUNET_SYSERR;
1301   }
1302   GNUNET_free (op_name);
1303
1304   /* session */
1305   GNUNET_asprintf(&op_name, "op-%u-address-session", op_counter);
1306   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1307       sec_name, op_name, &o->address_session))
1308   {
1309     fprintf (stderr, "Missing address-session in operation %u `%s' in episode `%s'\n",
1310         op_counter, "ADD_ADDRESS", op_name);
1311     GNUNET_free (op_name);
1312     return GNUNET_SYSERR;
1313   }
1314   GNUNET_free (op_name);
1315
1316   /* network */
1317   GNUNET_asprintf(&op_name, "op-%u-address-network", op_counter);
1318   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1319       sec_name, op_name, &op_network))
1320   {
1321     fprintf (stderr, "Missing address-network in operation %u `%s' in episode `%s'\n",
1322         op_counter, "ADD_ADDRESS", op_name);
1323     GNUNET_free (op_name);
1324     return GNUNET_SYSERR;
1325   }
1326   else
1327   {
1328     GNUNET_STRINGS_utf8_toupper (op_network,op_network);
1329     if (0 == strcmp(op_network, "UNSPECIFIED"))
1330     {
1331       o->address_network = GNUNET_ATS_NET_UNSPECIFIED;
1332     }
1333     else if (0 == strcmp(op_network, "LOOPBACK"))
1334     {
1335       o->address_network = GNUNET_ATS_NET_LOOPBACK;
1336     }
1337     else if (0 == strcmp(op_network, "LAN"))
1338     {
1339       o->address_network = GNUNET_ATS_NET_LAN;
1340     }
1341     else if (0 == strcmp(op_network, "WAN"))
1342     {
1343       o->address_network = GNUNET_ATS_NET_WAN;
1344     }
1345     else if (0 == strcmp(op_network, "WLAN"))
1346     {
1347       o->address_network = GNUNET_ATS_NET_WLAN;
1348     }
1349     else if (0 == strcmp(op_network, "BT"))
1350     {
1351       o->address_network = GNUNET_ATS_NET_BT;
1352     }
1353     else
1354     {
1355       fprintf (stderr, "Invalid address-network in operation %u `%s' in episode `%s': `%s'\n",
1356           op_counter, "ADD_ADDRESS", op_name, op_network);
1357       GNUNET_free (op_network);
1358       GNUNET_free (op_name);
1359       return GNUNET_SYSERR;
1360     }
1361   }
1362   GNUNET_free (op_network);
1363   GNUNET_free (op_name);
1364
1365   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1366       "Found operation %s: [%llu:%llu] address `%s' plugin `%s' \n",
1367       "ADD_ADDRESS", o->peer_id, o->address_id, o->address, o->plugin);
1368
1369   return GNUNET_OK;
1370 }
1371
1372 static int
1373 load_op_del_address (struct GNUNET_ATS_TEST_Operation *o,
1374     struct Episode *e,
1375     int op_counter,
1376     char *sec_name,
1377     const struct GNUNET_CONFIGURATION_Handle *cfg)
1378 {
1379   char *op_name;
1380   //char *op_network;
1381
1382   /* peer pid */
1383   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1384   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1385       sec_name, op_name, &o->peer_id))
1386   {
1387     fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
1388         op_counter, "DEL_ADDRESS", op_name);
1389     GNUNET_free (op_name);
1390     return GNUNET_SYSERR;
1391   }
1392   GNUNET_free (op_name);
1393
1394   /* address pid */
1395   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1396   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1397       sec_name, op_name, &o->address_id))
1398   {
1399     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1400         op_counter, "DEL_ADDRESS", op_name);
1401     GNUNET_free (op_name);
1402     return GNUNET_SYSERR;
1403   }
1404   GNUNET_free (op_name);
1405
1406 #if 0
1407   /* plugin */
1408   GNUNET_asprintf(&op_name, "op-%u-plugin", op_counter);
1409   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1410       sec_name, op_name, &o->plugin))
1411   {
1412     fprintf (stderr, "Missing plugin in operation %u `%s' in episode `%s'\n",
1413         op_counter, "DEL_ADDRESS", op_name);
1414     GNUNET_free (op_name);
1415     return GNUNET_SYSERR;
1416   }
1417   GNUNET_free (op_name);
1418
1419   /* address  */
1420   GNUNET_asprintf(&op_name, "op-%u-address", op_counter);
1421   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1422       sec_name, op_name, &o->address))
1423   {
1424     fprintf (stderr, "Missing address in operation %u `%s' in episode `%s'\n",
1425         op_counter, "DEL_ADDRESS", op_name);
1426     GNUNET_free (op_name);
1427     return GNUNET_SYSERR;
1428   }
1429   GNUNET_free (op_name);
1430
1431   /* session */
1432   GNUNET_asprintf(&op_name, "op-%u-address-session", op_counter);
1433   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1434       sec_name, op_name, &o->address_session))
1435   {
1436     fprintf (stderr, "Missing address-session in operation %u `%s' in episode `%s'\n",
1437         op_counter, "DEL_ADDRESS", op_name);
1438     GNUNET_free (op_name);
1439     return GNUNET_SYSERR;
1440   }
1441   GNUNET_free (op_name);
1442 #endif
1443
1444   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1445       "Found operation %s: [%llu:%llu] address `%s' plugin `%s' \n",
1446       "DEL_ADDRESS", o->peer_id, o->address_id, o->address, o->plugin);
1447
1448   return GNUNET_OK;
1449 }
1450
1451 static enum GNUNET_ATS_Property
1452 parse_preference_string (const char * str)
1453 {
1454   int c = 0;
1455   char *props[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1456
1457   for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1458     if (0 == strcmp(str, props[c]))
1459       return c;
1460   return 0;
1461 };
1462
1463 static int
1464 load_op_start_set_preference (struct GNUNET_ATS_TEST_Operation *o,
1465     struct Episode *e,
1466     int op_counter,
1467     char *sec_name,
1468     const struct GNUNET_CONFIGURATION_Handle *cfg)
1469 {
1470   char *op_name;
1471   char *type;
1472   char *pref;
1473
1474   /* peer pid */
1475   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1476   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1477       sec_name, op_name, &o->peer_id))
1478   {
1479     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1480         op_counter, "START_SET_PREFERENCE", op_name);
1481     GNUNET_free (op_name);
1482     return GNUNET_SYSERR;
1483   }
1484   GNUNET_free (op_name);
1485
1486   /* address pid */
1487   GNUNET_asprintf(&op_name, "op-%u-client-id", op_counter);
1488   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1489       sec_name, op_name, &o->client_id))
1490   {
1491     fprintf (stderr, "Missing client-id in operation %u `%s' in episode `%s'\n",
1492         op_counter, "START_SET_PREFERENCE", op_name);
1493     GNUNET_free (op_name);
1494     return GNUNET_SYSERR;
1495   }
1496   GNUNET_free (op_name);
1497
1498   /* generator */
1499   GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
1500   if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
1501           sec_name, op_name, &type)) )
1502   {
1503     fprintf (stderr, "Missing type in operation %u `%s' in episode `%s'\n",
1504         op_counter, "START_SET_PREFERENCE", op_name);
1505     GNUNET_free (op_name);
1506     return GNUNET_SYSERR;
1507   }
1508
1509   /* Load arguments for set_rate, start_send, set_preference */
1510   if (0 == strcmp (type, "constant"))
1511   {
1512     o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
1513   }
1514   else if (0 == strcmp (type, "linear"))
1515   {
1516     o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
1517   }
1518   else if (0 == strcmp (type, "sinus"))
1519   {
1520     o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
1521   }
1522   else if (0 == strcmp (type, "random"))
1523   {
1524     o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
1525   }
1526   else
1527   {
1528     fprintf (stderr, "Invalid generator type %u `%s' in episode %u\n",
1529         op_counter, op_name, e->id);
1530     GNUNET_free (type);
1531     GNUNET_free (op_name);
1532     return GNUNET_SYSERR;
1533   }
1534   GNUNET_free (type);
1535   GNUNET_free (op_name);
1536
1537
1538   /* Get base rate */
1539   GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
1540   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1541       sec_name, op_name, &o->base_rate))
1542   {
1543     fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
1544         op_counter, op_name, e->id);
1545     GNUNET_free (op_name);
1546     return GNUNET_SYSERR;
1547   }
1548   GNUNET_free (op_name);
1549
1550
1551   /* Get max rate */
1552   GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
1553   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1554       sec_name, op_name, &o->max_rate))
1555   {
1556     if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
1557         (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
1558         (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
1559     {
1560       fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
1561           op_counter, op_name, e->id);
1562       GNUNET_free (op_name);
1563       return GNUNET_SYSERR;
1564     }
1565   }
1566   GNUNET_free (op_name);
1567
1568   /* Get period */
1569   GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
1570   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1571       sec_name, op_name, &o->period))
1572   {
1573     o->period = e->duration;
1574   }
1575   GNUNET_free (op_name);
1576
1577   /* Get frequency */
1578   GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
1579   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1580       sec_name, op_name, &o->frequency))
1581   {
1582       fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
1583           op_counter, op_name, e->id);
1584       GNUNET_free (op_name);
1585       return GNUNET_SYSERR;
1586   }
1587   GNUNET_free (op_name);
1588
1589   /* Get preference */
1590   GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
1591   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1592       sec_name, op_name, &pref))
1593   {
1594       fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
1595           op_counter, op_name, e->id);
1596       GNUNET_free (op_name);
1597       return GNUNET_SYSERR;
1598   }
1599
1600   if (0 == (o->pref_type = parse_preference_string(pref)))
1601   {
1602       fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
1603           op_counter, op_name, e->id);
1604       GNUNET_free (op_name);
1605       GNUNET_free (pref);
1606       return GNUNET_SYSERR;
1607   }
1608   GNUNET_free (pref);
1609   GNUNET_free (op_name);
1610
1611   /* Get feedback delay */
1612   GNUNET_asprintf(&op_name, "op-%u-feedback_delay", op_counter);
1613   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_time (cfg,
1614       sec_name, op_name, &o->feedback_delay))
1615   {
1616       fprintf (stderr, "Using feedback delay %llu in operation %u `%s' in episode %u\n",
1617           (long long unsigned int) o->feedback_delay.rel_value_us,
1618           op_counter, op_name, e->id);
1619   }
1620   else
1621     o->feedback_delay = GNUNET_TIME_UNIT_FOREVER_REL;
1622   GNUNET_free (op_name);
1623
1624   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1625       "Found operation %s: [%llu:%llu]: %s = %llu\n",
1626       "START_SET_PREFERENCE", o->peer_id, o->address_id,
1627       GNUNET_ATS_print_preference_type(o->pref_type), o->base_rate);
1628
1629   return GNUNET_OK;
1630 }
1631
1632 static int
1633 load_op_stop_set_preference (struct GNUNET_ATS_TEST_Operation *o,
1634     struct Episode *e,
1635     int op_counter,
1636     char *sec_name,
1637     const struct GNUNET_CONFIGURATION_Handle *cfg)
1638 {
1639   char *op_name;
1640   char *pref;
1641
1642   /* peer pid */
1643   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1644   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1645       sec_name, op_name, &o->peer_id))
1646   {
1647     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1648         op_counter, "STOP_SET_PREFERENCE", op_name);
1649     GNUNET_free (op_name);
1650     return GNUNET_SYSERR;
1651   }
1652   GNUNET_free (op_name);
1653
1654   /* address pid */
1655   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1656   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1657       sec_name, op_name, &o->address_id))
1658   {
1659     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1660         op_counter, "STOP_SET_PREFERENCE", op_name);
1661     GNUNET_free (op_name);
1662     return GNUNET_SYSERR;
1663   }
1664   GNUNET_free (op_name);
1665
1666   /* Get preference */
1667   GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
1668   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1669       sec_name, op_name, &pref))
1670   {
1671     fprintf (stderr, "Missing preference in operation %u `%s' in episode `%s'\n",
1672         op_counter, "STOP_SET_PREFERENCE", op_name);
1673       GNUNET_free (op_name);
1674       return GNUNET_SYSERR;
1675   }
1676
1677   if (0 == (o->pref_type = parse_preference_string(pref)))
1678   {
1679       fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
1680           op_counter, op_name, e->id);
1681       GNUNET_free (op_name);
1682       GNUNET_free (pref);
1683       return GNUNET_SYSERR;
1684   }
1685   GNUNET_free (pref);
1686   GNUNET_free (op_name);
1687
1688   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1689       "Found operation %s: [%llu:%llu]: %s\n",
1690       "STOP_SET_PREFERENCE", o->peer_id, o->address_id,
1691       GNUNET_ATS_print_preference_type(o->pref_type));
1692   return GNUNET_OK;
1693 }
1694
1695
1696 static enum GNUNET_ATS_Property
1697 parse_property_string (const char *str)
1698 {
1699   enum GNUNET_ATS_Property c;
1700
1701   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
1702     if (0 == strcmp(str,
1703                     GNUNET_ATS_print_property_type (c)))
1704       return c;
1705   return 0;
1706 }
1707
1708
1709 static int
1710 load_op_start_set_property(struct GNUNET_ATS_TEST_Operation *o,
1711     struct Episode *e,
1712     int op_counter,
1713     char *sec_name,
1714     const struct GNUNET_CONFIGURATION_Handle *cfg)
1715 {
1716   char *op_name;
1717   char *type;
1718   char *prop;
1719
1720   /* peer pid */
1721   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1722   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1723       sec_name, op_name, &o->peer_id))
1724   {
1725     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1726         op_counter, "START_SET_PROPERTY", op_name);
1727     GNUNET_free (op_name);
1728     return GNUNET_SYSERR;
1729   }
1730   GNUNET_free (op_name);
1731
1732   /* address pid */
1733   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1734   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1735       sec_name, op_name, &o->address_id))
1736   {
1737     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1738         op_counter, "START_SET_PROPERTY", op_name);
1739     GNUNET_free (op_name);
1740     return GNUNET_SYSERR;
1741   }
1742   GNUNET_free (op_name);
1743
1744   /* generator */
1745   GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
1746   if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
1747           sec_name, op_name, &type)) )
1748   {
1749     fprintf (stderr, "Missing type in operation %u `%s' in episode `%s'\n",
1750         op_counter, "START_SET_PROPERTY", op_name);
1751     GNUNET_free (op_name);
1752     return GNUNET_SYSERR;
1753   }
1754
1755   /* Load arguments for set_rate, start_send, set_preference */
1756   if (0 == strcmp (type, "constant"))
1757   {
1758     o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
1759   }
1760   else if (0 == strcmp (type, "linear"))
1761   {
1762     o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
1763   }
1764   else if (0 == strcmp (type, "sinus"))
1765   {
1766     o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
1767   }
1768   else if (0 == strcmp (type, "random"))
1769   {
1770     o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
1771   }
1772   else
1773   {
1774     fprintf (stderr, "Invalid generator type %u `%s' in episode %u\n",
1775         op_counter, op_name, e->id);
1776     GNUNET_free (type);
1777     GNUNET_free (op_name);
1778     return GNUNET_SYSERR;
1779   }
1780   GNUNET_free (type);
1781   GNUNET_free (op_name);
1782
1783
1784   /* Get base rate */
1785   GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
1786   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1787       sec_name, op_name, &o->base_rate))
1788   {
1789     fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
1790         op_counter, op_name, e->id);
1791     GNUNET_free (op_name);
1792     return GNUNET_SYSERR;
1793   }
1794   GNUNET_free (op_name);
1795
1796
1797   /* Get max rate */
1798   GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
1799   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1800       sec_name, op_name, &o->max_rate))
1801   {
1802     if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
1803         (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
1804         (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
1805     {
1806       fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
1807           op_counter, op_name, e->id);
1808       GNUNET_free (op_name);
1809       return GNUNET_SYSERR;
1810     }
1811   }
1812   GNUNET_free (op_name);
1813
1814   /* Get period */
1815   GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
1816   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1817       sec_name, op_name, &o->period))
1818   {
1819     o->period = e->duration;
1820   }
1821   GNUNET_free (op_name);
1822
1823   /* Get frequency */
1824   GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
1825   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1826       sec_name, op_name, &o->frequency))
1827   {
1828       fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
1829           op_counter, op_name, e->id);
1830       GNUNET_free (op_name);
1831       return GNUNET_SYSERR;
1832   }
1833   GNUNET_free (op_name);
1834
1835   /* Get preference */
1836   GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
1837   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1838       sec_name, op_name, &prop))
1839   {
1840       fprintf (stderr, "Missing property in operation %u `%s' in episode %u\n",
1841           op_counter, op_name, e->id);
1842       GNUNET_free (op_name);
1843       GNUNET_free_non_null (prop);
1844       return GNUNET_SYSERR;
1845   }
1846
1847   if (0 == (o->prop_type = parse_property_string(prop)))
1848   {
1849       fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
1850           op_counter, op_name, e->id);
1851       GNUNET_free (op_name);
1852       GNUNET_free (prop);
1853       return GNUNET_SYSERR;
1854   }
1855
1856   GNUNET_free (prop);
1857   GNUNET_free (op_name);
1858
1859   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1860       "Found operation %s: [%llu:%llu] %s = %llu\n",
1861       "START_SET_PROPERTY", o->peer_id, o->address_id,
1862       GNUNET_ATS_print_property_type (o->prop_type), o->base_rate);
1863
1864   return GNUNET_OK;
1865 }
1866
1867 static int
1868 load_op_stop_set_property (struct GNUNET_ATS_TEST_Operation *o,
1869     struct Episode *e,
1870     int op_counter,
1871     char *sec_name,
1872     const struct GNUNET_CONFIGURATION_Handle *cfg)
1873 {
1874   char *op_name;
1875   char *pref;
1876
1877   /* peer pid */
1878   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1879   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1880       sec_name, op_name, &o->peer_id))
1881   {
1882     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1883         op_counter, "STOP_SET_PROPERTY", op_name);
1884     GNUNET_free (op_name);
1885     return GNUNET_SYSERR;
1886   }
1887   GNUNET_free (op_name);
1888
1889   /* address pid */
1890   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1891   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1892       sec_name, op_name, &o->address_id))
1893   {
1894     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1895         op_counter, "STOP_SET_PROPERTY", op_name);
1896     GNUNET_free (op_name);
1897     return GNUNET_SYSERR;
1898   }
1899   GNUNET_free (op_name);
1900
1901   /* Get property */
1902   GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
1903   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1904       sec_name, op_name, &pref))
1905   {
1906     fprintf (stderr, "Missing property in operation %u `%s' in episode `%s'\n",
1907         op_counter, "STOP_SET_PROPERTY", op_name);
1908       GNUNET_free (op_name);
1909       GNUNET_free_non_null (pref);
1910       return GNUNET_SYSERR;
1911   }
1912
1913   if (0 == (o->prop_type = parse_property_string(pref)))
1914   {
1915       fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
1916           op_counter, op_name, e->id);
1917       GNUNET_free (op_name);
1918       GNUNET_free_non_null (pref);
1919       return GNUNET_SYSERR;
1920   }
1921
1922   GNUNET_free (pref);
1923   GNUNET_free (op_name);
1924
1925   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1926       "Found operation %s: [%llu:%llu] %s\n",
1927       "STOP_SET_PROPERTY", o->peer_id, o->address_id,
1928       GNUNET_ATS_print_property_type (o->prop_type));
1929
1930   return GNUNET_OK;
1931 }
1932
1933
1934 static int
1935 load_op_start_request (struct GNUNET_ATS_TEST_Operation *o,
1936     struct Episode *e,
1937     int op_counter,
1938     char *sec_name,
1939     const struct GNUNET_CONFIGURATION_Handle *cfg)
1940 {
1941   char *op_name;
1942
1943   /* peer pid */
1944   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1945   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1946       sec_name, op_name, &o->peer_id))
1947   {
1948     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1949         op_counter, "START_REQUEST", op_name);
1950     GNUNET_free (op_name);
1951     return GNUNET_SYSERR;
1952   }
1953   GNUNET_free (op_name);
1954   return GNUNET_OK;
1955 }
1956
1957 static int
1958 load_op_stop_request (struct GNUNET_ATS_TEST_Operation *o,
1959     struct Episode *e,
1960     int op_counter,
1961     char *sec_name,
1962     const struct GNUNET_CONFIGURATION_Handle *cfg)
1963 {
1964   char *op_name;
1965
1966   /* peer pid */
1967   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1968   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1969       sec_name, op_name, &o->peer_id))
1970   {
1971     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1972         op_counter, "STOP_REQUEST", op_name);
1973     GNUNET_free (op_name);
1974     return GNUNET_SYSERR;
1975   }
1976   GNUNET_free (op_name);
1977   return GNUNET_OK;
1978 }
1979
1980
1981 static int
1982 load_episode (struct Experiment *e, struct Episode *cur,
1983     struct GNUNET_CONFIGURATION_Handle *cfg)
1984 {
1985   struct GNUNET_ATS_TEST_Operation *o;
1986   char *sec_name;
1987   char *op_name;
1988   char *op;
1989   int op_counter = 0;
1990   int res;
1991   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Parsing episode %u\n",cur->id);
1992   GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
1993
1994   while (1)
1995   {
1996     /* Load operation */
1997     GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
1998     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
1999         sec_name, op_name, &op))
2000     {
2001       GNUNET_free (op_name);
2002       break;
2003     }
2004     o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
2005     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "==== Parsing operation %u: `%s'\n",
2006         cur->id, op_name);
2007
2008     /* operations = set_rate, start_send, stop_send, set_preference */
2009     if (0 == strcmp (op, "address_add"))
2010     {
2011       o->type = SOLVER_OP_ADD_ADDRESS;
2012       res = load_op_add_address (o, cur,
2013           op_counter, sec_name, cfg);
2014     }
2015     else if (0 == strcmp (op, "address_del"))
2016     {
2017       o->type = SOLVER_OP_DEL_ADDRESS;
2018       res = load_op_del_address (o, cur,
2019           op_counter, sec_name, cfg);
2020     }
2021     else if (0 == strcmp (op, "start_set_property"))
2022     {
2023       o->type = SOLVER_OP_START_SET_PROPERTY;
2024       res = load_op_start_set_property (o, cur,
2025           op_counter, sec_name, cfg);
2026     }
2027     else if (0 == strcmp (op, "stop_set_property"))
2028     {
2029       o->type = SOLVER_OP_STOP_SET_PROPERTY;
2030       res = load_op_stop_set_property (o, cur,
2031           op_counter, sec_name, cfg);
2032     }
2033     else if (0 == strcmp (op, "start_set_preference"))
2034     {
2035       o->type = SOLVER_OP_START_SET_PREFERENCE;
2036       res =  load_op_start_set_preference (o, cur,
2037           op_counter, sec_name, cfg);
2038     }
2039     else if (0 == strcmp (op, "stop_set_preference"))
2040     {
2041       o->type = SOLVER_OP_STOP_SET_PREFERENCE;
2042       res =  load_op_stop_set_preference (o, cur,
2043           op_counter, sec_name, cfg);
2044     }
2045     else if (0 == strcmp (op, "start_request"))
2046     {
2047       o->type = SOLVER_OP_START_REQUEST;
2048       res = load_op_start_request (o, cur,
2049           op_counter, sec_name, cfg);
2050     }
2051     else if (0 == strcmp (op, "stop_request"))
2052     {
2053       o->type = SOLVER_OP_STOP_REQUEST;
2054       res = load_op_stop_request(o, cur,
2055           op_counter, sec_name, cfg);
2056     }
2057     else
2058     {
2059       fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
2060           op_counter, op, cur->id);
2061       res = GNUNET_SYSERR;
2062     }
2063
2064     GNUNET_free (op);
2065     GNUNET_free (op_name);
2066
2067     if (GNUNET_SYSERR == res)
2068     {
2069       GNUNET_free (o);
2070       GNUNET_free (sec_name);
2071       return GNUNET_SYSERR;
2072     }
2073
2074     GNUNET_CONTAINER_DLL_insert_tail (cur->head,cur->tail, o);
2075     op_counter++;
2076   }
2077   GNUNET_free (sec_name);
2078   return GNUNET_OK;
2079 }
2080
2081 static int
2082 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
2083 {
2084   int e_counter = 0;
2085   char *sec_name;
2086   struct GNUNET_TIME_Relative e_duration;
2087   struct Episode *cur;
2088   struct Episode *last;
2089
2090   e_counter = 0;
2091   last = NULL;
2092   while (1)
2093   {
2094     GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
2095     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
2096         sec_name, "duration", &e_duration))
2097     {
2098       GNUNET_free (sec_name);
2099       break;
2100     }
2101
2102     cur = GNUNET_new (struct Episode);
2103     cur->duration = e_duration;
2104     cur->id = e_counter;
2105
2106     if (GNUNET_OK != load_episode (e, cur, cfg))
2107     {
2108       GNUNET_free (sec_name);
2109       GNUNET_free (cur);
2110       return GNUNET_SYSERR;
2111     }
2112
2113     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Found episode %u with duration %s \n",
2114         e_counter,
2115         GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
2116
2117     /* Update experiment */
2118     e->num_episodes ++;
2119     e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
2120     /* Put in linked list */
2121     if (NULL == last)
2122       e->start = cur;
2123     else
2124       last->next = cur;
2125
2126     GNUNET_free (sec_name);
2127     e_counter ++;
2128     last = cur;
2129   }
2130   return e_counter;
2131 }
2132
2133 static void
2134 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
2135 {
2136   struct Experiment *e = cls;
2137   e->experiment_timeout_task = NULL;
2138   fprintf (stderr, "Experiment timeout!\n");
2139
2140   if (NULL != e->episode_timeout_task)
2141   {
2142     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
2143     e->episode_timeout_task = NULL;
2144   }
2145
2146   e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
2147       GNUNET_SYSERR);
2148 }
2149
2150 struct ATS_Address *
2151 create_ats_address (const struct GNUNET_PeerIdentity *peer,
2152                 const char *plugin_name,
2153                 const void *plugin_addr,
2154                 size_t plugin_addr_len,
2155                 uint32_t session_id,
2156                 uint32_t network)
2157 {
2158   struct ATS_Address *aa = NULL;
2159
2160   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len + strlen (plugin_name) + 1);
2161   aa->atsi = GNUNET_new (struct GNUNET_ATS_Information);
2162   aa->atsi[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
2163   aa->atsi[0].value = htonl (network);
2164   aa->atsi_count = 1;
2165
2166   aa->peer = *peer;
2167   aa->addr_len = plugin_addr_len;
2168   aa->addr = &aa[1];
2169   aa->plugin = (char *) &aa[1] + plugin_addr_len;
2170   memcpy (&aa[1], plugin_addr, plugin_addr_len);
2171   memcpy (aa->plugin, plugin_name, strlen (plugin_name) + 1);
2172   aa->session_id = session_id;
2173   aa->active = GNUNET_NO;
2174   aa->used = GNUNET_NO;
2175   aa->solver_information = NULL;
2176   aa->assigned_bw_in = 0;
2177   aa->assigned_bw_out = 0;
2178
2179   return aa;
2180 }
2181
2182
2183
2184 static void
2185 enforce_add_address (struct GNUNET_ATS_TEST_Operation *op)
2186 {
2187   struct TestPeer *p;
2188   struct TestAddress *a;
2189   int c;
2190
2191   if (NULL == (p = find_peer_by_id (op->peer_id)))
2192   {
2193     p = GNUNET_new (struct TestPeer);
2194     p->id = op->peer_id;
2195     p->assigned_bw_in = 0;
2196     p->assigned_bw_out = 0;
2197     memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
2198     for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
2199     {
2200       p->pref_abs[c] = DEFAULT_ABS_PREFERENCE;
2201       p->pref_norm[c] = DEFAULT_REL_PREFERENCE;
2202     }
2203
2204     GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, p);
2205   }
2206
2207   if (NULL != (find_address_by_id (p, op->address_id)))
2208   {
2209     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Duplicate address %u for peer %u\n",
2210         op->address_id, op->peer_id);
2211     return;
2212   }
2213
2214   a = GNUNET_new (struct TestAddress);
2215   a->aid = op->address_id;
2216   a->network = op->address_network;
2217   a->ats_addr = create_ats_address (&p->peer_id, op->plugin, op->address,
2218       strlen (op->address) + 1, op->address_session, op->address_network);
2219   memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
2220   GNUNET_CONTAINER_DLL_insert_tail (p->addr_head, p->addr_tail, a);
2221
2222   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
2223     a->prop_norm[c] = DEFAULT_REL_QUALITY;
2224
2225   GNUNET_CONTAINER_multipeermap_put (sh->addresses, &p->peer_id, a->ats_addr,
2226     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2227
2228   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Adding address %u for peer %u in network `%s'\n",
2229     op->address_id, op->peer_id, GNUNET_ATS_print_network_type(a->network));
2230
2231   sh->sf->s_add (sh->sf->cls, a->ats_addr, op->address_network);
2232
2233 }
2234
2235
2236 static void
2237 enforce_del_address (struct GNUNET_ATS_TEST_Operation *op)
2238 {
2239   struct TestPeer *p;
2240   struct TestAddress *a;
2241   struct PropertyGenerator *pg;
2242
2243   if (NULL == (p = find_peer_by_id (op->peer_id)))
2244   {
2245     GNUNET_break (0);
2246     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2247         "Deleting address for unknown peer %u\n", op->peer_id);
2248     return;
2249   }
2250
2251   if (NULL == (a =find_address_by_id (p, op->address_id)))
2252   {
2253     GNUNET_break (0);
2254     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2255         "Deleting address for unknown peer %u\n", op->peer_id);
2256     return;
2257   }
2258
2259   while (NULL != (pg = find_prop_gen (p->id, a->aid, 0)))
2260   {
2261     GNUNET_ATS_solver_generate_property_stop (pg);
2262   }
2263
2264   GNUNET_assert (GNUNET_YES ==
2265                  GNUNET_CONTAINER_multipeermap_remove (sh->addresses,
2266                                                        &p->peer_id,
2267                                                        a->ats_addr));
2268   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2269               "Removing address %u for peer %u\n",
2270               op->address_id,
2271               op->peer_id);
2272
2273   sh->sf->s_del (sh->sf->cls, a->ats_addr, GNUNET_NO);
2274
2275   if (NULL != l)
2276   {
2277     GNUNET_ATS_solver_logging_now (l);
2278   }
2279   GNUNET_CONTAINER_DLL_remove(p->addr_head, p->addr_tail, a);
2280
2281   GNUNET_free_non_null(a->ats_addr->atsi);
2282   GNUNET_free (a->ats_addr);
2283   GNUNET_free (a);
2284
2285 }
2286
2287 static void
2288 enforce_start_property (struct GNUNET_ATS_TEST_Operation *op)
2289 {
2290   struct PropertyGenerator *pg;
2291   struct TestPeer *p;
2292   struct TestAddress *a;
2293
2294   if (NULL != (pg = find_prop_gen (op->peer_id, op->address_id, op->prop_type)))
2295   {
2296     GNUNET_ATS_solver_generate_property_stop (pg);
2297     GNUNET_free (pg);
2298   }
2299
2300   if (NULL == (p = find_peer_by_id (op->peer_id)))
2301   {
2302     GNUNET_break (0);
2303     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2304         "Starting property generation for unknown peer %u\n", op->peer_id);
2305     return;
2306   }
2307
2308   if (NULL == (a = find_address_by_id (p, op->address_id)))
2309   {
2310     GNUNET_break (0);
2311     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2312         "Setting property for unknown address %u\n", op->peer_id);
2313     return;
2314   }
2315
2316   GNUNET_ATS_solver_generate_property_start (op->peer_id,
2317     op->address_id,
2318     p, a,
2319     op->gen_type,
2320     op->base_rate,
2321     op->max_rate,
2322     op->period,
2323     op->frequency,
2324     op->prop_type);
2325 }
2326
2327 static void
2328 enforce_stop_property (struct GNUNET_ATS_TEST_Operation *op)
2329 {
2330   struct PropertyGenerator *pg = find_prop_gen(op->peer_id, op->address_id,
2331       op->prop_type);
2332   if (NULL != pg)
2333   {
2334     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2335         "Stopping preference generation for peer %u address %u\n", op->peer_id,
2336         op->address_id);
2337     GNUNET_ATS_solver_generate_property_stop (pg);
2338   }
2339   else
2340   {
2341     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2342         "Cannot find preference generator for peer %u address %u\n",
2343         op->peer_id, op->address_id);
2344     GNUNET_break (0);
2345   }
2346 }
2347
2348 static void
2349 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
2350 {
2351   struct PreferenceGenerator *pg;
2352   if (NULL != (pg = find_pref_gen (op->peer_id, op->pref_type)))
2353   {
2354     GNUNET_ATS_solver_generate_preferences_stop (pg);
2355     GNUNET_free (pg);
2356   }
2357
2358   if (NULL == (find_peer_by_id (op->peer_id)))
2359   {
2360     GNUNET_break (0);
2361     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2362         "Starting preference generation for unknown peer %u\n", op->peer_id);
2363     return;
2364   }
2365
2366   GNUNET_ATS_solver_generate_preferences_start (op->peer_id,
2367     op->address_id,
2368     op->client_id,
2369     op->gen_type,
2370     op->base_rate,
2371     op->max_rate,
2372     op->period,
2373     op->frequency,
2374     op->pref_type,
2375     op->frequency);
2376 }
2377
2378 static void
2379 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
2380 {
2381   struct PreferenceGenerator *pg = find_pref_gen(op->peer_id,
2382       op->pref_type);
2383   if (NULL != pg)
2384   {
2385     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2386         "Stopping property generation for peer %u address %u\n", op->peer_id,
2387         op->address_id);
2388     GNUNET_ATS_solver_generate_preferences_stop (pg);
2389   }
2390   else
2391   {
2392     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2393         "Cannot find preference generator for peer %u address %u\n",
2394         op->peer_id, op->address_id);
2395     GNUNET_break (0);
2396   }
2397 }
2398
2399
2400 static void
2401 enforce_start_request (struct GNUNET_ATS_TEST_Operation *op)
2402 {
2403   struct TestPeer *p;
2404   const struct ATS_Address *res;
2405
2406   if (NULL == (p = find_peer_by_id (op->peer_id)))
2407   {
2408     GNUNET_break (0);
2409     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2410         "Requesting address for unknown peer %u\n", op->peer_id);
2411     return;
2412   }
2413
2414   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Requesting address for peer %u\n",
2415       op->peer_id);
2416   p->is_requested = GNUNET_YES;
2417
2418   res = sh->sf->s_get (sh->sf->cls, &p->peer_id);
2419   if (NULL != res)
2420   {
2421     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Suggested address for peer %u: %llu %llu\n",
2422         op->peer_id,
2423         res->assigned_bw_in,
2424         res->assigned_bw_out);
2425     if (NULL != l)
2426       GNUNET_ATS_solver_logging_now (l);
2427   }
2428 }
2429
2430 static void
2431 enforce_stop_request (struct GNUNET_ATS_TEST_Operation *op)
2432 {
2433   struct TestPeer *p;
2434
2435   if (NULL == (p = find_peer_by_id (op->peer_id)))
2436   {
2437     GNUNET_break (0);
2438     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2439         "Requesting address for unknown peer %u\n", op->peer_id);
2440     return;
2441   }
2442
2443
2444
2445   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stop requesting address for peer %u\n",
2446       op->peer_id);
2447   p->is_requested = GNUNET_NO;
2448   p->assigned_bw_in = 0;
2449   p->assigned_bw_out = 0;
2450   sh->sf->s_get_stop (sh->sf->cls, &p->peer_id);
2451
2452   if (NULL != l)
2453   {
2454     GNUNET_ATS_solver_logging_now (l);
2455   }
2456
2457 }
2458
2459 static void enforce_episode (struct Episode *ep)
2460 {
2461   struct GNUNET_ATS_TEST_Operation *cur;
2462   for (cur = ep->head; NULL != cur; cur = cur->next)
2463   {
2464     switch (cur->type) {
2465       case SOLVER_OP_ADD_ADDRESS:
2466         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
2467             print_op (cur->type), cur->peer_id, cur->address_id);
2468         enforce_add_address (cur);
2469         break;
2470       case SOLVER_OP_DEL_ADDRESS:
2471         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
2472             print_op (cur->type), cur->peer_id, cur->address_id);
2473         enforce_del_address (cur);
2474         break;
2475       case SOLVER_OP_START_SET_PROPERTY:
2476         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2477             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2478         enforce_start_property (cur);
2479         break;
2480       case SOLVER_OP_STOP_SET_PROPERTY:
2481         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2482             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2483         enforce_stop_property (cur);
2484         break;
2485       case SOLVER_OP_START_SET_PREFERENCE:
2486         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2487             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2488         enforce_start_preference (cur);
2489         break;
2490       case SOLVER_OP_STOP_SET_PREFERENCE:
2491         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2492             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2493         enforce_stop_preference (cur);
2494         break;
2495       case SOLVER_OP_START_REQUEST:
2496         fprintf (stderr, "Enforcing operation: %s [%llu]\n",
2497             print_op (cur->type), cur->peer_id);
2498         enforce_start_request (cur);
2499         break;
2500       case SOLVER_OP_STOP_REQUEST:
2501         fprintf (stderr, "Enforcing operation: %s [%llu]\n",
2502             print_op (cur->type), cur->peer_id);
2503         enforce_stop_request (cur);
2504         break;
2505       default:
2506         break;
2507     }
2508   }
2509 }
2510
2511 static void
2512 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
2513 {
2514   struct Experiment *e = cls;
2515   e->episode_timeout_task = NULL;
2516   if (NULL != e->ep_done_cb)
2517     e->ep_done_cb (e->cur);
2518
2519   /* Scheduling next */
2520   e->cur = e->cur->next;
2521   if (NULL == e->cur)
2522   {
2523     /* done */
2524     fprintf (stderr, "Last episode done!\n");
2525     if (NULL != e->experiment_timeout_task)
2526     {
2527       GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
2528       e->experiment_timeout_task = NULL;
2529     }
2530     e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
2531     return;
2532   }
2533
2534   fprintf (stderr, "Running episode %u with timeout %s\n",
2535       e->cur->id,
2536       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
2537   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
2538       &timeout_episode, e);
2539   enforce_episode(e->cur);
2540
2541
2542 }
2543
2544
2545 void
2546 GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
2547     GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
2548     GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
2549 {
2550   fprintf (stderr, "Running experiment `%s'  with timeout %s\n", e->name,
2551       GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
2552   e->e_done_cb = e_done_cb;
2553   e->ep_done_cb = ep_done_cb;
2554   e->start_time = GNUNET_TIME_absolute_get();
2555
2556   /* Start total time out */
2557   e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
2558       &timeout_experiment, e);
2559
2560   /* Start */
2561   if (NULL == e->start)
2562   {
2563     GNUNET_break (0);
2564     return;
2565   }
2566
2567   e->cur = e->start;
2568   fprintf (stderr, "Running episode %u with timeout %s\n",
2569       e->cur->id,
2570       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
2571   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
2572       &timeout_episode, e);
2573   enforce_episode(e->cur);
2574
2575 }
2576
2577 void
2578 GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
2579 {
2580   if (NULL != e->experiment_timeout_task)
2581   {
2582     GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
2583     e->experiment_timeout_task = NULL;
2584   }
2585   if (NULL != e->episode_timeout_task)
2586   {
2587     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
2588     e->episode_timeout_task = NULL;
2589   }
2590   if (NULL != e->cfg)
2591   {
2592     GNUNET_CONFIGURATION_destroy(e->cfg);
2593     e->cfg = NULL;
2594   }
2595   free_experiment (e);
2596 }
2597
2598
2599 struct Experiment *
2600 GNUNET_ATS_solvers_experimentation_load (char *filename)
2601 {
2602   struct Experiment *e;
2603   struct GNUNET_CONFIGURATION_Handle *cfg;
2604   e = NULL;
2605
2606   cfg = GNUNET_CONFIGURATION_create();
2607   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
2608   {
2609     fprintf (stderr, "Failed to load `%s'\n", filename);
2610     GNUNET_CONFIGURATION_destroy (cfg);
2611     return NULL;
2612   }
2613
2614   e = create_experiment ();
2615
2616   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
2617       "name", &e->name))
2618   {
2619     fprintf (stderr, "Invalid %s \n", "name");
2620     free_experiment (e);
2621     return NULL;
2622   }
2623   else
2624     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment name: `%s'\n", e->name);
2625
2626   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
2627       "log_prefix", &e->log_prefix))
2628   {
2629     fprintf (stderr, "Invalid %s \n", "log_prefix");
2630     free_experiment (e);
2631     return NULL;
2632   }
2633   else
2634     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging prefix: `%s'\n",
2635         e->log_prefix);
2636
2637   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
2638       "log_output_dir", &e->log_output_dir))
2639   {
2640     e->log_output_dir = NULL;
2641   }
2642   else
2643     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging output directory: `%s'\n",
2644         e->log_output_dir);
2645
2646
2647   if (GNUNET_SYSERR == (e->log_append_time_stamp = GNUNET_CONFIGURATION_get_value_yesno(cfg,
2648       "experiment", "log_append_time_stamp")))
2649     e->log_append_time_stamp = GNUNET_YES;
2650   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging append timestamp: `%s'\n",
2651       (GNUNET_YES == e->log_append_time_stamp) ? "yes" : "no");
2652
2653
2654   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
2655       "cfg_file", &e->cfg_file))
2656   {
2657     fprintf (stderr, "Invalid %s \n", "cfg_file");
2658     free_experiment (e);
2659     return NULL;
2660   }
2661   else
2662   {
2663     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment configuration: `%s'\n", e->cfg_file);
2664     e->cfg = GNUNET_CONFIGURATION_create();
2665     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (e->cfg, e->cfg_file))
2666     {
2667       fprintf (stderr, "Invalid configuration %s \n", "cfg_file");
2668       free_experiment (e);
2669       return NULL;
2670     }
2671
2672   }
2673
2674   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
2675       "log_freq", &e->log_freq))
2676   {
2677     fprintf (stderr, "Invalid %s \n", "log_freq");
2678     free_experiment (e);
2679     return NULL;
2680   }
2681   else
2682     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging frequency: `%s'\n",
2683         GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
2684
2685   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
2686       "max_duration", &e->max_duration))
2687   {
2688     fprintf (stderr, "Invalid %s", "max_duration");
2689     free_experiment (e);
2690     return NULL;
2691   }
2692   else
2693     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment duration: `%s'\n",
2694         GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
2695
2696   if (GNUNET_SYSERR == load_episodes (e, cfg))
2697   {
2698     GNUNET_ATS_solvers_experimentation_stop (e);
2699     GNUNET_CONFIGURATION_destroy (cfg);
2700     e = NULL;
2701     fprintf (stderr, "Failed to load experiment\n");
2702     return NULL;
2703   }
2704   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loaded %u episodes with total duration %s\n",
2705       e->num_episodes,
2706       GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
2707
2708   GNUNET_CONFIGURATION_destroy (cfg);
2709   return e;
2710 }
2711
2712
2713
2714 /**
2715  * Solver
2716  */
2717
2718 static int
2719 free_all_it (void *cls,
2720     const struct GNUNET_PeerIdentity *key,
2721     void *value)
2722 {
2723   struct ATS_Address *address = value;
2724   GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (sh->env.addresses,
2725       key, value));
2726   GNUNET_free (address);
2727
2728   return GNUNET_OK;
2729 }
2730
2731 void
2732 GNUNET_ATS_solvers_solver_stop (struct SolverHandle *sh)
2733 {
2734  GNUNET_STATISTICS_destroy ((struct GNUNET_STATISTICS_Handle *) sh->env.stats,
2735      GNUNET_NO);
2736  GNUNET_PLUGIN_unload (sh->plugin, sh->sf);
2737  sh->sf = NULL;
2738  GAS_normalization_stop();
2739
2740  GNUNET_CONTAINER_multipeermap_iterate (sh->addresses,
2741                                         &free_all_it,
2742                                         NULL);
2743  GNUNET_CONTAINER_multipeermap_destroy(sh->addresses);
2744  GNUNET_free (sh->plugin);
2745  GNUNET_free (sh);
2746 }
2747
2748
2749 /**
2750  * Load quotas for networks from configuration
2751  *
2752  * @param cfg configuration handle
2753  * @param out_dest where to write outbound quotas
2754  * @param in_dest where to write inbound quotas
2755  * @param dest_length length of inbound and outbound arrays
2756  * @return number of networks loaded
2757  */
2758 unsigned int
2759 GNUNET_ATS_solvers_load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
2760                                                  unsigned long long *out_dest,
2761                                                  unsigned long long *in_dest,
2762                                                  int dest_length)
2763 {
2764   char * entry_in = NULL;
2765   char * entry_out = NULL;
2766   char * quota_out_str;
2767   char * quota_in_str;
2768   int c;
2769   int res;
2770
2771   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
2772   {
2773     in_dest[c] = 0;
2774     out_dest[c] = 0;
2775     GNUNET_asprintf (&entry_out,
2776                      "%s_QUOTA_OUT",
2777                      GNUNET_ATS_print_network_type (c));
2778     GNUNET_asprintf (&entry_in,
2779                      "%s_QUOTA_IN",
2780                      GNUNET_ATS_print_network_type (c));
2781
2782     /* quota out */
2783     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
2784     {
2785       res = GNUNET_NO;
2786       if (0 == strcmp(quota_out_str, BIG_M_STRING))
2787       {
2788         out_dest[c] = GNUNET_ATS_MaxBandwidth;
2789         res = GNUNET_YES;
2790       }
2791       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
2792         res = GNUNET_YES;
2793       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,  &out_dest[c])))
2794          res = GNUNET_YES;
2795
2796       if (GNUNET_NO == res)
2797       {
2798           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2799                       _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
2800                       GNUNET_ATS_print_network_type (c),
2801                       quota_out_str,
2802                       GNUNET_ATS_DefaultBandwidth);
2803           out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2804       }
2805       else
2806       {
2807           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2808                       "Outbound quota configure for network `%s' is %llu\n",
2809                       GNUNET_ATS_print_network_type (c),
2810                       out_dest[c]);
2811       }
2812       GNUNET_free (quota_out_str);
2813     }
2814     else
2815     {
2816       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2817                   _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
2818                   GNUNET_ATS_print_network_type (c),
2819                   GNUNET_ATS_DefaultBandwidth);
2820       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2821     }
2822
2823     /* quota in */
2824     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
2825     {
2826       res = GNUNET_NO;
2827       if (0 == strcmp(quota_in_str, BIG_M_STRING))
2828       {
2829         in_dest[c] = GNUNET_ATS_MaxBandwidth;
2830         res = GNUNET_YES;
2831       }
2832       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
2833         res = GNUNET_YES;
2834       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,  &in_dest[c])))
2835          res = GNUNET_YES;
2836
2837       if (GNUNET_NO == res)
2838       {
2839           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2840                       _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
2841                       GNUNET_ATS_print_network_type (c),
2842                       quota_in_str,
2843                       GNUNET_ATS_DefaultBandwidth);
2844           in_dest[c] = GNUNET_ATS_DefaultBandwidth;
2845       }
2846       else
2847       {
2848           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2849                       "Inbound quota configured for network `%s' is %llu\n",
2850                       GNUNET_ATS_print_network_type (c),
2851                       in_dest[c]);
2852       }
2853       GNUNET_free (quota_in_str);
2854     }
2855     else
2856     {
2857       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2858                   _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
2859                   GNUNET_ATS_print_network_type (c),
2860                   GNUNET_ATS_DefaultBandwidth);
2861       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2862     }
2863     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2864                 "Loaded quota for network `%s' (in/out): %llu %llu\n",
2865                 GNUNET_ATS_print_network_type (c),
2866                 in_dest[c],
2867                 out_dest[c]);
2868     GNUNET_free (entry_out);
2869     GNUNET_free (entry_in);
2870   }
2871   return GNUNET_ATS_NetworkTypeCount;
2872 }
2873
2874
2875 /**
2876  * Information callback for the solver
2877  *
2878  * @param cls the closure
2879  * @param op the solver operation
2880  * @param stat status of the solver operation
2881  * @param add additional solver information
2882  */
2883 static void
2884 solver_info_cb (void *cls,
2885                 enum GAS_Solver_Operation op,
2886                 enum GAS_Solver_Status stat,
2887                 enum GAS_Solver_Additional_Information add)
2888 {
2889   char *add_info;
2890   switch (add) {
2891     case GAS_INFO_NONE:
2892       add_info = "GAS_INFO_NONE";
2893       break;
2894     case GAS_INFO_FULL:
2895       add_info = "GAS_INFO_MLP_FULL";
2896       break;
2897     case GAS_INFO_UPDATED:
2898       add_info = "GAS_INFO_MLP_UPDATED";
2899       break;
2900     case GAS_INFO_PROP_ALL:
2901       add_info = "GAS_INFO_PROP_ALL";
2902       break;
2903     case GAS_INFO_PROP_SINGLE:
2904       add_info = "GAS_INFO_PROP_SINGLE";
2905       break;
2906     default:
2907       add_info = "INVALID";
2908       break;
2909   }
2910
2911   switch (op)
2912   {
2913     case GAS_OP_SOLVE_START:
2914       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2915           "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
2916           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
2917       return;
2918     case GAS_OP_SOLVE_STOP:
2919       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2920           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
2921           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
2922       return;
2923
2924     case GAS_OP_SOLVE_SETUP_START:
2925       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2926           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
2927           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2928       return;
2929
2930     case GAS_OP_SOLVE_SETUP_STOP:
2931       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2932           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
2933           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2934       return;
2935
2936     case GAS_OP_SOLVE_MLP_LP_START:
2937       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2938           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
2939           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2940       return;
2941     case GAS_OP_SOLVE_MLP_LP_STOP:
2942       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2943           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
2944           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2945       return;
2946
2947     case GAS_OP_SOLVE_MLP_MLP_START:
2948       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2949           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
2950           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2951       return;
2952     case GAS_OP_SOLVE_MLP_MLP_STOP:
2953       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2954           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
2955           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2956       return;
2957     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
2958       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2959           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
2960           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2961       return;
2962     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
2963       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2964           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
2965           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2966       return;
2967     default:
2968       break;
2969     }
2970 }
2971
2972 static void
2973 solver_bandwidth_changed_cb (void *cls, struct ATS_Address *address)
2974 {
2975   struct GNUNET_TIME_Relative duration;
2976   struct TestPeer *p;
2977   static struct PreferenceGenerator *pg;
2978   uint32_t delta;
2979   if ( (0 == address->assigned_bw_out) && (0 == address->assigned_bw_in) )
2980   {
2981     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2982                 "Solver notified to disconnect peer `%s'\n",
2983                 GNUNET_i2s (&address->peer));
2984   }
2985   p = find_peer_by_pid(&address->peer);
2986   if (NULL == p)
2987     return;
2988   p->assigned_bw_out = address->assigned_bw_out;
2989   p->assigned_bw_in = address->assigned_bw_in;
2990
2991   for (pg = pref_gen_head; NULL != pg; pg = pg->next)
2992   {
2993     if (pg->peer == p->id)
2994     {
2995       duration = GNUNET_TIME_absolute_get_duration(pg->feedback_last_bw_update);
2996       delta = duration.rel_value_us * pg->last_assigned_bw_out;
2997       pg->feedback_bw_out_acc += delta;
2998
2999       delta = duration.rel_value_us * pg->last_assigned_bw_in;
3000       pg->feedback_bw_in_acc += delta;
3001
3002       pg->last_assigned_bw_in = address->assigned_bw_in;
3003       pg->last_assigned_bw_out = address->assigned_bw_out;
3004       pg->feedback_last_bw_update = GNUNET_TIME_absolute_get();
3005     }
3006   }
3007
3008   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3009       "Bandwidth changed addresses %s %p to %u Bps out / %u Bps in\n",
3010       GNUNET_i2s (&address->peer),
3011       address,
3012       address->assigned_bw_out,
3013           address->assigned_bw_in);
3014
3015   if (NULL != l)
3016     GNUNET_ATS_solver_logging_now (l);
3017
3018   return;
3019 }
3020
3021 const double *
3022 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
3023 {
3024   struct TestPeer *p;
3025   if (GNUNET_YES == opt_disable_normalization)
3026   {
3027     if (NULL == (p = find_peer_by_pid (id)))
3028       return NULL;
3029     return p->pref_abs;
3030   }
3031   else
3032     return GAS_normalization_get_preferences_by_peer (NULL,
3033                                                       id);
3034 }
3035
3036
3037 const double *
3038 get_property_cb (void *cls, const struct ATS_Address *address)
3039 {
3040   struct TestPeer *p;
3041   struct TestAddress *a;
3042
3043   if (GNUNET_YES == opt_disable_normalization)
3044   {
3045     p = find_peer_by_pid (&address->peer);
3046     if (NULL == p)
3047       return NULL;
3048     a = find_address_by_ats_address (p, address);
3049     return a->prop_abs;
3050   }
3051   return GAS_normalization_get_properties (NULL,
3052                                            address);
3053 }
3054
3055
3056 struct SolverHandle *
3057 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
3058 {
3059   struct SolverHandle *sh;
3060   char * solver_str;
3061
3062   switch (type) {
3063     case GNUNET_ATS_SOLVER_PROPORTIONAL:
3064       solver_str = "proportional";
3065       break;
3066     case GNUNET_ATS_SOLVER_MLP:
3067       solver_str = "mlp";
3068       break;
3069     case GNUNET_ATS_SOLVER_RIL:
3070       solver_str = "ril";
3071       break;
3072     default:
3073       GNUNET_break (0);
3074       return NULL;
3075       break;
3076   }
3077
3078   sh = GNUNET_new (struct SolverHandle);
3079   GNUNET_asprintf (&sh->plugin,
3080                    "libgnunet_plugin_ats_%s",
3081                    solver_str);
3082   sh->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
3083
3084   /* setup environment */
3085   sh->env.cfg = e->cfg;
3086   sh->env.stats = GNUNET_STATISTICS_create ("ats", e->cfg);
3087   sh->env.addresses = sh->addresses;
3088   sh->env.bandwidth_changed_cb = &solver_bandwidth_changed_cb;
3089   sh->env.get_preferences = &get_preferences_cb;
3090   sh->env.get_property = &get_property_cb;
3091   sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
3092   sh->env.info_cb = &solver_info_cb;
3093   sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
3094
3095   /* start normalization */
3096   GAS_normalization_start ();
3097
3098   /* load quotas */
3099   if (GNUNET_ATS_NetworkTypeCount != GNUNET_ATS_solvers_load_quotas (e->cfg,
3100       sh->env.out_quota, sh->env.in_quota, GNUNET_ATS_NetworkTypeCount))
3101   {
3102     GNUNET_break(0);
3103     GNUNET_free (sh->plugin);
3104     GNUNET_free (sh);
3105     end_now ();
3106     return NULL;
3107   }
3108
3109   sh->sf = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
3110   if (NULL == sh->sf)
3111   {
3112     fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
3113     GNUNET_break(0);
3114     GNUNET_free (sh->plugin);
3115     GNUNET_free (sh);
3116     end_now ();
3117     return NULL;
3118   }
3119   return sh;
3120 }
3121
3122
3123 static void
3124 done ()
3125 {
3126   struct TestPeer *cur;
3127   struct TestPeer *next;
3128
3129   struct TestAddress *cur_a;
3130   struct TestAddress *next_a;
3131
3132   /* Stop logging */
3133   GNUNET_ATS_solver_logging_stop (l);
3134
3135   /* Stop all preference generation */
3136   GNUNET_ATS_solver_generate_preferences_stop_all ();
3137
3138   /* Stop all property generation */
3139   GNUNET_ATS_solver_generate_property_stop_all ();
3140
3141   if (opt_print)
3142   {
3143     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Printing log information \n");
3144     GNUNET_ATS_solver_logging_eval (l);
3145   }
3146   if (opt_save)
3147   {
3148     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Saving log information \n");
3149     GNUNET_ATS_solver_logging_write_to_disk (l, e->log_append_time_stamp,
3150         e->log_output_dir);
3151   }
3152
3153   if (NULL != l)
3154   {
3155     GNUNET_ATS_solver_logging_free (l);
3156     l = NULL;
3157   }
3158
3159   /* Clean up experiment */
3160   if (NULL != e)
3161   {
3162     GNUNET_ATS_solvers_experimentation_stop (e);
3163     e = NULL;
3164   }
3165
3166   next = peer_head;
3167   while  (NULL != (cur = next))
3168   {
3169     next = cur->next;
3170     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, cur);
3171     next_a = cur->addr_head;
3172     while  (NULL != (cur_a = next_a))
3173     {
3174       next_a = cur_a->next;
3175       GNUNET_CONTAINER_DLL_remove (cur->addr_head, cur->addr_tail, cur_a);
3176       GNUNET_free (cur_a);
3177     }
3178     GNUNET_free (cur);
3179   }
3180   if (NULL != sh)
3181   {
3182     GNUNET_ATS_solvers_solver_stop (sh);
3183     sh = NULL;
3184   }
3185
3186   /* Shutdown */
3187   end_now();
3188 }
3189
3190 static void
3191 experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,int success)
3192 {
3193   if (GNUNET_OK == success)
3194     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment done successful in %s\n",
3195         GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
3196   else
3197     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
3198
3199   GNUNET_SCHEDULER_add_now (&done, NULL);
3200 }
3201
3202 static void
3203 episode_done_cb (struct Episode *ep)
3204 {
3205   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
3206 }
3207
3208
3209
3210 /**
3211  * Do shutdown
3212  */
3213 static void
3214 end_now ()
3215 {
3216   if (NULL != e)
3217   {
3218     GNUNET_ATS_solvers_experimentation_stop (e);
3219     e = NULL;
3220   }
3221   if (NULL != sh)
3222   {
3223     GNUNET_ATS_solvers_solver_stop (sh);
3224     sh = NULL;
3225   }
3226 }
3227
3228 static void
3229 run (void *cls, char * const *args, const char *cfgfile,
3230     const struct GNUNET_CONFIGURATION_Handle *cfg)
3231 {
3232   enum GNUNET_ATS_Solvers solver;
3233   int c;
3234
3235   if (NULL == opt_exp_file)
3236   {
3237     fprintf (stderr, "No experiment given ...\n");
3238     res = 1;
3239     end_now ();
3240     return;
3241   }
3242
3243   if (NULL == opt_solver)
3244   {
3245     fprintf (stderr, "No solver given ...\n");
3246     res = 1;
3247     end_now ();
3248     return;
3249   }
3250
3251   if (0 == strcmp(opt_solver, "mlp"))
3252   {
3253     solver = GNUNET_ATS_SOLVER_MLP;
3254   }
3255   else if (0 == strcmp(opt_solver, "proportional"))
3256   {
3257     solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
3258   }
3259   else if (0 == strcmp(opt_solver, "ril"))
3260   {
3261     solver = GNUNET_ATS_SOLVER_RIL;
3262   }
3263   else
3264   {
3265     fprintf (stderr, "No solver given ...");
3266     res = 1;
3267     end_now ();
3268     return;
3269   }
3270
3271   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
3272     default_properties[c] = DEFAULT_REL_QUALITY;
3273
3274   for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
3275     default_preferences[c] = DEFAULT_REL_PREFERENCE;
3276
3277   /* load experiment */
3278   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading experiment\n");
3279   e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
3280   if (NULL == e)
3281   {
3282     fprintf (stderr, "Failed to load experiment ...\n");
3283     res = 1;
3284     end_now ();
3285     return;
3286   }
3287
3288   /* load solver */
3289   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading solver\n");
3290   sh = GNUNET_ATS_solvers_solver_start (solver);
3291   if (NULL == sh)
3292   {
3293     fprintf (stderr, "Failed to start solver ...\n");
3294     end_now ();
3295     res = 1;
3296     return;
3297   }
3298
3299   /* start logging */
3300   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Start logging \n");
3301   l = GNUNET_ATS_solver_logging_start (e->log_freq);
3302
3303   /* run experiment */
3304   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Running experiment \n");
3305   GNUNET_ATS_solvers_experimentation_run (e, episode_done_cb,
3306       experiment_done_cb);
3307
3308   /* WAIT */
3309 }
3310
3311
3312 /**
3313  * Main function of the benchmark
3314  *
3315  * @param argc argument count
3316  * @param argv argument values
3317  */
3318 int
3319 main (int argc, char *argv[])
3320 {
3321   opt_exp_file = NULL;
3322   opt_solver = NULL;
3323   opt_log = GNUNET_NO;
3324   opt_save = GNUNET_NO;
3325
3326   res = 0;
3327
3328   static struct GNUNET_GETOPT_CommandLineOption options[] =
3329   {
3330     { 's', "solver", NULL,
3331         gettext_noop ("solver to use"),
3332         1, &GNUNET_GETOPT_set_string, &opt_solver},
3333     {  'e', "experiment", NULL,
3334       gettext_noop ("experiment to use"),
3335       1, &GNUNET_GETOPT_set_string, &opt_exp_file},
3336     {  'V', "verbose", NULL,
3337       gettext_noop ("be verbose"),
3338       0, &GNUNET_GETOPT_set_one, &opt_verbose},
3339     {  'p', "print", NULL,
3340       gettext_noop ("print logging"),
3341       0, &GNUNET_GETOPT_set_one, &opt_print},
3342     {  'f', "file", NULL,
3343         gettext_noop ("save logging to disk"),
3344         0, &GNUNET_GETOPT_set_one, &opt_save},
3345     {  'd', "dn", NULL,
3346         gettext_noop ("disable normalization"),
3347         0, &GNUNET_GETOPT_set_one, &opt_disable_normalization},
3348     GNUNET_GETOPT_OPTION_END
3349   };
3350
3351   GNUNET_PROGRAM_run (argc, argv, "gnunet-ats-solver-eval",
3352       NULL, options, &run, argv[0]);
3353
3354   return res;
3355 }
3356 /* end of file ats-testing-experiment.c*/