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