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