Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sanitize the way parsing callbacks of the routing submodule are registered
[simgrid.git] / src / surf / surf_routing.c
1 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <pcre.h>               /* regular expression library */
8
9 #include "simgrid/platf_interface.h" // platform creation API internal interface
10
11 #include "surf_routing_private.h"
12 #include "surf/surf_routing.h"
13 #include "surf/surfxml_parse_values.h"
14
15 xbt_lib_t host_lib;
16 int ROUTING_HOST_LEVEL; //Routing level
17 int     SURF_CPU_LEVEL;         //Surf cpu level
18 int SURF_WKS_LEVEL;             //Surf workstation level
19 int SIMIX_HOST_LEVEL;   //Simix level
20 int     MSG_HOST_LEVEL;         //Msg level
21 int     SD_HOST_LEVEL;          //Simdag level
22 int     COORD_HOST_LEVEL;       //Coordinates level
23 int NS3_HOST_LEVEL;             //host node for ns3
24
25 xbt_lib_t link_lib;
26 int SD_LINK_LEVEL;              //Simdag level
27 int SURF_LINK_LEVEL;    //Surf level
28 int NS3_LINK_LEVEL;             //link for ns3
29
30 xbt_lib_t as_router_lib;
31 int ROUTING_ASR_LEVEL;  //Routing level
32 int COORD_ASR_LEVEL;    //Coordinates level
33 int NS3_ASR_LEVEL;              //host node for ns3
34
35 static xbt_dict_t patterns = NULL;
36 static xbt_dict_t random_value = NULL;
37
38 /* Global vars */
39 routing_global_t global_routing = NULL;
40 routing_component_t current_routing = NULL;
41 model_type_t current_routing_model = NULL;
42
43 /* global parse functions */
44 xbt_dynar_t link_list = NULL;    /* temporary store of current list link of a route */
45 static const char *src = NULL;        /* temporary store the source name of a route */
46 static const char *dst = NULL;        /* temporary store the destination name of a route */
47 static char *gw_src = NULL;     /* temporary store the gateway source name of a route */
48 static char *gw_dst = NULL;     /* temporary store the gateway destination name of a route */
49 static double_f_cpvoid_t get_link_latency = NULL;
50
51 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
52
53 static void routing_parse_Speer(sg_platf_peer_cbarg_t peer);          /* peer bypass */
54 static void routing_parse_Srandom(void);        /* random bypass */
55
56 static char* replace_random_parameter(char * chaine);
57 static void clean_routing_after_parse(void);
58
59 /* this lines are only for replace use like index in the model table */
60 typedef enum {
61   SURF_MODEL_FULL = 0,
62   SURF_MODEL_FLOYD,
63   SURF_MODEL_DIJKSTRA,
64   SURF_MODEL_DIJKSTRACACHE,
65   SURF_MODEL_NONE,
66   SURF_MODEL_RULEBASED,
67   SURF_MODEL_VIVALDI,
68   SURF_MODEL_CLUSTER
69 } e_routing_types;
70
71 struct s_model_type routing_models[] = {
72     { "Full", "Full routing data (fast, large memory requirements, fully expressive)",
73         model_full_create, model_full_load, model_full_unload, model_full_end
74     },
75     { "Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
76         model_floyd_create, model_floyd_load, model_floyd_unload, model_floyd_end
77     },
78     { "Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
79         model_dijkstra_create, model_dijkstra_both_load,
80         model_dijkstra_both_unload, model_dijkstra_both_end
81     },
82     { "DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
83         model_dijkstracache_create, model_dijkstra_both_load, model_dijkstra_both_unload, model_dijkstra_both_end
84     },
85     { "none", "No routing (usable with Constant network only)",
86         model_none_create, model_none_load, model_none_unload, model_none_end
87     },
88     { "RuleBased", "Rule-Based routing data (...)",
89         model_rulebased_create, model_none_load, model_none_unload, model_none_end
90     },
91     { "Vivaldi", "Vivaldi routing",
92         model_vivaldi_create, model_none_load, model_none_unload, model_none_end
93     },
94     {"Cluster", "Cluster routing",
95         model_cluster_create, model_none_load, model_cluster_unload, model_none_end
96     },
97     {NULL, NULL, NULL, NULL, NULL, NULL}
98 };
99
100 /**
101  * \brief Add a "host" to the network element list
102  */
103 static void parse_S_host(sg_platf_host_cbarg_t host) {
104   network_element_info_t info = NULL;
105   if (current_routing->hierarchy == SURF_ROUTING_NULL)
106     current_routing->hierarchy = SURF_ROUTING_BASE;
107   xbt_assert(!xbt_lib_get_or_null(host_lib, host->id,ROUTING_HOST_LEVEL),
108       "Reading a host, processing unit \"%s\" already exists",
109       host->id);
110   xbt_assert(current_routing->set_processing_unit,
111       "no defined method \"set_processing_unit\" in \"%s\"",
112       current_routing->name);
113   (*(current_routing->set_processing_unit)) (current_routing, host->id);
114   info = xbt_new0(s_network_element_info_t, 1);
115   info->rc_component = current_routing;
116   info->rc_type = SURF_NETWORK_ELEMENT_HOST;
117   xbt_lib_set(host_lib,host->id,ROUTING_HOST_LEVEL,(void *) info);
118   if (host->coord && strcmp(host->coord,"")) {
119     if(!COORD_HOST_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
120     xbt_dynar_t ctn = xbt_str_split_str(host->coord, " ");
121     xbt_dynar_shrink(ctn, 0);
122     xbt_lib_set(host_lib,host->id,COORD_HOST_LEVEL,(void *) ctn);
123   }
124 }
125
126 /**
127  * \brief Add a "router" to the network element list
128  */
129 static void parse_S_router(sg_platf_router_cbarg_t router)
130 {
131   network_element_info_t info = NULL;
132   if (current_routing->hierarchy == SURF_ROUTING_NULL)
133     current_routing->hierarchy = SURF_ROUTING_BASE;
134   xbt_assert(!xbt_lib_get_or_null(as_router_lib,router->id, ROUTING_ASR_LEVEL),
135       "Reading a router, processing unit \"%s\" already exists",
136       router->id);
137   xbt_assert(current_routing->set_processing_unit,
138       "no defined method \"set_processing_unit\" in \"%s\"",
139       current_routing->name);
140   (*(current_routing->set_processing_unit)) (current_routing, router->id);
141   info = xbt_new0(s_network_element_info_t, 1);
142   info->rc_component = current_routing;
143   info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
144
145   xbt_lib_set(as_router_lib,router->id,ROUTING_ASR_LEVEL,(void *) info);
146   if (strcmp(router->coord,"")) {
147     if(!COORD_ASR_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
148     xbt_dynar_t ctn = xbt_str_split_str(router->coord, " ");
149     xbt_dynar_shrink(ctn, 0);
150     xbt_lib_set(as_router_lib,router->id,COORD_ASR_LEVEL,(void *) ctn);
151   }
152 }
153
154 /**
155  * \brief Set the endponints for a route
156  */
157 static void parse_S_route_new_and_endpoints(const char *src_id, const char *dst_id)
158 {
159   if (src != NULL && dst != NULL && link_list != NULL)
160     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
161         src_id, dst_id);
162   src = src_id;
163   dst = dst_id;
164   xbt_assert(strlen(src) > 0 || strlen(dst) > 0,
165       "Some limits are null in the route between \"%s\" and \"%s\"",
166       src, dst);
167   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
168 }
169
170 /**
171  * \brief Set the endpoints for a route from XML
172  */
173 static void parse_S_route_new_and_endpoints_XML(void)
174 {
175   parse_S_route_new_and_endpoints(A_surfxml_route_src,
176       A_surfxml_route_dst);
177 }
178
179 /**
180  * \brief Set the endponints and gateways for a ASroute
181  */
182 static void parse_S_ASroute_new_and_endpoints(void)
183 {
184   if (src != NULL && dst != NULL && link_list != NULL)
185     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
186         A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
187   src = A_surfxml_ASroute_src;
188   dst = A_surfxml_ASroute_dst;
189   gw_src = A_surfxml_ASroute_gw_src;
190   gw_dst = A_surfxml_ASroute_gw_dst;
191   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
192       || strlen(gw_dst) > 0,
193       "Some limits are null in the route between \"%s\" and \"%s\"",
194       src, dst);
195   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
196 }
197
198 /**
199  * \brief Set the endponints for a bypassRoute
200  */
201 static void parse_S_bypassRoute_new_and_endpoints(void)
202 {
203   if (src != NULL && dst != NULL && link_list != NULL)
204     THROWF(arg_error, 0,
205         "Bypass Route between %s to %s can not be defined",
206         A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
207   src = A_surfxml_bypassRoute_src;
208   dst = A_surfxml_bypassRoute_dst;
209   gw_src = A_surfxml_bypassRoute_gw_src;
210   gw_dst = A_surfxml_bypassRoute_gw_dst;
211   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
212       || strlen(gw_dst) > 0,
213       "Some limits are null in the route between \"%s\" and \"%s\"",
214       src, dst);
215   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
216 }
217
218 /**
219  * \brief Set a new link on the actual list of link for a route or ASroute from XML
220  */
221
222 static void routing_parse_link_ctn(void)
223 {
224   char *link_id;
225   switch (A_surfxml_link_ctn_direction) {
226   case AU_surfxml_link_ctn_direction:
227   case A_surfxml_link_ctn_direction_NONE:
228     link_id = xbt_strdup(A_surfxml_link_ctn_id);
229     break;
230   case A_surfxml_link_ctn_direction_UP:
231     link_id = bprintf("%s_UP", A_surfxml_link_ctn_id);
232     break;
233   case A_surfxml_link_ctn_direction_DOWN:
234     link_id = bprintf("%s_DOWN", A_surfxml_link_ctn_id);
235   }
236   xbt_dynar_push(link_list,&link_id);
237 }
238
239 /**
240  * \brief Store the route by calling the set_route function of the current routing component
241  */
242 static void parse_E_route_store_route(void)
243 {
244   name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
245   route->generic_route.link_list = link_list;
246   xbt_assert(current_routing->set_route,
247       "no defined method \"set_route\" in \"%s\"",
248       current_routing->name);
249   (*(current_routing->set_route)) (current_routing, src, dst, route);
250   link_list = NULL;
251   src = NULL;
252   dst = NULL;
253 }
254
255 /**
256  * \brief Store the ASroute by calling the set_ASroute function of the current routing component
257  */
258 static void parse_E_ASroute_store_route(void)
259 {
260   name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
261   e_route->generic_route.link_list = link_list;
262   e_route->src_gateway = xbt_strdup(gw_src);
263   e_route->dst_gateway = xbt_strdup(gw_dst);
264   xbt_assert(current_routing->set_ASroute,
265       "no defined method \"set_ASroute\" in \"%s\"",
266       current_routing->name);
267   (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
268   link_list = NULL;
269   src = NULL;
270   dst = NULL;
271   gw_src = NULL;
272   gw_dst = NULL;
273 }
274
275 /**
276  * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
277  */
278 static void parse_E_bypassRoute_store_route(void)
279 {
280   route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
281   e_route->generic_route.link_list = link_list;
282   e_route->src_gateway = xbt_strdup(gw_src);
283   e_route->dst_gateway = xbt_strdup(gw_dst);
284   xbt_assert(current_routing->set_bypassroute,
285       "no defined method \"set_bypassroute\" in \"%s\"",
286       current_routing->name);
287   (*(current_routing->set_bypassroute)) (current_routing, src, dst,
288       e_route);
289   link_list = NULL;
290   src = NULL;
291   dst = NULL;
292   gw_src = NULL;
293   gw_dst = NULL;
294 }
295
296 /**
297  * \brief Make a new routing component to the platform
298  *
299  * Add a new autonomous system to the platform. Any elements (such as host,
300  * router or sub-AS) added after this call and before the corresponding call
301  * to sg_platf_new_AS_close() will be added to this AS.
302  *
303  * Once this function was called, the configuration concerning the used
304  * models cannot be changed anymore.
305  *
306  * @param AS_id name of this autonomous system. Must be unique in the platform
307  * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
308  */
309 void routing_AS_open(const char *AS_id, const char *wanted_routing_type)
310 {
311   routing_component_t new_routing;
312   model_type_t model = NULL;
313   int cpt;
314
315   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag and before the first <AS>-like */
316
317   /* search the routing model */
318   for (cpt = 0; routing_models[cpt].name; cpt++)
319     if (!strcmp(wanted_routing_type, routing_models[cpt].name))
320       model = &routing_models[cpt];
321   /* if its not exist, error */
322   if (model == NULL) {
323     fprintf(stderr, "Routing model %s not found. Existing models:\n",
324         wanted_routing_type);
325     for (cpt = 0; routing_models[cpt].name; cpt++)
326       fprintf(stderr, "   %s: %s\n", routing_models[cpt].name,
327           routing_models[cpt].desc);
328     xbt_die(NULL);
329   }
330
331   /* make a new routing component */
332   new_routing = (routing_component_t) (*(model->create)) ();
333   new_routing->routing = model;
334   new_routing->hierarchy = SURF_ROUTING_NULL;
335   new_routing->name = xbt_strdup(AS_id);
336   new_routing->routing_sons = xbt_dict_new();
337
338   if (current_routing == NULL && global_routing->root == NULL) {
339
340     /* it is the first one */
341     new_routing->routing_father = NULL;
342     global_routing->root = new_routing;
343
344   } else if (current_routing != NULL && global_routing->root != NULL) {
345
346     xbt_assert(!xbt_dict_get_or_null
347         (current_routing->routing_sons, AS_id),
348         "The AS \"%s\" already exists", AS_id);
349     /* it is a part of the tree */
350     new_routing->routing_father = current_routing;
351     /* set the father behavior */
352     if (current_routing->hierarchy == SURF_ROUTING_NULL)
353       current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
354     /* add to the sons dictionary */
355     xbt_dict_set(current_routing->routing_sons, AS_id,
356         (void *) new_routing, NULL);
357     /* add to the father element list */
358     (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
359     /* unload the prev parse rules */
360     (*(current_routing->routing->unload)) ();
361
362   } else {
363     THROWF(arg_error, 0, "All defined components must be belong to a AS");
364   }
365   /* set the new parse rules */
366   (*(new_routing->routing->load)) ();
367   /* set the new current component of the tree */
368   current_routing = new_routing;
369 }
370
371 /**
372  * \brief Specify that the current description of AS is finished
373  *
374  * Once you've declared all the content of your AS, you have to close
375  * it with this call. Your AS is not usable until you call this function.
376  *
377  * @fixme: this call is not as robust as wanted: bad things WILL happen
378  * if you call it twice for the same AS, or if you forget calling it, or
379  * even if you add stuff to a closed AS
380  *
381  */
382 void routing_AS_close() {
383
384   if (current_routing == NULL) {
385     THROWF(arg_error, 0, "Close an AS, but none was under construction");
386   } else {
387     network_element_info_t info = NULL;
388     xbt_assert(!xbt_lib_get_or_null(as_router_lib,current_routing->name, ROUTING_ASR_LEVEL),
389         "The AS \"%s\" already exists",current_routing->name);
390     info = xbt_new0(s_network_element_info_t, 1);
391     info->rc_component = current_routing->routing_father;
392     info->rc_type = SURF_NETWORK_ELEMENT_AS;
393     xbt_lib_set(as_router_lib,current_routing->name,ROUTING_ASR_LEVEL,(void *) info);
394
395     (*(current_routing->routing->unload)) ();
396     (*(current_routing->routing->end)) ();
397     current_routing = current_routing->routing_father;
398     if (current_routing != NULL)
399       (*(current_routing->routing->load)) ();
400   }
401 }
402
403 /* Aux Business methods */
404
405 /**
406  * \brief Get the AS name of the element
407  *
408  * \param name the host name
409  *
410  */
411 static char* elements_As_name(const char *name)
412                                         {
413   routing_component_t as_comp;
414
415   /* (1) find the as where the host is located */
416   as_comp = ((network_element_info_t)
417       xbt_lib_get_or_null(host_lib,name, ROUTING_HOST_LEVEL))->rc_component;
418   return as_comp->name;
419                                         }
420
421
422 /**
423  * \brief Get the AS father and the first elements of the chain
424  *
425  * \param src the source host name 
426  * \param dst the destination host name
427  * 
428  * Get the common father of the to processing units, and the first different 
429  * father in the chain
430  */
431 static void elements_father(const char *src, const char *dst,
432     routing_component_t *res_father,
433     routing_component_t *res_src,
434     routing_component_t *res_dst)
435 {
436   xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
437 #define ELEMENTS_FATHER_MAXDEPTH 16 /* increase if it is not enough */
438   routing_component_t src_as, dst_as;
439   routing_component_t path_src[ELEMENTS_FATHER_MAXDEPTH];
440   routing_component_t path_dst[ELEMENTS_FATHER_MAXDEPTH];
441   int index_src = 0;
442   int index_dst = 0;
443   routing_component_t current;
444   routing_component_t current_src;
445   routing_component_t current_dst;
446   routing_component_t father;
447
448   /* (1) find the as where the src and dst are located */
449   network_element_info_t src_data = xbt_lib_get_or_null(host_lib, src,
450       ROUTING_HOST_LEVEL);
451   network_element_info_t dst_data = xbt_lib_get_or_null(host_lib, dst,
452       ROUTING_HOST_LEVEL);
453   if (!src_data)
454     src_data = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
455   if (!dst_data)
456     dst_data = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
457   src_as = src_data->rc_component;
458   dst_as = dst_data->rc_component;
459
460   xbt_assert(src_as && dst_as,
461       "Ask for route \"from\"(%s) or \"to\"(%s) no found", src, dst);
462
463   /* (2) find the path to the root routing component */
464   for (current = src_as ; current != NULL ; current = current->routing_father) {
465     if (index_src >= ELEMENTS_FATHER_MAXDEPTH)
466       xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_src");
467     path_src[index_src++] = current;
468   }
469   for (current = dst_as ; current != NULL ; current = current->routing_father) {
470     if (index_dst >= ELEMENTS_FATHER_MAXDEPTH)
471       xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_dst");
472     path_dst[index_dst++] = current;
473   }
474
475   /* (3) find the common father */
476   do {
477     current_src = path_src[--index_src];
478     current_dst = path_dst[--index_dst];
479   } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
480
481   /* (4) they are not in the same routing component, make the path */
482   if (current_src == current_dst)
483     father = current_src;
484   else
485     father = path_src[index_src + 1];
486
487   /* (5) result generation */
488   *res_father = father;         /* first the common father of src and dst */
489   *res_src = current_src;       /* second the first different father of src */
490   *res_dst = current_dst;       /* three  the first different father of dst */
491
492 #undef ELEMENTS_FATHER_MAXDEPTH
493 }
494
495 /* Global Business methods */
496
497 /**
498  * \brief Recursive function for get_route_latency
499  *
500  * \param src the source host name 
501  * \param dst the destination host name
502  * \param *e_route the route where the links are stored
503  * \param *latency the latency, if needed
504  * 
505  * This function is called by "get_route" and "get_latency". It allows to walk
506  * recursively through the routing components tree.
507  */
508 static void _get_route_latency(const char *src, const char *dst,
509     xbt_dynar_t *route, double *latency)
510 {
511   XBT_DEBUG("Solve route/latency  \"%s\" to \"%s\"", src, dst);
512   xbt_assert(src && dst, "bad parameters for \"_get_route_latency\" method");
513
514   routing_component_t common_father;
515   routing_component_t src_father;
516   routing_component_t dst_father;
517   elements_father(src, dst, &common_father, &src_father, &dst_father);
518
519   if (src_father == dst_father) { /* SURF_ROUTING_BASE */
520
521     route_extended_t e_route = NULL;
522     if (route) {
523       e_route = common_father->get_route(common_father, src, dst);
524       xbt_assert(e_route, "no route between \"%s\" and \"%s\"", src, dst);
525       *route = e_route->generic_route.link_list;
526     }
527     if (latency) {
528       *latency = common_father->get_latency(common_father, src, dst, e_route);
529       xbt_assert(*latency >= 0.0,
530           "latency error on route between \"%s\" and \"%s\"", src, dst);
531     }
532     if (e_route) {
533       xbt_free(e_route->src_gateway);
534       xbt_free(e_route->dst_gateway);
535       xbt_free(e_route);
536     }
537
538   } else {                      /* SURF_ROUTING_RECURSIVE */
539
540     route_extended_t e_route_bypass = NULL;
541     if (common_father->get_bypass_route)
542       e_route_bypass = common_father->get_bypass_route(common_father, src, dst);
543
544     xbt_assert(!latency || !e_route_bypass,
545         "Bypass cannot work yet with get_latency");
546
547     route_extended_t e_route_cnt = e_route_bypass
548         ? e_route_bypass
549             : common_father->get_route(common_father,
550                 src_father->name, dst_father->name);
551
552     xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
553         src_father->name, dst_father->name);
554
555     xbt_assert((e_route_cnt->src_gateway == NULL) ==
556         (e_route_cnt->dst_gateway == NULL),
557         "bad gateway for route between \"%s\" and \"%s\"", src, dst);
558
559     if (route) {
560       *route = xbt_dynar_new(global_routing->size_of_link, NULL);
561     }
562     if (latency) {
563       *latency = common_father->get_latency(common_father,
564           src_father->name, dst_father->name,
565           e_route_cnt);
566       xbt_assert(*latency >= 0.0,
567           "latency error on route between \"%s\" and \"%s\"",
568           src_father->name, dst_father->name);
569     }
570
571     void *link;
572     unsigned int cpt;
573
574     if (strcmp(src, e_route_cnt->src_gateway)) {
575       double latency_src;
576       xbt_dynar_t route_src;
577
578       _get_route_latency(src, e_route_cnt->src_gateway,
579           (route ? &route_src : NULL),
580           (latency ? &latency_src : NULL));
581       if (route) {
582         xbt_assert(route_src, "no route between \"%s\" and \"%s\"",
583             src, e_route_cnt->src_gateway);
584         xbt_dynar_foreach(route_src, cpt, link) {
585           xbt_dynar_push(*route, &link);
586         }
587         xbt_dynar_free(&route_src);
588       }
589       if (latency) {
590         xbt_assert(latency_src >= 0.0,
591             "latency error on route between \"%s\" and \"%s\"",
592             src, e_route_cnt->src_gateway);
593         *latency += latency_src;
594       }
595     }
596
597     if (route) {
598       xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
599         xbt_dynar_push(*route, &link);
600       }
601     }
602
603     if (strcmp(e_route_cnt->dst_gateway, dst)) {
604       double latency_dst;
605       xbt_dynar_t route_dst;
606
607       _get_route_latency(e_route_cnt->dst_gateway, dst,
608           (route ? &route_dst : NULL),
609           (latency ? &latency_dst : NULL));
610       if (route) {
611         xbt_assert(route_dst, "no route between \"%s\" and \"%s\"",
612             e_route_cnt->dst_gateway, dst);
613         xbt_dynar_foreach(route_dst, cpt, link) {
614           xbt_dynar_push(*route, &link);
615         }
616         xbt_dynar_free(&route_dst);
617       }
618       if (latency) {
619         xbt_assert(latency_dst >= 0.0,
620             "latency error on route between \"%s\" and \"%s\"",
621             e_route_cnt->dst_gateway, dst);
622         *latency += latency_dst;
623       }
624     }
625
626     generic_free_extended_route(e_route_cnt);
627   }
628 }
629
630 /**
631  * \brief Generic function for get_route, get_route_no_cleanup, and get_latency
632  */
633 static void get_route_latency(const char *src, const char *dst,
634     xbt_dynar_t *route, double *latency, int cleanup)
635 {
636   _get_route_latency(src, dst, route, latency);
637   xbt_assert(!route || *route, "no route between \"%s\" and \"%s\"", src, dst);
638   xbt_assert(!latency || *latency >= 0.0,
639       "latency error on route between \"%s\" and \"%s\"", src, dst);
640   if (route) {
641     xbt_dynar_free(&global_routing->last_route);
642     global_routing->last_route = cleanup ? *route : NULL;
643   }
644 }
645
646 /**
647  * \brief Generic method: find a route between hosts
648  *
649  * \param src the source host name 
650  * \param dst the destination host name
651  * 
652  * walk through the routing components tree and find a route between hosts
653  * by calling the differents "get_route" functions in each routing component.
654  * No need to free the returned dynar. It will be freed at the next call.
655  */
656 static xbt_dynar_t get_route(const char *src, const char *dst)
657 {
658   xbt_dynar_t route = NULL;
659   get_route_latency(src, dst, &route, NULL, 1);
660   return route;
661 }
662
663 /**
664  * \brief Generic method: find a route between hosts
665  *
666  * \param src the source host name
667  * \param dst the destination host name
668  *
669  * same as get_route, but return NULL if any exception is raised.
670  */
671 static xbt_dynar_t get_route_or_null(const char *src, const char *dst)
672 {
673   xbt_dynar_t route = NULL;
674   xbt_ex_t exception;
675   TRY {
676     get_route_latency(src, dst, &route, NULL, 1);
677   }CATCH(exception) {
678     xbt_ex_free(exception);
679     return NULL;
680   }
681   return route;
682 }
683
684 /**
685  * \brief Generic method: find a route between hosts
686  *
687  * \param src the source host name
688  * \param dst the destination host name
689  *
690  * walk through the routing components tree and find a route between hosts
691  * by calling the differents "get_route" functions in each routing component.
692  * Leaves the caller the responsability to clean the returned dynar.
693  */
694 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
695 {
696   xbt_dynar_t route = NULL;
697   get_route_latency(src, dst, &route, NULL, 0);
698   return route;
699 }
700
701 /*Get Latency*/
702 static double get_latency(const char *src, const char *dst)
703 {
704   double latency = -1.0;
705   get_route_latency(src, dst, NULL, &latency, 0);
706   return latency;
707 }
708
709 static int surf_parse_models_setup_already_called=0;
710 /* Call the last initialization functions, that must be called after the
711  * <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
712  */
713 void surf_parse_models_setup()
714 {
715   if (surf_parse_models_setup_already_called)
716     return;
717   surf_parse_models_setup_already_called=1;
718   surf_config_models_setup();
719 }
720
721
722 /**
723  * \brief Recursive function for finalize
724  *
725  * \param rc the source host name 
726  * 
727  * This fuction is call by "finalize". It allow to finalize the 
728  * AS or routing components. It delete all the structures.
729  */
730 static void _finalize(routing_component_t rc)
731 {
732   if (rc) {
733     xbt_dict_cursor_t cursor = NULL;
734     char *key;
735     routing_component_t elem;
736     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
737       _finalize(elem);
738     }
739     xbt_dict_t tmp_sons = rc->routing_sons;
740     char *tmp_name = rc->name;
741     xbt_dict_free(&tmp_sons);
742     xbt_free(tmp_name);
743     xbt_assert(rc->finalize, "no defined method \"finalize\" in \"%s\"",
744         current_routing->name);
745     (*(rc->finalize)) (rc);
746   }
747 }
748
749 /**
750  * \brief Generic method: delete all the routing structures
751  * 
752  * walk through the routing components tree and delete the structures
753  * by calling the differents "finalize" functions in each routing component
754  */
755 static void finalize(void)
756 {
757   /* delete recursively all the tree */
758   _finalize(global_routing->root);
759   /* delete last_route */
760   xbt_dynar_free(&(global_routing->last_route));
761   /* delete global routing structure */
762   xbt_free(global_routing);
763   /* make sure that we will reinit the models while loading the platf once reinited -- HACK but there is no proper surf_routing_init() */
764   surf_parse_models_setup_already_called = 0;
765 }
766
767 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
768 {
769   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
770
771   //adding my one link routes
772   unsigned int cpt;
773   void *link;
774   xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
775   if (onelink_mine) {
776     xbt_dynar_foreach(onelink_mine, cpt, link) {
777       xbt_dynar_push(ret, &link);
778     }
779   }
780   //recursing
781   char *key;
782   xbt_dict_cursor_t cursor = NULL;
783   routing_component_t rc_child;
784   xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
785     xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
786     if (onelink_child) {
787       xbt_dynar_foreach(onelink_child, cpt, link) {
788         xbt_dynar_push(ret, &link);
789       }
790     }
791   }
792   return ret;
793 }
794
795 static xbt_dynar_t get_onelink_routes(void)
796 {
797   return recursive_get_onelink_routes(global_routing->root);
798 }
799
800 e_surf_network_element_type_t get_network_element_type(const char
801     *name)
802 {
803   network_element_info_t rc = NULL;
804
805   rc = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
806   if(rc) return rc->rc_type;
807
808   rc = xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
809   if(rc) return rc->rc_type;
810
811   return SURF_NETWORK_ELEMENT_NULL;
812 }
813
814 /**
815  * \brief Generic method: create the global routing schema
816  * 
817  * Make a global routing structure and set all the parsing functions.
818  */
819 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
820 {
821   /* config the uniq global routing */
822   global_routing = xbt_new0(s_routing_global_t, 1);
823   global_routing->root = NULL;
824   global_routing->get_route = get_route;
825   global_routing->get_route_or_null = get_route_or_null;
826   global_routing->get_latency = get_latency;
827   global_routing->get_route_no_cleanup = get_route_no_cleanup;
828   global_routing->get_onelink_routes = get_onelink_routes;
829   global_routing->get_route_latency = get_route_latency;
830   global_routing->get_network_element_type = get_network_element_type;
831   global_routing->finalize = finalize;
832   global_routing->loopback = loopback;
833   global_routing->size_of_link = size_of_links;
834   global_routing->last_route = NULL;
835   get_link_latency = get_link_latency_fun;
836   /* no current routing at moment */
837   current_routing = NULL;
838 }
839
840 void routing_register_callbacks() {
841   sg_platf_host_add_cb(parse_S_host);
842   sg_platf_router_add_cb(parse_S_router);
843
844   surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
845
846   surfxml_add_callback(STag_surfxml_route_cb_list,
847       &parse_S_route_new_and_endpoints_XML);
848   surfxml_add_callback(STag_surfxml_ASroute_cb_list,
849       &parse_S_ASroute_new_and_endpoints);
850   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
851       &parse_S_bypassRoute_new_and_endpoints);
852
853   surfxml_add_callback(ETag_surfxml_link_ctn_cb_list, &routing_parse_link_ctn);
854
855   surfxml_add_callback(ETag_surfxml_route_cb_list,
856       &parse_E_route_store_route);
857   surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
858       &parse_E_ASroute_store_route);
859   surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
860       &parse_E_bypassRoute_store_route);
861
862   surfxml_add_callback(STag_surfxml_cluster_cb_list,
863       &routing_parse_Scluster);
864
865   sg_platf_peer_add_cb(routing_parse_Speer); // FIXME: inline in the sg_platf_new_peer instead
866   sg_platf_postparse_add_cb(clean_routing_after_parse);
867
868 #ifdef HAVE_TRACING
869   instr_routing_define_callbacks();
870 #endif
871 }
872
873 /* ************************************************** */
874 /* ********** PATERN FOR NEW ROUTING **************** */
875
876 /* The minimal configuration of a new routing model need the next functions,
877  * also you need to set at the start of the file, the new model in the model
878  * list. Remember keep the null ending of the list.
879  */
880 /*** Routing model structure ***/
881 // typedef struct {
882 //   s_routing_component_t generic_routing;
883 //   /* things that your routing model need */
884 // } s_routing_component_NEW_t,*routing_component_NEW_t;
885
886 /*** Parse routing model functions ***/
887 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
888 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
889 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
890 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
891 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
892
893 /*** Business methods ***/
894 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
895 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
896 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
897
898 /*** Creation routing model functions ***/
899 // static void* model_NEW_create(void) {
900 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
901 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
902 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
903 //   new_component->generic_routing.set_route = model_NEW_set_route;
904 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
905 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
906 //   new_component->generic_routing.get_route = NEW_get_route;
907 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
908 //   new_component->generic_routing.finalize = NEW_finalize;
909 //   /* initialization of internal structures */
910 //   return new_component;
911 // } /* mandatory */
912 // static void  model_NEW_load(void) {}   /* mandatory */
913 // static void  model_NEW_unload(void) {} /* mandatory */
914 // static void  model_NEW_end(void) {}    /* mandatory */
915
916 /* ************************************************************************** */
917 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
918
919 void generic_set_processing_unit(routing_component_t rc,
920     const char *name)
921 {
922   XBT_DEBUG("Load process unit \"%s\"", name);
923   int *id = xbt_new0(int, 1);
924   xbt_dict_t _to_index;
925   _to_index = current_routing->to_index;
926   *id = xbt_dict_length(_to_index);
927   xbt_dict_set(_to_index, name, id, xbt_free);
928 }
929
930 void generic_set_autonomous_system(routing_component_t rc,
931     const char *name)
932 {
933   XBT_DEBUG("Load Autonomous system \"%s\"", name);
934   int *id = xbt_new0(int, 1);
935   xbt_dict_t _to_index;
936   _to_index = current_routing->to_index;
937   *id = xbt_dict_length(_to_index);
938   xbt_dict_set(_to_index, name, id, xbt_free);
939 }
940
941 int surf_pointer_resource_cmp(const void *a, const void *b)
942 {
943   return a != b;
944 }
945
946 int surf_link_resource_cmp(const void *a, const void *b)
947 {
948   return !!memcmp(a,b,global_routing->size_of_link);
949 }
950
951 void generic_set_bypassroute(routing_component_t rc,
952     const char *src, const char *dst,
953     route_extended_t e_route)
954 {
955   XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
956   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
957   char *route_name;
958
959   route_name = bprintf("%s#%s", src, dst);
960   xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
961       "Invalid count of links, must be greater than zero (%s,%s)",
962       src, dst);
963   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
964       "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
965       src, e_route->src_gateway, dst, e_route->dst_gateway);
966
967   route_extended_t new_e_route =
968       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
969   xbt_dynar_free(&(e_route->generic_route.link_list));
970   xbt_free(e_route);
971
972   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
973       (void (*)(void *)) generic_free_extended_route);
974   xbt_free(route_name);
975 }
976
977 /* ************************************************************************** */
978 /* *********************** GENERIC BUSINESS METHODS ************************* */
979
980 double generic_get_link_latency(routing_component_t rc,
981     const char *src, const char *dst,
982     route_extended_t route)
983 {
984   int need_to_clean = route?0:1;
985   void * link;
986   unsigned int i;
987   double latency = 0.0;
988
989   route = route?route:rc->get_route(rc,src,dst);
990
991   xbt_dynar_foreach(route->generic_route.link_list,i,link) {
992     latency += get_link_latency(link);
993   }
994   if(need_to_clean) generic_free_extended_route(route);
995   return latency;
996 }
997
998 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
999 {
1000   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
1001 }
1002
1003 route_extended_t generic_get_bypassroute(routing_component_t rc,
1004     const char *src,
1005     const char *dst)
1006 {
1007   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1008   routing_component_t src_as, dst_as;
1009   int index_src, index_dst;
1010   xbt_dynar_t path_src = NULL;
1011   xbt_dynar_t path_dst = NULL;
1012   routing_component_t current = NULL;
1013   routing_component_t *current_src = NULL;
1014   routing_component_t *current_dst = NULL;
1015
1016   /* (1) find the as where the src and dst are located */
1017   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1018   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1019   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1020   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1021
1022   if(src_data == NULL || dst_data == NULL)
1023     xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1024         src, dst, rc->name);
1025
1026   src_as = ((network_element_info_t)src_data)->rc_component;
1027   dst_as = ((network_element_info_t)dst_data)->rc_component;
1028
1029   /* (2) find the path to the root routing component */
1030   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1031   current = src_as;
1032   while (current != NULL) {
1033     xbt_dynar_push(path_src, &current);
1034     current = current->routing_father;
1035   }
1036   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1037   current = dst_as;
1038   while (current != NULL) {
1039     xbt_dynar_push(path_dst, &current);
1040     current = current->routing_father;
1041   }
1042
1043   /* (3) find the common father */
1044   index_src = path_src->used - 1;
1045   index_dst = path_dst->used - 1;
1046   current_src = xbt_dynar_get_ptr(path_src, index_src);
1047   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1048   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1049     xbt_dynar_pop_ptr(path_src);
1050     xbt_dynar_pop_ptr(path_dst);
1051     index_src--;
1052     index_dst--;
1053     current_src = xbt_dynar_get_ptr(path_src, index_src);
1054     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1055   }
1056
1057   int max_index_src = path_src->used - 1;
1058   int max_index_dst = path_dst->used - 1;
1059
1060   int max_index = max(max_index_src, max_index_dst);
1061   int i, max;
1062
1063   route_extended_t e_route_bypass = NULL;
1064
1065   for (max = 0; max <= max_index; max++) {
1066     for (i = 0; i < max; i++) {
1067       if (i <= max_index_src && max <= max_index_dst) {
1068         char *route_name = bprintf("%s#%s",
1069             (*(routing_component_t *)
1070                 (xbt_dynar_get_ptr
1071                     (path_src, i)))->name,
1072                     (*(routing_component_t *)
1073                         (xbt_dynar_get_ptr
1074                             (path_dst, max)))->name);
1075         e_route_bypass =
1076             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1077         xbt_free(route_name);
1078       }
1079       if (e_route_bypass)
1080         break;
1081       if (max <= max_index_src && i <= max_index_dst) {
1082         char *route_name = bprintf("%s#%s",
1083             (*(routing_component_t *)
1084                 (xbt_dynar_get_ptr
1085                     (path_src, max)))->name,
1086                     (*(routing_component_t *)
1087                         (xbt_dynar_get_ptr
1088                             (path_dst, i)))->name);
1089         e_route_bypass =
1090             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1091         xbt_free(route_name);
1092       }
1093       if (e_route_bypass)
1094         break;
1095     }
1096
1097     if (e_route_bypass)
1098       break;
1099
1100     if (max <= max_index_src && max <= max_index_dst) {
1101       char *route_name = bprintf("%s#%s",
1102           (*(routing_component_t *)
1103               (xbt_dynar_get_ptr
1104                   (path_src, max)))->name,
1105                   (*(routing_component_t *)
1106                       (xbt_dynar_get_ptr
1107                           (path_dst, max)))->name);
1108       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1109       xbt_free(route_name);
1110     }
1111     if (e_route_bypass)
1112       break;
1113   }
1114
1115   xbt_dynar_free(&path_src);
1116   xbt_dynar_free(&path_dst);
1117
1118   route_extended_t new_e_route = NULL;
1119
1120   if (e_route_bypass) {
1121     void *link;
1122     unsigned int cpt = 0;
1123     new_e_route = xbt_new0(s_route_extended_t, 1);
1124     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1125     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1126     new_e_route->generic_route.link_list =
1127         xbt_dynar_new(global_routing->size_of_link, NULL);
1128     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1129       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1130     }
1131   }
1132
1133   return new_e_route;
1134 }
1135
1136 /* ************************************************************************** */
1137 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1138
1139 route_t
1140 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1141     void *data, int order)
1142 {
1143
1144   char *link_name;
1145   route_t new_route;
1146   unsigned int cpt;
1147   xbt_dynar_t links = NULL, links_id = NULL;
1148
1149   new_route = xbt_new0(s_route_t, 1);
1150   new_route->link_list =
1151       xbt_dynar_new(global_routing->size_of_link, NULL);
1152
1153   xbt_assert(hierarchy == SURF_ROUTING_BASE,
1154       "the hierarchy type is not SURF_ROUTING_BASE");
1155
1156   links = ((route_t) data)->link_list;
1157
1158
1159   links_id = new_route->link_list;
1160
1161   xbt_dynar_foreach(links, cpt, link_name) {
1162
1163     void *link =
1164         xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1165     if (link) {
1166       if (order)
1167         xbt_dynar_push(links_id, &link);
1168       else
1169         xbt_dynar_unshift(links_id, &link);
1170     } else
1171       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1172   }
1173
1174   return new_route;
1175 }
1176
1177 route_extended_t
1178 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1179     void *data, int order)
1180 {
1181
1182   char *link_name;
1183   route_extended_t e_route, new_e_route;
1184   route_t route;
1185   unsigned int cpt;
1186   xbt_dynar_t links = NULL, links_id = NULL;
1187
1188   new_e_route = xbt_new0(s_route_extended_t, 1);
1189   new_e_route->generic_route.link_list =
1190       xbt_dynar_new(global_routing->size_of_link, NULL);
1191   new_e_route->src_gateway = NULL;
1192   new_e_route->dst_gateway = NULL;
1193
1194   xbt_assert(hierarchy == SURF_ROUTING_BASE
1195       || hierarchy == SURF_ROUTING_RECURSIVE,
1196       "the hierarchy type is not defined");
1197
1198   if (hierarchy == SURF_ROUTING_BASE) {
1199
1200     route = (route_t) data;
1201     links = route->link_list;
1202
1203   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1204
1205     e_route = (route_extended_t) data;
1206     xbt_assert(e_route->src_gateway
1207         && e_route->dst_gateway, "bad gateway, is null");
1208     links = e_route->generic_route.link_list;
1209
1210     /* remeber not erase the gateway names */
1211     new_e_route->src_gateway = strdup(e_route->src_gateway);
1212     new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1213   }
1214
1215   links_id = new_e_route->generic_route.link_list;
1216
1217   xbt_dynar_foreach(links, cpt, link_name) {
1218
1219     void *link =
1220         xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1221     if (link) {
1222       if (order)
1223         xbt_dynar_push(links_id, &link);
1224       else
1225         xbt_dynar_unshift(links_id, &link);
1226     } else
1227       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1228   }
1229
1230   return new_e_route;
1231 }
1232
1233 void generic_free_route(route_t route)
1234 {
1235   if (route) {
1236     xbt_dynar_free(&(route->link_list));
1237     xbt_free(route);
1238   }
1239 }
1240
1241 void generic_free_extended_route(route_extended_t e_route)
1242 {
1243   if (e_route) {
1244     xbt_dynar_free(&(e_route->generic_route.link_list));
1245     if (e_route->src_gateway)
1246       xbt_free(e_route->src_gateway);
1247     if (e_route->dst_gateway)
1248       xbt_free(e_route->dst_gateway);
1249     xbt_free(e_route);
1250   }
1251 }
1252
1253 static routing_component_t generic_as_exist(routing_component_t find_from,
1254     routing_component_t to_find)
1255 {
1256   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1257   xbt_dict_cursor_t cursor = NULL;
1258   char *key;
1259   int found = 0;
1260   routing_component_t elem;
1261   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1262     if (to_find == elem || generic_as_exist(elem, to_find)) {
1263       found = 1;
1264       break;
1265     }
1266   }
1267   if (found)
1268     return to_find;
1269   return NULL;
1270 }
1271
1272 routing_component_t
1273 generic_autonomous_system_exist(routing_component_t rc, char *element)
1274 {
1275   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1276   routing_component_t element_as, result, elem;
1277   xbt_dict_cursor_t cursor = NULL;
1278   char *key;
1279   element_as = ((network_element_info_t)
1280       xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1281   result = ((routing_component_t) - 1);
1282   if (element_as != rc)
1283     result = generic_as_exist(rc, element_as);
1284
1285   int found = 0;
1286   if (result) {
1287     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1288       found = !strcmp(elem->name, element);
1289       if (found)
1290         break;
1291     }
1292     if (found)
1293       return element_as;
1294   }
1295   return NULL;
1296 }
1297
1298 routing_component_t
1299 generic_processing_units_exist(routing_component_t rc, char *element)
1300 {
1301   routing_component_t element_as;
1302   element_as = ((network_element_info_t)
1303       xbt_lib_get_or_null(host_lib,
1304           element, ROUTING_HOST_LEVEL))->rc_component;
1305   if (element_as == rc)
1306     return element_as;
1307   return generic_as_exist(rc, element_as);
1308 }
1309
1310 void generic_src_dst_check(routing_component_t rc, const char *src,
1311     const char *dst)
1312 {
1313
1314   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1315   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1316   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1317   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1318
1319   if(src_data == NULL || dst_data == NULL)
1320     xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1321         src, dst, rc->name);
1322
1323   routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1324   routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1325
1326   if(src_as != dst_as)
1327     xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1328         src, src_as->name, dst, dst_as->name);
1329   if(rc != dst_as)
1330     xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1331         src,dst,src_as->name, dst_as->name,rc->name);
1332 }
1333
1334 void routing_parse_Scluster(void) {
1335   char *host_id, *groups, *link_id = NULL;
1336
1337   s_sg_platf_host_cbarg_t host;
1338   s_sg_platf_link_cbarg_t link;
1339
1340   if( strcmp(struct_cluster->availability_trace,"")
1341       || strcmp(struct_cluster->state_trace,"") )
1342   {
1343     if(xbt_dict_size(patterns)==0)
1344       patterns = xbt_dict_new();
1345     xbt_dict_set(patterns,"id",xbt_strdup(struct_cluster->id),free);
1346     xbt_dict_set(patterns,"prefix",xbt_strdup(struct_cluster->prefix),free);
1347     xbt_dict_set(patterns,"suffix",xbt_strdup(struct_cluster->suffix),free);
1348   }
1349
1350   unsigned int iter;
1351   int start, end, i;
1352   xbt_dynar_t radical_elements;
1353   xbt_dynar_t radical_ends;
1354
1355   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", struct_cluster->id);
1356   sg_platf_new_AS_open(struct_cluster->id,"Cluster");
1357
1358   //Make all hosts
1359   radical_elements = xbt_str_split(struct_cluster->radical, ",");
1360   xbt_dynar_foreach(radical_elements, iter, groups) {
1361     memset(&host,0,sizeof(host));
1362
1363     radical_ends = xbt_str_split(groups, "-");
1364     switch (xbt_dynar_length(radical_ends)) {
1365     case 1:
1366       start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1367       host_id = bprintf("%s%d%s", struct_cluster->prefix, start, struct_cluster->suffix);
1368       link_id = bprintf("%s_link_%d", struct_cluster->id, start);
1369
1370       XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->power);
1371       host.id = host_id;
1372       if(strcmp(struct_cluster->availability_trace,"")){
1373         xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1374         char* tmp_availability_file = xbt_strdup(struct_cluster->availability_trace);
1375         xbt_str_varsubst(tmp_availability_file,patterns);
1376         XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1377         host.power_trace = tmgr_trace_new(tmp_availability_file);
1378         xbt_free(tmp_availability_file);
1379       }
1380       else
1381       {
1382         XBT_DEBUG("\tavailability_file=\"\"");
1383       }
1384       if(strcmp(struct_cluster->state_trace,"")){
1385         char *tmp_state_file = xbt_strdup(struct_cluster->state_trace);
1386         xbt_str_varsubst(tmp_state_file,patterns);
1387         XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1388         host.state_trace = tmgr_trace_new(tmp_state_file);
1389         xbt_free(tmp_state_file);
1390       }
1391       else
1392       {
1393         XBT_DEBUG("\tstate_file=\"\"");
1394       }
1395
1396       host.power_peak = struct_cluster->power;
1397       host.power_scale = 1.0;
1398       host.core_amount = struct_cluster->core_amount;
1399       host.initial_state = SURF_RESOURCE_ON;
1400       host.coord = "";
1401       sg_platf_new_host(&host);
1402       XBT_DEBUG("</host>");
1403
1404
1405       XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->bw, struct_cluster->lat);
1406
1407       memset(&link,0,sizeof(link));
1408       link.id = link_id;
1409       link.bandwidth = struct_cluster->bw;
1410       link.latency = struct_cluster->lat;
1411       link.state = SURF_RESOURCE_ON;
1412
1413       switch (struct_cluster->sharing_policy) {
1414       case A_surfxml_cluster_sharing_policy_FATPIPE:
1415         link.policy = SURF_LINK_FATPIPE;
1416         break;
1417       case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
1418         link.policy = SURF_LINK_FULLDUPLEX;
1419         break;
1420       default:
1421         link.policy = SURF_LINK_SHARED;
1422       }
1423
1424       sg_platf_new_link(&link);
1425
1426       surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1427       if (struct_cluster->sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX){
1428         char* tmp_link =  bprintf("%s_UP",link_id);
1429         info->link_up   = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1430         free(tmp_link);
1431         tmp_link =  bprintf("%s_DOWN",link_id);
1432         info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1433         free(tmp_link);
1434       }
1435       else{
1436         info->link_up   = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1437         info->link_down = info->link_up;
1438       }
1439       surf_routing_cluster_add_link(host_id,info);
1440       xbt_free(link_id);
1441       xbt_free(host_id);
1442
1443       break;
1444
1445       case 2:
1446
1447         start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1448         end=  surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1449         for (i = start; i <= end; i++) {
1450           host_id = bprintf("%s%d%s", struct_cluster->prefix, i, struct_cluster->suffix);
1451           link_id = bprintf("%s_link_%d", struct_cluster->id, i);
1452
1453           XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->power);
1454           host.id = host_id;
1455           if(strcmp(struct_cluster->availability_trace,"")){
1456             xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1457             char* tmp_availability_file = xbt_strdup(struct_cluster->availability_trace);
1458             xbt_str_varsubst(tmp_availability_file,patterns);
1459             XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1460             host.power_trace = tmgr_trace_new(tmp_availability_file);
1461             xbt_free(tmp_availability_file);
1462           }
1463           else
1464           {
1465             XBT_DEBUG("\tavailability_file=\"\"");
1466           }
1467           if(strcmp(struct_cluster->state_trace,"")){
1468             char *tmp_state_file = xbt_strdup(struct_cluster->state_trace);
1469             xbt_str_varsubst(tmp_state_file,patterns);
1470             XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1471             host.state_trace = tmgr_trace_new(tmp_state_file);
1472             xbt_free(tmp_state_file);
1473           }
1474           else
1475           {
1476             XBT_DEBUG("\tstate_file=\"\"");
1477           }
1478
1479           host.power_peak = struct_cluster->power;
1480           host.power_scale = 1.0;
1481           host.core_amount = struct_cluster->core_amount;
1482           host.initial_state = SURF_RESOURCE_ON;
1483           host.coord = "";
1484           sg_platf_new_host(&host);
1485           XBT_DEBUG("</host>");
1486
1487           XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->bw, struct_cluster->lat);
1488
1489           memset(&link,0,sizeof(link));
1490           link.id = link_id;
1491           link.bandwidth = struct_cluster->bw;
1492           link.latency = struct_cluster->lat;
1493           link.state = SURF_RESOURCE_ON;
1494
1495           switch (struct_cluster->sharing_policy) {
1496           case A_surfxml_cluster_sharing_policy_SHARED:
1497             link.policy = SURF_LINK_SHARED;
1498             break;
1499           case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
1500             link.policy = SURF_LINK_FULLDUPLEX;
1501             break;
1502           case A_surfxml_cluster_sharing_policy_FATPIPE:
1503             link.policy = SURF_LINK_FATPIPE;
1504             break;
1505           default:
1506             surf_parse_error(bprintf("Invalid cluster sharing policy for cluster %s",struct_cluster->id));
1507           }
1508           sg_platf_new_link(&link);
1509
1510           surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1511           if (link.policy == SURF_LINK_FULLDUPLEX) {
1512             char* tmp_link =  bprintf("%s_UP",link_id);
1513             info->link_up   = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1514             free(tmp_link);
1515             tmp_link =  bprintf("%s_DOWN",link_id);
1516             info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1517             free(tmp_link);
1518           }
1519           else{
1520             info->link_up   = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1521             info->link_down = info->link_up;
1522           }
1523           surf_routing_cluster_add_link(host_id,info);
1524
1525           xbt_free(link_id);
1526           xbt_free(host_id);
1527
1528         }
1529         break;
1530
1531       default:
1532         XBT_DEBUG("Malformed radical");
1533         break;
1534     }
1535
1536     xbt_dynar_free(&radical_ends);
1537   }
1538   xbt_dynar_free(&radical_elements);
1539
1540   // Add a router. It is magically used thanks to the way in which surf_routing_cluster is written, and it's very useful to connect clusters together
1541   XBT_DEBUG(" ");
1542   XBT_DEBUG("<router id=\"%s\"/>", struct_cluster->router_id);
1543   s_sg_platf_router_cbarg_t router;
1544   char *newid=NULL;
1545   memset(&router,0,sizeof(router));
1546   router.id = struct_cluster->router_id;
1547   router.coord = "";
1548   if (!router.id || !strcmp(router.id,""))
1549     router.id = newid = bprintf("%s%s_router%s", struct_cluster->prefix, struct_cluster->id, struct_cluster->suffix);
1550   sg_platf_new_router(&router);
1551   if (newid)
1552     free(newid);
1553
1554   //Make the backbone
1555   if( (struct_cluster->bb_bw!= 0)  && (struct_cluster->bb_lat!=0)  ){
1556     char *link_backbone = bprintf("%s_backbone", struct_cluster->id);
1557     XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,struct_cluster->bb_bw, struct_cluster->bb_lat);
1558
1559     memset(&link,0,sizeof(link));
1560     link.id = link_backbone;
1561     link.bandwidth = struct_cluster->bb_bw;
1562     link.latency = struct_cluster->bb_lat;
1563     link.state = SURF_RESOURCE_ON;
1564
1565     switch (struct_cluster->bb_sharing_policy) {
1566     case A_surfxml_cluster_bb_sharing_policy_FATPIPE:
1567       link.policy = SURF_LINK_FATPIPE;
1568       break;
1569     case A_surfxml_cluster_bb_sharing_policy_SHARED:
1570       link.policy = SURF_LINK_SHARED;
1571       break;
1572     default:
1573       surf_parse_error(bprintf("Invalid bb sharing policy in cluster %s",struct_cluster->id));
1574     }
1575
1576     sg_platf_new_link(&link);
1577
1578     surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1579     info->link_up   = xbt_lib_get_or_null(link_lib, link_backbone, SURF_LINK_LEVEL);
1580     info->link_down = info->link_up;
1581     surf_routing_cluster_add_link(struct_cluster->id, info);
1582
1583     free(link_backbone);
1584   }
1585
1586   XBT_DEBUG(" ");
1587
1588   char *new_suffix = xbt_strdup("");
1589
1590   radical_elements = xbt_str_split(struct_cluster->suffix, ".");
1591   xbt_dynar_foreach(radical_elements, iter, groups) {
1592     if (strcmp(groups, "")) {
1593       char *old_suffix = new_suffix;
1594       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1595       free(old_suffix);
1596     }
1597   }
1598
1599   xbt_dynar_free(&radical_elements);
1600   xbt_free(new_suffix);
1601
1602   if( strcmp(struct_cluster->availability_trace,"")
1603       || strcmp(struct_cluster->state_trace,"") )
1604     xbt_dict_free(&patterns);
1605
1606   XBT_DEBUG("</AS>");
1607   sg_platf_new_AS_close();
1608   XBT_DEBUG(" ");
1609 }
1610 /*
1611  * This function take a string and replace parameters from patterns dict.
1612  * It returns the new value.
1613  */
1614 static char* replace_random_parameter(char * string)
1615                                         {
1616   char *test_string = NULL;
1617
1618   if(xbt_dict_size(random_value)==0)
1619     return string;
1620
1621   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1622   test_string = bprintf("${%s}", string);
1623   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1624
1625   if (strcmp(test_string,"")) { //if not empty, keep this value.
1626     xbt_free(string);
1627     string = test_string;
1628   } //In other case take old value (without ${})
1629   else
1630     free(test_string);
1631   return string;
1632                                         }
1633
1634 static void clean_routing_after_parse(void)
1635 {
1636   xbt_dict_free(&random_value);
1637   xbt_dict_free(&patterns);
1638 }
1639
1640 static void routing_parse_Speer(sg_platf_peer_cbarg_t peer)
1641 {
1642   static int AX_ptr = 0;
1643   char *host_id = NULL;
1644   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1645
1646   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1647   static unsigned int surfxml_buffer_stack_stack[1024];
1648
1649   surfxml_buffer_stack_stack[0] = 0;
1650
1651   surfxml_bufferstack_push(1);
1652
1653   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", peer->id);
1654   sg_platf_new_AS_open(peer->id, "Full");
1655
1656   XBT_DEBUG(" ");
1657   host_id = HOST_PEER(peer->id);
1658   router_id = ROUTER_PEER(peer->id);
1659   link_id_up = LINK_UP_PEER(peer->id);
1660   link_id_down = LINK_DOWN_PEER(peer->id);
1661
1662   link_router = bprintf("%s_link_router", peer->id);
1663   link_backbone = bprintf("%s_backbone", peer->id);
1664
1665   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->power);
1666   s_sg_platf_host_cbarg_t host;
1667   memset(&host,0,sizeof(host));
1668   host.initial_state = SURF_RESOURCE_ON;
1669   host.id = host_id;
1670   host.power_peak = peer->power;
1671   host.power_scale = 1.0;
1672   host.power_trace = peer->availability_trace;
1673   host.state_trace = peer->state_trace;
1674   host.core_amount = 1;
1675   sg_platf_new_host(&host);
1676
1677
1678   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, peer->coord);
1679   s_sg_platf_router_cbarg_t router;
1680   memset(&router,0,sizeof(router));
1681   router.id = router_id;
1682   router.coord = peer->coord;
1683   sg_platf_new_router(&router);
1684
1685   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id_up, peer->bw_in, peer->lat);
1686   s_sg_platf_link_cbarg_t link;
1687   memset(&link,0,sizeof(link));
1688   link.state = SURF_RESOURCE_ON;
1689   link.policy = SURF_LINK_SHARED;
1690   link.id = link_id_up;
1691   link.bandwidth = peer->bw_in;
1692   link.latency = peer->lat;
1693   sg_platf_new_link(&link);
1694
1695   // FIXME: dealing with full duplex is not the role of this piece of code, I'd say [Mt]
1696   // Instead, it should be created fullduplex, and the models will do what's needed in this case
1697   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id_down, peer->bw_out, peer->lat);
1698   link.id = link_id_down;
1699   sg_platf_new_link(&link);
1700
1701   XBT_DEBUG(" ");
1702
1703   // begin here
1704   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1705   XBT_DEBUG("symmetrical=\"NO\">");
1706   SURFXML_BUFFER_SET(route_src, host_id);
1707   SURFXML_BUFFER_SET(route_dst, router_id);
1708   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1709   SURFXML_START_TAG(route);
1710
1711   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1712   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1713   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1714   SURFXML_START_TAG(link_ctn);
1715   SURFXML_END_TAG(link_ctn);
1716
1717   XBT_DEBUG("</route>");
1718   SURFXML_END_TAG(route);
1719
1720   //Opposite Route
1721   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1722   XBT_DEBUG("symmetrical=\"NO\">");
1723   SURFXML_BUFFER_SET(route_src, router_id);
1724   SURFXML_BUFFER_SET(route_dst, host_id);
1725   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1726   SURFXML_START_TAG(route);
1727
1728   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
1729   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
1730   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1731   SURFXML_START_TAG(link_ctn);
1732   SURFXML_END_TAG(link_ctn);
1733
1734   XBT_DEBUG("</route>");
1735   SURFXML_END_TAG(route);
1736
1737   XBT_DEBUG("</AS>");
1738   sg_platf_new_AS_close();
1739   XBT_DEBUG(" ");
1740
1741   //xbt_dynar_free(&tab_elements_num);
1742   free(host_id);
1743   free(router_id);
1744   free(link_router);
1745   free(link_backbone);
1746   free(link_id_up);
1747   free(link_id_down);
1748   surfxml_bufferstack_pop(1);
1749 }
1750
1751 static void routing_parse_Srandom(void)
1752 {
1753   double mean, std, min, max, seed;
1754   char *random_id = A_surfxml_random_id;
1755   char *random_radical = A_surfxml_random_radical;
1756   char *rd_name = NULL;
1757   char *rd_value;
1758   mean = surf_parse_get_double(A_surfxml_random_mean);
1759   std  = surf_parse_get_double(A_surfxml_random_std_deviation);
1760   min  = surf_parse_get_double(A_surfxml_random_min);
1761   max  = surf_parse_get_double(A_surfxml_random_max);
1762   seed = surf_parse_get_double(A_surfxml_random_seed);
1763
1764   double res = 0;
1765   int i = 0;
1766   random_data_t random = xbt_new0(s_random_data_t, 1);
1767   char *tmpbuf;
1768
1769   xbt_dynar_t radical_elements;
1770   unsigned int iter;
1771   char *groups;
1772   int start, end;
1773   xbt_dynar_t radical_ends;
1774
1775   random->generator = A_surfxml_random_generator;
1776   random->seed = seed;
1777   random->min = min;
1778   random->max = max;
1779
1780   /* Check user stupidities */
1781   if (max < min)
1782     THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
1783   if (mean < min)
1784     THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
1785         min);
1786   if (mean > max)
1787     THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
1788         max);
1789
1790   /* normalize the mean and standard deviation before storing */
1791   random->mean = (mean - min) / (max - min);
1792   random->std = std / (max - min);
1793
1794   if (random->mean * (1 - random->mean) < random->std * random->std)
1795     THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
1796         random->mean, random->std);
1797
1798   XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
1799       random_id,
1800       random->min,
1801       random->max,
1802       random->mean,
1803       random->std,
1804       random->generator,
1805       random->seed,
1806       random_radical);
1807
1808   if(xbt_dict_size(random_value)==0)
1809     random_value = xbt_dict_new();
1810
1811   if(!strcmp(random_radical,""))
1812   {
1813     res = random_generate(random);
1814     rd_value = bprintf("%f",res);
1815     xbt_dict_set(random_value, random_id, rd_value, free);
1816   }
1817   else
1818   {
1819     radical_elements = xbt_str_split(random_radical, ",");
1820     xbt_dynar_foreach(radical_elements, iter, groups) {
1821       radical_ends = xbt_str_split(groups, "-");
1822       switch (xbt_dynar_length(radical_ends)) {
1823       case 1:
1824         xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
1825         res = random_generate(random);
1826         tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
1827         xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1828         xbt_free(tmpbuf);
1829         break;
1830
1831       case 2:
1832         start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1833         end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1834         for (i = start; i <= end; i++) {
1835           xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
1836           res = random_generate(random);
1837           tmpbuf = bprintf("%s%d",random_id,i);
1838           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1839           xbt_free(tmpbuf);
1840         }
1841         break;
1842       default:
1843         XBT_INFO("Malformed radical");
1844         break;
1845       }
1846       res = random_generate(random);
1847       rd_name  = bprintf("%s_router",random_id);
1848       rd_value = bprintf("%f",res);
1849       xbt_dict_set(random_value, rd_name, rd_value, free);
1850
1851       xbt_dynar_free(&radical_ends);
1852     }
1853     free(rd_name);
1854     xbt_dynar_free(&radical_elements);
1855   }
1856 }