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