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