Logo AND Algorithmique Numérique Distribuée

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