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