Logo AND Algorithmique Numérique Distribuée

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