use ATS functions to convert quality network type to string, instead of using string API
[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 = NULL;
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 (NULL != 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 = NULL;
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 (NULL != l->logging_task)
517     GNUNET_SCHEDULER_cancel (l->logging_task);
518   l->logging_task = NULL;
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 = NULL;
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 (NULL != pg->set_task)
706   {
707     GNUNET_SCHEDULER_cancel (pg->set_task);
708     pg->set_task = NULL;
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 = NULL;
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 = NULL;
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 (NULL != pg->feedback_task)
1034   {
1035     GNUNET_SCHEDULER_cancel (pg->feedback_task);
1036     pg->feedback_task = NULL;
1037   }
1038
1039   if (NULL != pg->set_task)
1040   {
1041     GNUNET_SCHEDULER_cancel (pg->set_task);
1042     pg->set_task = NULL;
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
1710 static enum GNUNET_ATS_Property
1711 parse_property_string (const char *str)
1712 {
1713   enum GNUNET_ATS_Property c;
1714
1715   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
1716     if (0 == strcmp(str,
1717                     GNUNET_ATS_print_property_type (c)))
1718       return c;
1719   return 0;
1720 }
1721
1722
1723 static int
1724 load_op_start_set_property(struct GNUNET_ATS_TEST_Operation *o,
1725     struct Episode *e,
1726     int op_counter,
1727     char *sec_name,
1728     const struct GNUNET_CONFIGURATION_Handle *cfg)
1729 {
1730   char *op_name;
1731   char *type;
1732   char *prop;
1733
1734   /* peer pid */
1735   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1736   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1737       sec_name, op_name, &o->peer_id))
1738   {
1739     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1740         op_counter, "START_SET_PROPERTY", op_name);
1741     GNUNET_free (op_name);
1742     return GNUNET_SYSERR;
1743   }
1744   GNUNET_free (op_name);
1745
1746   /* address pid */
1747   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1748   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1749       sec_name, op_name, &o->address_id))
1750   {
1751     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1752         op_counter, "START_SET_PROPERTY", op_name);
1753     GNUNET_free (op_name);
1754     return GNUNET_SYSERR;
1755   }
1756   GNUNET_free (op_name);
1757
1758   /* generator */
1759   GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
1760   if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
1761           sec_name, op_name, &type)) )
1762   {
1763     fprintf (stderr, "Missing type in operation %u `%s' in episode `%s'\n",
1764         op_counter, "START_SET_PROPERTY", op_name);
1765     GNUNET_free (op_name);
1766     return GNUNET_SYSERR;
1767   }
1768
1769   /* Load arguments for set_rate, start_send, set_preference */
1770   if (0 == strcmp (type, "constant"))
1771   {
1772     o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
1773   }
1774   else if (0 == strcmp (type, "linear"))
1775   {
1776     o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
1777   }
1778   else if (0 == strcmp (type, "sinus"))
1779   {
1780     o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
1781   }
1782   else if (0 == strcmp (type, "random"))
1783   {
1784     o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
1785   }
1786   else
1787   {
1788     fprintf (stderr, "Invalid generator type %u `%s' in episode %u\n",
1789         op_counter, op_name, e->id);
1790     GNUNET_free (type);
1791     GNUNET_free (op_name);
1792     return GNUNET_SYSERR;
1793   }
1794   GNUNET_free (type);
1795   GNUNET_free (op_name);
1796
1797
1798   /* Get base rate */
1799   GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
1800   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1801       sec_name, op_name, &o->base_rate))
1802   {
1803     fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
1804         op_counter, op_name, e->id);
1805     GNUNET_free (op_name);
1806     return GNUNET_SYSERR;
1807   }
1808   GNUNET_free (op_name);
1809
1810
1811   /* Get max rate */
1812   GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
1813   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1814       sec_name, op_name, &o->max_rate))
1815   {
1816     if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
1817         (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
1818         (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
1819     {
1820       fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
1821           op_counter, op_name, e->id);
1822       GNUNET_free (op_name);
1823       return GNUNET_SYSERR;
1824     }
1825   }
1826   GNUNET_free (op_name);
1827
1828   /* Get period */
1829   GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
1830   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1831       sec_name, op_name, &o->period))
1832   {
1833     o->period = e->duration;
1834   }
1835   GNUNET_free (op_name);
1836
1837   /* Get frequency */
1838   GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
1839   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
1840       sec_name, op_name, &o->frequency))
1841   {
1842       fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
1843           op_counter, op_name, e->id);
1844       GNUNET_free (op_name);
1845       return GNUNET_SYSERR;
1846   }
1847   GNUNET_free (op_name);
1848
1849   /* Get preference */
1850   GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
1851   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1852       sec_name, op_name, &prop))
1853   {
1854       fprintf (stderr, "Missing property in operation %u `%s' in episode %u\n",
1855           op_counter, op_name, e->id);
1856       GNUNET_free (op_name);
1857       GNUNET_free_non_null (prop);
1858       return GNUNET_SYSERR;
1859   }
1860
1861   if (0 == (o->prop_type = parse_property_string(prop)))
1862   {
1863       fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
1864           op_counter, op_name, e->id);
1865       GNUNET_free (op_name);
1866       GNUNET_free (prop);
1867       return GNUNET_SYSERR;
1868   }
1869
1870   GNUNET_free (prop);
1871   GNUNET_free (op_name);
1872
1873   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1874       "Found operation %s: [%llu:%llu] %s = %llu\n",
1875       "START_SET_PROPERTY", o->peer_id, o->address_id,
1876       GNUNET_ATS_print_property_type (o->prop_type), o->base_rate);
1877
1878   return GNUNET_OK;
1879 }
1880
1881 static int
1882 load_op_stop_set_property (struct GNUNET_ATS_TEST_Operation *o,
1883     struct Episode *e,
1884     int op_counter,
1885     char *sec_name,
1886     const struct GNUNET_CONFIGURATION_Handle *cfg)
1887 {
1888   char *op_name;
1889   char *pref;
1890
1891   /* peer pid */
1892   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1893   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1894       sec_name, op_name, &o->peer_id))
1895   {
1896     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1897         op_counter, "STOP_SET_PROPERTY", op_name);
1898     GNUNET_free (op_name);
1899     return GNUNET_SYSERR;
1900   }
1901   GNUNET_free (op_name);
1902
1903   /* address pid */
1904   GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
1905   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1906       sec_name, op_name, &o->address_id))
1907   {
1908     fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
1909         op_counter, "STOP_SET_PROPERTY", op_name);
1910     GNUNET_free (op_name);
1911     return GNUNET_SYSERR;
1912   }
1913   GNUNET_free (op_name);
1914
1915   /* Get property */
1916   GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
1917   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
1918       sec_name, op_name, &pref))
1919   {
1920     fprintf (stderr, "Missing property in operation %u `%s' in episode `%s'\n",
1921         op_counter, "STOP_SET_PROPERTY", op_name);
1922       GNUNET_free (op_name);
1923       GNUNET_free_non_null (pref);
1924       return GNUNET_SYSERR;
1925   }
1926
1927   if (0 == (o->prop_type = parse_property_string(pref)))
1928   {
1929       fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
1930           op_counter, op_name, e->id);
1931       GNUNET_free (op_name);
1932       GNUNET_free_non_null (pref);
1933       return GNUNET_SYSERR;
1934   }
1935
1936   GNUNET_free (pref);
1937   GNUNET_free (op_name);
1938
1939   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1940       "Found operation %s: [%llu:%llu] %s\n",
1941       "STOP_SET_PROPERTY", o->peer_id, o->address_id,
1942       GNUNET_ATS_print_property_type (o->prop_type));
1943
1944   return GNUNET_OK;
1945 }
1946
1947
1948 static int
1949 load_op_start_request (struct GNUNET_ATS_TEST_Operation *o,
1950     struct Episode *e,
1951     int op_counter,
1952     char *sec_name,
1953     const struct GNUNET_CONFIGURATION_Handle *cfg)
1954 {
1955   char *op_name;
1956
1957   /* peer pid */
1958   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1959   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1960       sec_name, op_name, &o->peer_id))
1961   {
1962     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1963         op_counter, "START_REQUEST", op_name);
1964     GNUNET_free (op_name);
1965     return GNUNET_SYSERR;
1966   }
1967   GNUNET_free (op_name);
1968   return GNUNET_OK;
1969 }
1970
1971 static int
1972 load_op_stop_request (struct GNUNET_ATS_TEST_Operation *o,
1973     struct Episode *e,
1974     int op_counter,
1975     char *sec_name,
1976     const struct GNUNET_CONFIGURATION_Handle *cfg)
1977 {
1978   char *op_name;
1979
1980   /* peer pid */
1981   GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
1982   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
1983       sec_name, op_name, &o->peer_id))
1984   {
1985     fprintf (stderr, "Missing peer-id in operation %u  `%s' in episode `%s'\n",
1986         op_counter, "STOP_REQUEST", op_name);
1987     GNUNET_free (op_name);
1988     return GNUNET_SYSERR;
1989   }
1990   GNUNET_free (op_name);
1991   return GNUNET_OK;
1992 }
1993
1994
1995 static int
1996 load_episode (struct Experiment *e, struct Episode *cur,
1997     struct GNUNET_CONFIGURATION_Handle *cfg)
1998 {
1999   struct GNUNET_ATS_TEST_Operation *o;
2000   char *sec_name;
2001   char *op_name;
2002   char *op;
2003   int op_counter = 0;
2004   int res;
2005   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Parsing episode %u\n",cur->id);
2006   GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
2007
2008   while (1)
2009   {
2010     /* Load operation */
2011     GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
2012     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
2013         sec_name, op_name, &op))
2014     {
2015       GNUNET_free (op_name);
2016       break;
2017     }
2018     o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
2019     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "==== Parsing operation %u: `%s'\n",
2020         cur->id, op_name);
2021
2022     /* operations = set_rate, start_send, stop_send, set_preference */
2023     if (0 == strcmp (op, "address_add"))
2024     {
2025       o->type = SOLVER_OP_ADD_ADDRESS;
2026       res = load_op_add_address (o, cur,
2027           op_counter, sec_name, cfg);
2028     }
2029     else if (0 == strcmp (op, "address_del"))
2030     {
2031       o->type = SOLVER_OP_DEL_ADDRESS;
2032       res = load_op_del_address (o, cur,
2033           op_counter, sec_name, cfg);
2034     }
2035     else if (0 == strcmp (op, "start_set_property"))
2036     {
2037       o->type = SOLVER_OP_START_SET_PROPERTY;
2038       res = load_op_start_set_property (o, cur,
2039           op_counter, sec_name, cfg);
2040     }
2041     else if (0 == strcmp (op, "stop_set_property"))
2042     {
2043       o->type = SOLVER_OP_STOP_SET_PROPERTY;
2044       res = load_op_stop_set_property (o, cur,
2045           op_counter, sec_name, cfg);
2046     }
2047     else if (0 == strcmp (op, "start_set_preference"))
2048     {
2049       o->type = SOLVER_OP_START_SET_PREFERENCE;
2050       res =  load_op_start_set_preference (o, cur,
2051           op_counter, sec_name, cfg);
2052     }
2053     else if (0 == strcmp (op, "stop_set_preference"))
2054     {
2055       o->type = SOLVER_OP_STOP_SET_PREFERENCE;
2056       res =  load_op_stop_set_preference (o, cur,
2057           op_counter, sec_name, cfg);
2058     }
2059     else if (0 == strcmp (op, "start_request"))
2060     {
2061       o->type = SOLVER_OP_START_REQUEST;
2062       res = load_op_start_request (o, cur,
2063           op_counter, sec_name, cfg);
2064     }
2065     else if (0 == strcmp (op, "stop_request"))
2066     {
2067       o->type = SOLVER_OP_STOP_REQUEST;
2068       res = load_op_stop_request(o, cur,
2069           op_counter, sec_name, cfg);
2070     }
2071     else
2072     {
2073       fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
2074           op_counter, op, cur->id);
2075       res = GNUNET_SYSERR;
2076     }
2077
2078     GNUNET_free (op);
2079     GNUNET_free (op_name);
2080
2081     if (GNUNET_SYSERR == res)
2082     {
2083       GNUNET_free (o);
2084       GNUNET_free (sec_name);
2085       return GNUNET_SYSERR;
2086     }
2087
2088     GNUNET_CONTAINER_DLL_insert_tail (cur->head,cur->tail, o);
2089     op_counter++;
2090   }
2091   GNUNET_free (sec_name);
2092   return GNUNET_OK;
2093 }
2094
2095 static int
2096 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
2097 {
2098   int e_counter = 0;
2099   char *sec_name;
2100   struct GNUNET_TIME_Relative e_duration;
2101   struct Episode *cur;
2102   struct Episode *last;
2103
2104   e_counter = 0;
2105   last = NULL;
2106   while (1)
2107   {
2108     GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
2109     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
2110         sec_name, "duration", &e_duration))
2111     {
2112       GNUNET_free (sec_name);
2113       break;
2114     }
2115
2116     cur = GNUNET_new (struct Episode);
2117     cur->duration = e_duration;
2118     cur->id = e_counter;
2119
2120     if (GNUNET_OK != load_episode (e, cur, cfg))
2121     {
2122       GNUNET_free (sec_name);
2123       GNUNET_free (cur);
2124       return GNUNET_SYSERR;
2125     }
2126
2127     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Found episode %u with duration %s \n",
2128         e_counter,
2129         GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
2130
2131     /* Update experiment */
2132     e->num_episodes ++;
2133     e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
2134     /* Put in linked list */
2135     if (NULL == last)
2136       e->start = cur;
2137     else
2138       last->next = cur;
2139
2140     GNUNET_free (sec_name);
2141     e_counter ++;
2142     last = cur;
2143   }
2144   return e_counter;
2145 }
2146
2147 static void
2148 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
2149 {
2150   struct Experiment *e = cls;
2151   e->experiment_timeout_task = NULL;
2152   fprintf (stderr, "Experiment timeout!\n");
2153
2154   if (NULL != e->episode_timeout_task)
2155   {
2156     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
2157     e->episode_timeout_task = NULL;
2158   }
2159
2160   e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
2161       GNUNET_SYSERR);
2162 }
2163
2164 struct ATS_Address *
2165 create_ats_address (const struct GNUNET_PeerIdentity *peer,
2166                 const char *plugin_name,
2167                 const void *plugin_addr,
2168                 size_t plugin_addr_len,
2169                 uint32_t session_id,
2170                 uint32_t network)
2171 {
2172   struct ATS_Address *aa = NULL;
2173
2174   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len + strlen (plugin_name) + 1);
2175   aa->atsi = GNUNET_new (struct GNUNET_ATS_Information);
2176   aa->atsi[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
2177   aa->atsi[0].value = htonl (network);
2178   aa->atsi_count = 1;
2179
2180   aa->peer = *peer;
2181   aa->addr_len = plugin_addr_len;
2182   aa->addr = &aa[1];
2183   aa->plugin = (char *) &aa[1] + plugin_addr_len;
2184   memcpy (&aa[1], plugin_addr, plugin_addr_len);
2185   memcpy (aa->plugin, plugin_name, strlen (plugin_name) + 1);
2186   aa->session_id = session_id;
2187   aa->active = GNUNET_NO;
2188   aa->used = GNUNET_NO;
2189   aa->solver_information = NULL;
2190   aa->assigned_bw_in = 0;
2191   aa->assigned_bw_out = 0;
2192
2193   return aa;
2194 }
2195
2196
2197
2198 static void
2199 enforce_add_address (struct GNUNET_ATS_TEST_Operation *op)
2200 {
2201   struct TestPeer *p;
2202   struct TestAddress *a;
2203   int c;
2204
2205   if (NULL == (p = find_peer_by_id (op->peer_id)))
2206   {
2207     p = GNUNET_new (struct TestPeer);
2208     p->id = op->peer_id;
2209     p->assigned_bw_in = 0;
2210     p->assigned_bw_out = 0;
2211     memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
2212     for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
2213     {
2214       p->pref_abs[c] = DEFAULT_ABS_PREFERENCE;
2215       p->pref_norm[c] = DEFAULT_REL_PREFERENCE;
2216     }
2217
2218     GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, p);
2219   }
2220
2221   if (NULL != (find_address_by_id (p, op->address_id)))
2222   {
2223     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Duplicate address %u for peer %u\n",
2224         op->address_id, op->peer_id);
2225     return;
2226   }
2227
2228   a = GNUNET_new (struct TestAddress);
2229   a->aid = op->address_id;
2230   a->network = op->address_network;
2231   a->ats_addr = create_ats_address (&p->peer_id, op->plugin, op->address,
2232       strlen (op->address) + 1, op->address_session, op->address_network);
2233   memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
2234   GNUNET_CONTAINER_DLL_insert_tail (p->addr_head, p->addr_tail, a);
2235
2236   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
2237     a->prop_norm[c] = DEFAULT_REL_QUALITY;
2238
2239   GNUNET_CONTAINER_multipeermap_put (sh->addresses, &p->peer_id, a->ats_addr,
2240     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2241
2242   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Adding address %u for peer %u in network `%s'\n",
2243     op->address_id, op->peer_id, GNUNET_ATS_print_network_type(a->network));
2244
2245   sh->env.sf.s_add (sh->solver, a->ats_addr, op->address_network);
2246
2247 }
2248
2249
2250 static void
2251 enforce_del_address (struct GNUNET_ATS_TEST_Operation *op)
2252 {
2253   struct TestPeer *p;
2254   struct TestAddress *a;
2255   struct PropertyGenerator *pg;
2256
2257   if (NULL == (p = find_peer_by_id (op->peer_id)))
2258   {
2259     GNUNET_break (0);
2260     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2261         "Deleting address for unknown peer %u\n", op->peer_id);
2262     return;
2263   }
2264
2265   if (NULL == (a =find_address_by_id (p, op->address_id)))
2266   {
2267     GNUNET_break (0);
2268     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2269         "Deleting address for unknown peer %u\n", op->peer_id);
2270     return;
2271   }
2272
2273   while (NULL != (pg = find_prop_gen (p->id, a->aid, 0)))
2274   {
2275     GNUNET_ATS_solver_generate_property_stop (pg);
2276   }
2277
2278   GNUNET_assert (GNUNET_YES ==
2279                  GNUNET_CONTAINER_multipeermap_remove (sh->addresses,
2280                                                        &p->peer_id,
2281                                                        a->ats_addr));
2282   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2283               "Removing address %u for peer %u\n",
2284               op->address_id,
2285               op->peer_id);
2286
2287   sh->env.sf.s_del (sh->solver, a->ats_addr, GNUNET_NO);
2288
2289   if (NULL != l)
2290   {
2291     GNUNET_ATS_solver_logging_now (l);
2292   }
2293   GNUNET_CONTAINER_DLL_remove(p->addr_head, p->addr_tail, a);
2294
2295   GNUNET_free_non_null(a->ats_addr->atsi);
2296   GNUNET_free (a->ats_addr);
2297   GNUNET_free (a);
2298
2299 }
2300
2301 static void
2302 enforce_start_property (struct GNUNET_ATS_TEST_Operation *op)
2303 {
2304   struct PropertyGenerator *pg;
2305   struct TestPeer *p;
2306   struct TestAddress *a;
2307
2308   if (NULL != (pg = find_prop_gen (op->peer_id, op->address_id, op->prop_type)))
2309   {
2310     GNUNET_ATS_solver_generate_property_stop (pg);
2311     GNUNET_free (pg);
2312   }
2313
2314   if (NULL == (p = find_peer_by_id (op->peer_id)))
2315   {
2316     GNUNET_break (0);
2317     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2318         "Starting property generation for unknown peer %u\n", op->peer_id);
2319     return;
2320   }
2321
2322   if (NULL == (a = find_address_by_id (p, op->address_id)))
2323   {
2324     GNUNET_break (0);
2325     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2326         "Setting property for unknown address %u\n", op->peer_id);
2327     return;
2328   }
2329
2330   GNUNET_ATS_solver_generate_property_start (op->peer_id,
2331     op->address_id,
2332     p, a,
2333     op->gen_type,
2334     op->base_rate,
2335     op->max_rate,
2336     op->period,
2337     op->frequency,
2338     op->prop_type);
2339 }
2340
2341 static void
2342 enforce_stop_property (struct GNUNET_ATS_TEST_Operation *op)
2343 {
2344   struct PropertyGenerator *pg = find_prop_gen(op->peer_id, op->address_id,
2345       op->prop_type);
2346   if (NULL != pg)
2347   {
2348     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2349         "Stopping preference generation for peer %u address %u\n", op->peer_id,
2350         op->address_id);
2351     GNUNET_ATS_solver_generate_property_stop (pg);
2352   }
2353   else
2354   {
2355     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2356         "Cannot find preference generator for peer %u address %u\n",
2357         op->peer_id, op->address_id);
2358     GNUNET_break (0);
2359   }
2360 }
2361
2362 static void
2363 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
2364 {
2365   struct PreferenceGenerator *pg;
2366   if (NULL != (pg = find_pref_gen (op->peer_id, op->pref_type)))
2367   {
2368     GNUNET_ATS_solver_generate_preferences_stop (pg);
2369     GNUNET_free (pg);
2370   }
2371
2372   if (NULL == (find_peer_by_id (op->peer_id)))
2373   {
2374     GNUNET_break (0);
2375     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2376         "Starting preference generation for unknown peer %u\n", op->peer_id);
2377     return;
2378   }
2379
2380   GNUNET_ATS_solver_generate_preferences_start (op->peer_id,
2381     op->address_id,
2382     op->client_id,
2383     op->gen_type,
2384     op->base_rate,
2385     op->max_rate,
2386     op->period,
2387     op->frequency,
2388     op->pref_type,
2389     op->frequency);
2390 }
2391
2392 static void
2393 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
2394 {
2395   struct PreferenceGenerator *pg = find_pref_gen(op->peer_id,
2396       op->pref_type);
2397   if (NULL != pg)
2398   {
2399     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2400         "Stopping property generation for peer %u address %u\n", op->peer_id,
2401         op->address_id);
2402     GNUNET_ATS_solver_generate_preferences_stop (pg);
2403   }
2404   else
2405   {
2406     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2407         "Cannot find preference generator for peer %u address %u\n",
2408         op->peer_id, op->address_id);
2409     GNUNET_break (0);
2410   }
2411 }
2412
2413
2414 static void
2415 enforce_start_request (struct GNUNET_ATS_TEST_Operation *op)
2416 {
2417   struct TestPeer *p;
2418   const struct ATS_Address *res;
2419
2420   if (NULL == (p = find_peer_by_id (op->peer_id)))
2421   {
2422     GNUNET_break (0);
2423     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2424         "Requesting address for unknown peer %u\n", op->peer_id);
2425     return;
2426   }
2427
2428   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Requesting address for peer %u\n",
2429       op->peer_id);
2430   p->is_requested = GNUNET_YES;
2431
2432   res = sh->env.sf.s_get (sh->solver, &p->peer_id);
2433   if (NULL != res)
2434   {
2435     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Suggested address for peer %u: %llu %llu\n",
2436         op->peer_id,
2437         res->assigned_bw_in,
2438         res->assigned_bw_out);
2439     if (NULL != l)
2440       GNUNET_ATS_solver_logging_now (l);
2441   }
2442 }
2443
2444 static void
2445 enforce_stop_request (struct GNUNET_ATS_TEST_Operation *op)
2446 {
2447   struct TestPeer *p;
2448
2449   if (NULL == (p = find_peer_by_id (op->peer_id)))
2450   {
2451     GNUNET_break (0);
2452     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2453         "Requesting address for unknown peer %u\n", op->peer_id);
2454     return;
2455   }
2456
2457
2458
2459   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stop requesting address for peer %u\n",
2460       op->peer_id);
2461   p->is_requested = GNUNET_NO;
2462   p->assigned_bw_in = 0;
2463   p->assigned_bw_out = 0;
2464   sh->env.sf.s_get_stop (sh->solver, &p->peer_id);
2465
2466   if (NULL != l)
2467   {
2468     GNUNET_ATS_solver_logging_now (l);
2469   }
2470
2471 }
2472
2473 static void enforce_episode (struct Episode *ep)
2474 {
2475   struct GNUNET_ATS_TEST_Operation *cur;
2476   for (cur = ep->head; NULL != cur; cur = cur->next)
2477   {
2478     switch (cur->type) {
2479       case SOLVER_OP_ADD_ADDRESS:
2480         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
2481             print_op (cur->type), cur->peer_id, cur->address_id);
2482         enforce_add_address (cur);
2483         break;
2484       case SOLVER_OP_DEL_ADDRESS:
2485         fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
2486             print_op (cur->type), cur->peer_id, cur->address_id);
2487         enforce_del_address (cur);
2488         break;
2489       case SOLVER_OP_START_SET_PROPERTY:
2490         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2491             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2492         enforce_start_property (cur);
2493         break;
2494       case SOLVER_OP_STOP_SET_PROPERTY:
2495         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2496             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2497         enforce_stop_property (cur);
2498         break;
2499       case SOLVER_OP_START_SET_PREFERENCE:
2500         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2501             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2502         enforce_start_preference (cur);
2503         break;
2504       case SOLVER_OP_STOP_SET_PREFERENCE:
2505         fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
2506             print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
2507         enforce_stop_preference (cur);
2508         break;
2509       case SOLVER_OP_START_REQUEST:
2510         fprintf (stderr, "Enforcing operation: %s [%llu]\n",
2511             print_op (cur->type), cur->peer_id);
2512         enforce_start_request (cur);
2513         break;
2514       case SOLVER_OP_STOP_REQUEST:
2515         fprintf (stderr, "Enforcing operation: %s [%llu]\n",
2516             print_op (cur->type), cur->peer_id);
2517         enforce_stop_request (cur);
2518         break;
2519       default:
2520         break;
2521     }
2522   }
2523 }
2524
2525 static void
2526 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
2527 {
2528   struct Experiment *e = cls;
2529   e->episode_timeout_task = NULL;
2530   if (NULL != e->ep_done_cb)
2531     e->ep_done_cb (e->cur);
2532
2533   /* Scheduling next */
2534   e->cur = e->cur->next;
2535   if (NULL == e->cur)
2536   {
2537     /* done */
2538     fprintf (stderr, "Last episode done!\n");
2539     if (NULL != e->experiment_timeout_task)
2540     {
2541       GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
2542       e->experiment_timeout_task = NULL;
2543     }
2544     e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
2545     return;
2546   }
2547
2548   fprintf (stderr, "Running episode %u with timeout %s\n",
2549       e->cur->id,
2550       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
2551   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
2552       &timeout_episode, e);
2553   enforce_episode(e->cur);
2554
2555
2556 }
2557
2558
2559 void
2560 GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
2561     GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
2562     GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
2563 {
2564   fprintf (stderr, "Running experiment `%s'  with timeout %s\n", e->name,
2565       GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
2566   e->e_done_cb = e_done_cb;
2567   e->ep_done_cb = ep_done_cb;
2568   e->start_time = GNUNET_TIME_absolute_get();
2569
2570   /* Start total time out */
2571   e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
2572       &timeout_experiment, e);
2573
2574   /* Start */
2575   if (NULL == e->start)
2576   {
2577     GNUNET_break (0);
2578     return;
2579   }
2580
2581   e->cur = e->start;
2582   fprintf (stderr, "Running episode %u with timeout %s\n",
2583       e->cur->id,
2584       GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
2585   e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
2586       &timeout_episode, e);
2587   enforce_episode(e->cur);
2588
2589 }
2590
2591 void
2592 GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
2593 {
2594   if (NULL != e->experiment_timeout_task)
2595   {
2596     GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
2597     e->experiment_timeout_task = NULL;
2598   }
2599   if (NULL != e->episode_timeout_task)
2600   {
2601     GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
2602     e->episode_timeout_task = NULL;
2603   }
2604   if (NULL != e->cfg)
2605   {
2606     GNUNET_CONFIGURATION_destroy(e->cfg);
2607     e->cfg = NULL;
2608   }
2609   free_experiment (e);
2610 }
2611
2612
2613 struct Experiment *
2614 GNUNET_ATS_solvers_experimentation_load (char *filename)
2615 {
2616   struct Experiment *e;
2617   struct GNUNET_CONFIGURATION_Handle *cfg;
2618   e = NULL;
2619
2620   cfg = GNUNET_CONFIGURATION_create();
2621   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
2622   {
2623     fprintf (stderr, "Failed to load `%s'\n", filename);
2624     GNUNET_CONFIGURATION_destroy (cfg);
2625     return NULL;
2626   }
2627
2628   e = create_experiment ();
2629
2630   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
2631       "name", &e->name))
2632   {
2633     fprintf (stderr, "Invalid %s \n", "name");
2634     free_experiment (e);
2635     return NULL;
2636   }
2637   else
2638     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment name: `%s'\n", e->name);
2639
2640   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
2641       "log_prefix", &e->log_prefix))
2642   {
2643     fprintf (stderr, "Invalid %s \n", "log_prefix");
2644     free_experiment (e);
2645     return NULL;
2646   }
2647   else
2648     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging prefix: `%s'\n",
2649         e->log_prefix);
2650
2651   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
2652       "log_output_dir", &e->log_output_dir))
2653   {
2654     e->log_output_dir = NULL;
2655   }
2656   else
2657     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging output directory: `%s'\n",
2658         e->log_output_dir);
2659
2660
2661   if (GNUNET_SYSERR == (e->log_append_time_stamp = GNUNET_CONFIGURATION_get_value_yesno(cfg,
2662       "experiment", "log_append_time_stamp")))
2663     e->log_append_time_stamp = GNUNET_YES;
2664   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging append timestamp: `%s'\n",
2665       (GNUNET_YES == e->log_append_time_stamp) ? "yes" : "no");
2666
2667
2668   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
2669       "cfg_file", &e->cfg_file))
2670   {
2671     fprintf (stderr, "Invalid %s \n", "cfg_file");
2672     free_experiment (e);
2673     return NULL;
2674   }
2675   else
2676   {
2677     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment configuration: `%s'\n", e->cfg_file);
2678     e->cfg = GNUNET_CONFIGURATION_create();
2679     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (e->cfg, e->cfg_file))
2680     {
2681       fprintf (stderr, "Invalid configuration %s \n", "cfg_file");
2682       free_experiment (e);
2683       return NULL;
2684     }
2685
2686   }
2687
2688   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
2689       "log_freq", &e->log_freq))
2690   {
2691     fprintf (stderr, "Invalid %s \n", "log_freq");
2692     free_experiment (e);
2693     return NULL;
2694   }
2695   else
2696     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging frequency: `%s'\n",
2697         GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
2698
2699   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
2700       "max_duration", &e->max_duration))
2701   {
2702     fprintf (stderr, "Invalid %s", "max_duration");
2703     free_experiment (e);
2704     return NULL;
2705   }
2706   else
2707     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment duration: `%s'\n",
2708         GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
2709
2710   if (GNUNET_SYSERR == load_episodes (e, cfg))
2711   {
2712     GNUNET_ATS_solvers_experimentation_stop (e);
2713     GNUNET_CONFIGURATION_destroy (cfg);
2714     e = NULL;
2715     fprintf (stderr, "Failed to load experiment\n");
2716     return NULL;
2717   }
2718   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loaded %u episodes with total duration %s\n",
2719       e->num_episodes,
2720       GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
2721
2722   GNUNET_CONFIGURATION_destroy (cfg);
2723   return e;
2724 }
2725
2726
2727
2728 /**
2729  * Solver
2730  */
2731
2732 static int
2733 free_all_it (void *cls,
2734     const struct GNUNET_PeerIdentity *key,
2735     void *value)
2736 {
2737   struct ATS_Address *address = value;
2738   GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (sh->env.addresses,
2739       key, value));
2740   GNUNET_free (address);
2741
2742   return GNUNET_OK;
2743 }
2744
2745 void
2746 GNUNET_ATS_solvers_solver_stop (struct SolverHandle *sh)
2747 {
2748  GNUNET_STATISTICS_destroy ((struct GNUNET_STATISTICS_Handle *) sh->env.stats,
2749      GNUNET_NO);
2750  GNUNET_PLUGIN_unload (sh->plugin, sh->solver);
2751
2752  GAS_normalization_stop();
2753
2754  GNUNET_CONTAINER_multipeermap_iterate (sh->addresses, &free_all_it, NULL);
2755  GNUNET_CONTAINER_multipeermap_destroy(sh->addresses);
2756  GNUNET_free (sh->plugin);
2757  GNUNET_free (sh);
2758 }
2759
2760 /**
2761  * Load quotas for networks from configuration
2762  *
2763  * @param cfg configuration handle
2764  * @param out_dest where to write outbound quotas
2765  * @param in_dest where to write inbound quotas
2766  * @param dest_length length of inbound and outbound arrays
2767  * @return number of networks loaded
2768  */
2769 unsigned int
2770 GNUNET_ATS_solvers_load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
2771                                                  unsigned long long *out_dest,
2772                                                  unsigned long long *in_dest,
2773                                                  int dest_length)
2774 {
2775   char * entry_in = NULL;
2776   char * entry_out = NULL;
2777   char * quota_out_str;
2778   char * quota_in_str;
2779   int c;
2780   int res;
2781
2782   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
2783   {
2784     in_dest[c] = 0;
2785     out_dest[c] = 0;
2786     GNUNET_asprintf (&entry_out,
2787                      "%s_QUOTA_OUT",
2788                      GNUNET_ATS_print_network_type (c));
2789     GNUNET_asprintf (&entry_in,
2790                      "%s_QUOTA_IN",
2791                      GNUNET_ATS_print_network_type (c));
2792
2793     /* quota out */
2794     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
2795     {
2796       res = GNUNET_NO;
2797       if (0 == strcmp(quota_out_str, BIG_M_STRING))
2798       {
2799         out_dest[c] = GNUNET_ATS_MaxBandwidth;
2800         res = GNUNET_YES;
2801       }
2802       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
2803         res = GNUNET_YES;
2804       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,  &out_dest[c])))
2805          res = GNUNET_YES;
2806
2807       if (GNUNET_NO == res)
2808       {
2809           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2810                       _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
2811                       GNUNET_ATS_print_network_type (c),
2812                       quota_out_str,
2813                       GNUNET_ATS_DefaultBandwidth);
2814           out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2815       }
2816       else
2817       {
2818           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2819                       "Outbound quota configure for network `%s' is %llu\n",
2820                       GNUNET_ATS_print_network_type (c),
2821                       out_dest[c]);
2822       }
2823       GNUNET_free (quota_out_str);
2824     }
2825     else
2826     {
2827       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2828                   _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
2829                   GNUNET_ATS_print_network_type (c),
2830                   GNUNET_ATS_DefaultBandwidth);
2831       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2832     }
2833
2834     /* quota in */
2835     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
2836     {
2837       res = GNUNET_NO;
2838       if (0 == strcmp(quota_in_str, BIG_M_STRING))
2839       {
2840         in_dest[c] = GNUNET_ATS_MaxBandwidth;
2841         res = GNUNET_YES;
2842       }
2843       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
2844         res = GNUNET_YES;
2845       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,  &in_dest[c])))
2846          res = GNUNET_YES;
2847
2848       if (GNUNET_NO == res)
2849       {
2850           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2851                       _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
2852                       GNUNET_ATS_print_network_type (c),
2853                       quota_in_str,
2854                       GNUNET_ATS_DefaultBandwidth);
2855           in_dest[c] = GNUNET_ATS_DefaultBandwidth;
2856       }
2857       else
2858       {
2859           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2860                       "Inbound quota configured for network `%s' is %llu\n",
2861                       GNUNET_ATS_print_network_type (c),
2862                       in_dest[c]);
2863       }
2864       GNUNET_free (quota_in_str);
2865     }
2866     else
2867     {
2868       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2869                   _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
2870                   GNUNET_ATS_print_network_type (c),
2871                   GNUNET_ATS_DefaultBandwidth);
2872       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2873     }
2874     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2875                 "Loaded quota for network `%s' (in/out): %llu %llu\n",
2876                 GNUNET_ATS_print_network_type (c),
2877                 in_dest[c],
2878                 out_dest[c]);
2879     GNUNET_free (entry_out);
2880     GNUNET_free (entry_in);
2881   }
2882   return GNUNET_ATS_NetworkTypeCount;
2883 }
2884
2885
2886 /**
2887  * Information callback for the solver
2888  *
2889  * @param cls the closure
2890  * @param op the solver operation
2891  * @param stat status of the solver operation
2892  * @param add additional solver information
2893  */
2894 static void
2895 solver_info_cb (void *cls,
2896                 enum GAS_Solver_Operation op,
2897                 enum GAS_Solver_Status stat,
2898                 enum GAS_Solver_Additional_Information add)
2899 {
2900   char *add_info;
2901   switch (add) {
2902     case GAS_INFO_NONE:
2903       add_info = "GAS_INFO_NONE";
2904       break;
2905     case GAS_INFO_FULL:
2906       add_info = "GAS_INFO_MLP_FULL";
2907       break;
2908     case GAS_INFO_UPDATED:
2909       add_info = "GAS_INFO_MLP_UPDATED";
2910       break;
2911     case GAS_INFO_PROP_ALL:
2912       add_info = "GAS_INFO_PROP_ALL";
2913       break;
2914     case GAS_INFO_PROP_SINGLE:
2915       add_info = "GAS_INFO_PROP_SINGLE";
2916       break;
2917     default:
2918       add_info = "INVALID";
2919       break;
2920   }
2921
2922   switch (op)
2923   {
2924     case GAS_OP_SOLVE_START:
2925       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2926           "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
2927           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
2928       return;
2929     case GAS_OP_SOLVE_STOP:
2930       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2931           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
2932           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
2933       return;
2934
2935     case GAS_OP_SOLVE_SETUP_START:
2936       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2937           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
2938           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2939       return;
2940
2941     case GAS_OP_SOLVE_SETUP_STOP:
2942       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2943           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
2944           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2945       return;
2946
2947     case GAS_OP_SOLVE_MLP_LP_START:
2948       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2949           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
2950           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2951       return;
2952     case GAS_OP_SOLVE_MLP_LP_STOP:
2953       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2954           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
2955           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2956       return;
2957
2958     case GAS_OP_SOLVE_MLP_MLP_START:
2959       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2960           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
2961           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2962       return;
2963     case GAS_OP_SOLVE_MLP_MLP_STOP:
2964       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2965           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
2966           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2967       return;
2968     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
2969       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2970           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
2971           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2972       return;
2973     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
2974       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2975           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
2976           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
2977       return;
2978     default:
2979       break;
2980     }
2981 }
2982
2983 static void
2984 solver_bandwidth_changed_cb (void *cls, struct ATS_Address *address)
2985 {
2986   struct GNUNET_TIME_Relative duration;
2987   struct TestPeer *p;
2988   static struct PreferenceGenerator *pg;
2989   uint32_t delta;
2990   if ( (0 == address->assigned_bw_out) && (0 == address->assigned_bw_in) )
2991   {
2992     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2993                 "Solver notified to disconnect peer `%s'\n",
2994                 GNUNET_i2s (&address->peer));
2995   }
2996   p = find_peer_by_pid(&address->peer);
2997   if (NULL == p)
2998     return;
2999   p->assigned_bw_out = address->assigned_bw_out;
3000   p->assigned_bw_in = address->assigned_bw_in;
3001
3002   for (pg = pref_gen_head; NULL != pg; pg = pg->next)
3003   {
3004     if (pg->peer == p->id)
3005     {
3006       duration = GNUNET_TIME_absolute_get_duration(pg->feedback_last_bw_update);
3007       delta = duration.rel_value_us * pg->last_assigned_bw_out;
3008       pg->feedback_bw_out_acc += delta;
3009
3010       delta = duration.rel_value_us * pg->last_assigned_bw_in;
3011       pg->feedback_bw_in_acc += delta;
3012
3013       pg->last_assigned_bw_in = address->assigned_bw_in;
3014       pg->last_assigned_bw_out = address->assigned_bw_out;
3015       pg->feedback_last_bw_update = GNUNET_TIME_absolute_get();
3016     }
3017   }
3018
3019   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3020       "Bandwidth changed addresses %s %p to %u Bps out / %u Bps in\n",
3021       GNUNET_i2s (&address->peer),
3022       address,
3023       address->assigned_bw_out,
3024           address->assigned_bw_in);
3025
3026   if (NULL != l)
3027     GNUNET_ATS_solver_logging_now (l);
3028
3029   return;
3030 }
3031
3032 const double *
3033 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
3034 {
3035   struct TestPeer *p;
3036   if (GNUNET_YES == opt_disable_normalization)
3037   {
3038     if (NULL == (p = find_peer_by_pid (id)))
3039       return NULL;
3040     return p->pref_abs;
3041   }
3042   else
3043     return GAS_normalization_get_preferences_by_peer (id);
3044 }
3045
3046
3047 const double *
3048 get_property_cb (void *cls, const struct ATS_Address *address)
3049 {
3050   struct TestPeer *p;
3051   struct TestAddress *a;
3052
3053   if (GNUNET_YES == opt_disable_normalization)
3054   {
3055     p = find_peer_by_pid (&address->peer);
3056     if (NULL == p)
3057       return NULL;
3058     a = find_address_by_ats_address (p, address);
3059     return a->prop_abs;
3060   }
3061   return GAS_normalization_get_properties ((struct ATS_Address *) address);
3062 }
3063
3064
3065 static void
3066 set_updated_property ( struct ATS_Address *address, uint32_t type, double prop_rel)
3067 {
3068   struct TestPeer *p;
3069   struct TestAddress *a;
3070
3071   if (NULL == (p = find_peer_by_pid (&address->peer)))
3072   {
3073     GNUNET_break (0);
3074     return;
3075   }
3076
3077   if (NULL == (a = find_address_by_ats_address (p, address)))
3078   {
3079     GNUNET_break (0);
3080     return;
3081   }
3082   a->prop_norm[type] = prop_rel;
3083   sh->env.sf.s_address_update_property (sh->solver, address, type, a->prop_abs [type], prop_rel);
3084 }
3085
3086
3087 static void
3088 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
3089     uint32_t type, double prop_rel)
3090 {
3091   struct TestPeer *p;
3092   struct PreferenceGenerator *pg;
3093   struct GNUNET_TIME_Relative duration;
3094   uint32_t delta;
3095
3096   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
3097       "Normalized property %s for peer `%s' changed to %.3f \n",
3098       GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
3099       prop_rel);
3100
3101   if (NULL != (p = find_peer_by_pid (&address->peer)))
3102   {
3103     for (pg = pref_gen_head; NULL != pg; pg = pg->next)
3104     {
3105       if (pg->peer == p->id)
3106       {
3107         duration = GNUNET_TIME_absolute_get_duration(pg->feedback_last_delay_update);
3108         delta = duration.rel_value_us * pg->last_delay_value;
3109         pg->feedback_delay_acc += delta;
3110
3111         pg->last_delay_value = prop_rel;
3112         pg->feedback_last_bw_update = GNUNET_TIME_absolute_get();
3113       }
3114     }
3115
3116   }
3117
3118   set_updated_property (address, type, prop_rel);
3119 }
3120
3121 static void
3122 set_updated_preference (const struct GNUNET_PeerIdentity *peer,
3123     enum GNUNET_ATS_PreferenceKind kind,
3124     double pref_rel)
3125 {
3126   struct TestPeer *p;
3127
3128   if (NULL == (p = find_peer_by_pid (peer)))
3129   {
3130     GNUNET_break (0);
3131     return;
3132   }
3133
3134   p->pref_norm[kind] = pref_rel;
3135   sh->env.sf.s_pref (sh->solver, peer, kind, pref_rel);
3136 }
3137
3138
3139 static void
3140 normalized_preference_changed_cb (void *cls,
3141     const struct GNUNET_PeerIdentity *peer,
3142     enum GNUNET_ATS_PreferenceKind kind,
3143     double pref_rel)
3144 {
3145   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
3146       "Normalized preference %s for peer `%s' changed to %.3f \n",
3147       GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer),
3148       pref_rel);
3149
3150   set_updated_preference(peer, kind, pref_rel);
3151 }
3152
3153
3154 struct SolverHandle *
3155 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
3156 {
3157   struct SolverHandle *sh;
3158   char * solver_str;
3159   int c;
3160
3161   switch (type) {
3162     case GNUNET_ATS_SOLVER_PROPORTIONAL:
3163       solver_str = "proportional";
3164       break;
3165     case GNUNET_ATS_SOLVER_MLP:
3166       solver_str = "mlp";
3167       break;
3168     case GNUNET_ATS_SOLVER_RIL:
3169       solver_str = "ril";
3170       break;
3171     default:
3172       GNUNET_break (0);
3173       return NULL;
3174       break;
3175   }
3176
3177   sh = GNUNET_new (struct SolverHandle);
3178   GNUNET_asprintf (&sh->plugin, "libgnunet_plugin_ats_%s", solver_str);
3179
3180   sh->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
3181
3182   /* setup environment */
3183   sh->env.cfg = e->cfg;
3184   sh->env.stats = GNUNET_STATISTICS_create ("ats", e->cfg);
3185   sh->env.addresses = sh->addresses;
3186   sh->env.bandwidth_changed_cb = &solver_bandwidth_changed_cb;
3187   sh->env.get_preferences = &get_preferences_cb;
3188   sh->env.get_property = &get_property_cb;
3189   sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
3190   sh->env.info_cb = &solver_info_cb;
3191   sh->env.info_cb_cls = NULL;
3192   sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
3193   int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
3194   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
3195     sh->env.networks[c] = networks[c];
3196
3197
3198   /* start normalization */
3199   GAS_normalization_start (&normalized_preference_changed_cb, NULL,
3200       &normalized_property_changed_cb, NULL );
3201
3202   /* load quotas */
3203   if (GNUNET_ATS_NetworkTypeCount != GNUNET_ATS_solvers_load_quotas (e->cfg,
3204       sh->env.out_quota, sh->env.in_quota, GNUNET_ATS_NetworkTypeCount))
3205   {
3206     GNUNET_break(0);
3207     GNUNET_free (sh->plugin);
3208     GNUNET_free (sh);
3209     end_now ();
3210     return NULL;
3211   }
3212
3213   sh->solver = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
3214   if (NULL == sh->solver)
3215   {
3216     fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
3217     GNUNET_break(0);
3218     GNUNET_free (sh->plugin);
3219     GNUNET_free (sh);
3220     end_now ();
3221     return NULL;
3222   }
3223   return sh;
3224 }
3225
3226 static void
3227 done ()
3228 {
3229   struct TestPeer *cur;
3230   struct TestPeer *next;
3231
3232   struct TestAddress *cur_a;
3233   struct TestAddress *next_a;
3234
3235   /* Stop logging */
3236   GNUNET_ATS_solver_logging_stop (l);
3237
3238   /* Stop all preference generation */
3239   GNUNET_ATS_solver_generate_preferences_stop_all ();
3240
3241   /* Stop all property generation */
3242   GNUNET_ATS_solver_generate_property_stop_all ();
3243
3244   if (opt_print)
3245   {
3246     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Printing log information \n");
3247     GNUNET_ATS_solver_logging_eval (l);
3248   }
3249   if (opt_save)
3250   {
3251     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Saving log information \n");
3252     GNUNET_ATS_solver_logging_write_to_disk (l, e->log_append_time_stamp,
3253         e->log_output_dir);
3254   }
3255
3256   if (NULL != l)
3257   {
3258     GNUNET_ATS_solver_logging_free (l);
3259     l = NULL;
3260   }
3261
3262   /* Clean up experiment */
3263   if (NULL != e)
3264   {
3265     GNUNET_ATS_solvers_experimentation_stop (e);
3266     e = NULL;
3267   }
3268
3269   next = peer_head;
3270   while  (NULL != (cur = next))
3271   {
3272     next = cur->next;
3273     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, cur);
3274     next_a = cur->addr_head;
3275     while  (NULL != (cur_a = next_a))
3276     {
3277       next_a = cur_a->next;
3278       GNUNET_CONTAINER_DLL_remove (cur->addr_head, cur->addr_tail, cur_a);
3279       GNUNET_free (cur_a);
3280     }
3281     GNUNET_free (cur);
3282   }
3283   if (NULL != sh)
3284   {
3285     GNUNET_ATS_solvers_solver_stop (sh);
3286     sh = NULL;
3287   }
3288
3289   /* Shutdown */
3290   end_now();
3291 }
3292
3293 static void
3294 experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,int success)
3295 {
3296   if (GNUNET_OK == success)
3297     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment done successful in %s\n",
3298         GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
3299   else
3300     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
3301
3302   GNUNET_SCHEDULER_add_now (&done, NULL);
3303 }
3304
3305 static void
3306 episode_done_cb (struct Episode *ep)
3307 {
3308   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
3309 }
3310
3311
3312
3313 /**
3314  * Do shutdown
3315  */
3316 static void
3317 end_now ()
3318 {
3319   if (NULL != e)
3320   {
3321     GNUNET_ATS_solvers_experimentation_stop (e);
3322     e = NULL;
3323   }
3324   if (NULL != sh)
3325   {
3326     GNUNET_ATS_solvers_solver_stop (sh);
3327     sh = NULL;
3328   }
3329 }
3330
3331 static void
3332 run (void *cls, char * const *args, const char *cfgfile,
3333     const struct GNUNET_CONFIGURATION_Handle *cfg)
3334 {
3335   enum GNUNET_ATS_Solvers solver;
3336   int c;
3337
3338   if (NULL == opt_exp_file)
3339   {
3340     fprintf (stderr, "No experiment given ...\n");
3341     res = 1;
3342     end_now ();
3343     return;
3344   }
3345
3346   if (NULL == opt_solver)
3347   {
3348     fprintf (stderr, "No solver given ...\n");
3349     res = 1;
3350     end_now ();
3351     return;
3352   }
3353
3354   if (0 == strcmp(opt_solver, "mlp"))
3355   {
3356     solver = GNUNET_ATS_SOLVER_MLP;
3357   }
3358   else if (0 == strcmp(opt_solver, "proportional"))
3359   {
3360     solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
3361   }
3362   else if (0 == strcmp(opt_solver, "ril"))
3363   {
3364     solver = GNUNET_ATS_SOLVER_RIL;
3365   }
3366   else
3367   {
3368     fprintf (stderr, "No solver given ...");
3369     res = 1;
3370     end_now ();
3371     return;
3372   }
3373
3374   for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
3375     default_properties[c] = DEFAULT_REL_QUALITY;
3376
3377   for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
3378     default_preferences[c] = DEFAULT_REL_PREFERENCE;
3379
3380   /* load experiment */
3381   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading experiment\n");
3382   e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
3383   if (NULL == e)
3384   {
3385     fprintf (stderr, "Failed to load experiment ...\n");
3386     res = 1;
3387     end_now ();
3388     return;
3389   }
3390
3391   /* load solver */
3392   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading solver\n");
3393   sh = GNUNET_ATS_solvers_solver_start (solver);
3394   if (NULL == sh)
3395   {
3396     fprintf (stderr, "Failed to start solver ...\n");
3397     end_now ();
3398     res = 1;
3399     return;
3400   }
3401
3402   /* start logging */
3403   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Start logging \n");
3404   l = GNUNET_ATS_solver_logging_start (e->log_freq);
3405
3406   /* run experiment */
3407   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Running experiment \n");
3408   GNUNET_ATS_solvers_experimentation_run (e, episode_done_cb,
3409       experiment_done_cb);
3410
3411   /* WAIT */
3412 }
3413
3414
3415 /**
3416  * Main function of the benchmark
3417  *
3418  * @param argc argument count
3419  * @param argv argument values
3420  */
3421 int
3422 main (int argc, char *argv[])
3423 {
3424   opt_exp_file = NULL;
3425   opt_solver = NULL;
3426   opt_log = GNUNET_NO;
3427   opt_save = GNUNET_NO;
3428
3429   res = 0;
3430
3431   static struct GNUNET_GETOPT_CommandLineOption options[] =
3432   {
3433     { 's', "solver", NULL,
3434         gettext_noop ("solver to use"),
3435         1, &GNUNET_GETOPT_set_string, &opt_solver},
3436     {  'e', "experiment", NULL,
3437       gettext_noop ("experiment to use"),
3438       1, &GNUNET_GETOPT_set_string, &opt_exp_file},
3439     {  'V', "verbose", NULL,
3440       gettext_noop ("be verbose"),
3441       0, &GNUNET_GETOPT_set_one, &opt_verbose},
3442     {  'p', "print", NULL,
3443       gettext_noop ("print logging"),
3444       0, &GNUNET_GETOPT_set_one, &opt_print},
3445     {  'f', "file", NULL,
3446         gettext_noop ("save logging to disk"),
3447         0, &GNUNET_GETOPT_set_one, &opt_save},
3448     {  'd', "dn", NULL,
3449         gettext_noop ("disable normalization"),
3450         0, &GNUNET_GETOPT_set_one, &opt_disable_normalization},
3451     GNUNET_GETOPT_OPTION_END
3452   };
3453
3454   GNUNET_PROGRAM_run (argc, argv, "gnunet-ats-solver-eval",
3455       NULL, options, &run, argv[0]);
3456
3457   return res;
3458 }
3459 /* end of file ats-testing-experiment.c*/