Logo AND Algorithmique Numérique Distribuée

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