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