Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Optimisation of get_route.
[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 xbt_dynar_t elements_father(const char *src, const char *dst)
580 {
581
582   xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
583
584   xbt_dynar_t result = xbt_dynar_new(sizeof(char *), NULL);
585
586   routing_component_t src_as, dst_as;
587   int index_src, index_dst, index_father_src, index_father_dst;
588   xbt_dynar_t path_src = NULL;
589   xbt_dynar_t path_dst = NULL;
590   routing_component_t current = NULL;
591   routing_component_t *current_src = NULL;
592   routing_component_t *current_dst = NULL;
593   routing_component_t *father = NULL;
594
595   /* (1) find the as where the src and dst are located */
596   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
597   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
598   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
599   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
600   src_as = ((network_element_info_t)src_data)->rc_component;
601   dst_as = ((network_element_info_t)dst_data)->rc_component;
602
603   xbt_assert(src_as
604               && dst_as,
605               "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
606               dst);
607
608   /* (2) find the path to the root routing component */
609   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
610   current = src_as;
611   while (current != NULL) {
612     xbt_dynar_push(path_src, &current);
613     current = current->routing_father;
614   }
615   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
616   current = dst_as;
617   while (current != NULL) {
618     xbt_dynar_push(path_dst, &current);
619     current = current->routing_father;
620   }
621
622   /* (3) find the common father */
623   index_src = path_src->used - 1;
624   index_dst = path_dst->used - 1;
625   current_src = xbt_dynar_get_ptr(path_src, index_src);
626   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
627   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
628     current_src = xbt_dynar_get_ptr(path_src, index_src);
629     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
630     index_src--;
631     index_dst--;
632   }
633   index_src++;
634   index_dst++;
635   current_src = xbt_dynar_get_ptr(path_src, index_src);
636   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
637
638   /* (4) they are not in the same routing component, make the path */
639   index_father_src = index_src + 1;
640   index_father_dst = index_dst + 1;
641
642   if (*current_src == *current_dst)
643     father = current_src;
644   else
645     father = xbt_dynar_get_ptr(path_src, index_father_src);
646
647   /* (5) result generation */
648   xbt_dynar_push(result, father);       /* first same the father of src and dst */
649   xbt_dynar_push(result, current_src);  /* second the first different father of src */
650   xbt_dynar_push(result, current_dst);  /* three  the first different father of dst */
651
652   xbt_dynar_free(&path_src);
653   xbt_dynar_free(&path_dst);
654
655   return result;
656 }
657
658 /* Global Business methods */
659
660 /**
661  * \brief Recursive function for get_route
662  *
663  * \param src the source host name 
664  * \param dst the destination host name
665  * \param *e_route the route where the links are stored
666  * 
667  * This function is called by "get_route". It allows to walk recursively
668  * through the routing components tree.
669  */
670 static void _get_route(const char *src, const char *dst,route_extended_t *e_route)
671 {
672   XBT_DEBUG("Solve route  \"%s\" to \"%s\"", src, dst);
673   xbt_assert(src && dst, "bad parameters for \"_get_route\" method");
674
675   xbt_dynar_t elem_father_list = elements_father(src, dst);
676
677   routing_component_t common_father =
678       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
679   routing_component_t src_father =
680       xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
681   routing_component_t dst_father =
682       xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
683
684   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
685
686         (*e_route) =
687           (*(common_father->get_route)) (common_father, src, dst);
688         xbt_assert((*e_route), "no route between \"%s\" and \"%s\"", src,
689                           dst);
690
691   } else {                      /* SURF_ROUTING_RECURSIVE */
692
693         void *link;
694         unsigned int cpt = 0;
695
696         route_extended_t e_route_cnt = NULL;
697         route_extended_t e_route_src = NULL;
698         route_extended_t e_route_dst = NULL;
699     route_extended_t e_route_bypass = NULL;
700
701     (*e_route) = xbt_new0(s_route_extended_t, 1);
702     (*e_route)->src_gateway = NULL;
703     (*e_route)->dst_gateway = NULL;
704     (*e_route)->generic_route.link_list =
705         xbt_dynar_new(global_routing->size_of_link, NULL);
706
707     if (common_father->get_bypass_route)
708       e_route_bypass =
709           (*(common_father->get_bypass_route)) (common_father, src, dst);
710
711     if (e_route_bypass)
712       e_route_cnt = e_route_bypass;
713     else
714       e_route_cnt =
715           (*(common_father->get_route)) (common_father, src_father->name,
716                                          dst_father->name);
717
718     xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
719                 src_father->name, dst_father->name);
720
721     xbt_assert((e_route_cnt->src_gateway == NULL) ==
722                 (e_route_cnt->dst_gateway == NULL),
723                 "bad gateway for route between \"%s\" and \"%s\"", src,
724                 dst);
725
726     if (strcmp(src, e_route_cnt->src_gateway)) {
727       _get_route(src, e_route_cnt->src_gateway, &e_route_src);
728       xbt_assert(e_route_src, "no route between \"%s\" and \"%s\"", src,
729                   e_route_cnt->src_gateway);
730       xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
731         xbt_dynar_push((*e_route)->generic_route.link_list, &link);
732       }
733     }
734
735     xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
736       xbt_dynar_push((*e_route)->generic_route.link_list, &link);
737     }
738
739     if (strcmp(e_route_cnt->dst_gateway, dst)) {
740       _get_route(e_route_cnt->dst_gateway, dst, &e_route_dst);
741       xbt_assert(e_route_dst, "no route between \"%s\" and \"%s\"",
742                   e_route_cnt->dst_gateway, dst);
743       xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
744         xbt_dynar_push((*e_route)->generic_route.link_list, &link);
745       }
746     }
747
748     (*e_route)->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
749     (*e_route)->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
750
751     generic_free_extended_route(e_route_src);
752     generic_free_extended_route(e_route_cnt);
753     generic_free_extended_route(e_route_dst);
754   }
755
756   xbt_dynar_free(&elem_father_list);
757 }
758
759 static double _get_latency(const char *src, const char *dst)
760 {
761   double latency, latency_src, latency_dst = 0.0;
762
763   XBT_DEBUG("Solve route  \"%s\" to \"%s\"", src, dst);
764   xbt_assert(src && dst, "bad parameters for \"_get_route\" method");
765
766   route_extended_t e_route_cnt;
767
768   xbt_dynar_t elem_father_list = elements_father(src, dst);
769
770   routing_component_t common_father =
771       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
772   routing_component_t src_father =
773       xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
774   routing_component_t dst_father =
775       xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
776
777   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
778
779       latency =
780           (*(common_father->get_latency)) (common_father, src, dst, NULL);
781       xbt_assert(latency>=0, "no route between \"%s\" and \"%s\"", src,
782                   dst);
783
784   } else {                      /* SURF_ROUTING_RECURSIVE */
785      route_extended_t e_route_bypass = NULL;
786     if (common_father->get_bypass_route)
787       e_route_bypass =
788           (*(common_father->get_bypass_route)) (common_father, src, dst);
789
790     xbt_assert(!e_route_bypass,"Bypass cannot work yet with get_latency");
791                                                  
792     e_route_cnt =
793           (*(common_father->get_route)) (common_father, src_father->name,
794                                          dst_father->name);
795
796     xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
797                 src_father->name, dst_father->name);
798
799     xbt_assert((e_route_cnt->src_gateway == NULL) ==
800                 (e_route_cnt->dst_gateway == NULL),
801                 "bad gateway for route between \"%s\" and \"%s\"", src,
802                 dst);
803
804     latency = (*(common_father->get_latency)) (common_father, src_father->name, dst_father->name, e_route_cnt);
805
806     xbt_assert(latency>=0, "no route between \"%s\" and \"%s\"",
807                 src_father->name, dst_father->name);
808
809     if (strcmp(src,e_route_cnt->src_gateway)) {
810
811       latency_src = _get_latency(src, e_route_cnt->src_gateway);
812       xbt_assert(latency_src>=0, "no route between \"%s\" and \"%s\"", src,
813                   e_route_cnt->src_gateway);
814       latency += latency_src;
815     }
816
817     if (strcmp(e_route_cnt->dst_gateway,dst)) {
818     
819       latency_dst = _get_latency(e_route_cnt->dst_gateway, dst);
820       xbt_assert(latency_dst>=0, "no route between \"%s\" and \"%s\"",
821                   e_route_cnt->dst_gateway, dst);
822       latency += latency_dst;
823     }
824         
825   }
826
827   xbt_dynar_free(&elem_father_list);
828
829   return latency;
830 }
831
832 /**
833  * \brief Generic method: find a route between hosts
834  *
835  * \param src the source host name 
836  * \param dst the destination host name
837  * 
838  * walk through the routing components tree and find a route between hosts
839  * by calling the differents "get_route" functions in each routing component.
840  * No need to free the returned dynar. It will be freed at the next call.
841  */
842 static xbt_dynar_t get_route(const char *src, const char *dst)
843 {
844
845   route_extended_t e_route = NULL;
846
847   _get_route(src, dst, &e_route);
848   xbt_assert(e_route, "no route between \"%s\" and \"%s\"", src, dst);
849
850   if (global_routing->last_route)
851     xbt_dynar_free(&(global_routing->last_route));
852   global_routing->last_route = e_route->generic_route.link_list;
853
854   if (e_route->src_gateway)
855     xbt_free(e_route->src_gateway);
856   if (e_route->dst_gateway)
857     xbt_free(e_route->dst_gateway);
858
859   xbt_free(e_route);
860
861   return global_routing->last_route;
862 }
863
864 /**
865  * \brief Generic method: find a route between hosts
866  *
867  * \param src the source host name
868  * \param dst the destination host name
869  *
870  * walk through the routing components tree and find a route between hosts
871  * by calling the differents "get_route" functions in each routing component.
872  * Leaves the caller the responsability to clean the returned dynar.
873  */
874 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
875 {
876         xbt_dynar_t d = get_route(src,dst);
877         global_routing->last_route = NULL;
878         return d;
879 }
880
881 /*Get Latency*/
882 static double get_latency(const char *src, const char *dst)
883 {
884
885   double latency = -1.0;
886   latency = _get_latency(src, dst);
887   xbt_assert(latency>=0.0, "no route between \"%s\" and \"%s\"", src, dst);
888   return latency;
889 }
890
891 /**
892  * \brief Recursive function for finalize
893  *
894  * \param rc the source host name 
895  * 
896  * This fuction is call by "finalize". It allow to finalize the 
897  * AS or routing components. It delete all the structures.
898  */
899 static void _finalize(routing_component_t rc)
900 {
901   if (rc) {
902     xbt_dict_cursor_t cursor = NULL;
903     char *key;
904     routing_component_t elem;
905     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
906       _finalize(elem);
907     }
908     xbt_dict_t tmp_sons = rc->routing_sons;
909     char *tmp_name = rc->name;
910     xbt_dict_free(&tmp_sons);
911     xbt_free(tmp_name);
912     xbt_assert(rc->finalize, "no defined method \"finalize\" in \"%s\"",
913                 current_routing->name);
914     (*(rc->finalize)) (rc);
915   }
916 }
917
918 /**
919  * \brief Generic method: delete all the routing structures
920  * 
921  * walk through the routing components tree and delete the structures
922  * by calling the differents "finalize" functions in each routing component
923  */
924 static void finalize(void)
925 {
926   /* delete recursibly all the tree */
927   _finalize(global_routing->root);
928   /* delete last_route */
929   xbt_dynar_free(&(global_routing->last_route));
930   /* delete global routing structure */
931   xbt_free(global_routing);
932 }
933
934 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
935 {
936   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
937
938   //adding my one link routes
939   unsigned int cpt;
940   void *link;
941   xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
942   if (onelink_mine) {
943     xbt_dynar_foreach(onelink_mine, cpt, link) {
944       xbt_dynar_push(ret, &link);
945     }
946   }
947   //recursing
948   char *key;
949   xbt_dict_cursor_t cursor = NULL;
950   routing_component_t rc_child;
951   xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
952     xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
953     if (onelink_child) {
954       xbt_dynar_foreach(onelink_child, cpt, link) {
955         xbt_dynar_push(ret, &link);
956       }
957     }
958   }
959   return ret;
960 }
961
962 static xbt_dynar_t get_onelink_routes(void)
963 {
964   return recursive_get_onelink_routes(global_routing->root);
965 }
966
967 e_surf_network_element_type_t get_network_element_type(const char
968                                                               *name)
969 {
970   network_element_info_t rc = NULL;
971
972   rc = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
973   if(rc) return rc->rc_type;
974
975   rc = xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
976   if(rc) return rc->rc_type;
977
978   return SURF_NETWORK_ELEMENT_NULL;
979 }
980
981 /**
982  * \brief Generic method: create the global routing schema
983  * 
984  * Make a global routing structure and set all the parsing functions.
985  */
986 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
987 {
988   /* config the uniq global routing */
989   global_routing = xbt_new0(s_routing_global_t, 1);
990   global_routing->root = NULL;
991   global_routing->get_route = get_route;
992   global_routing->get_latency = get_latency;
993   global_routing->get_route_no_cleanup = get_route_no_cleanup;
994   global_routing->get_onelink_routes = get_onelink_routes;
995   global_routing->get_network_element_type = get_network_element_type;
996   global_routing->finalize = finalize;
997   global_routing->loopback = loopback;
998   global_routing->size_of_link = size_of_links;
999   global_routing->last_route = NULL;
1000   get_link_latency = get_link_latency_fun;
1001   /* no current routing at moment */
1002   current_routing = NULL;
1003
1004   /* parse generic elements */
1005   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
1006   surfxml_add_callback(ETag_surfxml_host_cb_list, &parse_E_host_XML);
1007   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router_XML);
1008
1009   surfxml_add_callback(STag_surfxml_route_cb_list,
1010                        &parse_S_route_new_and_endpoints_XML);
1011   surfxml_add_callback(STag_surfxml_ASroute_cb_list,
1012                        &parse_S_ASroute_new_and_endpoints);
1013   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
1014                        &parse_S_bypassRoute_new_and_endpoints);
1015
1016   surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
1017                        &parse_E_link_ctn_new_elem_XML);
1018
1019   surfxml_add_callback(ETag_surfxml_route_cb_list,
1020                        &parse_E_route_store_route);
1021   surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
1022                        &parse_E_ASroute_store_route);
1023   surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
1024                        &parse_E_bypassRoute_store_route);
1025
1026   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
1027   surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
1028
1029   surfxml_add_callback(STag_surfxml_cluster_cb_list,
1030                        &routing_parse_Scluster);
1031
1032   surfxml_add_callback(STag_surfxml_peer_cb_list,
1033                          &routing_parse_Speer);
1034
1035   surfxml_add_callback(ETag_surfxml_platform_cb_list,
1036                                                   &clean_dict_random);
1037
1038 #ifdef HAVE_TRACING
1039   instr_routing_define_callbacks();
1040 #endif
1041 }
1042
1043 void surf_parse_add_callback_config(void)
1044 {
1045         surfxml_add_callback(STag_surfxml_config_cb_list, &routing_parse_Sconfig);
1046         surfxml_add_callback(ETag_surfxml_config_cb_list, &routing_parse_Econfig);
1047         surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties_XML);
1048         surfxml_add_callback(STag_surfxml_AS_cb_list, &surf_parse_models_setup);
1049         surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
1050 }
1051
1052 void surf_parse_models_setup()
1053 {
1054         routing_parse_Erandom();
1055         surfxml_del_callback(STag_surfxml_AS_cb_list, surf_parse_models_setup);
1056         surf_config_models_setup(platform_filename);
1057         free(platform_filename);
1058 }
1059
1060 /* ************************************************** */
1061 /* ********** PATERN FOR NEW ROUTING **************** */
1062
1063 /* The minimal configuration of a new routing model need the next functions,
1064  * also you need to set at the start of the file, the new model in the model
1065  * list. Remember keep the null ending of the list.
1066  */
1067 /*** Routing model structure ***/
1068 // typedef struct {
1069 //   s_routing_component_t generic_routing;
1070 //   /* things that your routing model need */
1071 // } s_routing_component_NEW_t,*routing_component_NEW_t;
1072
1073 /*** Parse routing model functions ***/
1074 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
1075 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
1076 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
1077 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
1078 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
1079
1080 /*** Business methods ***/
1081 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
1082 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
1083 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
1084
1085 /*** Creation routing model functions ***/
1086 // static void* model_NEW_create(void) {
1087 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
1088 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
1089 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
1090 //   new_component->generic_routing.set_route = model_NEW_set_route;
1091 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
1092 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
1093 //   new_component->generic_routing.get_route = NEW_get_route;
1094 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
1095 //   new_component->generic_routing.finalize = NEW_finalize;
1096 //   /* initialization of internal structures */
1097 //   return new_component;
1098 // } /* mandatory */
1099 // static void  model_NEW_load(void) {}   /* mandatory */
1100 // static void  model_NEW_unload(void) {} /* mandatory */
1101 // static void  model_NEW_end(void) {}    /* mandatory */
1102
1103 /* ************************************************************************** */
1104 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
1105
1106 void generic_set_processing_unit(routing_component_t rc,
1107                                         const char *name)
1108 {
1109   XBT_DEBUG("Load process unit \"%s\"", name);
1110   int *id = xbt_new0(int, 1);
1111   xbt_dict_t _to_index;
1112   _to_index = current_routing->to_index;
1113   *id = xbt_dict_length(_to_index);
1114   xbt_dict_set(_to_index, name, id, xbt_free);
1115 }
1116
1117 void generic_set_autonomous_system(routing_component_t rc,
1118                                           const char *name)
1119 {
1120   XBT_DEBUG("Load Autonomous system \"%s\"", name);
1121   int *id = xbt_new0(int, 1);
1122   xbt_dict_t _to_index;
1123   _to_index = current_routing->to_index;
1124   *id = xbt_dict_length(_to_index);
1125   xbt_dict_set(_to_index, name, id, xbt_free);
1126 }
1127
1128 int surf_pointer_resource_cmp(const void *a, const void *b)
1129 {
1130   return a != b;
1131 }
1132
1133 int surf_link_resource_cmp(const void *a, const void *b)
1134 {
1135   return !!memcmp(a,b,global_routing->size_of_link);
1136 }
1137
1138 void generic_set_bypassroute(routing_component_t rc,
1139                                     const char *src, const char *dst,
1140                                     route_extended_t e_route)
1141 {
1142   XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
1143   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1144   char *route_name;
1145
1146   route_name = bprintf("%s#%s", src, dst);
1147   xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
1148               "Invalid count of links, must be greater than zero (%s,%s)",
1149               src, dst);
1150   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
1151               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
1152               src, e_route->src_gateway, dst, e_route->dst_gateway);
1153
1154   route_extended_t new_e_route =
1155       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
1156   xbt_dynar_free(&(e_route->generic_route.link_list));
1157   xbt_free(e_route);
1158
1159   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
1160                (void (*)(void *)) generic_free_extended_route);
1161   xbt_free(route_name);
1162 }
1163
1164 /* ************************************************************************** */
1165 /* *********************** GENERIC BUSINESS METHODS ************************* */
1166
1167 double generic_get_link_latency(routing_component_t rc,
1168                                        const char *src, const char *dst,
1169                                        route_extended_t route)
1170 {
1171         int need_to_clean = route?0:1;
1172         void * link;
1173         unsigned int i;
1174         double latency = 0.0;
1175
1176         route = route?route:rc->get_route(rc,src,dst);
1177
1178         xbt_dynar_foreach(route->generic_route.link_list,i,link) {
1179                 latency += get_link_latency(link);
1180         }
1181         if(need_to_clean) generic_free_extended_route(route);
1182   return latency;
1183 }
1184
1185 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
1186 {
1187   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
1188 }
1189
1190 route_extended_t generic_get_bypassroute(routing_component_t rc,
1191                                                 const char *src,
1192                                                 const char *dst)
1193 {
1194   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1195   routing_component_t src_as, dst_as;
1196   int index_src, index_dst;
1197   xbt_dynar_t path_src = NULL;
1198   xbt_dynar_t path_dst = NULL;
1199   routing_component_t current = NULL;
1200   routing_component_t *current_src = NULL;
1201   routing_component_t *current_dst = NULL;
1202
1203   /* (1) find the as where the src and dst are located */
1204   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1205   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1206   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1207   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1208
1209   if(src_data == NULL || dst_data == NULL)
1210           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1211                      src, dst, rc->name);
1212
1213   src_as = ((network_element_info_t)src_data)->rc_component;
1214   dst_as = ((network_element_info_t)dst_data)->rc_component;
1215
1216   /* (2) find the path to the root routing component */
1217   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1218   current = src_as;
1219   while (current != NULL) {
1220     xbt_dynar_push(path_src, &current);
1221     current = current->routing_father;
1222   }
1223   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1224   current = dst_as;
1225   while (current != NULL) {
1226     xbt_dynar_push(path_dst, &current);
1227     current = current->routing_father;
1228   }
1229
1230   /* (3) find the common father */
1231   index_src = path_src->used - 1;
1232   index_dst = path_dst->used - 1;
1233   current_src = xbt_dynar_get_ptr(path_src, index_src);
1234   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1235   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1236     routing_component_t *tmp_src, *tmp_dst;
1237     tmp_src = xbt_dynar_pop_ptr(path_src);
1238     tmp_dst = xbt_dynar_pop_ptr(path_dst);
1239     index_src--;
1240     index_dst--;
1241     current_src = xbt_dynar_get_ptr(path_src, index_src);
1242     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1243   }
1244
1245   int max_index_src = path_src->used - 1;
1246   int max_index_dst = path_dst->used - 1;
1247
1248   int max_index = max(max_index_src, max_index_dst);
1249   int i, max;
1250
1251   route_extended_t e_route_bypass = NULL;
1252
1253   for (max = 0; max <= max_index; max++) {
1254     for (i = 0; i < max; i++) {
1255       if (i <= max_index_src && max <= max_index_dst) {
1256         char *route_name = bprintf("%s#%s",
1257                                    (*(routing_component_t *)
1258                                     (xbt_dynar_get_ptr
1259                                      (path_src, i)))->name,
1260                                    (*(routing_component_t *)
1261                                     (xbt_dynar_get_ptr
1262                                      (path_dst, max)))->name);
1263         e_route_bypass =
1264             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1265         xbt_free(route_name);
1266       }
1267       if (e_route_bypass)
1268         break;
1269       if (max <= max_index_src && i <= max_index_dst) {
1270         char *route_name = bprintf("%s#%s",
1271                                    (*(routing_component_t *)
1272                                     (xbt_dynar_get_ptr
1273                                      (path_src, max)))->name,
1274                                    (*(routing_component_t *)
1275                                     (xbt_dynar_get_ptr
1276                                      (path_dst, i)))->name);
1277         e_route_bypass =
1278             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1279         xbt_free(route_name);
1280       }
1281       if (e_route_bypass)
1282         break;
1283     }
1284
1285     if (e_route_bypass)
1286       break;
1287
1288     if (max <= max_index_src && max <= max_index_dst) {
1289       char *route_name = bprintf("%s#%s",
1290                                  (*(routing_component_t *)
1291                                   (xbt_dynar_get_ptr
1292                                    (path_src, max)))->name,
1293                                  (*(routing_component_t *)
1294                                   (xbt_dynar_get_ptr
1295                                    (path_dst, max)))->name);
1296       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1297       xbt_free(route_name);
1298     }
1299     if (e_route_bypass)
1300       break;
1301   }
1302
1303   xbt_dynar_free(&path_src);
1304   xbt_dynar_free(&path_dst);
1305
1306   route_extended_t new_e_route = NULL;
1307
1308   if (e_route_bypass) {
1309     void *link;
1310     unsigned int cpt = 0;
1311     new_e_route = xbt_new0(s_route_extended_t, 1);
1312     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1313     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1314     new_e_route->generic_route.link_list =
1315         xbt_dynar_new(global_routing->size_of_link, NULL);
1316     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1317       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1318     }
1319   }
1320
1321   return new_e_route;
1322 }
1323
1324 /* ************************************************************************** */
1325 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1326
1327 route_t
1328 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1329                            void *data, int order)
1330 {
1331
1332   char *link_name;
1333   route_t new_route;
1334   unsigned int cpt;
1335   xbt_dynar_t links = NULL, links_id = NULL;
1336
1337   new_route = xbt_new0(s_route_t, 1);
1338   new_route->link_list =
1339       xbt_dynar_new(global_routing->size_of_link, NULL);
1340
1341   xbt_assert(hierarchy == SURF_ROUTING_BASE,
1342               "the hierarchy type is not SURF_ROUTING_BASE");
1343
1344   links = ((route_t) data)->link_list;
1345
1346
1347   links_id = new_route->link_list;
1348
1349   xbt_dynar_foreach(links, cpt, link_name) {
1350
1351     void *link =
1352                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1353     if (link) {
1354       if (order)
1355         xbt_dynar_push(links_id, &link);
1356       else
1357         xbt_dynar_unshift(links_id, &link);
1358     } else
1359       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1360   }
1361
1362   return new_route;
1363 }
1364
1365 route_extended_t
1366 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1367                            void *data, int order)
1368 {
1369
1370   char *link_name;
1371   route_extended_t e_route, new_e_route;
1372   route_t route;
1373   unsigned int cpt;
1374   xbt_dynar_t links = NULL, links_id = NULL;
1375
1376   new_e_route = xbt_new0(s_route_extended_t, 1);
1377   new_e_route->generic_route.link_list =
1378       xbt_dynar_new(global_routing->size_of_link, NULL);
1379   new_e_route->src_gateway = NULL;
1380   new_e_route->dst_gateway = NULL;
1381
1382   xbt_assert(hierarchy == SURF_ROUTING_BASE
1383               || hierarchy == SURF_ROUTING_RECURSIVE,
1384               "the hierarchy type is not defined");
1385
1386   if (hierarchy == SURF_ROUTING_BASE) {
1387
1388     route = (route_t) data;
1389     links = route->link_list;
1390
1391   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1392
1393     e_route = (route_extended_t) data;
1394     xbt_assert(e_route->src_gateway
1395                 && e_route->dst_gateway, "bad gateway, is null");
1396     links = e_route->generic_route.link_list;
1397
1398     /* remeber not erase the gateway names */
1399     new_e_route->src_gateway = strdup(e_route->src_gateway);
1400     new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1401   }
1402
1403   links_id = new_e_route->generic_route.link_list;
1404
1405   xbt_dynar_foreach(links, cpt, link_name) {
1406
1407     void *link =
1408                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1409     if (link) {
1410       if (order)
1411         xbt_dynar_push(links_id, &link);
1412       else
1413         xbt_dynar_unshift(links_id, &link);
1414     } else
1415       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1416   }
1417
1418   return new_e_route;
1419 }
1420
1421 void generic_free_route(route_t route)
1422 {
1423   if (route) {
1424     xbt_dynar_free(&(route->link_list));
1425     xbt_free(route);
1426   }
1427 }
1428
1429 void generic_free_extended_route(route_extended_t e_route)
1430 {
1431   if (e_route) {
1432     xbt_dynar_free(&(e_route->generic_route.link_list));
1433     if (e_route->src_gateway)
1434       xbt_free(e_route->src_gateway);
1435     if (e_route->dst_gateway)
1436       xbt_free(e_route->dst_gateway);
1437     xbt_free(e_route);
1438   }
1439 }
1440
1441 static routing_component_t generic_as_exist(routing_component_t find_from,
1442                                             routing_component_t to_find)
1443 {
1444   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1445   xbt_dict_cursor_t cursor = NULL;
1446   char *key;
1447   int found = 0;
1448   routing_component_t elem;
1449   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1450     if (to_find == elem || generic_as_exist(elem, to_find)) {
1451       found = 1;
1452       break;
1453     }
1454   }
1455   if (found)
1456     return to_find;
1457   return NULL;
1458 }
1459
1460 routing_component_t
1461 generic_autonomous_system_exist(routing_component_t rc, char *element)
1462 {
1463   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1464   routing_component_t element_as, result, elem;
1465   xbt_dict_cursor_t cursor = NULL;
1466   char *key;
1467   element_as = ((network_element_info_t)
1468                 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1469   result = ((routing_component_t) - 1);
1470   if (element_as != rc)
1471     result = generic_as_exist(rc, element_as);
1472
1473   int found = 0;
1474   if (result) {
1475     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1476       found = !strcmp(elem->name, element);
1477       if (found)
1478         break;
1479     }
1480     if (found)
1481       return element_as;
1482   }
1483   return NULL;
1484 }
1485
1486 routing_component_t
1487 generic_processing_units_exist(routing_component_t rc, char *element)
1488 {
1489   routing_component_t element_as;
1490   element_as = ((network_element_info_t)
1491                 xbt_lib_get_or_null(host_lib,
1492                  element, ROUTING_HOST_LEVEL))->rc_component;
1493   if (element_as == rc)
1494     return element_as;
1495   return generic_as_exist(rc, element_as);
1496 }
1497
1498 void generic_src_dst_check(routing_component_t rc, const char *src,
1499                                   const char *dst)
1500 {
1501
1502   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1503   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1504   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1505   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1506
1507   if(src_data == NULL || dst_data == NULL)
1508           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1509                      src, dst, rc->name);
1510
1511   routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1512   routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1513
1514   if(src_as != dst_as)
1515           xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1516               src, src_as->name, dst, dst_as->name);
1517   if(rc != dst_as)
1518          xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1519      src,dst,src_as->name, dst_as->name,rc->name);
1520 }
1521
1522 static void routing_parse_Sconfig(void)
1523 {
1524   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
1525 }
1526
1527 static void routing_parse_Econfig(void)
1528 {
1529   xbt_dict_cursor_t cursor = NULL;
1530   char *key;
1531   char *elem;
1532   char *cfg;
1533   xbt_dict_foreach(current_property_set, cursor, key, elem) {
1534           cfg = bprintf("%s:%s",key,elem);
1535           if(xbt_cfg_is_default_value(_surf_cfg_set, key))
1536                   xbt_cfg_set_parse(_surf_cfg_set, cfg);
1537           else
1538                   XBT_INFO("The custom configuration '%s' is already define by user!",key);
1539           free(cfg);
1540   }
1541   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
1542 }
1543
1544 void routing_parse_Scluster(void)
1545 {
1546   static int AX_ptr = 0;
1547
1548   char *cluster_id = A_surfxml_cluster_id;
1549   char *cluster_prefix = A_surfxml_cluster_prefix;
1550   char *cluster_suffix = A_surfxml_cluster_suffix;
1551   char *cluster_radical = A_surfxml_cluster_radical;
1552   char *cluster_power = A_surfxml_cluster_power;
1553   char *cluster_core = A_surfxml_cluster_core;
1554   char *cluster_bw = A_surfxml_cluster_bw;
1555   char *cluster_lat = A_surfxml_cluster_lat;
1556   char *temp_cluster_bw = NULL;
1557   char *temp_cluster_lat = NULL;
1558   char *temp_cluster_power = NULL;
1559   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
1560   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
1561   char *cluster_availability_file = A_surfxml_cluster_availability_file;
1562   char *cluster_state_file = A_surfxml_cluster_state_file;
1563   char *host_id, *groups, *link_id = NULL;
1564   char *router_id, *link_backbone;
1565   char *availability_file = xbt_strdup(cluster_availability_file);
1566   char *state_file = xbt_strdup(cluster_state_file);
1567
1568   if(xbt_dict_size(patterns)==0)
1569           patterns = xbt_dict_new();
1570
1571   xbt_dict_set(patterns,"id",cluster_id,NULL);
1572   xbt_dict_set(patterns,"prefix",cluster_prefix,NULL);
1573   xbt_dict_set(patterns,"suffix",cluster_suffix,NULL);
1574
1575 #ifdef HAVE_PCRE_LIB
1576   char *route_src_dst;
1577 #endif
1578   unsigned int iter;
1579   int start, end, i;
1580   xbt_dynar_t radical_elements;
1581   xbt_dynar_t radical_ends;
1582   int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
1583   int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
1584
1585 #ifndef HAVE_PCRE_LIB
1586   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
1587   char *route_src, *route_dst;
1588   int j;
1589 #endif
1590
1591   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1592   static unsigned int surfxml_buffer_stack_stack[1024];
1593
1594   surfxml_buffer_stack_stack[0] = 0;
1595
1596   surfxml_bufferstack_push(1);
1597
1598   SURFXML_BUFFER_SET(AS_id, cluster_id);
1599 #ifdef HAVE_PCRE_LIB
1600   SURFXML_BUFFER_SET(AS_routing, "RuleBased");
1601   XBT_DEBUG("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
1602 #else
1603   SURFXML_BUFFER_SET(AS_routing, "Full");
1604   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
1605 #endif
1606   SURFXML_START_TAG(AS);
1607
1608   radical_elements = xbt_str_split(cluster_radical, ",");
1609   xbt_dynar_foreach(radical_elements, iter, groups) {
1610     radical_ends = xbt_str_split(groups, "-");
1611     switch (xbt_dynar_length(radical_ends)) {
1612     case 1:
1613       surf_parse_get_int(&start,
1614                          xbt_dynar_get_as(radical_ends, 0, char *));
1615       host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
1616 #ifndef HAVE_PCRE_LIB
1617       xbt_dynar_push_as(tab_elements_num, int, start);
1618 #endif
1619       link_id = bprintf("%s_link_%d", cluster_id, start);
1620
1621       xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1622       temp_cluster_power = xbt_strdup(cluster_power);
1623       temp_cluster_power = replace_random_parameter(temp_cluster_power);
1624       XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, temp_cluster_power);
1625       A_surfxml_host_state = A_surfxml_host_state_ON;
1626       SURFXML_BUFFER_SET(host_id, host_id);
1627       SURFXML_BUFFER_SET(host_power, temp_cluster_power);
1628       SURFXML_BUFFER_SET(host_core, cluster_core);
1629       SURFXML_BUFFER_SET(host_availability, "1.0");
1630       SURFXML_BUFFER_SET(host_coordinates, "");
1631       xbt_free(availability_file);
1632       availability_file = xbt_strdup(cluster_availability_file);
1633       xbt_free(state_file);
1634       state_file = xbt_strdup(cluster_state_file);
1635       XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(availability_file,patterns));
1636       XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(state_file,patterns));
1637       SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(availability_file,patterns));
1638       SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(state_file,patterns));
1639       XBT_DEBUG("</host>");
1640       SURFXML_START_TAG(host);
1641       SURFXML_END_TAG(host);
1642
1643
1644       temp_cluster_bw = xbt_strdup(cluster_bw);
1645       temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
1646       temp_cluster_lat = xbt_strdup(cluster_lat);
1647       temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
1648       XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,temp_cluster_bw, cluster_lat);
1649       A_surfxml_link_state = A_surfxml_link_state_ON;
1650       A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1651       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1652           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1653       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1654           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1655       SURFXML_BUFFER_SET(link_id, link_id);
1656       SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
1657       SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
1658       SURFXML_BUFFER_SET(link_bandwidth_file, "");
1659       SURFXML_BUFFER_SET(link_latency_file, "");
1660       SURFXML_BUFFER_SET(link_state_file, "");
1661       SURFXML_START_TAG(link);
1662       SURFXML_END_TAG(link);
1663
1664       xbt_free(temp_cluster_bw);
1665       xbt_free(temp_cluster_lat);
1666       xbt_free(temp_cluster_power);
1667       free(link_id);
1668       free(host_id);
1669       break;
1670
1671     case 2:
1672
1673       surf_parse_get_int(&start,
1674                          xbt_dynar_get_as(radical_ends, 0, char *));
1675       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
1676       for (i = start; i <= end; i++) {
1677         host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
1678 #ifndef HAVE_PCRE_LIB
1679         xbt_dynar_push_as(tab_elements_num, int, i);
1680 #endif
1681         link_id = bprintf("%s_link_%d", cluster_id, i);
1682
1683         xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1684         temp_cluster_power = xbt_strdup(cluster_power);
1685         temp_cluster_power = replace_random_parameter(temp_cluster_power);
1686         XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, temp_cluster_power);
1687         A_surfxml_host_state = A_surfxml_host_state_ON;
1688         SURFXML_BUFFER_SET(host_id, host_id);
1689         SURFXML_BUFFER_SET(host_power, temp_cluster_power);
1690         SURFXML_BUFFER_SET(host_core, cluster_core);
1691         SURFXML_BUFFER_SET(host_availability, "1.0");
1692         SURFXML_BUFFER_SET(host_coordinates, "");
1693         xbt_free(availability_file);
1694         availability_file = xbt_strdup(cluster_availability_file);
1695         xbt_free(state_file);
1696         state_file = xbt_strdup(cluster_state_file);
1697         XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(availability_file,patterns));
1698         XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(state_file,patterns));
1699         SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(availability_file,patterns));
1700         SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(state_file,patterns));
1701         XBT_DEBUG("</host>");
1702         SURFXML_START_TAG(host);
1703         SURFXML_END_TAG(host);
1704
1705         xbt_free(temp_cluster_power);
1706
1707         temp_cluster_bw = xbt_strdup(cluster_bw);
1708         temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
1709         temp_cluster_lat = xbt_strdup(cluster_lat);
1710         temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
1711         XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,temp_cluster_bw, cluster_lat);
1712         A_surfxml_link_state = A_surfxml_link_state_ON;
1713         A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1714         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1715             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1716         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1717             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1718         SURFXML_BUFFER_SET(link_id, link_id);
1719         SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
1720         SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
1721         SURFXML_BUFFER_SET(link_bandwidth_file, "");
1722         SURFXML_BUFFER_SET(link_latency_file, "");
1723         SURFXML_BUFFER_SET(link_state_file, "");
1724         SURFXML_START_TAG(link);
1725         SURFXML_END_TAG(link);
1726
1727         xbt_free(temp_cluster_bw);
1728         xbt_free(temp_cluster_lat);
1729         free(link_id);
1730         free(host_id);
1731       }
1732       break;
1733
1734     default:
1735       XBT_DEBUG("Malformed radical");
1736     }
1737
1738     xbt_dynar_free(&radical_ends);
1739   }
1740   xbt_dynar_free(&radical_elements);
1741
1742   XBT_DEBUG(" ");
1743   router_id =
1744       bprintf("%s%s_router%s", cluster_prefix, cluster_id,
1745               cluster_suffix);
1746   //link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
1747   link_backbone = bprintf("%s_backbone", cluster_id);
1748
1749   XBT_DEBUG("<router id=\"%s\"/>", router_id);
1750   SURFXML_BUFFER_SET(router_id, router_id);
1751   SURFXML_BUFFER_SET(router_coordinates, "");
1752   SURFXML_START_TAG(router);
1753   SURFXML_END_TAG(router);
1754
1755   //TODO
1756 //  xbt_dict_set(patterns, "radical", xbt_strdup("_router"), xbt_free);
1757 //  temp_cluster_bw = xbt_strdup(cluster_bw);
1758 //  temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
1759 //  temp_cluster_lat = xbt_strdup(cluster_lat);
1760 //  temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
1761 //  XBT_DEBUG("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_router,temp_cluster_bw, temp_cluster_lat);
1762 //  A_surfxml_link_state = A_surfxml_link_state_ON;
1763 //  A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1764 //  if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1765 //  {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1766 //  if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1767 //  {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1768 //  SURFXML_BUFFER_SET(link_id, link_router);
1769 //  SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
1770 //  SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
1771 //  SURFXML_BUFFER_SET(link_bandwidth_file, "");
1772 //  SURFXML_BUFFER_SET(link_latency_file, "");
1773 //  SURFXML_BUFFER_SET(link_state_file, "");
1774 //  SURFXML_START_TAG(link);
1775 //  SURFXML_END_TAG(link);
1776
1777 //  xbt_free(temp_cluster_bw);
1778 //  xbt_free(temp_cluster_lat);
1779
1780   XBT_DEBUG("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bb_bw, cluster_bb_lat);
1781   A_surfxml_link_state = A_surfxml_link_state_ON;
1782   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1783   if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
1784   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1785   SURFXML_BUFFER_SET(link_id, link_backbone);
1786   SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
1787   SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
1788   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1789   SURFXML_BUFFER_SET(link_latency_file, "");
1790   SURFXML_BUFFER_SET(link_state_file, "");
1791   SURFXML_START_TAG(link);
1792   SURFXML_END_TAG(link);
1793
1794   XBT_DEBUG(" ");
1795
1796 #ifdef HAVE_PCRE_LIB
1797   char *new_suffix = xbt_strdup("");
1798
1799   radical_elements = xbt_str_split(cluster_suffix, ".");
1800   xbt_dynar_foreach(radical_elements, iter, groups) {
1801     if (strcmp(groups, "")) {
1802       char *old_suffix = new_suffix;
1803       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1804       free(old_suffix);
1805     }
1806   }
1807   route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
1808   xbt_dynar_free(&radical_elements);
1809   free(new_suffix);
1810
1811   char *pcre_link_src = bprintf("%s_link_$1src", cluster_id);
1812   char *pcre_link_backbone = bprintf("%s_backbone", cluster_id);
1813   char *pcre_link_dst = bprintf("%s_link_$1dst", cluster_id);
1814
1815   //from router to router
1816   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, router_id);
1817   XBT_DEBUG("symmetrical=\"NO\">");
1818   SURFXML_BUFFER_SET(route_src, router_id);
1819   SURFXML_BUFFER_SET(route_dst, router_id);
1820   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1821   SURFXML_START_TAG(route);
1822
1823   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1824   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1825   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1826   SURFXML_START_TAG(link_ctn);
1827   SURFXML_END_TAG(link_ctn);
1828
1829   XBT_DEBUG("</route>");
1830   SURFXML_END_TAG(route);
1831
1832   //from host to router
1833   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, router_id);
1834   XBT_DEBUG("symmetrical=\"NO\">");
1835   SURFXML_BUFFER_SET(route_src, route_src_dst);
1836   SURFXML_BUFFER_SET(route_dst, router_id);
1837   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1838   SURFXML_START_TAG(route);
1839
1840   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_src);
1841   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_src);
1842   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1843   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1844   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1845   SURFXML_START_TAG(link_ctn);
1846   SURFXML_END_TAG(link_ctn);
1847
1848   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1849   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1850   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1851   SURFXML_START_TAG(link_ctn);
1852   SURFXML_END_TAG(link_ctn);
1853
1854   XBT_DEBUG("</route>");
1855   SURFXML_END_TAG(route);
1856
1857   //from router to host
1858   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, route_src_dst);
1859   XBT_DEBUG("symmetrical=\"NO\">");
1860   SURFXML_BUFFER_SET(route_src, router_id);
1861   SURFXML_BUFFER_SET(route_dst, route_src_dst);
1862   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1863   SURFXML_START_TAG(route);
1864
1865   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1866   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1867   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1868   SURFXML_START_TAG(link_ctn);
1869   SURFXML_END_TAG(link_ctn);
1870
1871   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_dst);
1872   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_dst);
1873   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1874   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1875   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1876   SURFXML_START_TAG(link_ctn);
1877   SURFXML_END_TAG(link_ctn);
1878
1879   XBT_DEBUG("</route>");
1880   SURFXML_END_TAG(route);
1881
1882   //from host to host
1883   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
1884   XBT_DEBUG("symmetrical=\"NO\">");
1885   SURFXML_BUFFER_SET(route_src, route_src_dst);
1886   SURFXML_BUFFER_SET(route_dst, route_src_dst);
1887   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1888   SURFXML_START_TAG(route);
1889
1890   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_src);
1891   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_src);
1892   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1893   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1894   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1895   SURFXML_START_TAG(link_ctn);
1896   SURFXML_END_TAG(link_ctn);
1897
1898   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1899   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1900   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1901   SURFXML_START_TAG(link_ctn);
1902   SURFXML_END_TAG(link_ctn);
1903
1904   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_dst);
1905   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_dst);
1906   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1907   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1908   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
1909   SURFXML_START_TAG(link_ctn);
1910   SURFXML_END_TAG(link_ctn);
1911
1912   XBT_DEBUG("</route>");
1913   SURFXML_END_TAG(route);
1914
1915   free(pcre_link_dst);
1916   free(pcre_link_backbone);
1917   free(pcre_link_src);
1918   free(route_src_dst);
1919 #else
1920   for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
1921     for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
1922       if (i == xbt_dynar_length(tab_elements_num)) {
1923         route_src = router_id;
1924       } else {
1925         route_src =
1926             bprintf("%s%d%s", cluster_prefix,
1927                     xbt_dynar_get_as(tab_elements_num, i, int),
1928                     cluster_suffix);
1929       }
1930
1931       if (j == xbt_dynar_length(tab_elements_num)) {
1932         route_dst = router_id;
1933       } else {
1934         route_dst =
1935             bprintf("%s%d%s", cluster_prefix,
1936                     xbt_dynar_get_as(tab_elements_num, j, int),
1937                     cluster_suffix);
1938       }
1939
1940       XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src, route_dst);
1941       XBT_DEBUG("symmetrical=\"NO\">");
1942       SURFXML_BUFFER_SET(route_src, route_src);
1943       SURFXML_BUFFER_SET(route_dst, route_dst);
1944       A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1945       SURFXML_START_TAG(route);
1946
1947       if (i != xbt_dynar_length(tab_elements_num)){
1948           route_src =
1949                 bprintf("%s_link_%d", cluster_id,
1950                         xbt_dynar_get_as(tab_elements_num, i, int));
1951                   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", route_src);
1952                   SURFXML_BUFFER_SET(link_ctn_id, route_src);
1953                   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1954                   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1955                   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1956                   SURFXML_START_TAG(link_ctn);
1957                   SURFXML_END_TAG(link_ctn);
1958                   free(route_src);
1959       }
1960
1961       XBT_DEBUG("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
1962       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
1963       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1964       SURFXML_START_TAG(link_ctn);
1965       SURFXML_END_TAG(link_ctn);
1966
1967       if (j != xbt_dynar_length(tab_elements_num)) {
1968           route_dst =
1969                 bprintf("%s_link_%d", cluster_id,
1970                         xbt_dynar_get_as(tab_elements_num, j, int));
1971                   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", route_dst);
1972                   SURFXML_BUFFER_SET(link_ctn_id, route_dst);
1973                   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1974                   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1975                   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
1976                   SURFXML_START_TAG(link_ctn);
1977                   SURFXML_END_TAG(link_ctn);
1978                   free(route_dst);
1979       }
1980
1981       XBT_DEBUG("</route>");
1982       SURFXML_END_TAG(route);
1983     }
1984   }
1985   xbt_dynar_free(&tab_elements_num);
1986
1987 #endif
1988
1989   free(router_id);
1990   free(link_backbone);
1991   //free(link_router);
1992   xbt_dict_free(&patterns);
1993   free(availability_file);
1994   free(state_file);
1995
1996   XBT_DEBUG("</AS>");
1997   SURFXML_END_TAG(AS);
1998   XBT_DEBUG(" ");
1999
2000   surfxml_bufferstack_pop(1);
2001 }
2002 /*
2003  * This function take a string and replace parameters from patterns dict.
2004  * It returns the new value.
2005  */
2006 static char* replace_random_parameter(char * string)
2007 {
2008   char *test_string = NULL;
2009
2010   if(xbt_dict_size(random_value)==0)
2011     return string;
2012
2013   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
2014   test_string = bprintf("${%s}", string);
2015   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
2016
2017   if (strcmp(test_string,"")) { //if not empty, keep this value.
2018     xbt_free(string);
2019     string = test_string;
2020   } //In other case take old value (without ${})
2021   else
2022         free(test_string);
2023   return string;
2024 }
2025
2026 static void clean_dict_random(void)
2027 {
2028         xbt_dict_free(&random_value);
2029         xbt_dict_free(&patterns);
2030 }
2031
2032 static void routing_parse_Speer(void)
2033 {
2034   static int AX_ptr = 0;
2035
2036   char *peer_id = A_surfxml_peer_id;
2037   char *peer_power = A_surfxml_peer_power;
2038   char *peer_bw_in = A_surfxml_peer_bw_in;
2039   char *peer_bw_out = A_surfxml_peer_bw_out;
2040   char *peer_lat = A_surfxml_peer_lat;
2041   char *peer_coord = A_surfxml_peer_coordinates;
2042   char *peer_state_file = A_surfxml_peer_state_file;
2043   char *peer_availability_file = A_surfxml_peer_availability_file;
2044
2045   char *host_id = NULL;
2046   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
2047
2048   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
2049   static unsigned int surfxml_buffer_stack_stack[1024];
2050
2051   surfxml_buffer_stack_stack[0] = 0;
2052
2053   surfxml_bufferstack_push(1);
2054
2055   SURFXML_BUFFER_SET(AS_id, peer_id);
2056
2057   SURFXML_BUFFER_SET(AS_routing, "Full");
2058   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", peer_id);
2059
2060   SURFXML_START_TAG(AS);
2061
2062   XBT_DEBUG(" ");
2063   host_id = bprintf("peer_%s", peer_id);
2064   router_id = bprintf("router_%s", peer_id);
2065   link_id_up = bprintf("link_%s_up", peer_id);
2066   link_id_down = bprintf("link_%s_down", peer_id);
2067
2068   link_router = bprintf("%s_link_router", peer_id);
2069   link_backbone = bprintf("%s_backbone", peer_id);
2070
2071   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, peer_power);
2072   A_surfxml_host_state = A_surfxml_host_state_ON;
2073   SURFXML_BUFFER_SET(host_id, host_id);
2074   SURFXML_BUFFER_SET(host_power, peer_power);
2075   SURFXML_BUFFER_SET(host_availability, "1.0");
2076   SURFXML_BUFFER_SET(host_availability_file, peer_availability_file);
2077   SURFXML_BUFFER_SET(host_state_file, peer_state_file);
2078   SURFXML_BUFFER_SET(host_coordinates, "");
2079   SURFXML_START_TAG(host);
2080   SURFXML_END_TAG(host);
2081
2082   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, peer_coord);
2083   SURFXML_BUFFER_SET(router_id, router_id);
2084   SURFXML_BUFFER_SET(router_coordinates, peer_coord);
2085   SURFXML_START_TAG(router);
2086   SURFXML_END_TAG(router);
2087
2088   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_up, peer_bw_in, peer_lat);
2089   A_surfxml_link_state = A_surfxml_link_state_ON;
2090   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
2091   SURFXML_BUFFER_SET(link_id, link_id_up);
2092   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_in);
2093   SURFXML_BUFFER_SET(link_latency, peer_lat);
2094   SURFXML_BUFFER_SET(link_bandwidth_file, "");
2095   SURFXML_BUFFER_SET(link_latency_file, "");
2096   SURFXML_BUFFER_SET(link_state_file, "");
2097   SURFXML_START_TAG(link);
2098   SURFXML_END_TAG(link);
2099
2100   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_down, peer_bw_out, peer_lat);
2101   A_surfxml_link_state = A_surfxml_link_state_ON;
2102   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
2103   SURFXML_BUFFER_SET(link_id, link_id_down);
2104   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_out);
2105   SURFXML_BUFFER_SET(link_latency, peer_lat);
2106   SURFXML_BUFFER_SET(link_bandwidth_file, "");
2107   SURFXML_BUFFER_SET(link_latency_file, "");
2108   SURFXML_BUFFER_SET(link_state_file, "");
2109   SURFXML_START_TAG(link);
2110   SURFXML_END_TAG(link);
2111
2112   XBT_DEBUG(" ");
2113
2114   // begin here
2115   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
2116   XBT_DEBUG("symmetrical=\"NO\">");
2117   SURFXML_BUFFER_SET(route_src, host_id);
2118   SURFXML_BUFFER_SET(route_dst, router_id);
2119   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
2120   SURFXML_START_TAG(route);
2121
2122   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
2123   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
2124   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
2125   SURFXML_START_TAG(link_ctn);
2126   SURFXML_END_TAG(link_ctn);
2127
2128   XBT_DEBUG("</route>");
2129   SURFXML_END_TAG(route);
2130
2131   //Opposite Route
2132   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
2133   XBT_DEBUG("symmetrical=\"NO\">");
2134   SURFXML_BUFFER_SET(route_src, router_id);
2135   SURFXML_BUFFER_SET(route_dst, host_id);
2136   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
2137   SURFXML_START_TAG(route);
2138
2139   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
2140   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
2141   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
2142   SURFXML_START_TAG(link_ctn);
2143   SURFXML_END_TAG(link_ctn);
2144
2145   XBT_DEBUG("</route>");
2146   SURFXML_END_TAG(route);
2147
2148   XBT_DEBUG("</AS>");
2149   SURFXML_END_TAG(AS);
2150   XBT_DEBUG(" ");
2151
2152   //xbt_dynar_free(&tab_elements_num);
2153         free(host_id);
2154         free(router_id);
2155         free(link_router);
2156         free(link_backbone);
2157         free(link_id_up);
2158         free(link_id_down);
2159   surfxml_bufferstack_pop(1);
2160 }
2161
2162 static void routing_parse_Srandom(void)
2163 {
2164           double mean, std, min, max, seed;
2165           char *random_id = A_surfxml_random_id;
2166           char *random_radical = A_surfxml_random_radical;
2167           char *rd_name = NULL;
2168           char *rd_value;
2169           surf_parse_get_double(&mean,A_surfxml_random_mean);
2170           surf_parse_get_double(&std,A_surfxml_random_std_deviation);
2171           surf_parse_get_double(&min,A_surfxml_random_min);
2172           surf_parse_get_double(&max,A_surfxml_random_max);
2173           surf_parse_get_double(&seed,A_surfxml_random_seed);
2174
2175           double res = 0;
2176           int i = 0;
2177           random_data_t random = xbt_new0(s_random_data_t, 1);
2178           char *tmpbuf;
2179
2180           xbt_dynar_t radical_elements;
2181           unsigned int iter;
2182           char *groups;
2183           int start, end;
2184           xbt_dynar_t radical_ends;
2185
2186           random->generator = A_surfxml_random_generator;
2187           random->seed = seed;
2188           random->min = min;
2189           random->max = max;
2190
2191           /* Check user stupidities */
2192           if (max < min)
2193             THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
2194           if (mean < min)
2195             THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
2196                    min);
2197           if (mean > max)
2198             THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
2199                    max);
2200
2201           /* normalize the mean and standard deviation before storing */
2202           random->mean = (mean - min) / (max - min);
2203           random->std = std / (max - min);
2204
2205           if (random->mean * (1 - random->mean) < random->std * random->std)
2206             THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
2207                    random->mean, random->std);
2208
2209           XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
2210           random_id,
2211           random->min,
2212           random->max,
2213           random->mean,
2214           random->std,
2215           random->generator,
2216           random->seed,
2217           random_radical);
2218
2219           if(xbt_dict_size(random_value)==0)
2220                   random_value = xbt_dict_new();
2221
2222           if(!strcmp(random_radical,""))
2223           {
2224                   res = random_generate(random);
2225                   rd_value = bprintf("%f",res);
2226                   xbt_dict_set(random_value, random_id, rd_value, free);
2227           }
2228           else
2229           {
2230                   radical_elements = xbt_str_split(random_radical, ",");
2231                   xbt_dynar_foreach(radical_elements, iter, groups) {
2232                         radical_ends = xbt_str_split(groups, "-");
2233                         switch (xbt_dynar_length(radical_ends)) {
2234                         case 1:
2235                                           xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
2236                                           res = random_generate(random);
2237                                           tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
2238                                           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2239                                           xbt_free(tmpbuf);
2240                                           break;
2241
2242                         case 2:   surf_parse_get_int(&start,
2243                                                                                  xbt_dynar_get_as(radical_ends, 0, char *));
2244                                           surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
2245                                           for (i = start; i <= end; i++) {
2246                                                   xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
2247                                                   res = random_generate(random);
2248                           tmpbuf = bprintf("%s%d",random_id,i);
2249                                                   xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2250                           xbt_free(tmpbuf);
2251                                           }
2252                                           break;
2253                         default:
2254                                 XBT_INFO("Malformed radical");
2255                         }
2256                         res = random_generate(random);
2257                         rd_name  = bprintf("%s_router",random_id);
2258                         rd_value = bprintf("%f",res);
2259                         xbt_dict_set(random_value, rd_name, rd_value, free);
2260
2261                         xbt_dynar_free(&radical_ends);
2262                   }
2263                   free(rd_name);
2264                   xbt_dynar_free(&radical_elements);
2265           }
2266 }
2267
2268 static void routing_parse_Erandom(void)
2269 {
2270         xbt_dict_cursor_t cursor = NULL;
2271         char *key;
2272         char *elem;
2273
2274         xbt_dict_foreach(random_value, cursor, key, elem) {
2275           XBT_DEBUG("%s = %s",key,elem);
2276         }
2277
2278 }
2279
2280
2281 /*
2282  * New methods to init the routing model component from the lua script
2283  */
2284
2285 /*
2286  * calling parse_S_AS_lua with lua values
2287  */
2288 void routing_AS_init(const char *AS_id, const char *AS_routing)
2289 {
2290   parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
2291 }
2292
2293 /*
2294  * calling parse_E_AS_lua to fisnish the creation of routing component
2295  */
2296 void routing_AS_end(const char *AS_id)
2297 {
2298   parse_E_AS_lua((char *) AS_id);
2299 }
2300
2301 /*
2302  * add a host to the network element list
2303  */
2304
2305 void routing_add_host(const char *host_id)
2306 {
2307   parse_S_host_lua((char *) host_id, (char*)""); // FIXME propagate coordinate system to lua
2308 }
2309
2310 /*
2311  * Set a new link on the actual list of link for a route or ASroute
2312  */
2313 void routing_add_link(const char *link_id)
2314 {
2315   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
2316 }
2317
2318 /*
2319  *Set the endpoints for a route
2320  */
2321 void routing_set_route(const char *src_id, const char *dst_id)
2322 {
2323   parse_S_route_new_and_endpoints_lua(src_id, dst_id);
2324 }
2325
2326 /*
2327  * Store the route by calling parse_E_route_store_route
2328  */
2329 void routing_store_route(void)
2330 {
2331   parse_E_route_store_route();
2332 }