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