Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix supernovae compilation
[simgrid.git] / src / surf / surf_routing.c
1 /* Copyright (c) 2009, 2010. 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 #ifdef HAVE_PCRE_LIB
8 #include <pcre.h>               /* regular expression library */
9 #endif
10 #include "surf_routing_private.h"
11
12 static xbt_dict_t patterns = NULL;
13 static xbt_dict_t random_value = NULL;
14
15 /* Global vars */
16 routing_global_t global_routing = NULL;
17 routing_component_t current_routing = NULL;
18 model_type_t current_routing_model = NULL;
19
20 /* global parse functions */
21 xbt_dynar_t link_list = NULL;    /* temporary store of current list link of a route */
22 static const char *src = NULL;        /* temporary store the source name of a route */
23 static const char *dst = NULL;        /* temporary store the destination name of a route */
24 static char *gw_src = NULL;     /* temporary store the gateway source name of a route */
25 static char *gw_dst = NULL;     /* temporary store the gateway destination name of a route */
26 static double_f_cpvoid_t get_link_latency = NULL;
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
29
30 static void routing_parse_Speer(void);          /* peer bypass */
31 static void routing_parse_Srandom(void);        /* random bypass */
32 static void routing_parse_Erandom(void);        /* random bypass */
33
34 static void routing_parse_Sconfig(void);        /* config Tag */
35 static void routing_parse_Econfig(void);        /* config Tag */
36
37 static char* replace_random_parameter(char * chaine);
38 static void clean_dict_random(void);
39
40 /* this lines are only for replace use like index in the model table */
41 typedef enum {
42   SURF_MODEL_FULL = 0,
43   SURF_MODEL_FLOYD,
44   SURF_MODEL_DIJKSTRA,
45   SURF_MODEL_DIJKSTRACACHE,
46   SURF_MODEL_NONE,
47 #ifdef HAVE_PCRE_LIB
48   SURF_MODEL_RULEBASED
49 #endif
50 } e_routing_types;
51
52 struct s_model_type routing_models[] = { {"Full",
53                                           "Full routing data (fast, large memory requirements, fully expressive)",
54                                           model_full_create,
55                                           model_full_load,
56                                           model_full_unload,
57                                           model_full_end},
58 {"Floyd",
59  "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
60  model_floyd_create, model_floyd_load, model_floyd_unload,
61  model_floyd_end},
62 {"Dijkstra",
63  "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
64  model_dijkstra_create, model_dijkstra_both_load,
65  model_dijkstra_both_unload, model_dijkstra_both_end},
66 {"DijkstraCache",
67  "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
68  model_dijkstracache_create, model_dijkstra_both_load,
69  model_dijkstra_both_unload, model_dijkstra_both_end},
70 {"none", "No routing (usable with Constant network only)",
71  model_none_create, model_none_load, model_none_unload, model_none_end},
72 #ifdef HAVE_PCRE_LIB
73 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
74  model_rulebased_load, model_rulebased_unload, model_rulebased_end},
75 {"Vivaldi", "Vivaldi routing", model_rulebased_create,
76   model_rulebased_load, model_rulebased_unload, model_rulebased_end},
77 #endif
78 {NULL, NULL, NULL, NULL, NULL, NULL}
79 };
80
81 static double euclidean_dist_comp(int index, xbt_dynar_t src, xbt_dynar_t dst)
82 {
83         double src_coord, dst_coord;
84
85         src_coord = atof(xbt_dynar_get_as(src, index, char *));
86         dst_coord = atof(xbt_dynar_get_as(dst, index, char *));
87
88         return (src_coord-dst_coord)*(src_coord-dst_coord);
89
90 }
91
92 static double base_vivaldi_get_latency (const char *src, const char *dst)
93 {
94   double euclidean_dist;
95   xbt_dynar_t src_ctn, dst_ctn;
96   src_ctn = xbt_lib_get_or_null(host_lib, src, COORD_HOST_LEVEL);
97   if(!src_ctn) src_ctn = xbt_lib_get_or_null(as_router_lib, src, COORD_ASR_LEVEL);
98   dst_ctn = xbt_lib_get_or_null(host_lib, dst, COORD_HOST_LEVEL);
99   if(!dst_ctn) dst_ctn = xbt_lib_get_or_null(as_router_lib, dst, COORD_ASR_LEVEL);
100
101   if(dst_ctn == NULL || src_ctn == NULL)
102   xbt_die("Coord src '%s' :%p   dst '%s' :%p",src,src_ctn,dst,dst_ctn);
103
104   euclidean_dist = sqrt (euclidean_dist_comp(0,src_ctn,dst_ctn)+euclidean_dist_comp(1,src_ctn,dst_ctn))
105                                  + fabs(atof(xbt_dynar_get_as(src_ctn, 2, char *)))+fabs(atof(xbt_dynar_get_as(dst_ctn, 2, char *)));
106
107   xbt_assert(euclidean_dist>=0, "Euclidean Dist is less than 0\"%s\" and \"%.2f\"", src, euclidean_dist);
108
109   return euclidean_dist;
110 }
111
112 static double vivaldi_get_link_latency (routing_component_t rc,const char *src, const char *dst, route_extended_t e_route)
113 {
114   if(get_network_element_type(src) == SURF_NETWORK_ELEMENT_AS) {
115           int need_to_clean = e_route?0:1;
116           double latency;
117           e_route = e_route?e_route:(*(rc->get_route)) (rc, src, dst);
118           latency = base_vivaldi_get_latency(e_route->src_gateway,e_route->dst_gateway);
119           if(need_to_clean) generic_free_extended_route(e_route);
120           return latency;
121   } else {
122           return base_vivaldi_get_latency(src,dst);
123   }
124 }
125
126 /**
127  * \brief Add a "host" to the network element list
128  */
129 static void parse_S_host(const char *host_id, const char* coord)
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(host_lib, host_id,ROUTING_HOST_LEVEL),
135               "Reading a host, processing unit \"%s\" already exists",
136               host_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, host_id);
141   info = xbt_new0(s_network_element_info_t, 1);
142   info->rc_component = current_routing;
143   info->rc_type = SURF_NETWORK_ELEMENT_HOST;
144   xbt_lib_set(host_lib,host_id,ROUTING_HOST_LEVEL,(void *) info);
145   if (strcmp(coord,"")) {
146         if(!COORD_HOST_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
147     xbt_dynar_t ctn = xbt_str_split_str(coord, " ");
148     xbt_dynar_shrink(ctn, 0);
149     xbt_lib_set(host_lib,host_id,COORD_HOST_LEVEL,(void *) ctn);
150   }
151 }
152
153 static void parse_E_host(void)
154 {
155          xbt_dict_cursor_t cursor = NULL;
156           char *key;
157           char *elem;
158
159           xbt_dict_foreach(current_property_set, cursor, key, elem) {
160                   XBT_DEBUG("property : %s = %s",key,elem);
161                 }
162 }
163
164 /*
165  * \brief Add a host to the network element list from XML
166  */
167 static void parse_S_host_XML(void)
168 {
169         parse_S_host(A_surfxml_host_id, A_surfxml_host_coordinates);
170 }
171 static void parse_E_host_XML(void)
172 {
173         parse_E_host();
174 }
175
176 /*
177  * \brief Add a host to the network element list from lua script
178  */
179 static void parse_S_host_lua(const char *host_id, const char *coord)
180 {
181   parse_S_host(host_id, coord);
182 }
183
184
185 /**
186  * \brief Add a "router" to the network element list
187  */
188 static void parse_S_router(const char *router_id)
189 {
190   network_element_info_t info = NULL;
191
192   if (current_routing->hierarchy == SURF_ROUTING_NULL)
193     current_routing->hierarchy = SURF_ROUTING_BASE;
194   xbt_assert(!xbt_lib_get_or_null(as_router_lib,A_surfxml_router_id, ROUTING_ASR_LEVEL),
195               "Reading a router, processing unit \"%s\" already exists",
196               router_id);
197   xbt_assert(current_routing->set_processing_unit,
198               "no defined method \"set_processing_unit\" in \"%s\"",
199               current_routing->name);
200   (*(current_routing->set_processing_unit)) (current_routing,
201                                              router_id);
202   info = xbt_new0(s_network_element_info_t, 1);
203   info->rc_component = current_routing;
204   info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
205
206   xbt_lib_set(as_router_lib,router_id,ROUTING_ASR_LEVEL,(void *) info);
207   if (strcmp(A_surfxml_router_coordinates,"")) {
208         if(!COORD_ASR_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
209     xbt_dynar_t ctn = xbt_str_split_str(A_surfxml_router_coordinates, " ");
210     xbt_dynar_shrink(ctn, 0);
211     xbt_lib_set(as_router_lib,router_id,COORD_ASR_LEVEL,(void *) ctn);
212   }
213 }
214
215 /**
216  * brief Add a "router" to the network element list from XML description
217  */
218 static void parse_S_router_XML(void)
219 {
220         return parse_S_router(A_surfxml_router_id);
221 }
222
223 /**
224  * brief Add a "router" to the network element list from XML description
225  */
226 static void parse_S_router_lua(const char* router_id)
227 {
228         return parse_S_router(router_id);
229 }
230
231 /**
232  * \brief Set the endponints for a route
233  */
234 static void parse_S_route_new_and_endpoints(const char *src_id, const char *dst_id)
235 {
236   if (src != NULL && dst != NULL && link_list != NULL)
237     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
238            src_id, dst_id);
239   src = src_id;
240   dst = dst_id;
241   xbt_assert(strlen(src) > 0 || strlen(dst) > 0,
242               "Some limits are null in the route between \"%s\" and \"%s\"",
243               src, dst);
244   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
245 }
246
247 /**
248  * \brief Set the endpoints for a route from XML
249  */
250 static void parse_S_route_new_and_endpoints_XML(void)
251 {
252   parse_S_route_new_and_endpoints(A_surfxml_route_src,
253                                   A_surfxml_route_dst);
254 }
255
256 /**
257  * \brief Set the endpoints for a route from lua
258  */
259 static void parse_S_route_new_and_endpoints_lua(const char *id_src, const char *id_dst)
260 {
261   parse_S_route_new_and_endpoints(id_src, id_dst);
262 }
263
264 /**
265  * \brief Set the endponints and gateways for a ASroute
266  */
267 static void parse_S_ASroute_new_and_endpoints(void)
268 {
269   if (src != NULL && dst != NULL && link_list != NULL)
270     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
271            A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
272   src = A_surfxml_ASroute_src;
273   dst = A_surfxml_ASroute_dst;
274   gw_src = A_surfxml_ASroute_gw_src;
275   gw_dst = A_surfxml_ASroute_gw_dst;
276   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
277               || strlen(gw_dst) > 0,
278               "Some limits are null in the route between \"%s\" and \"%s\"",
279               src, dst);
280   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
281 }
282
283 /**
284  * \brief Set the endponints for a bypassRoute
285  */
286 static void parse_S_bypassRoute_new_and_endpoints(void)
287 {
288   if (src != NULL && dst != NULL && link_list != NULL)
289     THROWF(arg_error, 0,
290            "Bypass Route between %s to %s can not be defined",
291            A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
292   src = A_surfxml_bypassRoute_src;
293   dst = A_surfxml_bypassRoute_dst;
294   gw_src = A_surfxml_bypassRoute_gw_src;
295   gw_dst = A_surfxml_bypassRoute_gw_dst;
296   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
297               || strlen(gw_dst) > 0,
298               "Some limits are null in the route between \"%s\" and \"%s\"",
299               src, dst);
300   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
301 }
302
303 /**
304  * \brief Set a new link on the actual list of link for a route or ASroute
305  */
306 static void parse_E_link_ctn_new_elem(const char *link_id)
307 {
308   char *val;
309   val = xbt_strdup(link_id);
310   xbt_dynar_push(link_list, &val);
311 }
312
313 /**
314  * \brief Set a new link on the actual list of link for a route or ASroute from XML
315  */
316
317 static void parse_E_link_ctn_new_elem_XML(void)
318 {
319   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
320     parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
321   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP) {
322     char *link_id = bprintf("%s_UP", A_surfxml_link_ctn_id);
323     parse_E_link_ctn_new_elem(link_id);
324     free(link_id);
325   }
326   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN) {
327     char *link_id = bprintf("%s_DOWN", A_surfxml_link_ctn_id);
328     parse_E_link_ctn_new_elem(link_id);
329     free(link_id);
330   }
331 }
332
333 /**
334  * \brief Set a new link on the actual list of link for a route or ASroute from lua
335  */
336 static void parse_E_link_c_ctn_new_elem_lua(const char *link_id)
337 {
338   parse_E_link_ctn_new_elem(link_id);
339 }
340
341 /**
342  * \brief Store the route by calling the set_route function of the current routing component
343  */
344 static void parse_E_route_store_route(void)
345 {
346   name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
347   route->generic_route.link_list = link_list;
348   xbt_assert(current_routing->set_route,
349               "no defined method \"set_route\" in \"%s\"",
350               current_routing->name);
351   (*(current_routing->set_route)) (current_routing, src, dst, route);
352   link_list = NULL;
353   src = NULL;
354   dst = NULL;
355 }
356
357 /**
358  * \brief Store the ASroute by calling the set_ASroute function of the current routing component
359  */
360 static void parse_E_ASroute_store_route(void)
361 {
362   name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
363   e_route->generic_route.link_list = link_list;
364   e_route->src_gateway = xbt_strdup(gw_src);
365   e_route->dst_gateway = xbt_strdup(gw_dst);
366   xbt_assert(current_routing->set_ASroute,
367               "no defined method \"set_ASroute\" in \"%s\"",
368               current_routing->name);
369   (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
370   link_list = NULL;
371   src = NULL;
372   dst = NULL;
373   gw_src = NULL;
374   gw_dst = NULL;
375 }
376
377 /**
378  * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
379  */
380 static void parse_E_bypassRoute_store_route(void)
381 {
382   route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
383   e_route->generic_route.link_list = link_list;
384   e_route->src_gateway = xbt_strdup(gw_src);
385   e_route->dst_gateway = xbt_strdup(gw_dst);
386   xbt_assert(current_routing->set_bypassroute,
387               "no defined method \"set_bypassroute\" in \"%s\"",
388               current_routing->name);
389   (*(current_routing->set_bypassroute)) (current_routing, src, dst,
390                                          e_route);
391   link_list = NULL;
392   src = NULL;
393   dst = NULL;
394   gw_src = NULL;
395   gw_dst = NULL;
396 }
397
398 /**
399  * \brief Make a new routing component
400  *
401  * make the new structure and
402  * set the parsing functions to allows parsing the part of the routing tree
403  */
404 static void parse_S_AS(char *AS_id, char *AS_routing)
405 {
406   routing_component_t new_routing;
407   model_type_t model = NULL;
408   char *wanted = AS_routing;
409   int cpt;
410   /* seach the routing model */
411   for (cpt = 0; routing_models[cpt].name; cpt++)
412     if (!strcmp(wanted, routing_models[cpt].name))
413       model = &routing_models[cpt];
414   /* if its not exist, error */
415   if (model == NULL) {
416     fprintf(stderr, "Routing model %s not found. Existing models:\n",
417             wanted);
418     for (cpt = 0; routing_models[cpt].name; cpt++)
419       if (!strcmp(wanted, routing_models[cpt].name))
420         fprintf(stderr, "   %s: %s\n", routing_models[cpt].name,
421                 routing_models[cpt].desc);
422     xbt_die(NULL);
423   }
424
425   /* make a new routing component */
426   new_routing = (routing_component_t) (*(model->create)) ();
427   new_routing->routing = model;
428   new_routing->hierarchy = SURF_ROUTING_NULL;
429   new_routing->name = xbt_strdup(AS_id);
430   new_routing->routing_sons = xbt_dict_new();
431
432   /* Hack for Vivaldi */
433   if(!strcmp(model->name,"Vivaldi"))
434         new_routing->get_latency = vivaldi_get_link_latency;
435
436   if (current_routing == NULL && global_routing->root == NULL) {
437
438     /* it is the first one */
439     new_routing->routing_father = NULL;
440     global_routing->root = new_routing;
441
442   } else if (current_routing != NULL && global_routing->root != NULL) {
443
444     xbt_assert(!xbt_dict_get_or_null
445                 (current_routing->routing_sons, AS_id),
446                 "The AS \"%s\" already exists", AS_id);
447     /* it is a part of the tree */
448     new_routing->routing_father = current_routing;
449     /* set the father behavior */
450     if (current_routing->hierarchy == SURF_ROUTING_NULL)
451       current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
452     /* add to the sons dictionary */
453     xbt_dict_set(current_routing->routing_sons, AS_id,
454                  (void *) new_routing, NULL);
455     /* add to the father element list */
456     (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
457     /* unload the prev parse rules */
458     (*(current_routing->routing->unload)) ();
459
460   } else {
461     THROWF(arg_error, 0, "All defined components must be belong to a AS");
462   }
463   /* set the new parse rules */
464   (*(new_routing->routing->load)) ();
465   /* set the new current component of the tree */
466   current_routing = new_routing;
467 }
468
469 /*
470  * Detect the routing model type of the routing component from XML platforms
471  */
472 static void parse_S_AS_XML(void)
473 {
474   parse_S_AS(A_surfxml_AS_id, A_surfxml_AS_routing);
475 }
476
477 /*
478  * define the routing model type of routing component from lua script
479  */
480 static void parse_S_AS_lua(char *id, char *mode)
481 {
482   parse_S_AS(id, mode);
483 }
484
485
486 /**
487  * \brief Finish the creation of a new routing component
488  *
489  * When you finish to read the routing component, other structures must be created. 
490  * the "end" method allow to do that for any routing model type
491  */
492 static void parse_E_AS(const char *AS_id)
493 {
494
495   if (current_routing == NULL) {
496     THROWF(arg_error, 0, "Close AS(%s), that never open", AS_id);
497   } else {
498     network_element_info_t info = NULL;
499     xbt_assert(!xbt_lib_get_or_null(as_router_lib,current_routing->name, ROUTING_ASR_LEVEL),
500                 "The AS \"%s\" already exists",current_routing->name);
501     info = xbt_new0(s_network_element_info_t, 1);
502     info->rc_component = current_routing->routing_father;
503     info->rc_type = SURF_NETWORK_ELEMENT_AS;
504     xbt_lib_set(as_router_lib,current_routing->name,ROUTING_ASR_LEVEL,(void *) info);
505
506     (*(current_routing->routing->unload)) ();
507     (*(current_routing->routing->end)) ();
508     current_routing = current_routing->routing_father;
509     if (current_routing != NULL)
510       (*(current_routing->routing->load)) ();
511   }
512 }
513
514 /*
515  * \brief Finish the creation of a new routing component from XML
516  */
517 static void parse_E_AS_XML(void)
518 {
519   parse_E_AS(A_surfxml_AS_id);
520 }
521
522 /*
523  * \brief Finish the creation of a new routing component from lua
524  */
525 static void parse_E_AS_lua(const char *id)
526 {
527   parse_E_AS(id);
528 }
529
530 /* Aux Business methods */
531
532 /**
533  * \brief Get the AS name of the element
534  *
535  * \param name the host name
536  *
537  */
538 static char* elements_As_name(const char *name)
539 {
540   routing_component_t as_comp;
541
542   /* (1) find the as where the host is located */
543   as_comp = ((network_element_info_t)
544             xbt_lib_get_or_null(host_lib,name, ROUTING_HOST_LEVEL))->rc_component;
545   return as_comp->name;
546 }
547
548
549 /**
550  * \brief Get the AS father and the first elements of the chain
551  *
552  * \param src the source host name 
553  * \param dst the destination host name
554  * 
555  * Get the common father of the to processing units, and the first different 
556  * father in the chain
557  */
558 static xbt_dynar_t elements_father(const char *src, const char *dst)
559 {
560
561   xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
562
563   xbt_dynar_t result = xbt_dynar_new(sizeof(char *), NULL);
564
565   routing_component_t src_as, dst_as;
566   int index_src, index_dst, index_father_src, index_father_dst;
567   xbt_dynar_t path_src = NULL;
568   xbt_dynar_t path_dst = NULL;
569   routing_component_t current = NULL;
570   routing_component_t *current_src = NULL;
571   routing_component_t *current_dst = NULL;
572   routing_component_t *father = NULL;
573
574   /* (1) find the as where the src and dst are located */
575   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
576   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
577   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
578   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
579   src_as = ((network_element_info_t)src_data)->rc_component;
580   dst_as = ((network_element_info_t)dst_data)->rc_component;
581
582   xbt_assert(src_as
583               && dst_as,
584               "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
585               dst);
586
587   /* (2) find the path to the root routing component */
588   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
589   current = src_as;
590   while (current != NULL) {
591     xbt_dynar_push(path_src, &current);
592     current = current->routing_father;
593   }
594   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
595   current = dst_as;
596   while (current != NULL) {
597     xbt_dynar_push(path_dst, &current);
598     current = current->routing_father;
599   }
600
601   /* (3) find the common father */
602   index_src = path_src->used - 1;
603   index_dst = path_dst->used - 1;
604   current_src = xbt_dynar_get_ptr(path_src, index_src);
605   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
606   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
607     current_src = xbt_dynar_get_ptr(path_src, index_src);
608     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
609     index_src--;
610     index_dst--;
611   }
612   index_src++;
613   index_dst++;
614   current_src = xbt_dynar_get_ptr(path_src, index_src);
615   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
616
617   /* (4) they are not in the same routing component, make the path */
618   index_father_src = index_src + 1;
619   index_father_dst = index_dst + 1;
620
621   if (*current_src == *current_dst)
622     father = current_src;
623   else
624     father = xbt_dynar_get_ptr(path_src, index_father_src);
625
626   /* (5) result generation */
627   xbt_dynar_push(result, father);       /* first same the father of src and dst */
628   xbt_dynar_push(result, current_src);  /* second the first different father of src */
629   xbt_dynar_push(result, current_dst);  /* three  the first different father of dst */
630
631   xbt_dynar_free(&path_src);
632   xbt_dynar_free(&path_dst);
633
634   return result;
635 }
636
637 /* Global Business methods */
638
639 /**
640  * \brief Recursive function for get_route
641  *
642  * \param src the source host name 
643  * \param dst the destination host name
644  * 
645  * This function is called by "get_route". It allows to walk recursively
646  * through the routing components tree.
647  */
648 static route_extended_t _get_route(const char *src, const char *dst)
649 {
650
651   void *link;
652   unsigned int cpt = 0;
653
654   XBT_DEBUG("Solve route  \"%s\" to \"%s\"", src, dst);
655
656   xbt_assert(src && dst, "bad parameters for \"_get_route\" method");
657
658   route_extended_t e_route, e_route_cnt, e_route_src = NULL, e_route_dst =
659       NULL;
660
661   xbt_dynar_t elem_father_list = elements_father(src, dst);
662
663   routing_component_t common_father =
664       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
665   routing_component_t src_father =
666       xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
667   routing_component_t dst_father =
668       xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
669
670   e_route = xbt_new0(s_route_extended_t, 1);
671   e_route->src_gateway = NULL;
672   e_route->dst_gateway = NULL;
673   e_route->generic_route.link_list =
674       xbt_dynar_new(global_routing->size_of_link, NULL);
675
676   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
677
678         e_route_cnt =
679           (*(common_father->get_route)) (common_father, src, dst);
680         xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"", src,
681                           dst);
682           // FIXME (optim): faire une copie et pas une série de push
683         xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
684         xbt_dynar_push(e_route->generic_route.link_list, &link);
685         }
686         generic_free_extended_route(e_route_cnt);
687
688   } else {                      /* SURF_ROUTING_RECURSIVE */
689
690     route_extended_t e_route_bypass = NULL;
691
692     if (common_father->get_bypass_route)
693       e_route_bypass =
694           (*(common_father->get_bypass_route)) (common_father, src, dst);
695
696     if (e_route_bypass)
697       e_route_cnt = e_route_bypass;
698     else
699       e_route_cnt =
700           (*(common_father->get_route)) (common_father, src_father->name,
701                                          dst_father->name);
702
703     xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
704                 src_father->name, dst_father->name);
705
706     xbt_assert((e_route_cnt->src_gateway == NULL) ==
707                 (e_route_cnt->dst_gateway == NULL),
708                 "bad gateway for route between \"%s\" and \"%s\"", src,
709                 dst);
710
711     if (strcmp(src, e_route_cnt->src_gateway)) {
712       e_route_src = _get_route(src, e_route_cnt->src_gateway);
713       xbt_assert(e_route_src, "no route between \"%s\" and \"%s\"", src,
714                   e_route_cnt->src_gateway);
715       xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
716         xbt_dynar_push(e_route->generic_route.link_list, &link);
717       }
718     }
719
720     xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
721       xbt_dynar_push(e_route->generic_route.link_list, &link);
722     }
723
724     if (strcmp(e_route_cnt->dst_gateway, dst)) {
725       e_route_dst = _get_route(e_route_cnt->dst_gateway, dst);
726       xbt_assert(e_route_dst, "no route between \"%s\" and \"%s\"",
727                   e_route_cnt->dst_gateway, dst);
728       xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
729         xbt_dynar_push(e_route->generic_route.link_list, &link);
730       }
731     }
732
733     e_route->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
734     e_route->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
735
736     generic_free_extended_route(e_route_src);
737     generic_free_extended_route(e_route_cnt);
738     generic_free_extended_route(e_route_dst);
739   }
740
741   xbt_dynar_free(&elem_father_list);
742
743   return e_route;
744 }
745
746 static double _get_latency(const char *src, const char *dst)
747 {
748   double latency, latency_src, latency_dst = 0.0;
749
750   XBT_DEBUG("Solve route  \"%s\" to \"%s\"", src, dst);
751   xbt_assert(src && dst, "bad parameters for \"_get_route\" method");
752
753   route_extended_t e_route_cnt;
754
755   xbt_dynar_t elem_father_list = elements_father(src, dst);
756
757   routing_component_t common_father =
758       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
759   routing_component_t src_father =
760       xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
761   routing_component_t dst_father =
762       xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
763
764   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
765
766       latency =
767           (*(common_father->get_latency)) (common_father, src, dst, NULL);
768       xbt_assert(latency>=0, "no route between \"%s\" and \"%s\"", src,
769                   dst);
770
771   } else {                      /* SURF_ROUTING_RECURSIVE */
772      route_extended_t e_route_bypass = NULL;
773     if (common_father->get_bypass_route)
774       e_route_bypass =
775           (*(common_father->get_bypass_route)) (common_father, src, dst);
776
777     xbt_assert(!e_route_bypass,"Bypass cannot work yet with get_latency");
778                                                  
779     e_route_cnt =
780           (*(common_father->get_route)) (common_father, src_father->name,
781                                          dst_father->name);
782
783     xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
784                 src_father->name, dst_father->name);
785
786     xbt_assert((e_route_cnt->src_gateway == NULL) ==
787                 (e_route_cnt->dst_gateway == NULL),
788                 "bad gateway for route between \"%s\" and \"%s\"", src,
789                 dst);
790
791     latency = (*(common_father->get_latency)) (common_father, src_father->name, dst_father->name, e_route_cnt);
792
793     xbt_assert(latency>=0, "no route between \"%s\" and \"%s\"",
794                 src_father->name, dst_father->name);
795
796     if (strcmp(src,e_route_cnt->src_gateway)) {
797
798       latency_src = _get_latency(src, e_route_cnt->src_gateway);
799       xbt_assert(latency_src>=0, "no route between \"%s\" and \"%s\"", src,
800                   e_route_cnt->src_gateway);
801       latency += latency_src;
802     }
803
804     if (strcmp(e_route_cnt->dst_gateway,dst)) {
805     
806       latency_dst = _get_latency(e_route_cnt->dst_gateway, dst);
807       xbt_assert(latency_dst>=0, "no route between \"%s\" and \"%s\"",
808                   e_route_cnt->dst_gateway, dst);
809       latency += latency_dst;
810     }
811         
812   }
813
814   xbt_dynar_free(&elem_father_list);
815
816   return latency;
817 }
818
819 /**
820  * \brief Generic method: find a route between hosts
821  *
822  * \param src the source host name 
823  * \param dst the destination host name
824  * 
825  * walk through the routing components tree and find a route between hosts
826  * by calling the differents "get_route" functions in each routing component.
827  * No need to free the returned dynar. It will be freed at the next call.
828  */
829 static xbt_dynar_t get_route(const char *src, const char *dst)
830 {
831
832   route_extended_t e_route;
833
834   e_route = _get_route(src, dst);
835   xbt_assert(e_route, "no route between \"%s\" and \"%s\"", src, dst);
836
837   if (global_routing->last_route)
838     xbt_dynar_free(&(global_routing->last_route));
839   global_routing->last_route = e_route->generic_route.link_list;
840
841   if (e_route->src_gateway)
842     xbt_free(e_route->src_gateway);
843   if (e_route->dst_gateway)
844     xbt_free(e_route->dst_gateway);
845
846   xbt_free(e_route);
847
848   return global_routing->last_route;
849 }
850
851 /**
852  * \brief Generic method: find a route between hosts
853  *
854  * \param src the source host name
855  * \param dst the destination host name
856  *
857  * walk through the routing components tree and find a route between hosts
858  * by calling the differents "get_route" functions in each routing component.
859  * Leaves the caller the responsability to clean the returned dynar.
860  */
861 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
862 {
863         xbt_dynar_t d = get_route(src,dst);
864         global_routing->last_route = NULL;
865         return d;
866 }
867
868 /*Get Latency*/
869 static double get_latency(const char *src, const char *dst)
870 {
871
872   double latency = -1.0;
873   latency = _get_latency(src, dst);
874   xbt_assert(latency>=0.0, "no route between \"%s\" and \"%s\"", src, dst);
875   return latency;
876 }
877
878 /**
879  * \brief Recursive function for finalize
880  *
881  * \param rc the source host name 
882  * 
883  * This fuction is call by "finalize". It allow to finalize the 
884  * AS or routing components. It delete all the structures.
885  */
886 static void _finalize(routing_component_t rc)
887 {
888   if (rc) {
889     xbt_dict_cursor_t cursor = NULL;
890     char *key;
891     routing_component_t elem;
892     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
893       _finalize(elem);
894     }
895     xbt_dict_t tmp_sons = rc->routing_sons;
896     char *tmp_name = rc->name;
897     xbt_dict_free(&tmp_sons);
898     xbt_free(tmp_name);
899     xbt_assert(rc->finalize, "no defined method \"finalize\" in \"%s\"",
900                 current_routing->name);
901     (*(rc->finalize)) (rc);
902   }
903 }
904
905 /**
906  * \brief Generic method: delete all the routing structures
907  * 
908  * walk through the routing components tree and delete the structures
909  * by calling the differents "finalize" functions in each routing component
910  */
911 static void finalize(void)
912 {
913   /* delete recursibly all the tree */
914   _finalize(global_routing->root);
915   /* delete last_route */
916   xbt_dynar_free(&(global_routing->last_route));
917   /* delete global routing structure */
918   xbt_free(global_routing);
919 }
920
921 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
922 {
923   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
924
925   //adding my one link routes
926   unsigned int cpt;
927   void *link;
928   xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
929   if (onelink_mine) {
930     xbt_dynar_foreach(onelink_mine, cpt, link) {
931       xbt_dynar_push(ret, &link);
932     }
933   }
934   //recursing
935   char *key;
936   xbt_dict_cursor_t cursor = NULL;
937   routing_component_t rc_child;
938   xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
939     xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
940     if (onelink_child) {
941       xbt_dynar_foreach(onelink_child, cpt, link) {
942         xbt_dynar_push(ret, &link);
943       }
944     }
945   }
946   return ret;
947 }
948
949 static xbt_dynar_t get_onelink_routes(void)
950 {
951   return recursive_get_onelink_routes(global_routing->root);
952 }
953
954 e_surf_network_element_type_t get_network_element_type(const char
955                                                               *name)
956 {
957   network_element_info_t rc = NULL;
958
959   rc = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
960   if(rc) return rc->rc_type;
961
962   rc = xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
963   if(rc) return rc->rc_type;
964
965   return SURF_NETWORK_ELEMENT_NULL;
966 }
967
968 /**
969  * \brief Generic method: create the global routing schema
970  * 
971  * Make a global routing structure and set all the parsing functions.
972  */
973 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
974 {
975   /* config the uniq global routing */
976   global_routing = xbt_new0(s_routing_global_t, 1);
977   global_routing->root = NULL;
978   global_routing->get_route = get_route;
979   global_routing->get_latency = get_latency;
980   global_routing->get_route_no_cleanup = get_route_no_cleanup;
981   global_routing->get_onelink_routes = get_onelink_routes;
982   global_routing->get_network_element_type = get_network_element_type;
983   global_routing->finalize = finalize;
984   global_routing->loopback = loopback;
985   global_routing->size_of_link = size_of_links;
986   global_routing->last_route = NULL;
987   get_link_latency = get_link_latency_fun;
988   /* no current routing at moment */
989   current_routing = NULL;
990
991   /* parse generic elements */
992   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
993   surfxml_add_callback(ETag_surfxml_host_cb_list, &parse_E_host_XML);
994   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router_XML);
995
996   surfxml_add_callback(STag_surfxml_route_cb_list,
997                        &parse_S_route_new_and_endpoints_XML);
998   surfxml_add_callback(STag_surfxml_ASroute_cb_list,
999                        &parse_S_ASroute_new_and_endpoints);
1000   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
1001                        &parse_S_bypassRoute_new_and_endpoints);
1002
1003   surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
1004                        &parse_E_link_ctn_new_elem_XML);
1005
1006   surfxml_add_callback(ETag_surfxml_route_cb_list,
1007                        &parse_E_route_store_route);
1008   surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
1009                        &parse_E_ASroute_store_route);
1010   surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
1011                        &parse_E_bypassRoute_store_route);
1012
1013   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
1014   surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
1015
1016   surfxml_add_callback(STag_surfxml_cluster_cb_list,
1017                        &routing_parse_Scluster);
1018
1019   surfxml_add_callback(STag_surfxml_peer_cb_list,
1020                          &routing_parse_Speer);
1021
1022   surfxml_add_callback(ETag_surfxml_platform_cb_list,
1023                                                   &clean_dict_random);
1024
1025 #ifdef HAVE_TRACING
1026   instr_routing_define_callbacks();
1027 #endif
1028 }
1029
1030 void surf_parse_add_callback_config(void)
1031 {
1032         surfxml_add_callback(STag_surfxml_config_cb_list, &routing_parse_Sconfig);
1033         surfxml_add_callback(ETag_surfxml_config_cb_list, &routing_parse_Econfig);
1034         surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties_XML);
1035         surfxml_add_callback(STag_surfxml_AS_cb_list, &surf_parse_models_setup);
1036         surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
1037 }
1038
1039 void surf_parse_models_setup()
1040 {
1041         routing_parse_Erandom();
1042         surfxml_del_callback(STag_surfxml_AS_cb_list, surf_parse_models_setup);
1043         surf_config_models_setup(platform_filename);
1044         free(platform_filename);
1045 }
1046
1047 /* ************************************************** */
1048 /* ********** PATERN FOR NEW ROUTING **************** */
1049
1050 /* The minimal configuration of a new routing model need the next functions,
1051  * also you need to set at the start of the file, the new model in the model
1052  * list. Remember keep the null ending of the list.
1053  */
1054 /*** Routing model structure ***/
1055 // typedef struct {
1056 //   s_routing_component_t generic_routing;
1057 //   /* things that your routing model need */
1058 // } s_routing_component_NEW_t,*routing_component_NEW_t;
1059
1060 /*** Parse routing model functions ***/
1061 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
1062 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
1063 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
1064 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
1065 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
1066
1067 /*** Business methods ***/
1068 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
1069 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
1070 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
1071
1072 /*** Creation routing model functions ***/
1073 // static void* model_NEW_create(void) {
1074 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
1075 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
1076 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
1077 //   new_component->generic_routing.set_route = model_NEW_set_route;
1078 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
1079 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
1080 //   new_component->generic_routing.get_route = NEW_get_route;
1081 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
1082 //   new_component->generic_routing.finalize = NEW_finalize;
1083 //   /* initialization of internal structures */
1084 //   return new_component;
1085 // } /* mandatory */
1086 // static void  model_NEW_load(void) {}   /* mandatory */
1087 // static void  model_NEW_unload(void) {} /* mandatory */
1088 // static void  model_NEW_end(void) {}    /* mandatory */
1089
1090 /* ************************************************************************** */
1091 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
1092
1093 void generic_set_processing_unit(routing_component_t rc,
1094                                         const char *name)
1095 {
1096   XBT_DEBUG("Load process unit \"%s\"", name);
1097   int *id = xbt_new0(int, 1);
1098   xbt_dict_t _to_index;
1099   _to_index = current_routing->to_index;
1100   *id = xbt_dict_length(_to_index);
1101   xbt_dict_set(_to_index, name, id, xbt_free);
1102 }
1103
1104 void generic_set_autonomous_system(routing_component_t rc,
1105                                           const char *name)
1106 {
1107   XBT_DEBUG("Load Autonomous system \"%s\"", name);
1108   int *id = xbt_new0(int, 1);
1109   xbt_dict_t _to_index;
1110   _to_index = current_routing->to_index;
1111   *id = xbt_dict_length(_to_index);
1112   xbt_dict_set(_to_index, name, id, xbt_free);
1113 }
1114
1115 int surf_pointer_resource_cmp(const void *a, const void *b)
1116 {
1117   return a != b;
1118 }
1119
1120 int surf_link_resource_cmp(const void *a, const void *b)
1121 {
1122   return !!memcmp(a,b,global_routing->size_of_link);
1123 }
1124
1125 void generic_set_bypassroute(routing_component_t rc,
1126                                     const char *src, const char *dst,
1127                                     route_extended_t e_route)
1128 {
1129   XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
1130   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1131   char *route_name;
1132
1133   route_name = bprintf("%s#%s", src, dst);
1134   xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
1135               "Invalid count of links, must be greater than zero (%s,%s)",
1136               src, dst);
1137   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
1138               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
1139               src, e_route->src_gateway, dst, e_route->dst_gateway);
1140
1141   route_extended_t new_e_route =
1142       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
1143   xbt_dynar_free(&(e_route->generic_route.link_list));
1144   xbt_free(e_route);
1145
1146   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
1147                (void (*)(void *)) generic_free_extended_route);
1148   xbt_free(route_name);
1149 }
1150
1151 /* ************************************************************************** */
1152 /* *********************** GENERIC BUSINESS METHODS ************************* */
1153
1154 double generic_get_link_latency(routing_component_t rc,
1155                                        const char *src, const char *dst,
1156                                        route_extended_t route)
1157 {
1158         int need_to_clean = route?0:1;
1159         void * link;
1160         unsigned int i;
1161         double latency = 0.0;
1162
1163         route = route?route:rc->get_route(rc,src,dst);
1164
1165         xbt_dynar_foreach(route->generic_route.link_list,i,link) {
1166                 latency += get_link_latency(link);
1167         }
1168         if(need_to_clean) generic_free_extended_route(route);
1169   return latency;
1170 }
1171
1172 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
1173 {
1174   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
1175 }
1176
1177 route_extended_t generic_get_bypassroute(routing_component_t rc,
1178                                                 const char *src,
1179                                                 const char *dst)
1180 {
1181   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1182   routing_component_t src_as, dst_as;
1183   int index_src, index_dst;
1184   xbt_dynar_t path_src = NULL;
1185   xbt_dynar_t path_dst = NULL;
1186   routing_component_t current = NULL;
1187   routing_component_t *current_src = NULL;
1188   routing_component_t *current_dst = NULL;
1189
1190   /* (1) find the as where the src and dst are located */
1191   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1192   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1193   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1194   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1195
1196   if(src_data == NULL || dst_data == NULL)
1197           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1198                      src, dst, rc->name);
1199
1200   src_as = ((network_element_info_t)src_data)->rc_component;
1201   dst_as = ((network_element_info_t)dst_data)->rc_component;
1202
1203   /* (2) find the path to the root routing component */
1204   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1205   current = src_as;
1206   while (current != NULL) {
1207     xbt_dynar_push(path_src, &current);
1208     current = current->routing_father;
1209   }
1210   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1211   current = dst_as;
1212   while (current != NULL) {
1213     xbt_dynar_push(path_dst, &current);
1214     current = current->routing_father;
1215   }
1216
1217   /* (3) find the common father */
1218   index_src = path_src->used - 1;
1219   index_dst = path_dst->used - 1;
1220   current_src = xbt_dynar_get_ptr(path_src, index_src);
1221   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1222   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1223     routing_component_t *tmp_src, *tmp_dst;
1224     tmp_src = xbt_dynar_pop_ptr(path_src);
1225     tmp_dst = xbt_dynar_pop_ptr(path_dst);
1226     index_src--;
1227     index_dst--;
1228     current_src = xbt_dynar_get_ptr(path_src, index_src);
1229     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1230   }
1231
1232   int max_index_src = path_src->used - 1;
1233   int max_index_dst = path_dst->used - 1;
1234
1235   int max_index = max(max_index_src, max_index_dst);
1236   int i, max;
1237
1238   route_extended_t e_route_bypass = NULL;
1239
1240   for (max = 0; max <= max_index; max++) {
1241     for (i = 0; i < max; i++) {
1242       if (i <= max_index_src && max <= max_index_dst) {
1243         char *route_name = bprintf("%s#%s",
1244                                    (*(routing_component_t *)
1245                                     (xbt_dynar_get_ptr
1246                                      (path_src, i)))->name,
1247                                    (*(routing_component_t *)
1248                                     (xbt_dynar_get_ptr
1249                                      (path_dst, max)))->name);
1250         e_route_bypass =
1251             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1252         xbt_free(route_name);
1253       }
1254       if (e_route_bypass)
1255         break;
1256       if (max <= max_index_src && i <= max_index_dst) {
1257         char *route_name = bprintf("%s#%s",
1258                                    (*(routing_component_t *)
1259                                     (xbt_dynar_get_ptr
1260                                      (path_src, max)))->name,
1261                                    (*(routing_component_t *)
1262                                     (xbt_dynar_get_ptr
1263                                      (path_dst, i)))->name);
1264         e_route_bypass =
1265             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1266         xbt_free(route_name);
1267       }
1268       if (e_route_bypass)
1269         break;
1270     }
1271
1272     if (e_route_bypass)
1273       break;
1274
1275     if (max <= max_index_src && max <= max_index_dst) {
1276       char *route_name = bprintf("%s#%s",
1277                                  (*(routing_component_t *)
1278                                   (xbt_dynar_get_ptr
1279                                    (path_src, max)))->name,
1280                                  (*(routing_component_t *)
1281                                   (xbt_dynar_get_ptr
1282                                    (path_dst, max)))->name);
1283       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1284       xbt_free(route_name);
1285     }
1286     if (e_route_bypass)
1287       break;
1288   }
1289
1290   xbt_dynar_free(&path_src);
1291   xbt_dynar_free(&path_dst);
1292
1293   route_extended_t new_e_route = NULL;
1294
1295   if (e_route_bypass) {
1296     void *link;
1297     unsigned int cpt = 0;
1298     new_e_route = xbt_new0(s_route_extended_t, 1);
1299     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1300     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1301     new_e_route->generic_route.link_list =
1302         xbt_dynar_new(global_routing->size_of_link, NULL);
1303     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1304       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1305     }
1306   }
1307
1308   return new_e_route;
1309 }
1310
1311 /* ************************************************************************** */
1312 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1313
1314 route_t
1315 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1316                            void *data, int order)
1317 {
1318
1319   char *link_name;
1320   route_t new_route;
1321   unsigned int cpt;
1322   xbt_dynar_t links = NULL, links_id = NULL;
1323
1324   new_route = xbt_new0(s_route_t, 1);
1325   new_route->link_list =
1326       xbt_dynar_new(global_routing->size_of_link, NULL);
1327
1328   xbt_assert(hierarchy == SURF_ROUTING_BASE,
1329               "the hierarchy type is not SURF_ROUTING_BASE");
1330
1331   links = ((route_t) data)->link_list;
1332
1333
1334   links_id = new_route->link_list;
1335
1336   xbt_dynar_foreach(links, cpt, link_name) {
1337
1338     void *link =
1339                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1340     if (link) {
1341       if (order)
1342         xbt_dynar_push(links_id, &link);
1343       else
1344         xbt_dynar_unshift(links_id, &link);
1345     } else
1346       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1347   }
1348
1349   return new_route;
1350 }
1351
1352 route_extended_t
1353 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1354                            void *data, int order)
1355 {
1356
1357   char *link_name;
1358   route_extended_t e_route, new_e_route;
1359   route_t route;
1360   unsigned int cpt;
1361   xbt_dynar_t links = NULL, links_id = NULL;
1362
1363   new_e_route = xbt_new0(s_route_extended_t, 1);
1364   new_e_route->generic_route.link_list =
1365       xbt_dynar_new(global_routing->size_of_link, NULL);
1366   new_e_route->src_gateway = NULL;
1367   new_e_route->dst_gateway = NULL;
1368
1369   xbt_assert(hierarchy == SURF_ROUTING_BASE
1370               || hierarchy == SURF_ROUTING_RECURSIVE,
1371               "the hierarchy type is not defined");
1372
1373   if (hierarchy == SURF_ROUTING_BASE) {
1374
1375     route = (route_t) data;
1376     links = route->link_list;
1377
1378   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1379
1380     e_route = (route_extended_t) data;
1381     xbt_assert(e_route->src_gateway
1382                 && e_route->dst_gateway, "bad gateway, is null");
1383     links = e_route->generic_route.link_list;
1384
1385     /* remeber not erase the gateway names */
1386     new_e_route->src_gateway = strdup(e_route->src_gateway);
1387     new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1388   }
1389
1390   links_id = new_e_route->generic_route.link_list;
1391
1392   xbt_dynar_foreach(links, cpt, link_name) {
1393
1394     void *link =
1395                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1396     if (link) {
1397       if (order)
1398         xbt_dynar_push(links_id, &link);
1399       else
1400         xbt_dynar_unshift(links_id, &link);
1401     } else
1402       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1403   }
1404
1405   return new_e_route;
1406 }
1407
1408 void generic_free_route(route_t route)
1409 {
1410   if (route) {
1411     xbt_dynar_free(&(route->link_list));
1412     xbt_free(route);
1413   }
1414 }
1415
1416 void generic_free_extended_route(route_extended_t e_route)
1417 {
1418   if (e_route) {
1419     xbt_dynar_free(&(e_route->generic_route.link_list));
1420     if (e_route->src_gateway)
1421       xbt_free(e_route->src_gateway);
1422     if (e_route->dst_gateway)
1423       xbt_free(e_route->dst_gateway);
1424     xbt_free(e_route);
1425   }
1426 }
1427
1428 static routing_component_t generic_as_exist(routing_component_t find_from,
1429                                             routing_component_t to_find)
1430 {
1431   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1432   xbt_dict_cursor_t cursor = NULL;
1433   char *key;
1434   int found = 0;
1435   routing_component_t elem;
1436   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1437     if (to_find == elem || generic_as_exist(elem, to_find)) {
1438       found = 1;
1439       break;
1440     }
1441   }
1442   if (found)
1443     return to_find;
1444   return NULL;
1445 }
1446
1447 routing_component_t
1448 generic_autonomous_system_exist(routing_component_t rc, char *element)
1449 {
1450   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1451   routing_component_t element_as, result, elem;
1452   xbt_dict_cursor_t cursor = NULL;
1453   char *key;
1454   element_as = ((network_element_info_t)
1455                 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1456   result = ((routing_component_t) - 1);
1457   if (element_as != rc)
1458     result = generic_as_exist(rc, element_as);
1459
1460   int found = 0;
1461   if (result) {
1462     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1463       found = !strcmp(elem->name, element);
1464       if (found)
1465         break;
1466     }
1467     if (found)
1468       return element_as;
1469   }
1470   return NULL;
1471 }
1472
1473 routing_component_t
1474 generic_processing_units_exist(routing_component_t rc, char *element)
1475 {
1476   routing_component_t element_as;
1477   element_as = ((network_element_info_t)
1478                 xbt_lib_get_or_null(host_lib,
1479                  element, ROUTING_HOST_LEVEL))->rc_component;
1480   if (element_as == rc)
1481     return element_as;
1482   return generic_as_exist(rc, element_as);
1483 }
1484
1485 void generic_src_dst_check(routing_component_t rc, const char *src,
1486                                   const char *dst)
1487 {
1488
1489   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1490   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1491   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1492   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1493
1494   if(src_data == NULL || dst_data == NULL)
1495           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1496                      src, dst, rc->name);
1497
1498   routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1499   routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1500
1501   if(src_as != dst_as)
1502           xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1503               src, src_as->name, dst, dst_as->name);
1504   if(rc != dst_as)
1505          xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1506      src,dst,src_as->name, dst_as->name,rc->name);
1507 }
1508
1509 static void routing_parse_Sconfig(void)
1510 {
1511   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
1512 }
1513
1514 static void routing_parse_Econfig(void)
1515 {
1516   xbt_dict_cursor_t cursor = NULL;
1517   char *key;
1518   char *elem;
1519   char *cfg;
1520   xbt_dict_foreach(current_property_set, cursor, key, elem) {
1521           cfg = bprintf("%s:%s",key,elem);
1522           if(xbt_cfg_is_default_value(_surf_cfg_set, key))
1523                   xbt_cfg_set_parse(_surf_cfg_set, cfg);
1524           else
1525                   XBT_INFO("The custom configuration '%s' is already define by user!",key);
1526           free(cfg);
1527   }
1528   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
1529 }
1530
1531 void routing_parse_Scluster(void)
1532 {
1533   static int AX_ptr = 0;
1534
1535   char *cluster_id = A_surfxml_cluster_id;
1536   char *cluster_prefix = A_surfxml_cluster_prefix;
1537   char *cluster_suffix = A_surfxml_cluster_suffix;
1538   char *cluster_radical = A_surfxml_cluster_radical;
1539   char *cluster_power = A_surfxml_cluster_power;
1540   char *cluster_core = A_surfxml_cluster_core;
1541   char *cluster_bw = A_surfxml_cluster_bw;
1542   char *cluster_lat = A_surfxml_cluster_lat;
1543   char *temp_cluster_bw = NULL;
1544   char *temp_cluster_lat = NULL;
1545   char *temp_cluster_power = NULL;
1546   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
1547   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
1548   char *cluster_availability_file = A_surfxml_cluster_availability_file;
1549   char *cluster_state_file = A_surfxml_cluster_state_file;
1550   char *host_id, *groups, *link_id = NULL;
1551   char *router_id, *link_backbone;
1552   char *availability_file = xbt_strdup(cluster_availability_file);
1553   char *state_file = xbt_strdup(cluster_state_file);
1554
1555   if(xbt_dict_size(patterns)==0)
1556           patterns = xbt_dict_new();
1557
1558   xbt_dict_set(patterns,"id",cluster_id,NULL);
1559   xbt_dict_set(patterns,"prefix",cluster_prefix,NULL);
1560   xbt_dict_set(patterns,"suffix",cluster_suffix,NULL);
1561
1562 #ifdef HAVE_PCRE_LIB
1563   char *route_src_dst;
1564 #endif
1565   unsigned int iter;
1566   int start, end, i;
1567   xbt_dynar_t radical_elements;
1568   xbt_dynar_t radical_ends;
1569   int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
1570   int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
1571
1572 #ifndef HAVE_PCRE_LIB
1573   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
1574   char *route_src, *route_dst;
1575   int j;
1576 #endif
1577
1578   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1579   static unsigned int surfxml_buffer_stack_stack[1024];
1580
1581   surfxml_buffer_stack_stack[0] = 0;
1582
1583   surfxml_bufferstack_push(1);
1584
1585   SURFXML_BUFFER_SET(AS_id, cluster_id);
1586 #ifdef HAVE_PCRE_LIB
1587   SURFXML_BUFFER_SET(AS_routing, "RuleBased");
1588   XBT_DEBUG("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
1589 #else
1590   SURFXML_BUFFER_SET(AS_routing, "Full");
1591   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
1592 #endif
1593   SURFXML_START_TAG(AS);
1594
1595   radical_elements = xbt_str_split(cluster_radical, ",");
1596   xbt_dynar_foreach(radical_elements, iter, groups) {
1597     radical_ends = xbt_str_split(groups, "-");
1598     switch (xbt_dynar_length(radical_ends)) {
1599     case 1:
1600       surf_parse_get_int(&start,
1601                          xbt_dynar_get_as(radical_ends, 0, char *));
1602       host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
1603 #ifndef HAVE_PCRE_LIB
1604       xbt_dynar_push_as(tab_elements_num, int, start);
1605 #endif
1606       link_id = bprintf("%s_link_%d", cluster_id, start);
1607
1608       xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1609       temp_cluster_power = xbt_strdup(cluster_power);
1610       temp_cluster_power = replace_random_parameter(temp_cluster_power);
1611       XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, temp_cluster_power);
1612       A_surfxml_host_state = A_surfxml_host_state_ON;
1613       SURFXML_BUFFER_SET(host_id, host_id);
1614       SURFXML_BUFFER_SET(host_power, temp_cluster_power);
1615       SURFXML_BUFFER_SET(host_core, cluster_core);
1616       SURFXML_BUFFER_SET(host_availability, "1.0");
1617       SURFXML_BUFFER_SET(host_coordinates, "");
1618       xbt_free(availability_file);
1619       availability_file = xbt_strdup(cluster_availability_file);
1620       xbt_free(state_file);
1621       state_file = xbt_strdup(cluster_state_file);
1622       XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(availability_file,patterns));
1623       XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(state_file,patterns));
1624       SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(availability_file,patterns));
1625       SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(state_file,patterns));
1626       XBT_DEBUG("</host>");
1627       SURFXML_START_TAG(host);
1628       SURFXML_END_TAG(host);
1629
1630
1631       temp_cluster_bw = xbt_strdup(cluster_bw);
1632       temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
1633       temp_cluster_lat = xbt_strdup(cluster_lat);
1634       temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
1635       XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,temp_cluster_bw, cluster_lat);
1636       A_surfxml_link_state = A_surfxml_link_state_ON;
1637       A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1638       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1639           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1640       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1641           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1642       SURFXML_BUFFER_SET(link_id, link_id);
1643       SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
1644       SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
1645       SURFXML_BUFFER_SET(link_bandwidth_file, "");
1646       SURFXML_BUFFER_SET(link_latency_file, "");
1647       SURFXML_BUFFER_SET(link_state_file, "");
1648       SURFXML_START_TAG(link);
1649       SURFXML_END_TAG(link);
1650
1651       xbt_free(temp_cluster_bw);
1652       xbt_free(temp_cluster_lat);
1653       xbt_free(temp_cluster_power);
1654       free(link_id);
1655       free(host_id);
1656       break;
1657
1658     case 2:
1659
1660       surf_parse_get_int(&start,
1661                          xbt_dynar_get_as(radical_ends, 0, char *));
1662       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
1663       for (i = start; i <= end; i++) {
1664         host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
1665 #ifndef HAVE_PCRE_LIB
1666         xbt_dynar_push_as(tab_elements_num, int, i);
1667 #endif
1668         link_id = bprintf("%s_link_%d", cluster_id, i);
1669
1670         xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1671         temp_cluster_power = xbt_strdup(cluster_power);
1672         temp_cluster_power = replace_random_parameter(temp_cluster_power);
1673         XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, temp_cluster_power);
1674         A_surfxml_host_state = A_surfxml_host_state_ON;
1675         SURFXML_BUFFER_SET(host_id, host_id);
1676         SURFXML_BUFFER_SET(host_power, temp_cluster_power);
1677         SURFXML_BUFFER_SET(host_core, cluster_core);
1678         SURFXML_BUFFER_SET(host_availability, "1.0");
1679         SURFXML_BUFFER_SET(host_coordinates, "");
1680         xbt_free(availability_file);
1681         availability_file = xbt_strdup(cluster_availability_file);
1682         xbt_free(state_file);
1683         state_file = xbt_strdup(cluster_state_file);
1684         XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(availability_file,patterns));
1685         XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(state_file,patterns));
1686         SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(availability_file,patterns));
1687         SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(state_file,patterns));
1688         XBT_DEBUG("</host>");
1689         SURFXML_START_TAG(host);
1690         SURFXML_END_TAG(host);
1691
1692         xbt_free(temp_cluster_power);
1693
1694         temp_cluster_bw = xbt_strdup(cluster_bw);
1695         temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
1696         temp_cluster_lat = xbt_strdup(cluster_lat);
1697         temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
1698         XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,temp_cluster_bw, cluster_lat);
1699         A_surfxml_link_state = A_surfxml_link_state_ON;
1700         A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1701         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1702             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1703         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1704             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1705         SURFXML_BUFFER_SET(link_id, link_id);
1706         SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
1707         SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
1708         SURFXML_BUFFER_SET(link_bandwidth_file, "");
1709         SURFXML_BUFFER_SET(link_latency_file, "");
1710         SURFXML_BUFFER_SET(link_state_file, "");
1711         SURFXML_START_TAG(link);
1712         SURFXML_END_TAG(link);
1713
1714         xbt_free(temp_cluster_bw);
1715         xbt_free(temp_cluster_lat);
1716         free(link_id);
1717         free(host_id);
1718       }
1719       break;
1720
1721     default:
1722       XBT_DEBUG("Malformed radical");
1723     }
1724
1725     xbt_dynar_free(&radical_ends);
1726   }
1727   xbt_dynar_free(&radical_elements);
1728
1729   XBT_DEBUG(" ");
1730   router_id =
1731       bprintf("%s%s_router%s", cluster_prefix, cluster_id,
1732               cluster_suffix);
1733   //link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
1734   link_backbone = bprintf("%s_backbone", cluster_id);
1735
1736   XBT_DEBUG("<router id=\"%s\"/>", router_id);
1737   SURFXML_BUFFER_SET(router_id, router_id);
1738   SURFXML_BUFFER_SET(router_coordinates, "");
1739   SURFXML_START_TAG(router);
1740   SURFXML_END_TAG(router);
1741
1742   //TODO
1743 //  xbt_dict_set(patterns, "radical", xbt_strdup("_router"), xbt_free);
1744 //  temp_cluster_bw = xbt_strdup(cluster_bw);
1745 //  temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
1746 //  temp_cluster_lat = xbt_strdup(cluster_lat);
1747 //  temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
1748 //  XBT_DEBUG("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_router,temp_cluster_bw, temp_cluster_lat);
1749 //  A_surfxml_link_state = A_surfxml_link_state_ON;
1750 //  A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1751 //  if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1752 //  {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1753 //  if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1754 //  {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1755 //  SURFXML_BUFFER_SET(link_id, link_router);
1756 //  SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
1757 //  SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
1758 //  SURFXML_BUFFER_SET(link_bandwidth_file, "");
1759 //  SURFXML_BUFFER_SET(link_latency_file, "");
1760 //  SURFXML_BUFFER_SET(link_state_file, "");
1761 //  SURFXML_START_TAG(link);
1762 //  SURFXML_END_TAG(link);
1763
1764 //  xbt_free(temp_cluster_bw);
1765 //  xbt_free(temp_cluster_lat);
1766
1767   XBT_DEBUG("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bb_bw, cluster_bb_lat);
1768   A_surfxml_link_state = A_surfxml_link_state_ON;
1769   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1770   if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
1771   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1772   SURFXML_BUFFER_SET(link_id, link_backbone);
1773   SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
1774   SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
1775   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1776   SURFXML_BUFFER_SET(link_latency_file, "");
1777   SURFXML_BUFFER_SET(link_state_file, "");
1778   SURFXML_START_TAG(link);
1779   SURFXML_END_TAG(link);
1780
1781   XBT_DEBUG(" ");
1782
1783 #ifdef HAVE_PCRE_LIB
1784   char *new_suffix = xbt_strdup("");
1785
1786   radical_elements = xbt_str_split(cluster_suffix, ".");
1787   xbt_dynar_foreach(radical_elements, iter, groups) {
1788     if (strcmp(groups, "")) {
1789       char *old_suffix = new_suffix;
1790       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1791       free(old_suffix);
1792     }
1793   }
1794   route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
1795   xbt_dynar_free(&radical_elements);
1796   free(new_suffix);
1797
1798   char *pcre_link_src = bprintf("%s_link_$1src", cluster_id);
1799   char *pcre_link_backbone = bprintf("%s_backbone", cluster_id);
1800   char *pcre_link_dst = bprintf("%s_link_$1dst", cluster_id);
1801
1802   //from router to router
1803   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, router_id);
1804   XBT_DEBUG("symmetrical=\"NO\">");
1805   SURFXML_BUFFER_SET(route_src, router_id);
1806   SURFXML_BUFFER_SET(route_dst, router_id);
1807   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1808   SURFXML_START_TAG(route);
1809
1810   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1811   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1812   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1813   SURFXML_START_TAG(link_ctn);
1814   SURFXML_END_TAG(link_ctn);
1815
1816   XBT_DEBUG("</route>");
1817   SURFXML_END_TAG(route);
1818
1819   //from host to router
1820   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, router_id);
1821   XBT_DEBUG("symmetrical=\"NO\">");
1822   SURFXML_BUFFER_SET(route_src, route_src_dst);
1823   SURFXML_BUFFER_SET(route_dst, router_id);
1824   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1825   SURFXML_START_TAG(route);
1826
1827   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_src);
1828   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_src);
1829   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1830   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1831   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1832   SURFXML_START_TAG(link_ctn);
1833   SURFXML_END_TAG(link_ctn);
1834
1835   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1836   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1837   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1838   SURFXML_START_TAG(link_ctn);
1839   SURFXML_END_TAG(link_ctn);
1840
1841   XBT_DEBUG("</route>");
1842   SURFXML_END_TAG(route);
1843
1844   //from router to host
1845   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, route_src_dst);
1846   XBT_DEBUG("symmetrical=\"NO\">");
1847   SURFXML_BUFFER_SET(route_src, router_id);
1848   SURFXML_BUFFER_SET(route_dst, route_src_dst);
1849   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1850   SURFXML_START_TAG(route);
1851
1852   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1853   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1854   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1855   SURFXML_START_TAG(link_ctn);
1856   SURFXML_END_TAG(link_ctn);
1857
1858   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_dst);
1859   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_dst);
1860   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1861   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1862   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1863   SURFXML_START_TAG(link_ctn);
1864   SURFXML_END_TAG(link_ctn);
1865
1866   XBT_DEBUG("</route>");
1867   SURFXML_END_TAG(route);
1868
1869   //from host to host
1870   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
1871   XBT_DEBUG("symmetrical=\"NO\">");
1872   SURFXML_BUFFER_SET(route_src, route_src_dst);
1873   SURFXML_BUFFER_SET(route_dst, route_src_dst);
1874   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1875   SURFXML_START_TAG(route);
1876
1877   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_src);
1878   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_src);
1879   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1880   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1881   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1882   SURFXML_START_TAG(link_ctn);
1883   SURFXML_END_TAG(link_ctn);
1884
1885   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1886   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1887   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1888   SURFXML_START_TAG(link_ctn);
1889   SURFXML_END_TAG(link_ctn);
1890
1891   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_dst);
1892   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_dst);
1893   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1894   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1895   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
1896   SURFXML_START_TAG(link_ctn);
1897   SURFXML_END_TAG(link_ctn);
1898
1899   XBT_DEBUG("</route>");
1900   SURFXML_END_TAG(route);
1901
1902   free(pcre_link_dst);
1903   free(pcre_link_backbone);
1904   free(pcre_link_src);
1905   free(route_src_dst);
1906 #else
1907   for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
1908     for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
1909       if (i == xbt_dynar_length(tab_elements_num)) {
1910         route_src = router_id;
1911       } else {
1912         route_src =
1913             bprintf("%s%d%s", cluster_prefix,
1914                     xbt_dynar_get_as(tab_elements_num, i, int),
1915                     cluster_suffix);
1916       }
1917
1918       if (j == xbt_dynar_length(tab_elements_num)) {
1919         route_dst = router_id;
1920       } else {
1921         route_dst =
1922             bprintf("%s%d%s", cluster_prefix,
1923                     xbt_dynar_get_as(tab_elements_num, j, int),
1924                     cluster_suffix);
1925       }
1926
1927       XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src, route_dst);
1928       XBT_DEBUG("symmetrical=\"NO\">");
1929       SURFXML_BUFFER_SET(route_src, route_src);
1930       SURFXML_BUFFER_SET(route_dst, route_dst);
1931       A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1932       SURFXML_START_TAG(route);
1933
1934       if (i != xbt_dynar_length(tab_elements_num)){
1935           route_src =
1936                 bprintf("%s_link_%d", cluster_id,
1937                         xbt_dynar_get_as(tab_elements_num, i, int));
1938                   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", route_src);
1939                   SURFXML_BUFFER_SET(link_ctn_id, route_src);
1940                   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1941                   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1942                   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1943                   SURFXML_START_TAG(link_ctn);
1944                   SURFXML_END_TAG(link_ctn);
1945                   free(route_src);
1946       }
1947
1948       XBT_DEBUG("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
1949       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
1950       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1951       SURFXML_START_TAG(link_ctn);
1952       SURFXML_END_TAG(link_ctn);
1953
1954       if (j != xbt_dynar_length(tab_elements_num)) {
1955           route_dst =
1956                 bprintf("%s_link_%d", cluster_id,
1957                         xbt_dynar_get_as(tab_elements_num, j, int));
1958                   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", route_dst);
1959                   SURFXML_BUFFER_SET(link_ctn_id, route_dst);
1960                   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1961                   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1962                   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
1963                   SURFXML_START_TAG(link_ctn);
1964                   SURFXML_END_TAG(link_ctn);
1965                   free(route_dst);
1966       }
1967
1968       XBT_DEBUG("</route>");
1969       SURFXML_END_TAG(route);
1970     }
1971   }
1972   xbt_dynar_free(&tab_elements_num);
1973
1974 #endif
1975
1976   free(router_id);
1977   free(link_backbone);
1978   //free(link_router);
1979   xbt_dict_free(&patterns);
1980   free(availability_file);
1981   free(state_file);
1982
1983   XBT_DEBUG("</AS>");
1984   SURFXML_END_TAG(AS);
1985   XBT_DEBUG(" ");
1986
1987   surfxml_bufferstack_pop(1);
1988 }
1989 /*
1990  * This function take a string and replace parameters from patterns dict.
1991  * It returns the new value.
1992  */
1993 static char* replace_random_parameter(char * string)
1994 {
1995   char *test_string = NULL;
1996
1997   if(xbt_dict_size(random_value)==0)
1998     return string;
1999
2000   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
2001   test_string = bprintf("${%s}", string);
2002   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
2003
2004   if (strcmp(test_string,"")) { //if not empty, keep this value.
2005     xbt_free(string);
2006     string = test_string;
2007   } //In other case take old value (without ${})
2008   else
2009         free(test_string);
2010   return string;
2011 }
2012
2013 static void clean_dict_random(void)
2014 {
2015         xbt_dict_free(&random_value);
2016         xbt_dict_free(&patterns);
2017 }
2018
2019 static void routing_parse_Speer(void)
2020 {
2021   static int AX_ptr = 0;
2022
2023   char *peer_id = A_surfxml_peer_id;
2024   char *peer_power = A_surfxml_peer_power;
2025   char *peer_bw_in = A_surfxml_peer_bw_in;
2026   char *peer_bw_out = A_surfxml_peer_bw_out;
2027   char *peer_lat = A_surfxml_peer_lat;
2028   char *peer_coord = A_surfxml_peer_coordinates;
2029   char *peer_state_file = A_surfxml_peer_state_file;
2030   char *peer_availability_file = A_surfxml_peer_availability_file;
2031
2032   char *host_id = NULL;
2033   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
2034
2035   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
2036   static unsigned int surfxml_buffer_stack_stack[1024];
2037
2038   surfxml_buffer_stack_stack[0] = 0;
2039
2040   surfxml_bufferstack_push(1);
2041
2042   SURFXML_BUFFER_SET(AS_id, peer_id);
2043
2044   SURFXML_BUFFER_SET(AS_routing, "Full");
2045   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", peer_id);
2046
2047   SURFXML_START_TAG(AS);
2048
2049   XBT_DEBUG(" ");
2050   host_id = bprintf("peer_%s", peer_id);
2051   router_id = bprintf("router_%s", peer_id);
2052   link_id_up = bprintf("link_%s_up", peer_id);
2053   link_id_down = bprintf("link_%s_down", peer_id);
2054
2055   link_router = bprintf("%s_link_router", peer_id);
2056   link_backbone = bprintf("%s_backbone", peer_id);
2057
2058   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, peer_power);
2059   A_surfxml_host_state = A_surfxml_host_state_ON;
2060   SURFXML_BUFFER_SET(host_id, host_id);
2061   SURFXML_BUFFER_SET(host_power, peer_power);
2062   SURFXML_BUFFER_SET(host_availability, "1.0");
2063   SURFXML_BUFFER_SET(host_availability_file, peer_availability_file);
2064   SURFXML_BUFFER_SET(host_state_file, peer_state_file);
2065   SURFXML_BUFFER_SET(host_coordinates, "");
2066   SURFXML_START_TAG(host);
2067   SURFXML_END_TAG(host);
2068
2069   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, peer_coord);
2070   SURFXML_BUFFER_SET(router_id, router_id);
2071   SURFXML_BUFFER_SET(router_coordinates, peer_coord);
2072   SURFXML_START_TAG(router);
2073   SURFXML_END_TAG(router);
2074
2075   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_up, peer_bw_in, peer_lat);
2076   A_surfxml_link_state = A_surfxml_link_state_ON;
2077   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
2078   SURFXML_BUFFER_SET(link_id, link_id_up);
2079   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_in);
2080   SURFXML_BUFFER_SET(link_latency, peer_lat);
2081   SURFXML_BUFFER_SET(link_bandwidth_file, "");
2082   SURFXML_BUFFER_SET(link_latency_file, "");
2083   SURFXML_BUFFER_SET(link_state_file, "");
2084   SURFXML_START_TAG(link);
2085   SURFXML_END_TAG(link);
2086
2087   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_down, peer_bw_out, peer_lat);
2088   A_surfxml_link_state = A_surfxml_link_state_ON;
2089   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
2090   SURFXML_BUFFER_SET(link_id, link_id_down);
2091   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_out);
2092   SURFXML_BUFFER_SET(link_latency, peer_lat);
2093   SURFXML_BUFFER_SET(link_bandwidth_file, "");
2094   SURFXML_BUFFER_SET(link_latency_file, "");
2095   SURFXML_BUFFER_SET(link_state_file, "");
2096   SURFXML_START_TAG(link);
2097   SURFXML_END_TAG(link);
2098
2099   XBT_DEBUG(" ");
2100
2101   // begin here
2102   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
2103   XBT_DEBUG("symmetrical=\"NO\">");
2104   SURFXML_BUFFER_SET(route_src, host_id);
2105   SURFXML_BUFFER_SET(route_dst, router_id);
2106   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
2107   SURFXML_START_TAG(route);
2108
2109   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
2110   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
2111   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
2112   SURFXML_START_TAG(link_ctn);
2113   SURFXML_END_TAG(link_ctn);
2114
2115   XBT_DEBUG("</route>");
2116   SURFXML_END_TAG(route);
2117
2118   //Opposite Route
2119   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
2120   XBT_DEBUG("symmetrical=\"NO\">");
2121   SURFXML_BUFFER_SET(route_src, router_id);
2122   SURFXML_BUFFER_SET(route_dst, host_id);
2123   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
2124   SURFXML_START_TAG(route);
2125
2126   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
2127   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
2128   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
2129   SURFXML_START_TAG(link_ctn);
2130   SURFXML_END_TAG(link_ctn);
2131
2132   XBT_DEBUG("</route>");
2133   SURFXML_END_TAG(route);
2134
2135   XBT_DEBUG("</AS>");
2136   SURFXML_END_TAG(AS);
2137   XBT_DEBUG(" ");
2138
2139   //xbt_dynar_free(&tab_elements_num);
2140         free(host_id);
2141         free(router_id);
2142         free(link_router);
2143         free(link_backbone);
2144         free(link_id_up);
2145         free(link_id_down);
2146   surfxml_bufferstack_pop(1);
2147 }
2148
2149 static void routing_parse_Srandom(void)
2150 {
2151           double mean, std, min, max, seed;
2152           char *random_id = A_surfxml_random_id;
2153           char *random_radical = A_surfxml_random_radical;
2154           char *rd_name = NULL;
2155           char *rd_value;
2156           surf_parse_get_double(&mean,A_surfxml_random_mean);
2157           surf_parse_get_double(&std,A_surfxml_random_std_deviation);
2158           surf_parse_get_double(&min,A_surfxml_random_min);
2159           surf_parse_get_double(&max,A_surfxml_random_max);
2160           surf_parse_get_double(&seed,A_surfxml_random_seed);
2161
2162           double res = 0;
2163           int i = 0;
2164           random_data_t random = xbt_new0(s_random_data_t, 1);
2165           char *tmpbuf;
2166
2167           xbt_dynar_t radical_elements;
2168           unsigned int iter;
2169           char *groups;
2170           int start, end;
2171           xbt_dynar_t radical_ends;
2172
2173           random->generator = A_surfxml_random_generator;
2174           random->seed = seed;
2175           random->min = min;
2176           random->max = max;
2177
2178           /* Check user stupidities */
2179           if (max < min)
2180             THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
2181           if (mean < min)
2182             THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
2183                    min);
2184           if (mean > max)
2185             THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
2186                    max);
2187
2188           /* normalize the mean and standard deviation before storing */
2189           random->mean = (mean - min) / (max - min);
2190           random->std = std / (max - min);
2191
2192           if (random->mean * (1 - random->mean) < random->std * random->std)
2193             THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
2194                    random->mean, random->std);
2195
2196           XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
2197           random_id,
2198           random->min,
2199           random->max,
2200           random->mean,
2201           random->std,
2202           random->generator,
2203           random->seed,
2204           random_radical);
2205
2206           if(xbt_dict_size(random_value)==0)
2207                   random_value = xbt_dict_new();
2208
2209           if(!strcmp(random_radical,""))
2210           {
2211                   res = random_generate(random);
2212                   rd_value = bprintf("%f",res);
2213                   xbt_dict_set(random_value, random_id, rd_value, free);
2214           }
2215           else
2216           {
2217                   radical_elements = xbt_str_split(random_radical, ",");
2218                   xbt_dynar_foreach(radical_elements, iter, groups) {
2219                         radical_ends = xbt_str_split(groups, "-");
2220                         switch (xbt_dynar_length(radical_ends)) {
2221                         case 1:
2222                                           xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
2223                                           res = random_generate(random);
2224                                           tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
2225                                           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2226                                           xbt_free(tmpbuf);
2227                                           break;
2228
2229                         case 2:   surf_parse_get_int(&start,
2230                                                                                  xbt_dynar_get_as(radical_ends, 0, char *));
2231                                           surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
2232                                           for (i = start; i <= end; i++) {
2233                                                   xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
2234                                                   res = random_generate(random);
2235                           tmpbuf = bprintf("%s%d",random_id,i);
2236                                                   xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2237                           xbt_free(tmpbuf);
2238                                           }
2239                                           break;
2240                         default:
2241                                 XBT_INFO("Malformed radical");
2242                         }
2243                         res = random_generate(random);
2244                         rd_name  = bprintf("%s_router",random_id);
2245                         rd_value = bprintf("%f",res);
2246                         xbt_dict_set(random_value, rd_name, rd_value, free);
2247
2248                         xbt_dynar_free(&radical_ends);
2249                   }
2250                   free(rd_name);
2251                   xbt_dynar_free(&radical_elements);
2252           }
2253 }
2254
2255 static void routing_parse_Erandom(void)
2256 {
2257         xbt_dict_cursor_t cursor = NULL;
2258         char *key;
2259         char *elem;
2260
2261         xbt_dict_foreach(random_value, cursor, key, elem) {
2262           XBT_DEBUG("%s = %s",key,elem);
2263         }
2264
2265 }
2266
2267
2268 /*
2269  * New methods to init the routing model component from the lua script
2270  */
2271
2272 /*
2273  * calling parse_S_AS_lua with lua values
2274  */
2275 void routing_AS_init(const char *AS_id, const char *AS_routing)
2276 {
2277   parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
2278 }
2279
2280 /*
2281  * calling parse_E_AS_lua to fisnish the creation of routing component
2282  */
2283 void routing_AS_end(const char *AS_id)
2284 {
2285   parse_E_AS_lua((char *) AS_id);
2286 }
2287
2288 /*
2289  * add a host to the network element list
2290  */
2291
2292 void routing_add_host(const char *host_id)
2293 {
2294   parse_S_host_lua((char *) host_id, (char*)""); // FIXME propagate coordinate system to lua
2295 }
2296
2297 /*
2298  * Set a new link on the actual list of link for a route or ASroute
2299  */
2300 void routing_add_link(const char *link_id)
2301 {
2302   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
2303 }
2304
2305 /*
2306  *Set the endpoints for a route
2307  */
2308 void routing_set_route(const char *src_id, const char *dst_id)
2309 {
2310   parse_S_route_new_and_endpoints_lua(src_id, dst_id);
2311 }
2312
2313 /*
2314  * Store the route by calling parse_E_route_store_route
2315  */
2316 void routing_store_route(void)
2317 {
2318   parse_E_route_store_route();
2319 }