Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
70ef7e7423a868cefe87aaaa864471cae5941d60
[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
8
9 #include <float.h>
10 #include "gras_config.h"
11
12 #ifdef HAVE_PCRE_LIB
13 #include <pcre.h>               /* regular expression library */
14 #endif
15 #include "surf_private.h"
16 #include "xbt/dynar.h"
17 #include "xbt/str.h"
18 #include "xbt/config.h"
19 #include "xbt/graph.h"
20 #include "xbt/set.h"
21 #include "surf/surfxml_parse.h"
22
23 xbt_dict_t patterns = NULL;
24 xbt_dict_t random_value = NULL;
25
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
27
28 /* Global vars */
29 routing_global_t global_routing = NULL;
30 routing_component_t current_routing = NULL;
31 model_type_t current_routing_model = NULL;
32 static double_f_cpvoid_t get_link_latency = NULL;
33
34 /* Prototypes of each model */
35 static void *model_full_create(void);   /* create structures for full routing model */
36 static void model_full_load(void);      /* load parse functions for full routing model */
37 static void model_full_unload(void);    /* unload parse functions for full routing model */
38 static void model_full_end(void);       /* finalize the creation of full routing model */
39 static void model_full_set_route(               /* Set the route and ASroute between src and dst */
40                 routing_component_t rc, const char *src, const char *dst, name_route_extended_t route);
41
42 static void *model_floyd_create(void);  /* create structures for floyd routing model */
43 static void model_floyd_load(void);     /* load parse functions for floyd routing model */
44 static void model_floyd_unload(void);   /* unload parse functions for floyd routing model */
45 static void model_floyd_end(void);      /* finalize the creation of floyd routing model */
46 static void model_floyd_set_route(routing_component_t rc, const char *src,
47         const char *dst, name_route_extended_t route);
48
49 static void *model_dijkstra_both_create(int cached);    /* create by calling dijkstra or dijkstracache */
50 static void *model_dijkstra_create(void);       /* create structures for dijkstra routing model */
51 static void *model_dijkstracache_create(void);  /* create structures for dijkstracache routing model */
52 static void model_dijkstra_both_load(void);     /* load parse functions for dijkstra routing model */
53 static void model_dijkstra_both_unload(void);   /* unload parse functions for dijkstra routing model */
54 static void model_dijkstra_both_end(void);      /* finalize the creation of dijkstra routing model */
55 static void model_dijkstra_both_set_route (routing_component_t rc, const char *src,
56                      const char *dst, name_route_extended_t route);
57
58 static void *model_rulebased_create(void);      /* create structures for rulebased routing model */
59 static void model_rulebased_load(void); /* load parse functions for rulebased routing model */
60 static void model_rulebased_unload(void);       /* unload parse functions for rulebased routing model */
61 static void model_rulebased_end(void);  /* finalize the creation of rulebased routing model */
62
63 static void *model_none_create(void);   /* none routing model */
64 static void model_none_load(void);      /* none routing model */
65 static void model_none_unload(void);    /* none routing model */
66 static void model_none_end(void);       /* none routing model */
67
68 static void routing_parse_Scluster(void);  /*cluster bypass */
69 static void routing_parse_Speer(void);          /*peer bypass */
70 static void routing_parse_Srandom(void);        /*random bypass */
71 static void routing_parse_Erandom(void);        /*random bypass */
72
73 static void routing_parse_Sconfig(void);        /*config Tag */
74 static void routing_parse_Econfig(void);        /*config Tag */
75
76 static char* replace_random_parameter(char * chaine);
77 static void clean_dict_random(void);
78
79 /* this lines are only for replace use like index in the model table */
80 typedef enum {
81   SURF_MODEL_FULL = 0,
82   SURF_MODEL_FLOYD,
83   SURF_MODEL_DIJKSTRA,
84   SURF_MODEL_DIJKSTRACACHE,
85   SURF_MODEL_NONE,
86 #ifdef HAVE_PCRE_LIB
87   SURF_MODEL_RULEBASED
88 #endif
89 } e_routing_types;
90
91
92 /* must finish with NULL and be careful if the order is changed */
93 struct s_model_type routing_models[] = { {"Full",
94                                           "Full routing data (fast, large memory requirements, fully expressive)",
95                                           model_full_create,
96                                           model_full_load,
97                                           model_full_unload,
98                                           model_full_end},
99 {"Floyd",
100  "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
101  model_floyd_create, model_floyd_load, model_floyd_unload,
102  model_floyd_end},
103 {"Dijkstra",
104  "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
105  model_dijkstra_create, model_dijkstra_both_load,
106  model_dijkstra_both_unload, model_dijkstra_both_end},
107 {"DijkstraCache",
108  "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
109  model_dijkstracache_create, model_dijkstra_both_load,
110  model_dijkstra_both_unload, model_dijkstra_both_end},
111 {"none", "No routing (usable with Constant network only)",
112  model_none_create, model_none_load, model_none_unload, model_none_end},
113 #ifdef HAVE_PCRE_LIB
114 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
115  model_rulebased_load, model_rulebased_unload, model_rulebased_end},
116 {"Vivaldi", "Vivaldi routing", model_rulebased_create,
117   model_rulebased_load, model_rulebased_unload, model_rulebased_end},
118 #endif
119 {NULL, NULL, NULL, NULL, NULL, NULL}
120 };
121
122 /* ************************************************************************** */
123 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
124
125 static void generic_set_processing_unit(routing_component_t rc,
126                                         const char *name);
127 static void generic_set_autonomous_system(routing_component_t rc,
128                                           const char *name);
129 static void generic_set_bypassroute(routing_component_t rc,
130                                     const char *src, const char *dst,
131                                     route_extended_t e_route);
132
133 static int surf_link_resource_cmp(const void *a, const void *b);
134 static int surf_pointer_resource_cmp(const void *a, const void *b);
135
136 /* ************************************************************************** */
137 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
138
139 static double generic_get_link_latency(routing_component_t rc, const char *src, const char *dst);
140 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc);
141 static route_extended_t generic_get_bypassroute(routing_component_t rc,
142                                                 const char *src,
143                                                 const char *dst);
144
145 /* ************************************************************************** */
146 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
147
148 static route_extended_t
149 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
150                            void *data, int order);
151 static route_t
152 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
153                            void *data, int order);
154 static void generic_free_route(route_t route);
155 static void generic_free_extended_route(route_extended_t e_route);
156 static routing_component_t
157 generic_autonomous_system_exist(routing_component_t rc, char *element);
158 static routing_component_t
159 generic_processing_units_exist(routing_component_t rc, char *element);
160 static void generic_src_dst_check(routing_component_t rc, const char *src,
161                                   const char *dst);
162
163 /* ************************************************************************** */
164 /* **************************** GLOBAL FUNCTIONS **************************** */
165
166 /* global parse functions */
167 static const char *src = NULL;        /* temporary store the source name of a route */
168 static const char *dst = NULL;        /* temporary store the destination name of a route */
169 static char *gw_src = NULL;     /* temporary store the gateway source name of a route */
170 static char *gw_dst = NULL;     /* temporary store the gateway destination name of a route */
171 static xbt_dynar_t link_list = NULL;    /* temporary store of current list link of a route */
172
173
174 static double eculidean_dist_comp(int index, xbt_dynar_t src, xbt_dynar_t dst)
175 {
176         double src_coord, dst_coord;
177
178         src_coord = atof(xbt_dynar_get_as(src, index, char *));
179         dst_coord = atof(xbt_dynar_get_as(dst, index, char *));
180
181         return (src_coord-dst_coord)*(src_coord-dst_coord);
182
183 }
184
185 static double vivaldi_get_link_latency (routing_component_t rc,const char *src, const char *dst)
186 {
187   double euclidean_dist;
188   xbt_dynar_t src_ctn, dst_ctn;
189   src_ctn = xbt_lib_get_or_null(host_lib, src, COORD_HOST_LEVEL);
190   if(!src_ctn) src_ctn = xbt_lib_get_or_null(as_router_lib, src, COORD_ASR_LEVEL);
191   dst_ctn = xbt_lib_get_or_null(host_lib, dst, COORD_HOST_LEVEL);
192   if(!dst_ctn) dst_ctn = xbt_lib_get_or_null(as_router_lib, dst, COORD_ASR_LEVEL);
193
194   if(dst_ctn == NULL || src_ctn == NULL)
195   xbt_die("Coord src '%s' :%p   dst '%s' :%p",src,src_ctn,dst,dst_ctn);
196
197   euclidean_dist = sqrt (eculidean_dist_comp(0,src_ctn,dst_ctn)+eculidean_dist_comp(1,src_ctn,dst_ctn))
198                                                                 +fabs(atof(xbt_dynar_get_as(src_ctn, 2, char *)))+fabs(atof(xbt_dynar_get_as(dst_ctn, 2, char *)));
199
200   xbt_assert(euclidean_dist>=0, "Euclidean Dist is less than 0\"%s\" and \"%.2f\"", src, euclidean_dist);
201
202   return euclidean_dist;
203
204   /*
205   x = atof(xbt_dynar_get_as(src_ctn, 0, char *))-atof(xbt_dynar_get_as(dst_ctn, 0, char *));
206   y = atof(xbt_dynar_get_as(src_ctn, 1, char *));
207   h = atof(xbt_dynar_get_as(ctn, 2, char *));
208   sqrt((c1->x - c2->x) * (c1->x - c2->x) + (c1->y - c2->y) * (c1->y - c2->y)) + fabs(c1->h) + fabs(c2->h);
209
210           if (strcmp(coord,"")) {
211         xbt_dynar_t ctn = xbt_str_split_str(coord, " ");
212         xbt_dynar_shrink(ctn,0);
213         xbt_lib_set(host_lib, host_id, COORD_HOST_LEVEL, ctn);
214   }
215         */
216 }
217
218 /**
219  * \brief Add a "host" to the network element list
220  */
221 static void parse_S_host(const char *host_id, const char* coord)
222 {
223   network_element_info_t info = NULL;
224   if (current_routing->hierarchy == SURF_ROUTING_NULL)
225     current_routing->hierarchy = SURF_ROUTING_BASE;
226   xbt_assert(!xbt_lib_get_or_null(host_lib, host_id,ROUTING_HOST_LEVEL),
227               "Reading a host, processing unit \"%s\" already exists",
228               host_id);
229   xbt_assert(current_routing->set_processing_unit,
230               "no defined method \"set_processing_unit\" in \"%s\"",
231               current_routing->name);
232   (*(current_routing->set_processing_unit)) (current_routing, host_id);
233   info = xbt_new0(s_network_element_info_t, 1);
234   info->rc_component = current_routing;
235   info->rc_type = SURF_NETWORK_ELEMENT_HOST;
236   xbt_lib_set(host_lib,host_id,ROUTING_HOST_LEVEL,(void *) info);
237   if (strcmp(coord,"")) {
238         if(!COORD_HOST_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
239     xbt_dynar_t ctn = xbt_str_split_str(coord, " ");
240     xbt_dynar_shrink(ctn, 0);
241     xbt_lib_set(host_lib,host_id,COORD_HOST_LEVEL,(void *) ctn);
242   }
243 }
244
245 static void parse_E_host(void)
246 {
247          xbt_dict_cursor_t cursor = NULL;
248           char *key;
249           char *elem;
250
251           xbt_dict_foreach(current_property_set, cursor, key, elem) {
252                   XBT_DEBUG("property : %s = %s",key,elem);
253                 }
254 }
255
256 /*
257  * \brief Add a host to the network element list from XML
258  */
259 static void parse_S_host_XML(void)
260 {
261         parse_S_host(A_surfxml_host_id, A_surfxml_host_coordinates);
262 }
263 static void parse_E_host_XML(void)
264 {
265         parse_E_host();
266 }
267
268 /*
269  * \brief Add a host to the network element list from lua script
270  */
271 static void parse_S_host_lua(const char *host_id, const char *coord)
272 {
273   parse_S_host(host_id, coord);
274 }
275
276
277 /**
278  * \brief Add a "router" to the network element list
279  */
280 static void parse_S_router(const char *router_id)
281 {
282   network_element_info_t info = NULL;
283
284   if (current_routing->hierarchy == SURF_ROUTING_NULL)
285     current_routing->hierarchy = SURF_ROUTING_BASE;
286   xbt_assert(!xbt_lib_get_or_null(as_router_lib,A_surfxml_router_id, ROUTING_ASR_LEVEL),
287               "Reading a router, processing unit \"%s\" already exists",
288               router_id);
289   xbt_assert(current_routing->set_processing_unit,
290               "no defined method \"set_processing_unit\" in \"%s\"",
291               current_routing->name);
292   (*(current_routing->set_processing_unit)) (current_routing,
293                                              router_id);
294   info = xbt_new0(s_network_element_info_t, 1);
295   info->rc_component = current_routing;
296   info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
297
298   xbt_lib_set(as_router_lib,router_id,ROUTING_ASR_LEVEL,(void *) info);
299   if (strcmp(A_surfxml_router_coordinates,"")) {
300         if(!COORD_ASR_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
301     xbt_dynar_t ctn = xbt_str_split_str(A_surfxml_router_coordinates, " ");
302     xbt_dynar_shrink(ctn, 0);
303     xbt_lib_set(as_router_lib,router_id,COORD_ASR_LEVEL,(void *) ctn);
304   }
305 }
306
307 /**
308  * brief Add a "router" to the network element list from XML description
309  */
310 static void parse_S_router_XML(void)
311 {
312         return parse_S_router(A_surfxml_router_id);
313 }
314
315 /**
316  * brief Add a "router" to the network element list from XML description
317  */
318 static void parse_S_router_lua(const char* router_id)
319 {
320         return parse_S_router(router_id);
321 }
322
323 /**
324  * \brief Set the endponints for a route
325  */
326 static void parse_S_route_new_and_endpoints(const char *src_id, const char *dst_id)
327 {
328   if (src != NULL && dst != NULL && link_list != NULL)
329     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
330            src_id, dst_id);
331   src = src_id;
332   dst = dst_id;
333   xbt_assert(strlen(src) > 0 || strlen(dst) > 0,
334               "Some limits are null in the route between \"%s\" and \"%s\"",
335               src, dst);
336   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
337 }
338
339 /**
340  * \brief Set the endpoints for a route from XML
341  */
342 static void parse_S_route_new_and_endpoints_XML(void)
343 {
344   parse_S_route_new_and_endpoints(A_surfxml_route_src,
345                                   A_surfxml_route_dst);
346 }
347
348 /**
349  * \brief Set the endpoints for a route from lua
350  */
351 static void parse_S_route_new_and_endpoints_lua(const char *id_src, const char *id_dst)
352 {
353   parse_S_route_new_and_endpoints(id_src, id_dst);
354 }
355
356 /**
357  * \brief Set the endponints and gateways for a ASroute
358  */
359 static void parse_S_ASroute_new_and_endpoints(void)
360 {
361   if (src != NULL && dst != NULL && link_list != NULL)
362     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
363            A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
364   src = A_surfxml_ASroute_src;
365   dst = A_surfxml_ASroute_dst;
366   gw_src = A_surfxml_ASroute_gw_src;
367   gw_dst = A_surfxml_ASroute_gw_dst;
368   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
369               || strlen(gw_dst) > 0,
370               "Some limits are null in the route between \"%s\" and \"%s\"",
371               src, dst);
372   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
373 }
374
375 /**
376  * \brief Set the endponints for a bypassRoute
377  */
378 static void parse_S_bypassRoute_new_and_endpoints(void)
379 {
380   if (src != NULL && dst != NULL && link_list != NULL)
381     THROWF(arg_error, 0,
382            "Bypass Route between %s to %s can not be defined",
383            A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
384   src = A_surfxml_bypassRoute_src;
385   dst = A_surfxml_bypassRoute_dst;
386   gw_src = A_surfxml_bypassRoute_gw_src;
387   gw_dst = A_surfxml_bypassRoute_gw_dst;
388   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
389               || strlen(gw_dst) > 0,
390               "Some limits are null in the route between \"%s\" and \"%s\"",
391               src, dst);
392   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
393 }
394
395 /**
396  * \brief Set a new link on the actual list of link for a route or ASroute
397  */
398 static void parse_E_link_ctn_new_elem(const char *link_id)
399 {
400   char *val;
401   val = xbt_strdup(link_id);
402   xbt_dynar_push(link_list, &val);
403 }
404
405 /**
406  * \brief Set a new link on the actual list of link for a route or ASroute from XML
407  */
408
409 static void parse_E_link_ctn_new_elem_XML(void)
410 {
411   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
412     parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
413   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP) {
414     char *link_id = bprintf("%s_UP", A_surfxml_link_ctn_id);
415     parse_E_link_ctn_new_elem(link_id);
416     free(link_id);
417   }
418   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN) {
419     char *link_id = bprintf("%s_DOWN", A_surfxml_link_ctn_id);
420     parse_E_link_ctn_new_elem(link_id);
421     free(link_id);
422   }
423 }
424
425 /**
426  * \brief Set a new link on the actual list of link for a route or ASroute from lua
427  */
428 static void parse_E_link_c_ctn_new_elem_lua(const char *link_id)
429 {
430   parse_E_link_ctn_new_elem(link_id);
431 }
432
433 /**
434  * \brief Store the route by calling the set_route function of the current routing component
435  */
436 static void parse_E_route_store_route(void)
437 {
438   name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
439   route->generic_route.link_list = link_list;
440   xbt_assert(current_routing->set_route,
441               "no defined method \"set_route\" in \"%s\"",
442               current_routing->name);
443   (*(current_routing->set_route)) (current_routing, src, dst, route);
444   link_list = NULL;
445   src = NULL;
446   dst = NULL;
447 }
448
449 /**
450  * \brief Store the ASroute by calling the set_ASroute function of the current routing component
451  */
452 static void parse_E_ASroute_store_route(void)
453 {
454   name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
455   e_route->generic_route.link_list = link_list;
456   e_route->src_gateway = xbt_strdup(gw_src);
457   e_route->dst_gateway = xbt_strdup(gw_dst);
458   xbt_assert(current_routing->set_ASroute,
459               "no defined method \"set_ASroute\" in \"%s\"",
460               current_routing->name);
461   (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
462   link_list = NULL;
463   src = NULL;
464   dst = NULL;
465   gw_src = NULL;
466   gw_dst = NULL;
467 }
468
469 /**
470  * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
471  */
472 static void parse_E_bypassRoute_store_route(void)
473 {
474   route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
475   e_route->generic_route.link_list = link_list;
476   e_route->src_gateway = xbt_strdup(gw_src);
477   e_route->dst_gateway = xbt_strdup(gw_dst);
478   xbt_assert(current_routing->set_bypassroute,
479               "no defined method \"set_bypassroute\" in \"%s\"",
480               current_routing->name);
481   (*(current_routing->set_bypassroute)) (current_routing, src, dst,
482                                          e_route);
483   link_list = NULL;
484   src = NULL;
485   dst = NULL;
486   gw_src = NULL;
487   gw_dst = NULL;
488 }
489
490 /**
491  * \brief Make a new routing component
492  *
493  * make the new structure and
494  * set the parsing functions to allows parsing the part of the routing tree
495  */
496 static void parse_S_AS(char *AS_id, char *AS_routing)
497 {
498   routing_component_t new_routing;
499   model_type_t model = NULL;
500   char *wanted = AS_routing;
501   int cpt;
502   /* seach the routing model */
503   for (cpt = 0; routing_models[cpt].name; cpt++)
504     if (!strcmp(wanted, routing_models[cpt].name))
505       model = &routing_models[cpt];
506   /* if its not exist, error */
507   if (model == NULL) {
508     fprintf(stderr, "Routing model %s not found. Existing models:\n",
509             wanted);
510     for (cpt = 0; routing_models[cpt].name; cpt++)
511       if (!strcmp(wanted, routing_models[cpt].name))
512         fprintf(stderr, "   %s: %s\n", routing_models[cpt].name,
513                 routing_models[cpt].desc);
514     xbt_die(NULL);
515   }
516
517   /* make a new routing component */
518   new_routing = (routing_component_t) (*(model->create)) ();
519   new_routing->routing = model;
520   new_routing->hierarchy = SURF_ROUTING_NULL;
521   new_routing->name = xbt_strdup(AS_id);
522   new_routing->routing_sons = xbt_dict_new();
523
524   /* Hack for Vivaldi */
525   if(!strcmp(model->name,"Vivaldi"))
526         new_routing->get_latency = vivaldi_get_link_latency;
527
528   if (current_routing == NULL && global_routing->root == NULL) {
529
530     /* it is the first one */
531     new_routing->routing_father = NULL;
532     global_routing->root = new_routing;
533
534   } else if (current_routing != NULL && global_routing->root != NULL) {
535
536     xbt_assert(!xbt_dict_get_or_null
537                 (current_routing->routing_sons, AS_id),
538                 "The AS \"%s\" already exists", AS_id);
539     /* it is a part of the tree */
540     new_routing->routing_father = current_routing;
541     /* set the father behavior */
542     if (current_routing->hierarchy == SURF_ROUTING_NULL)
543       current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
544     /* add to the sons dictionary */
545     xbt_dict_set(current_routing->routing_sons, AS_id,
546                  (void *) new_routing, NULL);
547     /* add to the father element list */
548     (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
549     /* unload the prev parse rules */
550     (*(current_routing->routing->unload)) ();
551
552   } else {
553     THROWF(arg_error, 0, "All defined components must be belong to a AS");
554   }
555   /* set the new parse rules */
556   (*(new_routing->routing->load)) ();
557   /* set the new current component of the tree */
558   current_routing = new_routing;
559 }
560
561 /*
562  * Detect the routing model type of the routing component from XML platforms
563  */
564 static void parse_S_AS_XML(void)
565 {
566   parse_S_AS(A_surfxml_AS_id, A_surfxml_AS_routing);
567 }
568
569 /*
570  * define the routing model type of routing component from lua script
571  */
572 static void parse_S_AS_lua(char *id, char *mode)
573 {
574   parse_S_AS(id, mode);
575 }
576
577
578 /**
579  * \brief Finish the creation of a new routing component
580  *
581  * When you finish to read the routing component, other structures must be created. 
582  * the "end" method allow to do that for any routing model type
583  */
584 static void parse_E_AS(const char *AS_id)
585 {
586
587   if (current_routing == NULL) {
588     THROWF(arg_error, 0, "Close AS(%s), that never open", AS_id);
589   } else {
590     network_element_info_t info = NULL;
591     xbt_assert(!xbt_lib_get_or_null(as_router_lib,current_routing->name, ROUTING_ASR_LEVEL),
592                 "The AS \"%s\" already exists",current_routing->name);
593     info = xbt_new0(s_network_element_info_t, 1);
594     info->rc_component = current_routing->routing_father;
595     info->rc_type = SURF_NETWORK_ELEMENT_AS;
596     xbt_lib_set(as_router_lib,current_routing->name,ROUTING_ASR_LEVEL,(void *) info);
597
598     (*(current_routing->routing->unload)) ();
599     (*(current_routing->routing->end)) ();
600     current_routing = current_routing->routing_father;
601     if (current_routing != NULL)
602       (*(current_routing->routing->load)) ();
603   }
604 }
605
606 /*
607  * \brief Finish the creation of a new routing component from XML
608  */
609 static void parse_E_AS_XML(void)
610 {
611   parse_E_AS(A_surfxml_AS_id);
612 }
613
614 /*
615  * \brief Finish the creation of a new routing component from lua
616  */
617 static void parse_E_AS_lua(const char *id)
618 {
619   parse_E_AS(id);
620 }
621
622 /* Aux Business methods */
623
624 /**
625  * \brief Get the AS name of the element
626  *
627  * \param name the host name
628  *
629  */
630 static char* elements_As_name(const char *name)
631 {
632   routing_component_t as_comp;
633
634   /* (1) find the as where the host is located */
635   as_comp = ((network_element_info_t)
636             xbt_lib_get_or_null(host_lib,name, ROUTING_HOST_LEVEL))->rc_component;
637   return as_comp->name;
638 }
639
640
641 /**
642  * \brief Get the AS father and the first elements of the chain
643  *
644  * \param src the source host name 
645  * \param dst the destination host name
646  * 
647  * Get the common father of the to processing units, and the first different 
648  * father in the chain
649  */
650 static xbt_dynar_t elements_father(const char *src, const char *dst)
651 {
652
653   xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
654
655   xbt_dynar_t result = xbt_dynar_new(sizeof(char *), NULL);
656
657   routing_component_t src_as, dst_as;
658   int index_src, index_dst, index_father_src, index_father_dst;
659   xbt_dynar_t path_src = NULL;
660   xbt_dynar_t path_dst = NULL;
661   routing_component_t current = NULL;
662   routing_component_t *current_src = NULL;
663   routing_component_t *current_dst = NULL;
664   routing_component_t *father = NULL;
665
666   /* (1) find the as where the src and dst are located */
667   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
668   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
669   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
670   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
671   src_as = ((network_element_info_t)src_data)->rc_component;
672   dst_as = ((network_element_info_t)dst_data)->rc_component;
673
674   xbt_assert(src_as
675               && dst_as,
676               "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
677               dst);
678
679   /* (2) find the path to the root routing component */
680   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
681   current = src_as;
682   while (current != NULL) {
683     xbt_dynar_push(path_src, &current);
684     current = current->routing_father;
685   }
686   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
687   current = dst_as;
688   while (current != NULL) {
689     xbt_dynar_push(path_dst, &current);
690     current = current->routing_father;
691   }
692
693   /* (3) find the common father */
694   index_src = path_src->used - 1;
695   index_dst = path_dst->used - 1;
696   current_src = xbt_dynar_get_ptr(path_src, index_src);
697   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
698   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
699     current_src = xbt_dynar_get_ptr(path_src, index_src);
700     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
701     index_src--;
702     index_dst--;
703   }
704   index_src++;
705   index_dst++;
706   current_src = xbt_dynar_get_ptr(path_src, index_src);
707   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
708
709   /* (4) they are not in the same routing component, make the path */
710   index_father_src = index_src + 1;
711   index_father_dst = index_dst + 1;
712
713   if (*current_src == *current_dst)
714     father = current_src;
715   else
716     father = xbt_dynar_get_ptr(path_src, index_father_src);
717
718   /* (5) result generation */
719   xbt_dynar_push(result, father);       /* first same the father of src and dst */
720   xbt_dynar_push(result, current_src);  /* second the first different father of src */
721   xbt_dynar_push(result, current_dst);  /* three  the first different father of dst */
722
723   xbt_dynar_free(&path_src);
724   xbt_dynar_free(&path_dst);
725
726   return result;
727 }
728
729 /* Global Business methods */
730
731 /**
732  * \brief Recursive function for get_route
733  *
734  * \param src the source host name 
735  * \param dst the destination host name
736  * 
737  * This fuction is call by "get_route". It allow to walk through the 
738  * routing components tree.
739  */
740 static route_extended_t _get_route(const char *src, const char *dst)
741 {
742
743   void *link;
744   unsigned int cpt = 0;
745
746   XBT_DEBUG("Solve route  \"%s\" to \"%s\"", src, dst);
747
748   xbt_assert(src && dst, "bad parameters for \"_get_route\" method");
749
750   route_extended_t e_route, e_route_cnt, e_route_src = NULL, e_route_dst =
751       NULL;
752
753   xbt_dynar_t elem_father_list = elements_father(src, dst);
754
755   routing_component_t common_father =
756       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
757   routing_component_t src_father =
758       xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
759   routing_component_t dst_father =
760       xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
761
762   e_route = xbt_new0(s_route_extended_t, 1);
763   e_route->src_gateway = NULL;
764   e_route->dst_gateway = NULL;
765   e_route->generic_route.link_list =
766       xbt_dynar_new(global_routing->size_of_link, NULL);
767
768   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
769
770     if (strcmp(src, dst)) {
771       e_route_cnt =
772           (*(common_father->get_route)) (common_father, src, dst);
773       xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"", src,
774                   dst);
775       xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
776         xbt_dynar_push(e_route->generic_route.link_list, &link);
777       }
778       generic_free_extended_route(e_route_cnt);
779     }
780
781   } else {                      /* SURF_ROUTING_RECURSIVE */
782
783     route_extended_t e_route_bypass = NULL;
784
785     if (common_father->get_bypass_route)
786       e_route_bypass =
787           (*(common_father->get_bypass_route)) (common_father, src, dst);
788
789     if (e_route_bypass)
790       e_route_cnt = e_route_bypass;
791     else
792       e_route_cnt =
793           (*(common_father->get_route)) (common_father, src_father->name,
794                                          dst_father->name);
795
796     xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
797                 src_father->name, dst_father->name);
798
799     xbt_assert((e_route_cnt->src_gateway == NULL) ==
800                 (e_route_cnt->dst_gateway == NULL),
801                 "bad gateway for route between \"%s\" and \"%s\"", src,
802                 dst);
803
804     if (strcmp(src, e_route_cnt->src_gateway)) {
805       e_route_src = _get_route(src, e_route_cnt->src_gateway);
806       xbt_assert(e_route_src, "no route between \"%s\" and \"%s\"", src,
807                   e_route_cnt->src_gateway);
808       xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
809         xbt_dynar_push(e_route->generic_route.link_list, &link);
810       }
811     }
812
813     xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
814       xbt_dynar_push(e_route->generic_route.link_list, &link);
815     }
816
817     if (strcmp(e_route_cnt->dst_gateway, dst)) {
818       e_route_dst = _get_route(e_route_cnt->dst_gateway, dst);
819       xbt_assert(e_route_dst, "no route between \"%s\" and \"%s\"",
820                   e_route_cnt->dst_gateway, dst);
821       xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
822         xbt_dynar_push(e_route->generic_route.link_list, &link);
823       }
824     }
825
826     e_route->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
827     e_route->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
828
829     generic_free_extended_route(e_route_src);
830     generic_free_extended_route(e_route_cnt);
831     generic_free_extended_route(e_route_dst);
832   }
833
834   xbt_dynar_free(&elem_father_list);
835
836   return e_route;
837 }
838
839 static double _get_latency(const char *src, const char *dst)
840 {
841   double latency, latency_src, latency_dst = 0.0;
842
843   XBT_DEBUG("Solve route  \"%s\" to \"%s\"", src, dst);
844   xbt_assert(src && dst, "bad parameters for \"_get_route\" method");
845
846   route_extended_t e_route_cnt;
847
848   xbt_dynar_t elem_father_list = elements_father(src, dst);
849
850   routing_component_t common_father =
851       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
852   routing_component_t src_father =
853       xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
854   routing_component_t dst_father =
855       xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
856
857   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
858
859     if (strcmp(src, dst)) {
860       latency =
861           (*(common_father->get_latency)) (common_father, src, dst);
862       xbt_assert(latency>=0, "no route between \"%s\" and \"%s\"", src,
863                   dst);
864      } else latency = 0;
865   } else {                      /* SURF_ROUTING_RECURSIVE */
866      route_extended_t e_route_bypass = NULL;
867     if (common_father->get_bypass_route)
868       e_route_bypass =
869           (*(common_father->get_bypass_route)) (common_father, src, dst);
870
871     xbt_assert(!e_route_bypass,"Bypass cannot work yet with get_latency");
872                                                  
873     e_route_cnt =
874           (*(common_father->get_route)) (common_father, src_father->name,
875                                          dst_father->name);
876
877     xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
878                 src_father->name, dst_father->name);
879
880     xbt_assert((e_route_cnt->src_gateway == NULL) ==
881                 (e_route_cnt->dst_gateway == NULL),
882                 "bad gateway for route between \"%s\" and \"%s\"", src,
883                 dst);            
884     latency =
885           (*(common_father->get_latency)) (common_father, elements_As_name(src),
886                           elements_As_name(dst));
887
888     xbt_assert(latency>=0, "no route between \"%s\" and \"%s\"",
889                 src_father->name, dst_father->name);
890     
891
892     if (src != e_route_cnt->src_gateway) {
893
894       latency_src = _get_latency(src, e_route_cnt->src_gateway);
895       xbt_assert(latency_src>=0, "no route between \"%s\" and \"%s\"", src,
896                   e_route_cnt->src_gateway);
897       latency += latency_src;
898     }
899
900     if (e_route_cnt->dst_gateway != dst) {
901     
902       latency_dst = _get_latency(e_route_cnt->dst_gateway, dst);
903       xbt_assert(latency_dst>=0, "no route between \"%s\" and \"%s\"",
904                   e_route_cnt->dst_gateway, dst);
905       latency += latency_dst;
906     }
907         
908   }
909
910   xbt_dynar_free(&elem_father_list);
911
912   return latency;
913 }
914
915 /**
916  * \brief Generic method: find a route between hosts
917  *
918  * \param src the source host name 
919  * \param dst the destination host name
920  * 
921  * walk through the routing components tree and find a route between hosts
922  * by calling the differents "get_route" functions in each routing component.
923  * No need to free the returned dynar. It will be freed at the next call.
924  */
925 static xbt_dynar_t get_route(const char *src, const char *dst)
926 {
927
928   route_extended_t e_route;
929   xbt_dynar_t elem_father_list = NULL;
930   routing_component_t common_father = NULL;
931
932   if (strcmp(src, dst))
933     e_route = _get_route(src, dst);
934   else {
935     elem_father_list = elements_father(src, dst);
936     common_father =
937         xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
938
939     e_route = (*(common_father->get_route)) (common_father, src, dst);
940     xbt_dynar_free(&elem_father_list);
941   }
942
943   xbt_assert(e_route, "no route between \"%s\" and \"%s\"", src, dst);
944
945   if (global_routing->last_route)
946     xbt_dynar_free(&(global_routing->last_route));
947   global_routing->last_route = e_route->generic_route.link_list;
948
949   if (e_route->src_gateway)
950     xbt_free(e_route->src_gateway);
951   if (e_route->dst_gateway)
952     xbt_free(e_route->dst_gateway);
953
954   xbt_free(e_route);
955
956 /*
957   if (xbt_dynar_length(global_routing->last_route) == 0)
958     return NULL;
959   else
960 */
961     return global_routing->last_route;
962 }
963
964 /**
965  * \brief Generic method: find a route between hosts
966  *
967  * \param src the source host name
968  * \param dst the destination host name
969  *
970  * walk through the routing components tree and find a route between hosts
971  * by calling the differents "get_route" functions in each routing component.
972  * Leaves the caller the responsability to clean the returned dynar.
973  */
974 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
975 {
976         xbt_dynar_t d = get_route(src,dst);
977         global_routing->last_route = NULL;
978         return d;
979 }
980
981 /*Get Latency*/
982 static double get_latency(const char *src, const char *dst)
983 {
984
985   double latency = -1.0;
986   xbt_dynar_t elem_father_list = elements_father(src, dst);
987   routing_component_t common_father =
988       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
989
990   if (strcmp(src, dst))
991     latency = _get_latency(src, dst);
992   else
993     latency = (*(common_father->get_latency)) (common_father, src, dst);
994
995   xbt_assert(latency>=0.0, "no route between \"%s\" and \"%s\"", src, dst);
996   xbt_dynar_free(&elem_father_list);
997
998   return latency;
999 }
1000
1001 /**
1002  * \brief Recursive function for finalize
1003  *
1004  * \param rc the source host name 
1005  * 
1006  * This fuction is call by "finalize". It allow to finalize the 
1007  * AS or routing components. It delete all the structures.
1008  */
1009 static void _finalize(routing_component_t rc)
1010 {
1011   if (rc) {
1012     xbt_dict_cursor_t cursor = NULL;
1013     char *key;
1014     routing_component_t elem;
1015     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
1016       _finalize(elem);
1017     }
1018     xbt_dict_t tmp_sons = rc->routing_sons;
1019     char *tmp_name = rc->name;
1020     xbt_dict_free(&tmp_sons);
1021     xbt_free(tmp_name);
1022     xbt_assert(rc->finalize, "no defined method \"finalize\" in \"%s\"",
1023                 current_routing->name);
1024     (*(rc->finalize)) (rc);
1025   }
1026 }
1027
1028 /**
1029  * \brief Generic method: delete all the routing structures
1030  * 
1031  * walk through the routing components tree and delete the structures
1032  * by calling the differents "finalize" functions in each routing component
1033  */
1034 static void finalize(void)
1035 {
1036   /* delete recursibly all the tree */
1037   _finalize(global_routing->root);
1038   /* delete last_route */
1039   xbt_dynar_free(&(global_routing->last_route));
1040   /* delete global routing structure */
1041   xbt_free(global_routing);
1042 }
1043
1044 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
1045 {
1046   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1047
1048   //adding my one link routes
1049   unsigned int cpt;
1050   void *link;
1051   xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
1052   if (onelink_mine) {
1053     xbt_dynar_foreach(onelink_mine, cpt, link) {
1054       xbt_dynar_push(ret, &link);
1055     }
1056   }
1057   //recursing
1058   char *key;
1059   xbt_dict_cursor_t cursor = NULL;
1060   routing_component_t rc_child;
1061   xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
1062     xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
1063     if (onelink_child) {
1064       xbt_dynar_foreach(onelink_child, cpt, link) {
1065         xbt_dynar_push(ret, &link);
1066       }
1067     }
1068   }
1069   return ret;
1070 }
1071
1072 static xbt_dynar_t get_onelink_routes(void)
1073 {
1074   return recursive_get_onelink_routes(global_routing->root);
1075 }
1076
1077 e_surf_network_element_type_t get_network_element_type(const char
1078                                                               *name)
1079 {
1080   network_element_info_t rc = NULL;
1081
1082   rc = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
1083   if(rc) return rc->rc_type;
1084
1085   rc = xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
1086   if(rc) return rc->rc_type;
1087
1088   return SURF_NETWORK_ELEMENT_NULL;
1089 }
1090
1091 /**
1092  * \brief Generic method: create the global routing schema
1093  * 
1094  * Make a global routing structure and set all the parsing functions.
1095  */
1096 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
1097 {
1098   /* config the uniq global routing */
1099   global_routing = xbt_new0(s_routing_global_t, 1);
1100   global_routing->root = NULL;
1101   global_routing->get_route = get_route;
1102   global_routing->get_latency = get_latency;
1103   global_routing->get_route_no_cleanup = get_route_no_cleanup;
1104   global_routing->get_onelink_routes = get_onelink_routes;
1105   global_routing->get_network_element_type = get_network_element_type;
1106   global_routing->finalize = finalize;
1107   global_routing->loopback = loopback;
1108   global_routing->size_of_link = size_of_links;
1109   global_routing->last_route = NULL;
1110   get_link_latency = get_link_latency_fun;
1111   /* no current routing at moment */
1112   current_routing = NULL;
1113
1114   /* parse generic elements */
1115   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
1116   surfxml_add_callback(ETag_surfxml_host_cb_list, &parse_E_host_XML);
1117   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router_XML);
1118
1119   surfxml_add_callback(STag_surfxml_route_cb_list,
1120                        &parse_S_route_new_and_endpoints_XML);
1121   surfxml_add_callback(STag_surfxml_ASroute_cb_list,
1122                        &parse_S_ASroute_new_and_endpoints);
1123   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
1124                        &parse_S_bypassRoute_new_and_endpoints);
1125
1126   surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
1127                        &parse_E_link_ctn_new_elem_XML);
1128
1129   surfxml_add_callback(ETag_surfxml_route_cb_list,
1130                        &parse_E_route_store_route);
1131   surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
1132                        &parse_E_ASroute_store_route);
1133   surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
1134                        &parse_E_bypassRoute_store_route);
1135
1136   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
1137   surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
1138
1139   surfxml_add_callback(STag_surfxml_cluster_cb_list,
1140                        &routing_parse_Scluster);
1141
1142   surfxml_add_callback(STag_surfxml_peer_cb_list,
1143                          &routing_parse_Speer);
1144
1145   surfxml_add_callback(ETag_surfxml_platform_cb_list,
1146                                                   &clean_dict_random);
1147
1148 #ifdef HAVE_TRACING
1149   instr_routing_define_callbacks();
1150 #endif
1151 }
1152
1153 void surf_parse_add_callback_config(void)
1154 {
1155         surfxml_add_callback(STag_surfxml_config_cb_list, &routing_parse_Sconfig);
1156         surfxml_add_callback(ETag_surfxml_config_cb_list, &routing_parse_Econfig);
1157         surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties_XML);
1158         surfxml_add_callback(STag_surfxml_AS_cb_list, &surf_parse_models_setup);
1159         surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
1160 }
1161
1162 void surf_parse_models_setup()
1163 {
1164         routing_parse_Erandom();
1165         surfxml_del_callback(STag_surfxml_AS_cb_list, surf_parse_models_setup);
1166         surf_config_models_setup(platform_filename);
1167         free(platform_filename);
1168 }
1169
1170 /* ************************************************************************** */
1171 /* *************************** FULL ROUTING ********************************* */
1172
1173 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
1174
1175 /* Routing model structure */
1176
1177 typedef struct {
1178   s_routing_component_t generic_routing;
1179   route_extended_t *routing_table;
1180 } s_routing_component_full_t, *routing_component_full_t;
1181
1182 /* Business methods */
1183 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
1184 {
1185   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1186
1187   routing_component_full_t routing = (routing_component_full_t) rc;
1188   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1189   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1190   char *k1, *d1, *k2, *d2;
1191   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
1192     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
1193       int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k1);
1194       int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k2);
1195       xbt_assert(src_id
1196                   && dst_id,
1197                   "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1198                   src, dst);
1199       route_extended_t route = TO_ROUTE_FULL(*src_id, *dst_id);
1200       if (route) {
1201         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1202           void *link =
1203               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1204                                            0);
1205           onelink_t onelink = xbt_new0(s_onelink_t, 1);
1206           onelink->link_ptr = link;
1207           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1208             onelink->src = xbt_strdup(k1);
1209             onelink->dst = xbt_strdup(k2);
1210           } else if (routing->generic_routing.hierarchy ==
1211                      SURF_ROUTING_RECURSIVE) {
1212             onelink->src = xbt_strdup(route->src_gateway);
1213             onelink->dst = xbt_strdup(route->dst_gateway);
1214           }
1215           xbt_dynar_push(ret, &onelink);
1216         }
1217       }
1218     }
1219   }
1220   return ret;
1221 }
1222
1223 static route_extended_t full_get_route(routing_component_t rc,
1224                                        const char *src, const char *dst)
1225 {
1226   xbt_assert(rc && src
1227               && dst,
1228               "Invalid params for \"get_route\" function at AS \"%s\"",
1229               rc->name);
1230
1231   /* set utils vars */
1232   routing_component_full_t routing = (routing_component_full_t) rc;
1233   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1234
1235   generic_src_dst_check(rc, src, dst);
1236   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1237   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1238   xbt_assert(src_id
1239               && dst_id,
1240               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1241               src, dst);
1242
1243   route_extended_t e_route = NULL;
1244   route_extended_t new_e_route = NULL;
1245   void *link;
1246   unsigned int cpt = 0;
1247
1248   e_route = TO_ROUTE_FULL(*src_id, *dst_id);
1249
1250   if (e_route) {
1251     new_e_route = xbt_new0(s_route_extended_t, 1);
1252     new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
1253     new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
1254     new_e_route->generic_route.link_list =
1255         xbt_dynar_new(global_routing->size_of_link, NULL);
1256     xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
1257       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1258     }
1259   }
1260   return new_e_route;
1261 }
1262
1263 static void full_finalize(routing_component_t rc)
1264 {
1265   routing_component_full_t routing = (routing_component_full_t) rc;
1266   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1267   int i, j;
1268   if (routing) {
1269     /* Delete routing table */
1270     for (i = 0; i < table_size; i++)
1271       for (j = 0; j < table_size; j++)
1272         generic_free_extended_route(TO_ROUTE_FULL(i, j));
1273     xbt_free(routing->routing_table);
1274     /* Delete bypass dict */
1275     xbt_dict_free(&rc->bypassRoutes);
1276     /* Delete index dict */
1277     xbt_dict_free(&rc->to_index);
1278     /* Delete structure */
1279     xbt_free(rc);
1280   }
1281 }
1282
1283 /* Creation routing model functions */
1284
1285 static void *model_full_create(void)
1286 {
1287   routing_component_full_t new_component =
1288       xbt_new0(s_routing_component_full_t, 1);
1289   new_component->generic_routing.set_processing_unit =
1290       generic_set_processing_unit;
1291   new_component->generic_routing.set_autonomous_system =
1292       generic_set_autonomous_system;
1293   new_component->generic_routing.set_route = model_full_set_route;
1294   new_component->generic_routing.set_ASroute = model_full_set_route;
1295   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1296   new_component->generic_routing.get_route = full_get_route;
1297   new_component->generic_routing.get_latency = generic_get_link_latency;
1298   new_component->generic_routing.get_onelink_routes =
1299       full_get_onelink_routes;
1300   new_component->generic_routing.get_bypass_route =
1301       generic_get_bypassroute;
1302   new_component->generic_routing.finalize = full_finalize;
1303   new_component->generic_routing.to_index = xbt_dict_new();
1304   new_component->generic_routing.bypassRoutes = xbt_dict_new();
1305   new_component->generic_routing.get_network_element_type = get_network_element_type;
1306   return new_component;
1307 }
1308
1309 static void model_full_load(void)
1310 {
1311   /* use "surfxml_add_callback" to add a parse function call */
1312 }
1313
1314 static void model_full_unload(void)
1315 {
1316   /* use "surfxml_del_callback" to remove a parse function call */
1317 }
1318
1319 static void model_full_end(void)
1320 {
1321   unsigned int i;
1322   route_extended_t e_route;
1323
1324   /* set utils vars */
1325   routing_component_full_t routing =
1326       ((routing_component_full_t) current_routing);
1327   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1328
1329   /* Create table if necessary */
1330   if(!routing->routing_table)
1331           routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
1332
1333   /* Add the loopback if needed */
1334   if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1335     for (i = 0; i < table_size; i++) {
1336       e_route = TO_ROUTE_FULL(i, i);
1337       if (!e_route) {
1338         e_route = xbt_new0(s_route_extended_t, 1);
1339         e_route->src_gateway = NULL;
1340         e_route->dst_gateway = NULL;
1341         e_route->generic_route.link_list =
1342             xbt_dynar_new(global_routing->size_of_link, NULL);
1343         xbt_dynar_push(e_route->generic_route.link_list,
1344                        &global_routing->loopback);
1345         TO_ROUTE_FULL(i, i) = e_route;
1346       }
1347     }
1348   }
1349 }
1350
1351 static void model_full_set_route(routing_component_t rc, const char *src,
1352                 const char *dst, name_route_extended_t route)
1353 {
1354         int *src_id, *dst_id;
1355         src_id = xbt_dict_get_or_null(rc->to_index, src);
1356         dst_id = xbt_dict_get_or_null(rc->to_index, dst);
1357         routing_component_full_t routing = ((routing_component_full_t) rc);
1358         size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1359
1360         xbt_assert(src_id
1361                           && dst_id, "Network elements %s or %s not found", src, dst);
1362
1363         xbt_assert(xbt_dynar_length(route->generic_route.link_list) > 0,
1364                           "Invalid count of links, must be greater than zero (%s,%s)",
1365                           src, dst);
1366
1367         if(!routing->routing_table)
1368                 routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
1369
1370         if(TO_ROUTE_FULL(*src_id, *dst_id))
1371         {
1372                 char * link_name;
1373                 unsigned int i;
1374                 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1375                 xbt_dynar_foreach(route->generic_route.link_list,i,link_name)
1376                 {
1377                         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1378                         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
1379                         xbt_dynar_push(link_route_to_test,&link);
1380                 }
1381                 xbt_assert(!xbt_dynar_compare(
1382                           (void*)TO_ROUTE_FULL(*src_id, *dst_id)->generic_route.link_list,
1383                           (void*)link_route_to_test,
1384                           (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1385                           "The route between \"%s\" and \"%s\" already exists. If you are trying to define a reverse route, you must set the symmetrical=no attribute to your routes tags.", src,dst);
1386         }
1387         else
1388         {
1389                   if(!route->dst_gateway && !route->src_gateway)
1390                           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
1391                   else{
1392                           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
1393                                  route->src_gateway, dst, route->dst_gateway);
1394                           if(global_routing->get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
1395                                   xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
1396                           if(global_routing->get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
1397                                   xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
1398                   }
1399               TO_ROUTE_FULL(*src_id, *dst_id) = generic_new_extended_route(rc->hierarchy,route,1);
1400               xbt_dynar_shrink(TO_ROUTE_FULL(*src_id, *dst_id)->generic_route.link_list, 0);
1401         }
1402
1403         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
1404                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
1405         {
1406                 if(route->dst_gateway && route->src_gateway)
1407                 {
1408                   char *gw_tmp ;
1409                   gw_tmp = route->src_gateway;
1410                   route->src_gateway = route->dst_gateway;
1411                   route->dst_gateway = gw_tmp;
1412                 }
1413                 if(TO_ROUTE_FULL(*dst_id, *src_id))
1414                 {
1415                         char * link_name;
1416                         unsigned int i;
1417                         xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1418                         for(i=xbt_dynar_length(route->generic_route.link_list) ;i>0 ;i--)
1419                         {
1420                                 link_name = xbt_dynar_get_as(route->generic_route.link_list,i-1,void *);
1421                                 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1422                                 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
1423                                 xbt_dynar_push(link_route_to_test,&link);
1424                         }
1425                         xbt_assert(!xbt_dynar_compare(
1426                                   (void*)TO_ROUTE_FULL(*dst_id, *src_id)->generic_route.link_list,
1427                               (void*)link_route_to_test,
1428                                   (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1429                                   "The route between \"%s\" and \"%s\" already exists", src,dst);
1430                 }
1431                 else
1432                 {
1433                           if(!route->dst_gateway && !route->src_gateway)
1434                                   XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
1435                           else
1436                                   XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
1437                                          route->src_gateway, src, route->dst_gateway);
1438                       TO_ROUTE_FULL(*dst_id, *src_id) = generic_new_extended_route(rc->hierarchy,route,0);
1439                       xbt_dynar_shrink(TO_ROUTE_FULL(*dst_id, *src_id)->generic_route.link_list, 0);
1440                 }
1441         }
1442
1443         if (rc->hierarchy == SURF_ROUTING_BASE) generic_free_route((route_t)route) ;
1444         else generic_free_extended_route((route_extended_t)route);
1445 }
1446
1447 /* ************************************************************************** */
1448 /* *************************** FLOYD ROUTING ******************************** */
1449
1450 #define TO_FLOYD_COST(i,j) (routing->cost_table)[(i)+(j)*table_size]
1451 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
1452 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
1453
1454 /* Routing model structure */
1455
1456 typedef struct {
1457   s_routing_component_t generic_routing;
1458   /* vars for calculate the floyd algorith. */
1459   int *predecessor_table;
1460   double *cost_table;
1461   route_extended_t *link_table; /* char* -> int* */
1462 } s_routing_component_floyd_t, *routing_component_floyd_t;
1463
1464 static route_extended_t floyd_get_route(routing_component_t rc,
1465                                         const char *src, const char *dst);
1466
1467 /* Business methods */
1468 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
1469 {
1470   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1471
1472   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1473   //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1474   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1475   char *k1, *d1, *k2, *d2;
1476   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
1477     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
1478       route_extended_t route = floyd_get_route(rc, k1, k2);
1479       if (route) {
1480         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1481           void *link =
1482               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1483                                            0);
1484           onelink_t onelink = xbt_new0(s_onelink_t, 1);
1485           onelink->link_ptr = link;
1486           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1487             onelink->src = xbt_strdup(k1);
1488             onelink->dst = xbt_strdup(k2);
1489           } else if (routing->generic_routing.hierarchy ==
1490                      SURF_ROUTING_RECURSIVE) {
1491             onelink->src = xbt_strdup(route->src_gateway);
1492             onelink->dst = xbt_strdup(route->dst_gateway);
1493           }
1494           xbt_dynar_push(ret, &onelink);
1495         }
1496       }
1497     }
1498   }
1499   return ret;
1500 }
1501
1502 static route_extended_t floyd_get_route(routing_component_t rc,
1503                                         const char *src, const char *dst)
1504 {
1505   xbt_assert(rc && src
1506               && dst,
1507               "Invalid params for \"get_route\" function at AS \"%s\"",
1508               rc->name);
1509
1510   /* set utils vars */
1511   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1512   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1513
1514   generic_src_dst_check(rc, src, dst);
1515   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1516   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1517   xbt_assert(src_id
1518               && dst_id,
1519               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1520               src, dst);
1521
1522   /* create a result route */
1523   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1524   new_e_route->generic_route.link_list =
1525       xbt_dynar_new(global_routing->size_of_link, NULL);
1526   new_e_route->src_gateway = NULL;
1527   new_e_route->dst_gateway = NULL;
1528
1529   int first = 1;
1530   int pred = *dst_id;
1531   int prev_pred = 0;
1532   char *gw_src = NULL, *gw_dst =
1533       NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1534   unsigned int cpt;
1535   void *link;
1536   xbt_dynar_t links;
1537
1538   do {
1539     prev_pred = pred;
1540     pred = TO_FLOYD_PRED(*src_id, pred);
1541     if (pred == -1)             /* if no pred in route -> no route to host */
1542       break;
1543     xbt_assert(TO_FLOYD_LINK(pred, prev_pred),
1544                 "Invalid link for the route between \"%s\" or \"%s\"", src,
1545                 dst);
1546
1547     prev_gw_src = gw_src;
1548     prev_gw_dst = gw_dst;
1549
1550     route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
1551     gw_src = e_route->src_gateway;
1552     gw_dst = e_route->dst_gateway;
1553
1554     if (first)
1555       first_gw = gw_dst;
1556
1557     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
1558         && strcmp(gw_dst, prev_gw_src)) {
1559       xbt_dynar_t e_route_as_to_as =
1560           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1561       xbt_assert(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1562                   gw_dst, prev_gw_src);
1563       links = e_route_as_to_as;
1564       int pos = 0;
1565       xbt_dynar_foreach(links, cpt, link) {
1566         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1567                             &link);
1568         pos++;
1569       }
1570     }
1571
1572     links = e_route->generic_route.link_list;
1573     xbt_dynar_foreach(links, cpt, link) {
1574       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1575     }
1576     first = 0;
1577
1578   } while (pred != *src_id);
1579   xbt_assert(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
1580               *src_id, *dst_id, src, dst);
1581
1582   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1583     new_e_route->src_gateway = xbt_strdup(gw_src);
1584     new_e_route->dst_gateway = xbt_strdup(first_gw);
1585   }
1586
1587   return new_e_route;
1588 }
1589
1590 static void floyd_finalize(routing_component_t rc)
1591 {
1592   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1593   int i, j;
1594   size_t table_size;
1595   if (routing) {
1596     table_size = xbt_dict_length(routing->generic_routing.to_index);
1597     /* Delete link_table */
1598     for (i = 0; i < table_size; i++)
1599       for (j = 0; j < table_size; j++)
1600         generic_free_extended_route(TO_FLOYD_LINK(i, j));
1601     xbt_free(routing->link_table);
1602     /* Delete bypass dict */
1603     xbt_dict_free(&routing->generic_routing.bypassRoutes);
1604     /* Delete index dict */
1605     xbt_dict_free(&(routing->generic_routing.to_index));
1606     /* Delete dictionary index dict, predecessor and links table */
1607     xbt_free(routing->predecessor_table);
1608     /* Delete structure */
1609     xbt_free(rc);
1610   }
1611 }
1612
1613 static void *model_floyd_create(void)
1614 {
1615   routing_component_floyd_t new_component =
1616       xbt_new0(s_routing_component_floyd_t, 1);
1617   new_component->generic_routing.set_processing_unit =
1618       generic_set_processing_unit;
1619   new_component->generic_routing.set_autonomous_system =
1620       generic_set_autonomous_system;
1621   new_component->generic_routing.set_route = model_floyd_set_route;
1622   new_component->generic_routing.set_ASroute = model_floyd_set_route;
1623   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1624   new_component->generic_routing.get_route = floyd_get_route;
1625   new_component->generic_routing.get_latency = generic_get_link_latency;
1626   new_component->generic_routing.get_onelink_routes =
1627       floyd_get_onelink_routes;
1628   new_component->generic_routing.get_bypass_route =
1629       generic_get_bypassroute;
1630   new_component->generic_routing.finalize = floyd_finalize;
1631   new_component->generic_routing.to_index = xbt_dict_new();
1632   new_component->generic_routing.bypassRoutes = xbt_dict_new();
1633   new_component->generic_routing.get_network_element_type = get_network_element_type;
1634   return new_component;
1635 }
1636
1637 static void model_floyd_load(void)
1638 {
1639   /* use "surfxml_add_callback" to add a parse function call */
1640 }
1641
1642 static void model_floyd_unload(void)
1643 {
1644   /* use "surfxml_del_callback" to remove a parse function call */
1645 }
1646
1647 static void model_floyd_end(void)
1648 {
1649
1650         routing_component_floyd_t routing =
1651           ((routing_component_floyd_t) current_routing);
1652
1653         unsigned int i, j, a, b, c;
1654
1655         /* set the size of table routing */
1656         size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1657
1658         if(!routing->link_table)
1659         {
1660                 /* Create Cost, Predecessor and Link tables */
1661                 routing->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
1662                 routing->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
1663                 routing->link_table = xbt_new0(route_extended_t, table_size * table_size);    /* actual link between src and dst */
1664
1665                 /* Initialize costs and predecessors */
1666                 for (i = 0; i < table_size; i++)
1667                 for (j = 0; j < table_size; j++) {
1668                   TO_FLOYD_COST(i, j) = DBL_MAX;
1669                   TO_FLOYD_PRED(i, j) = -1;
1670                   TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
1671                 }
1672         }
1673
1674         /* Add the loopback if needed */
1675         if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1676                 for (i = 0; i < table_size; i++) {
1677                   route_extended_t e_route = TO_FLOYD_LINK(i, i);
1678                   if (!e_route) {
1679                         e_route = xbt_new0(s_route_extended_t, 1);
1680                         e_route->src_gateway = NULL;
1681                         e_route->dst_gateway = NULL;
1682                         e_route->generic_route.link_list =
1683                                 xbt_dynar_new(global_routing->size_of_link, NULL);
1684                         xbt_dynar_push(e_route->generic_route.link_list,
1685                                                    &global_routing->loopback);
1686                         TO_FLOYD_LINK(i, i) = e_route;
1687                         TO_FLOYD_PRED(i, i) = i;
1688                         TO_FLOYD_COST(i, i) = 1;
1689                   }
1690                 }
1691         }
1692         /* Calculate path costs */
1693         for (c = 0; c < table_size; c++) {
1694                 for (a = 0; a < table_size; a++) {
1695                   for (b = 0; b < table_size; b++) {
1696                         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
1697                           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
1698                                   (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
1699                                    TO_FLOYD_COST(a, b))) {
1700                                 TO_FLOYD_COST(a, b) =
1701                                         TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
1702                                 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
1703                           }
1704                         }
1705                   }
1706                 }
1707         }
1708 }
1709
1710 static void model_floyd_set_route(routing_component_t rc, const char *src,
1711         const char *dst, name_route_extended_t route)
1712 {
1713         routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1714
1715         /* set the size of table routing */
1716         size_t table_size = xbt_dict_length(rc->to_index);
1717         int *src_id, *dst_id;
1718         int i,j;
1719
1720         src_id = xbt_dict_get_or_null(rc->to_index, src);
1721         dst_id = xbt_dict_get_or_null(rc->to_index, dst);
1722
1723         if(!routing->link_table)
1724         {
1725                 /* Create Cost, Predecessor and Link tables */
1726                 routing->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
1727                 routing->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
1728                 routing->link_table = xbt_new0(route_extended_t, table_size * table_size);    /* actual link between src and dst */
1729
1730                 /* Initialize costs and predecessors */
1731                 for (i = 0; i < table_size; i++)
1732                 for (j = 0; j < table_size; j++) {
1733                   TO_FLOYD_COST(i, j) = DBL_MAX;
1734                   TO_FLOYD_PRED(i, j) = -1;
1735                   TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
1736                 }
1737         }
1738
1739         if(TO_FLOYD_LINK(*src_id, *dst_id))
1740         {
1741                 if(!route->dst_gateway && !route->src_gateway)
1742                         XBT_DEBUG("See Route from \"%s\" to \"%s\"", src, dst);
1743                 else
1744                         XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
1745                                  route->src_gateway, dst, route->dst_gateway);
1746                 char * link_name;
1747                 unsigned int cpt;
1748                 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1749                 xbt_dynar_foreach(route->generic_route.link_list,cpt,link_name)
1750                 {
1751                         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1752                         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
1753                         xbt_dynar_push(link_route_to_test,&link);
1754                 }
1755                 xbt_assert(!xbt_dynar_compare(
1756                           (void*)TO_FLOYD_LINK(*src_id, *dst_id)->generic_route.link_list,
1757                           (void*)link_route_to_test,
1758                           (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1759                           "The route between \"%s\" and \"%s\" already exists", src,dst);
1760         }
1761         else
1762         {
1763                 if(!route->dst_gateway && !route->src_gateway)
1764                   XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
1765                 else{
1766                         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
1767                                  route->src_gateway, dst, route->dst_gateway);
1768                         if(global_routing->get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
1769                                 xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
1770                         if(global_routing->get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
1771                                 xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
1772                 }
1773             TO_FLOYD_LINK(*src_id, *dst_id) =
1774                         generic_new_extended_route(rc->hierarchy, route, 1);
1775             TO_FLOYD_PRED(*src_id, *dst_id) = *src_id;
1776             TO_FLOYD_COST(*src_id, *dst_id) =
1777                         ((TO_FLOYD_LINK(*src_id, *dst_id))->generic_route.link_list)->used;   /* count of links, old model assume 1 */
1778         }
1779
1780         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
1781                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
1782         {
1783                 if(TO_FLOYD_LINK(*dst_id, *src_id))
1784                 {
1785                         if(!route->dst_gateway && !route->src_gateway)
1786                           XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
1787                         else
1788                           XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
1789                                          route->src_gateway, src, route->dst_gateway);
1790                         char * link_name;
1791                         unsigned int i;
1792                         xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1793                         for(i=xbt_dynar_length(route->generic_route.link_list) ;i>0 ;i--)
1794                         {
1795                                 link_name = xbt_dynar_get_as(route->generic_route.link_list,i-1,void *);
1796                                 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1797                                 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
1798                                 xbt_dynar_push(link_route_to_test,&link);
1799                         }
1800                         xbt_assert(!xbt_dynar_compare(
1801                                   (void*)TO_FLOYD_LINK(*dst_id, *src_id)->generic_route.link_list,
1802                               (void*)link_route_to_test,
1803                                   (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1804                                   "The route between \"%s\" and \"%s\" already exists", src,dst);
1805                 }
1806                 else
1807                 {
1808                         if(route->dst_gateway && route->src_gateway)
1809                         {
1810                           char *gw_src = xbt_strdup(route->src_gateway);
1811                           char *gw_dst = xbt_strdup(route->dst_gateway);
1812                           route->src_gateway = gw_dst;
1813                           route->dst_gateway = gw_src;
1814                         }
1815
1816                         if(!route->dst_gateway && !route->src_gateway)
1817                           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
1818                         else
1819                           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
1820                                          route->src_gateway, src, route->dst_gateway);
1821
1822                     TO_FLOYD_LINK(*dst_id, *src_id) =
1823                                 generic_new_extended_route(rc->hierarchy, route, 0);
1824                     TO_FLOYD_PRED(*dst_id, *src_id) = *dst_id;
1825                     TO_FLOYD_COST(*dst_id, *src_id) =
1826                                 ((TO_FLOYD_LINK(*dst_id, *src_id))->generic_route.link_list)->used;   /* count of links, old model assume 1 */
1827                 }
1828         }
1829 }
1830
1831 /* ************************************************************************** */
1832 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1833
1834 typedef struct {
1835   s_routing_component_t generic_routing;
1836   xbt_graph_t route_graph;      /* xbt_graph */
1837   xbt_dict_t graph_node_map;    /* map */
1838   xbt_dict_t route_cache;       /* use in cache mode */
1839   int cached;
1840 } s_routing_component_dijkstra_t, *routing_component_dijkstra_t;
1841
1842
1843 typedef struct graph_node_data {
1844   int id;
1845   int graph_id;                 /* used for caching internal graph id's */
1846 } s_graph_node_data_t, *graph_node_data_t;
1847
1848 typedef struct graph_node_map_element {
1849   xbt_node_t node;
1850 } s_graph_node_map_element_t, *graph_node_map_element_t;
1851
1852 typedef struct route_cache_element {
1853   int *pred_arr;
1854   int size;
1855 } s_route_cache_element_t, *route_cache_element_t;
1856
1857 /* Free functions */
1858
1859 static void route_cache_elem_free(void *e)
1860 {
1861   route_cache_element_t elm = (route_cache_element_t) e;
1862   if (elm) {
1863     xbt_free(elm->pred_arr);
1864     xbt_free(elm);
1865   }
1866 }
1867
1868 static void graph_node_map_elem_free(void *e)
1869 {
1870   graph_node_map_element_t elm = (graph_node_map_element_t) e;
1871   if (elm) {
1872     xbt_free(elm);
1873   }
1874 }
1875
1876 static void graph_edge_data_free(void *e)
1877 {
1878   route_extended_t e_route = (route_extended_t) e;
1879   if (e_route) {
1880     xbt_dynar_free(&(e_route->generic_route.link_list));
1881     if (e_route->src_gateway)
1882       xbt_free(e_route->src_gateway);
1883     if (e_route->dst_gateway)
1884       xbt_free(e_route->dst_gateway);
1885     xbt_free(e_route);
1886   }
1887 }
1888
1889 /* Utility functions */
1890
1891 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc,
1892                                        int id, int graph_id)
1893 {
1894   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1895   xbt_node_t node = NULL;
1896   graph_node_data_t data = NULL;
1897   graph_node_map_element_t elm = NULL;
1898
1899   data = xbt_new0(struct graph_node_data, 1);
1900   data->id = id;
1901   data->graph_id = graph_id;
1902   node = xbt_graph_new_node(routing->route_graph, data);
1903
1904   elm = xbt_new0(struct graph_node_map_element, 1);
1905   elm->node = node;
1906   xbt_dict_set_ext(routing->graph_node_map, (char *) (&id), sizeof(int),
1907                    (xbt_set_elm_t) elm, &graph_node_map_elem_free);
1908
1909   return node;
1910 }
1911
1912 static graph_node_map_element_t
1913 graph_node_map_search(routing_component_dijkstra_t rc, int id)
1914 {
1915   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1916   graph_node_map_element_t elm = (graph_node_map_element_t)
1917       xbt_dict_get_or_null_ext(routing->graph_node_map,
1918                                (char *) (&id),
1919                                sizeof(int));
1920   return elm;
1921 }
1922
1923 /* Parsing */
1924
1925 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id,
1926                                int dst_id, route_extended_t e_route)
1927 {
1928   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1929   XBT_DEBUG("Load Route from \"%d\" to \"%d\"", src_id, dst_id);
1930   xbt_node_t src = NULL;
1931   xbt_node_t dst = NULL;
1932
1933   graph_node_map_element_t src_elm = (graph_node_map_element_t)
1934       xbt_dict_get_or_null_ext(routing->graph_node_map,
1935                                (char *) (&src_id),
1936                                sizeof(int));
1937   graph_node_map_element_t dst_elm = (graph_node_map_element_t)
1938       xbt_dict_get_or_null_ext(routing->graph_node_map,
1939                                (char *) (&dst_id),
1940                                sizeof(int));
1941
1942
1943   if (src_elm)
1944     src = src_elm->node;
1945
1946   if (dst_elm)
1947     dst = dst_elm->node;
1948
1949   /* add nodes if they don't exist in the graph */
1950   if (src_id == dst_id && src == NULL && dst == NULL) {
1951     src = route_graph_new_node(rc, src_id, -1);
1952     dst = src;
1953   } else {
1954     if (src == NULL) {
1955       src = route_graph_new_node(rc, src_id, -1);
1956     }
1957     if (dst == NULL) {
1958       dst = route_graph_new_node(rc, dst_id, -1);
1959     }
1960   }
1961
1962   /* add link as edge to graph */
1963   xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1964 }
1965
1966 static void add_loopback_dijkstra(routing_component_dijkstra_t rc)
1967 {
1968   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1969
1970   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1971
1972   xbt_node_t node = NULL;
1973   unsigned int cursor2;
1974   xbt_dynar_foreach(nodes, cursor2, node) {
1975     xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1976     xbt_edge_t edge = NULL;
1977     unsigned int cursor;
1978
1979     int found = 0;
1980     xbt_dynar_foreach(out_edges, cursor, edge) {
1981       xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1982       if (other_node == node) {
1983         found = 1;
1984         break;
1985       }
1986     }
1987
1988     if (!found) {
1989       route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
1990       e_route->src_gateway = NULL;
1991       e_route->dst_gateway = NULL;
1992       e_route->generic_route.link_list =
1993           xbt_dynar_new(global_routing->size_of_link, NULL);
1994       xbt_dynar_push(e_route->generic_route.link_list,
1995                      &global_routing->loopback);
1996       xbt_graph_new_edge(routing->route_graph, node, node, e_route);
1997     }
1998   }
1999 }
2000
2001 /* Business methods */
2002 static xbt_dynar_t dijkstra_get_onelink_routes(routing_component_t rc)
2003 {
2004   xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
2005 }
2006
2007 static route_extended_t dijkstra_get_route(routing_component_t rc,
2008                                            const char *src,
2009                                            const char *dst)
2010 {
2011   xbt_assert(rc && src
2012               && dst,
2013               "Invalid params for \"get_route\" function at AS \"%s\"",
2014               rc->name);
2015
2016   /* set utils vars */
2017   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
2018
2019   generic_src_dst_check(rc, src, dst);
2020   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
2021   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
2022   xbt_assert(src_id
2023               && dst_id,
2024               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
2025               src, dst);
2026
2027   /* create a result route */
2028   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
2029   new_e_route->generic_route.link_list =
2030       xbt_dynar_new(global_routing->size_of_link, NULL);
2031   new_e_route->src_gateway = NULL;
2032   new_e_route->dst_gateway = NULL;
2033
2034   int *pred_arr = NULL;
2035   int src_node_id = 0;
2036   int dst_node_id = 0;
2037   int *nodeid = NULL;
2038   int v;
2039   route_extended_t e_route;
2040   int size = 0;
2041   unsigned int cpt;
2042   void *link;
2043   xbt_dynar_t links = NULL;
2044   route_cache_element_t elm = NULL;
2045   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2046
2047   /* Use the graph_node id mapping set to quickly find the nodes */
2048   graph_node_map_element_t src_elm =
2049       graph_node_map_search(routing, *src_id);
2050   graph_node_map_element_t dst_elm =
2051       graph_node_map_search(routing, *dst_id);
2052   xbt_assert(src_elm != NULL
2053               && dst_elm != NULL, "src %d or dst %d does not exist",
2054               *src_id, *dst_id);
2055   src_node_id = ((graph_node_data_t)
2056                  xbt_graph_node_get_data(src_elm->node))->graph_id;
2057   dst_node_id = ((graph_node_data_t)
2058                  xbt_graph_node_get_data(dst_elm->node))->graph_id;
2059
2060   /* if the src and dst are the same *//* fixed, missing in the previous version */
2061   if (src_node_id == dst_node_id) {
2062
2063     xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
2064     xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
2065     xbt_edge_t edge =
2066         xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
2067
2068     xbt_assert(edge != NULL, "no route between host %d and %d", *src_id,
2069                 *dst_id);
2070
2071     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
2072
2073     links = e_route->generic_route.link_list;
2074     xbt_dynar_foreach(links, cpt, link) {
2075       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
2076     }
2077
2078     return new_e_route;
2079   }
2080
2081   if (routing->cached) {
2082     /*check if there is a cached predecessor list avail */
2083     elm = (route_cache_element_t)
2084         xbt_dict_get_or_null_ext(routing->route_cache, (char *) (&src_id),
2085                                  sizeof(int));
2086   }
2087
2088   if (elm) {                    /* cached mode and cache hit */
2089     pred_arr = elm->pred_arr;
2090   } else {                      /* not cached mode or cache miss */
2091     double *cost_arr = NULL;
2092     xbt_heap_t pqueue = NULL;
2093     int i = 0;
2094
2095     int nr_nodes = xbt_dynar_length(nodes);
2096     cost_arr = xbt_new0(double, nr_nodes);      /* link cost from src to other hosts */
2097     pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
2098     pqueue = xbt_heap_new(nr_nodes, xbt_free);
2099
2100     /* initialize */
2101     cost_arr[src_node_id] = 0.0;
2102
2103     for (i = 0; i < nr_nodes; i++) {
2104       if (i != src_node_id) {
2105         cost_arr[i] = DBL_MAX;
2106       }
2107
2108       pred_arr[i] = 0;
2109
2110       /* initialize priority queue */
2111       nodeid = xbt_new0(int, 1);
2112       *nodeid = i;
2113       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
2114
2115     }
2116
2117     /* apply dijkstra using the indexes from the graph's node array */
2118     while (xbt_heap_size(pqueue) > 0) {
2119       int *v_id = xbt_heap_pop(pqueue);
2120       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
2121       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
2122       xbt_edge_t edge = NULL;
2123       unsigned int cursor;
2124
2125       xbt_dynar_foreach(out_edges, cursor, edge) {
2126         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
2127         graph_node_data_t data = xbt_graph_node_get_data(u_node);
2128         int u_id = data->graph_id;
2129         route_extended_t tmp_e_route =
2130             (route_extended_t) xbt_graph_edge_get_data(edge);
2131         int cost_v_u = (tmp_e_route->generic_route.link_list)->used;    /* count of links, old model assume 1 */
2132
2133         if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
2134           pred_arr[u_id] = *v_id;
2135           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
2136           nodeid = xbt_new0(int, 1);
2137           *nodeid = u_id;
2138           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
2139         }
2140       }
2141
2142       /* free item popped from pqueue */
2143       xbt_free(v_id);
2144     }
2145
2146     xbt_free(cost_arr);
2147     xbt_heap_free(pqueue);
2148   }
2149
2150   /* compose route path with links */
2151   char *gw_src = NULL, *gw_dst =
2152       NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
2153
2154   for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
2155     xbt_node_t node_pred_v =
2156         xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
2157     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
2158     xbt_edge_t edge =
2159         xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
2160
2161     xbt_assert(edge != NULL, "no route between host %d and %d", *src_id,
2162                 *dst_id);
2163
2164     prev_gw_src = gw_src;
2165     prev_gw_dst = gw_dst;
2166
2167     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
2168     gw_src = e_route->src_gateway;
2169     gw_dst = e_route->dst_gateway;
2170
2171     if (v == dst_node_id)
2172       first_gw = gw_dst;
2173
2174     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
2175         && strcmp(gw_dst, prev_gw_src)) {
2176       xbt_dynar_t e_route_as_to_as =
2177           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
2178       xbt_assert(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
2179                   gw_dst, prev_gw_src);
2180       links = e_route_as_to_as;
2181       int pos = 0;
2182       xbt_dynar_foreach(links, cpt, link) {
2183         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
2184                             &link);
2185         pos++;
2186       }
2187     }
2188
2189     links = e_route->generic_route.link_list;
2190     xbt_dynar_foreach(links, cpt, link) {
2191       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
2192     }
2193     size++;
2194   }
2195
2196   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
2197     new_e_route->src_gateway = xbt_strdup(gw_src);
2198     new_e_route->dst_gateway = xbt_strdup(first_gw);
2199   }
2200
2201   if (routing->cached && elm == NULL) {
2202     /* add to predecessor list of the current src-host to cache */
2203     elm = xbt_new0(struct route_cache_element, 1);
2204     elm->pred_arr = pred_arr;
2205     elm->size = size;
2206     xbt_dict_set_ext(routing->route_cache, (char *) (&src_id), sizeof(int),
2207                      (xbt_set_elm_t) elm, &route_cache_elem_free);
2208   }
2209
2210   if (!routing->cached)
2211     xbt_free(pred_arr);
2212
2213   return new_e_route;
2214 }
2215
2216 static void dijkstra_finalize(routing_component_t rc)
2217 {
2218   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
2219
2220   if (routing) {
2221     xbt_graph_free_graph(routing->route_graph, &xbt_free,
2222                          &graph_edge_data_free, &xbt_free);
2223     xbt_dict_free(&routing->graph_node_map);
2224     if (routing->cached)
2225       xbt_dict_free(&routing->route_cache);
2226     /* Delete bypass dict */
2227     xbt_dict_free(&routing->generic_routing.bypassRoutes);
2228     /* Delete index dict */
2229     xbt_dict_free(&(routing->generic_routing.to_index));
2230     /* Delete structure */
2231     xbt_free(routing);
2232   }
2233 }
2234
2235 /* Creation routing model functions */
2236
2237 static void *model_dijkstra_both_create(int cached)
2238 {
2239   routing_component_dijkstra_t new_component =
2240       xbt_new0(s_routing_component_dijkstra_t, 1);
2241   new_component->generic_routing.set_processing_unit =
2242       generic_set_processing_unit;
2243   new_component->generic_routing.set_autonomous_system =
2244       generic_set_autonomous_system;
2245   new_component->generic_routing.set_route = model_dijkstra_both_set_route;
2246   new_component->generic_routing.set_ASroute = model_dijkstra_both_set_route;
2247   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
2248   new_component->generic_routing.get_route = dijkstra_get_route;
2249   new_component->generic_routing.get_latency = generic_get_link_latency;
2250   new_component->generic_routing.get_onelink_routes =
2251       dijkstra_get_onelink_routes;
2252   new_component->generic_routing.get_bypass_route =
2253       generic_get_bypassroute;
2254   new_component->generic_routing.finalize = dijkstra_finalize;
2255   new_component->cached = cached;
2256   new_component->generic_routing.to_index = xbt_dict_new();
2257   new_component->generic_routing.bypassRoutes = xbt_dict_new();
2258   new_component->generic_routing.get_network_element_type = get_network_element_type;
2259   return new_component;
2260 }
2261
2262 static void *model_dijkstra_create(void)
2263 {
2264   return model_dijkstra_both_create(0);
2265 }
2266
2267 static void *model_dijkstracache_create(void)
2268 {
2269   return model_dijkstra_both_create(1);
2270 }
2271
2272 static void model_dijkstra_both_load(void)
2273 {
2274   /* use "surfxml_add_callback" to add a parse function call */
2275 }
2276
2277 static void model_dijkstra_both_unload(void)
2278 {
2279   /* use "surfxml_del_callback" to remove a parse function call */
2280 }
2281
2282 static void model_dijkstra_both_end(void)
2283 {
2284   routing_component_dijkstra_t routing =
2285       (routing_component_dijkstra_t) current_routing;
2286
2287   xbt_node_t node = NULL;
2288   unsigned int cursor2;
2289   xbt_dynar_t nodes = NULL;
2290
2291   /* Create the topology graph */
2292   if(!routing->route_graph)
2293   routing->route_graph = xbt_graph_new_graph(1, NULL);
2294   if(!routing->graph_node_map)
2295   routing->graph_node_map = xbt_dict_new();
2296
2297   if (routing->cached && !routing->route_cache)
2298   routing->route_cache = xbt_dict_new();
2299
2300   /* Add the loopback if needed */
2301   if (current_routing->hierarchy == SURF_ROUTING_BASE)
2302     add_loopback_dijkstra(routing);
2303
2304   /* initialize graph indexes in nodes after graph has been built */
2305   nodes = xbt_graph_get_nodes(routing->route_graph);
2306
2307   xbt_dynar_foreach(nodes, cursor2, node) {
2308     graph_node_data_t data = xbt_graph_node_get_data(node);
2309     data->graph_id = cursor2;
2310   }
2311
2312 }
2313 static void model_dijkstra_both_set_route (routing_component_t rc, const char *src,
2314                      const char *dst, name_route_extended_t route)
2315 {
2316         routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
2317         int *src_id, *dst_id;
2318         src_id = xbt_dict_get_or_null(rc->to_index, src);
2319         dst_id = xbt_dict_get_or_null(rc->to_index, dst);
2320
2321     /* Create the topology graph */
2322         if(!routing->route_graph)
2323         routing->route_graph = xbt_graph_new_graph(1, NULL);
2324         if(!routing->graph_node_map)
2325         routing->graph_node_map = xbt_dict_new();
2326
2327         if (routing->cached && !routing->route_cache)
2328         routing->route_cache = xbt_dict_new();
2329
2330         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
2331                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
2332                 xbt_die("Route symmetrical not supported on model dijkstra");
2333
2334         if(!route->dst_gateway && !route->src_gateway)
2335           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
2336         else{
2337           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
2338                          route->src_gateway, dst, route->dst_gateway);
2339           if(global_routing->get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
2340                   xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
2341           if(global_routing->get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
2342                   xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
2343         }
2344
2345         route_extended_t e_route =
2346                 generic_new_extended_route(current_routing->hierarchy, route, 1);
2347         route_new_dijkstra(routing, *src_id, *dst_id, e_route);
2348 }
2349
2350 #ifdef HAVE_PCRE_LIB
2351 /* ************************************************** */
2352 /* ************** RULE-BASED ROUTING **************** */
2353
2354 /* Routing model structure */
2355
2356 typedef struct {
2357   s_routing_component_t generic_routing;
2358   xbt_dict_t dict_processing_units;
2359   xbt_dict_t dict_autonomous_systems;
2360   xbt_dynar_t list_route;
2361   xbt_dynar_t list_ASroute;
2362 } s_routing_component_rulebased_t, *routing_component_rulebased_t;
2363
2364 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
2365 typedef struct s_rule_route_extended s_rule_route_extended_t,
2366     *rule_route_extended_t;
2367
2368 struct s_rule_route {
2369   xbt_dynar_t re_str_link;      // dynar of char*
2370   pcre *re_src;
2371   pcre *re_dst;
2372 };
2373
2374 struct s_rule_route_extended {
2375   s_rule_route_t generic_rule_route;
2376   char *re_src_gateway;
2377   char *re_dst_gateway;
2378 };
2379
2380 static void rule_route_free(void *e)
2381 {
2382   rule_route_t *elem = (rule_route_t *) (e);
2383   if (*elem) {
2384     xbt_dynar_free(&(*elem)->re_str_link);
2385     pcre_free((*elem)->re_src);
2386     pcre_free((*elem)->re_dst);
2387     xbt_free(*elem);
2388   }
2389   *elem = NULL;
2390 }
2391
2392 static void rule_route_extended_free(void *e)
2393 {
2394   rule_route_extended_t *elem = (rule_route_extended_t *) e;
2395   if (*elem) {
2396     xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
2397     pcre_free((*elem)->generic_rule_route.re_src);
2398     pcre_free((*elem)->generic_rule_route.re_dst);
2399     xbt_free((*elem)->re_src_gateway);
2400     xbt_free((*elem)->re_dst_gateway);
2401     xbt_free(*elem);
2402   }
2403 }
2404
2405 /* Parse routing model functions */
2406
2407 static void model_rulebased_set_processing_unit(routing_component_t rc,
2408                                                 const char *name)
2409 {
2410   routing_component_rulebased_t routing =
2411       (routing_component_rulebased_t) rc;
2412   xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
2413 }
2414
2415 static void model_rulebased_set_autonomous_system(routing_component_t rc,
2416                                                   const char *name)
2417 {
2418   routing_component_rulebased_t routing =
2419       (routing_component_rulebased_t) rc;
2420   xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
2421                NULL);
2422 }
2423
2424 static void model_rulebased_set_route(routing_component_t rc,
2425                                       const char *src, const char *dst,
2426                                       name_route_extended_t route)
2427 {
2428   routing_component_rulebased_t routing =
2429       (routing_component_rulebased_t) rc;
2430   rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
2431   const char *error;
2432   int erroffset;
2433
2434   if(!strcmp(rc->routing->name,"Vivaldi")){
2435           if(xbt_dynar_length(route->generic_route.link_list) != 0)
2436                   xbt_die("You can't have link_ctn with Model Vivaldi.");
2437   }
2438
2439   ruleroute->re_src = pcre_compile(src, 0, &error, &erroffset, NULL);
2440   xbt_assert(ruleroute->re_src,
2441               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2442               erroffset, src, error);
2443   ruleroute->re_dst = pcre_compile(dst, 0, &error, &erroffset, NULL);
2444   xbt_assert(ruleroute->re_src,
2445               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2446               erroffset, dst, error);
2447   ruleroute->re_str_link = route->generic_route.link_list;
2448   xbt_dynar_push(routing->list_route, &ruleroute);
2449   xbt_free(route);
2450 }
2451
2452 static void model_rulebased_set_ASroute(routing_component_t rc,
2453                                         const char *src, const char *dst,
2454                                         name_route_extended_t route)
2455 {
2456   routing_component_rulebased_t routing =
2457       (routing_component_rulebased_t) rc;
2458   rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
2459   const char *error;
2460   int erroffset;
2461
2462   if(!strcmp(rc->routing->name,"Vivaldi")){
2463           if(xbt_dynar_length(route->generic_route.link_list) != 0)
2464                   xbt_die("You can't have link_ctn with Model Vivaldi.");
2465   }
2466
2467   ruleroute_e->generic_rule_route.re_src =
2468       pcre_compile(src, 0, &error, &erroffset, NULL);
2469   xbt_assert(ruleroute_e->generic_rule_route.re_src,
2470               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2471               erroffset, src, error);
2472   ruleroute_e->generic_rule_route.re_dst =
2473       pcre_compile(dst, 0, &error, &erroffset, NULL);
2474   xbt_assert(ruleroute_e->generic_rule_route.re_src,
2475               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2476               erroffset, dst, error);
2477   ruleroute_e->generic_rule_route.re_str_link =
2478       route->generic_route.link_list;
2479   ruleroute_e->re_src_gateway = route->src_gateway;
2480   ruleroute_e->re_dst_gateway = route->dst_gateway;
2481   xbt_dynar_push(routing->list_ASroute, &ruleroute_e);
2482 //  xbt_free(route->src_gateway);
2483 //  xbt_free(route->dst_gateway);
2484   xbt_free(route);
2485 }
2486
2487 static void model_rulebased_set_bypassroute(routing_component_t rc,
2488                                             const char *src,
2489                                             const char *dst,
2490                                             route_extended_t e_route)
2491 {
2492   xbt_die("bypass routing not supported for Route-Based model");
2493 }
2494
2495 #define BUFFER_SIZE 4096        /* result buffer size */
2496 #define OVECCOUNT 30            /* should be a multiple of 3 */
2497
2498 static char *remplace(char *value, const char **src_list, int src_size,
2499                       const char **dst_list, int dst_size)
2500 {
2501
2502   char result_result[BUFFER_SIZE];
2503   int i_result_buffer;
2504   int value_length = (int) strlen(value);
2505   int number = 0;
2506
2507   int i = 0;
2508   i_result_buffer = 0;
2509   do {
2510     if (value[i] == '$') {
2511       i++;                      // skip $
2512
2513       // find the number      
2514       int number_length = 0;
2515       while ('0' <= value[i + number_length]
2516              && value[i + number_length] <= '9') {
2517         number_length++;
2518       }
2519       xbt_assert(number_length != 0,
2520                   "bad string parameter, no number indication, at offset: %d (\"%s\")",
2521                   i, value);
2522
2523       // solve number
2524       number = atoi(value + i);
2525       i = i + number_length;
2526       xbt_assert(i + 2 < value_length,
2527                   "bad string parameter, too few chars, at offset: %d (\"%s\")",
2528                   i, value);
2529
2530       // solve the indication
2531       const char **param_list;
2532       int param_size;
2533       if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
2534         param_list = src_list;
2535         param_size = src_size;
2536       } else if (value[i] == 'd' && value[i + 1] == 's'
2537                  && value[i + 2] == 't') {
2538         param_list = dst_list;
2539         param_size = dst_size;
2540       } else {
2541         xbt_die("bad string parameter, support only \"src\" and \"dst\", "
2542                 "at offset: %d (\"%s\")", i, value);
2543       }
2544       i = i + 3;
2545
2546       xbt_assert(param_size >= number,
2547                   "bad string parameter, not enough length param_size, at offset: %d (\"%s\") %d %d",
2548                   i, value, param_size, number);
2549
2550       const char *param = param_list[number];
2551       int size = strlen(param);
2552       int cp;
2553       for (cp = 0; cp < size; cp++) {
2554         result_result[i_result_buffer] = param[cp];
2555         i_result_buffer++;
2556         if (i_result_buffer >= BUFFER_SIZE)
2557           break;
2558       }
2559     } else {
2560       result_result[i_result_buffer] = value[i];
2561       i_result_buffer++;
2562       i++;                      // next char
2563     }
2564
2565   } while (i < value_length && i_result_buffer < BUFFER_SIZE);
2566
2567   xbt_assert(i_result_buffer < BUFFER_SIZE,
2568               "solving string \"%s\", small buffer size (%d)", value,
2569               BUFFER_SIZE);
2570   result_result[i_result_buffer] = 0;
2571   return xbt_strdup(result_result);
2572 }
2573
2574 static route_extended_t rulebased_get_route(routing_component_t rc,
2575                                             const char *src,
2576                                             const char *dst);
2577 static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
2578 {
2579   xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
2580   routing_component_rulebased_t routing = (routing_component_rulebased_t)rc;
2581
2582   xbt_dict_cursor_t c1 = NULL;
2583   char *k1, *d1;
2584
2585   //find router
2586   char *router = NULL;
2587   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2588     if (strstr (k1, "router")){
2589       router = k1;
2590     }
2591   }
2592   if (!router){
2593     xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2594   }
2595
2596   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2597     route_extended_t route = rulebased_get_route (rc, router, k1);
2598
2599     int number_of_links = xbt_dynar_length(route->generic_route.link_list);
2600     if (number_of_links != 3) {
2601       xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2602     }
2603
2604     void *link_ptr;
2605     xbt_dynar_get_cpy (route->generic_route.link_list, 2, &link_ptr);
2606     onelink_t onelink = xbt_new0 (s_onelink_t, 1);
2607     onelink->src = xbt_strdup (k1);
2608     onelink->dst = xbt_strdup (router);
2609     onelink->link_ptr = link_ptr;
2610     xbt_dynar_push (ret, &onelink);
2611   }
2612   return ret;
2613 }
2614
2615 /* Business methods */
2616 static route_extended_t rulebased_get_route(routing_component_t rc,
2617                                             const char *src,
2618                                             const char *dst)
2619 {
2620   xbt_assert(rc && src
2621               && dst,
2622               "Invalid params for \"get_route\" function at AS \"%s\"",
2623               rc->name);
2624
2625   /* set utils vars */
2626   routing_component_rulebased_t routing =
2627       (routing_component_rulebased_t) rc;
2628
2629   int are_processing_units=0;
2630   xbt_dynar_t rule_list;
2631   if (xbt_dict_get_or_null(routing->dict_processing_units, src)
2632       && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
2633     are_processing_units = 1;
2634     rule_list = routing->list_route;
2635   } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
2636              && xbt_dict_get_or_null(routing->dict_autonomous_systems,
2637                                      dst)) {
2638     are_processing_units = 0;
2639     rule_list = routing->list_ASroute;
2640   } else
2641     xbt_die("Ask for route \"from\"(%s)  or \"to\"(%s) no found in "
2642             "the local table", src, dst);
2643
2644   int rc_src = -1;
2645   int rc_dst = -1;
2646   int src_length = (int) strlen(src);
2647   int dst_length = (int) strlen(dst);
2648
2649   xbt_dynar_t links_list =
2650       xbt_dynar_new(global_routing->size_of_link, NULL);
2651
2652   rule_route_t ruleroute;
2653   unsigned int cpt;
2654   int ovector_src[OVECCOUNT];
2655   int ovector_dst[OVECCOUNT];
2656   const char **list_src = NULL;
2657   const char **list_dst = NULL;
2658   int res;
2659   xbt_dynar_foreach(rule_list, cpt, ruleroute) {
2660     rc_src =
2661         pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
2662                   ovector_src, OVECCOUNT);
2663     if (rc_src >= 0) {
2664       rc_dst =
2665           pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
2666                     ovector_dst, OVECCOUNT);
2667       if (rc_dst >= 0) {
2668         res = pcre_get_substring_list(src, ovector_src, rc_src, &list_src);
2669         xbt_assert(!res, "error solving substring list for src \"%s\"", src);
2670         res = pcre_get_substring_list(dst, ovector_dst, rc_dst, &list_dst);
2671         xbt_assert(!res, "error solving substring list for src \"%s\"", dst);
2672         char *link_name;
2673         xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
2674           char *new_link_name =
2675               remplace(link_name, list_src, rc_src, list_dst, rc_dst);
2676           void *link =
2677                           xbt_lib_get_or_null(link_lib, new_link_name, SURF_LINK_LEVEL);
2678           if (link)
2679             xbt_dynar_push(links_list, &link);
2680           else
2681             THROWF(mismatch_error, 0, "Link %s not found", new_link_name);
2682           xbt_free(new_link_name);
2683         }
2684       }
2685     }
2686     if (rc_src >= 0 && rc_dst >= 0)
2687       break;
2688   }
2689
2690   route_extended_t new_e_route = NULL;
2691   if (rc_src >= 0 && rc_dst >= 0) {
2692     new_e_route = xbt_new0(s_route_extended_t, 1);
2693     new_e_route->generic_route.link_list = links_list;
2694   } else if (!strcmp(src, dst) && are_processing_units) {
2695     new_e_route = xbt_new0(s_route_extended_t, 1);
2696     xbt_dynar_push(links_list, &(global_routing->loopback));
2697     new_e_route->generic_route.link_list = links_list;
2698   } else {
2699     xbt_dynar_free(&link_list);
2700   }
2701
2702   if (!are_processing_units && new_e_route) {
2703     rule_route_extended_t ruleroute_extended =
2704         (rule_route_extended_t) ruleroute;
2705     new_e_route->src_gateway =
2706         remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
2707                  list_dst, rc_dst);
2708     new_e_route->dst_gateway =
2709         remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
2710                  list_dst, rc_dst);
2711   }
2712
2713   if (list_src)
2714     pcre_free_substring_list(list_src);
2715   if (list_dst)
2716     pcre_free_substring_list(list_dst);
2717
2718   return new_e_route;
2719 }
2720
2721 static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
2722                                                    const char *src,
2723                                                    const char *dst)
2724 {
2725   return NULL;
2726 }
2727
2728 static void rulebased_finalize(routing_component_t rc)
2729 {
2730   routing_component_rulebased_t routing =
2731       (routing_component_rulebased_t) rc;
2732   if (routing) {
2733     xbt_dict_free(&routing->dict_processing_units);
2734     xbt_dict_free(&routing->dict_autonomous_systems);
2735     xbt_dynar_free(&routing->list_route);
2736     xbt_dynar_free(&routing->list_ASroute);
2737     /* Delete structure */
2738     xbt_free(routing);
2739   }
2740 }
2741
2742 /* Creation routing model functions */
2743 static void *model_rulebased_create(void)
2744 {
2745   routing_component_rulebased_t new_component =
2746       xbt_new0(s_routing_component_rulebased_t, 1);
2747   new_component->generic_routing.set_processing_unit =
2748       model_rulebased_set_processing_unit;
2749   new_component->generic_routing.set_autonomous_system =
2750       model_rulebased_set_autonomous_system;
2751   new_component->generic_routing.set_route = model_rulebased_set_route;
2752   new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
2753   new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
2754   new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
2755   new_component->generic_routing.get_route = rulebased_get_route;
2756   new_component->generic_routing.get_latency = generic_get_link_latency;
2757   new_component->generic_routing.get_bypass_route = rulebased_get_bypass_route;
2758   new_component->generic_routing.finalize = rulebased_finalize;
2759   new_component->generic_routing.get_network_element_type = get_network_element_type;
2760   /* initialization of internal structures */
2761   new_component->dict_processing_units = xbt_dict_new();
2762   new_component->dict_autonomous_systems = xbt_dict_new();
2763   new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
2764   new_component->list_ASroute =
2765       xbt_dynar_new(sizeof(rule_route_extended_t),
2766                     &rule_route_extended_free);
2767   return new_component;
2768 }
2769
2770 static void model_rulebased_load(void)
2771 {
2772   /* use "surfxml_add_callback" to add a parse function call */
2773 }
2774
2775 static void model_rulebased_unload(void)
2776 {
2777   /* use "surfxml_del_callback" to remove a parse function call */
2778 }
2779
2780 static void model_rulebased_end(void)
2781 {
2782 }
2783
2784 #endif                          /* HAVE_PCRE_LIB */
2785
2786 /* ************************************************************************** */
2787 /* ******************************* NO ROUTING ******************************* */
2788
2789 /* Routing model structure */
2790 typedef struct {
2791   s_routing_component_t generic_routing;
2792 } s_routing_component_none_t, *routing_component_none_t;
2793
2794 /* Business methods */
2795 static xbt_dynar_t none_get_onelink_routes(routing_component_t rc)
2796 {
2797   return NULL;
2798 }
2799
2800 static route_extended_t none_get_route(routing_component_t rc,
2801                                        const char *src, const char *dst)
2802 {
2803   return NULL;
2804 }
2805
2806 static route_extended_t none_get_bypass_route(routing_component_t rc,
2807                                               const char *src,
2808                                               const char *dst)
2809 {
2810   return NULL;
2811 }
2812
2813 static void none_finalize(routing_component_t rc)
2814 {
2815   xbt_free(rc);
2816 }
2817
2818 static void none_set_processing_unit(routing_component_t rc,
2819                                      const char *name)
2820 {
2821 }
2822
2823 static void none_set_autonomous_system(routing_component_t rc,
2824                                        const char *name)
2825 {
2826 }
2827
2828 /* Creation routing model functions */
2829 static void *model_none_create(void)
2830 {
2831   routing_component_none_t new_component =
2832       xbt_new0(s_routing_component_none_t, 1);
2833   new_component->generic_routing.set_processing_unit =
2834       none_set_processing_unit;
2835   new_component->generic_routing.set_autonomous_system =
2836       none_set_autonomous_system;
2837   new_component->generic_routing.set_route = NULL;
2838   new_component->generic_routing.set_ASroute = NULL;
2839   new_component->generic_routing.set_bypassroute = NULL;
2840   new_component->generic_routing.get_route = none_get_route;
2841   new_component->generic_routing.get_onelink_routes =
2842       none_get_onelink_routes;
2843   new_component->generic_routing.get_bypass_route = none_get_bypass_route;
2844   new_component->generic_routing.finalize = none_finalize;
2845   return new_component;
2846 }
2847
2848 static void model_none_load(void)
2849 {
2850 }
2851
2852 static void model_none_unload(void)
2853 {
2854 }
2855
2856 static void model_none_end(void)
2857 {
2858 }
2859
2860 /* ************************************************** */
2861 /* ********** PATERN FOR NEW ROUTING **************** */
2862
2863 /* The minimal configuration of a new routing model need the next functions,
2864  * also you need to set at the start of the file, the new model in the model
2865  * list. Remember keep the null ending of the list.
2866  */
2867 /*** Routing model structure ***/
2868 // typedef struct {
2869 //   s_routing_component_t generic_routing;
2870 //   /* things that your routing model need */
2871 // } s_routing_component_NEW_t,*routing_component_NEW_t;
2872
2873 /*** Parse routing model functions ***/
2874 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
2875 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
2876 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
2877 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
2878 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
2879
2880 /*** Business methods ***/
2881 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2882 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2883 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
2884
2885 /*** Creation routing model functions ***/
2886 // static void* model_NEW_create(void) {
2887 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
2888 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
2889 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
2890 //   new_component->generic_routing.set_route = model_NEW_set_route;
2891 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
2892 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
2893 //   new_component->generic_routing.get_route = NEW_get_route;
2894 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
2895 //   new_component->generic_routing.finalize = NEW_finalize;
2896 //   /* initialization of internal structures */
2897 //   return new_component;
2898 // } /* mandatory */
2899 // static void  model_NEW_load(void) {}   /* mandatory */
2900 // static void  model_NEW_unload(void) {} /* mandatory */
2901 // static void  model_NEW_end(void) {}    /* mandatory */
2902
2903 /* ************************************************************************** */
2904 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
2905
2906 static void generic_set_processing_unit(routing_component_t rc,
2907                                         const char *name)
2908 {
2909   XBT_DEBUG("Load process unit \"%s\"", name);
2910   int *id = xbt_new0(int, 1);
2911   xbt_dict_t _to_index;
2912   _to_index = current_routing->to_index;
2913   *id = xbt_dict_length(_to_index);
2914   xbt_dict_set(_to_index, name, id, xbt_free);
2915 }
2916
2917 static void generic_set_autonomous_system(routing_component_t rc,
2918                                           const char *name)
2919 {
2920   XBT_DEBUG("Load Autonomous system \"%s\"", name);
2921   int *id = xbt_new0(int, 1);
2922   xbt_dict_t _to_index;
2923   _to_index = current_routing->to_index;
2924   *id = xbt_dict_length(_to_index);
2925   xbt_dict_set(_to_index, name, id, xbt_free);
2926 }
2927
2928 static int surf_pointer_resource_cmp(const void *a, const void *b)
2929 {
2930   return a != b;
2931 }
2932
2933 static int surf_link_resource_cmp(const void *a, const void *b)
2934 {
2935   return !!memcmp(a,b,global_routing->size_of_link);
2936 }
2937
2938 static void generic_set_bypassroute(routing_component_t rc,
2939                                     const char *src, const char *dst,
2940                                     route_extended_t e_route)
2941 {
2942   XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
2943   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2944   char *route_name;
2945
2946   route_name = bprintf("%s#%s", src, dst);
2947   xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2948               "Invalid count of links, must be greater than zero (%s,%s)",
2949               src, dst);
2950   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
2951               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
2952               src, e_route->src_gateway, dst, e_route->dst_gateway);
2953
2954   route_extended_t new_e_route =
2955       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
2956   xbt_dynar_free(&(e_route->generic_route.link_list));
2957   xbt_free(e_route);
2958
2959   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
2960                (void (*)(void *)) generic_free_extended_route);
2961   xbt_free(route_name);
2962 }
2963
2964 /* ************************************************************************** */
2965 /* *********************** GENERIC BUSINESS METHODS ************************* */
2966
2967 static double generic_get_link_latency(routing_component_t rc,
2968                                        const char *src, const char *dst)
2969 {
2970         route_extended_t route = rc->get_route(rc,src,dst);
2971         void * link;
2972         unsigned int i;
2973         double latency = 0.0;
2974
2975         xbt_dynar_foreach(route->generic_route.link_list,i,link) {
2976                 latency += get_link_latency(link);
2977         }
2978         generic_free_extended_route(route);
2979   return latency;
2980 }
2981
2982 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
2983 {
2984   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2985 }
2986
2987 static route_extended_t generic_get_bypassroute(routing_component_t rc,
2988                                                 const char *src,
2989                                                 const char *dst)
2990 {
2991   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2992   routing_component_t src_as, dst_as;
2993   int index_src, index_dst;
2994   xbt_dynar_t path_src = NULL;
2995   xbt_dynar_t path_dst = NULL;
2996   routing_component_t current = NULL;
2997   routing_component_t *current_src = NULL;
2998   routing_component_t *current_dst = NULL;
2999
3000   /* (1) find the as where the src and dst are located */
3001   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
3002   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
3003   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
3004   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
3005
3006   if(src_data == NULL || dst_data == NULL)
3007           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
3008                      src, dst, rc->name);
3009
3010   src_as = ((network_element_info_t)src_data)->rc_component;
3011   dst_as = ((network_element_info_t)dst_data)->rc_component;
3012
3013   /* (2) find the path to the root routing component */
3014   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
3015   current = src_as;
3016   while (current != NULL) {
3017     xbt_dynar_push(path_src, &current);
3018     current = current->routing_father;
3019   }
3020   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
3021   current = dst_as;
3022   while (current != NULL) {
3023     xbt_dynar_push(path_dst, &current);
3024     current = current->routing_father;
3025   }
3026
3027   /* (3) find the common father */
3028   index_src = path_src->used - 1;
3029   index_dst = path_dst->used - 1;
3030   current_src = xbt_dynar_get_ptr(path_src, index_src);
3031   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
3032   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
3033     routing_component_t *tmp_src, *tmp_dst;
3034     tmp_src = xbt_dynar_pop_ptr(path_src);
3035     tmp_dst = xbt_dynar_pop_ptr(path_dst);
3036     index_src--;
3037     index_dst--;
3038     current_src = xbt_dynar_get_ptr(path_src, index_src);
3039     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
3040   }
3041
3042   int max_index_src = path_src->used - 1;
3043   int max_index_dst = path_dst->used - 1;
3044
3045   int max_index = max(max_index_src, max_index_dst);
3046   int i, max;
3047
3048   route_extended_t e_route_bypass = NULL;
3049
3050   for (max = 0; max <= max_index; max++) {
3051     for (i = 0; i < max; i++) {
3052       if (i <= max_index_src && max <= max_index_dst) {
3053         char *route_name = bprintf("%s#%s",
3054                                    (*(routing_component_t *)
3055                                     (xbt_dynar_get_ptr
3056                                      (path_src, i)))->name,
3057                                    (*(routing_component_t *)
3058                                     (xbt_dynar_get_ptr
3059                                      (path_dst, max)))->name);
3060         e_route_bypass =
3061             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
3062         xbt_free(route_name);
3063       }
3064       if (e_route_bypass)
3065         break;
3066       if (max <= max_index_src && i <= max_index_dst) {
3067         char *route_name = bprintf("%s#%s",
3068                                    (*(routing_component_t *)
3069                                     (xbt_dynar_get_ptr
3070                                      (path_src, max)))->name,
3071                                    (*(routing_component_t *)
3072                                     (xbt_dynar_get_ptr
3073                                      (path_dst, i)))->name);
3074         e_route_bypass =
3075             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
3076         xbt_free(route_name);
3077       }
3078       if (e_route_bypass)
3079         break;
3080     }
3081
3082     if (e_route_bypass)
3083       break;
3084
3085     if (max <= max_index_src && max <= max_index_dst) {
3086       char *route_name = bprintf("%s#%s",
3087                                  (*(routing_component_t *)
3088                                   (xbt_dynar_get_ptr
3089                                    (path_src, max)))->name,
3090                                  (*(routing_component_t *)
3091                                   (xbt_dynar_get_ptr
3092                                    (path_dst, max)))->name);
3093       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
3094       xbt_free(route_name);
3095     }
3096     if (e_route_bypass)
3097       break;
3098   }
3099
3100   xbt_dynar_free(&path_src);
3101   xbt_dynar_free(&path_dst);
3102
3103   route_extended_t new_e_route = NULL;
3104
3105   if (e_route_bypass) {
3106     void *link;
3107     unsigned int cpt = 0;
3108     new_e_route = xbt_new0(s_route_extended_t, 1);
3109     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
3110     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
3111     new_e_route->generic_route.link_list =
3112         xbt_dynar_new(global_routing->size_of_link, NULL);
3113     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
3114       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
3115     }
3116   }
3117
3118   return new_e_route;
3119 }
3120
3121 /* ************************************************************************** */
3122 /* ************************* GENERIC AUX FUNCTIONS ************************** */
3123
3124 static route_t
3125 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
3126                            void *data, int order)
3127 {
3128
3129   char *link_name;
3130   route_t new_route;
3131   unsigned int cpt;
3132   xbt_dynar_t links = NULL, links_id = NULL;
3133
3134   new_route = xbt_new0(s_route_t, 1);
3135   new_route->link_list =
3136       xbt_dynar_new(global_routing->size_of_link, NULL);
3137
3138   xbt_assert(hierarchy == SURF_ROUTING_BASE,
3139               "the hierarchy type is not SURF_ROUTING_BASE");
3140
3141   links = ((route_t) data)->link_list;
3142
3143
3144   links_id = new_route->link_list;
3145
3146   xbt_dynar_foreach(links, cpt, link_name) {
3147
3148     void *link =
3149                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
3150     if (link) {
3151       if (order)
3152         xbt_dynar_push(links_id, &link);
3153       else
3154         xbt_dynar_unshift(links_id, &link);
3155     } else
3156       THROWF(mismatch_error, 0, "Link %s not found", link_name);
3157   }
3158
3159   return new_route;
3160 }
3161
3162 static route_extended_t
3163 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
3164                            void *data, int order)
3165 {
3166
3167   char *link_name;
3168   route_extended_t e_route, new_e_route;
3169   route_t route;
3170   unsigned int cpt;
3171   xbt_dynar_t links = NULL, links_id = NULL;
3172
3173   new_e_route = xbt_new0(s_route_extended_t, 1);
3174   new_e_route->generic_route.link_list =
3175       xbt_dynar_new(global_routing->size_of_link, NULL);
3176   new_e_route->src_gateway = NULL;
3177   new_e_route->dst_gateway = NULL;
3178
3179   xbt_assert(hierarchy == SURF_ROUTING_BASE
3180               || hierarchy == SURF_ROUTING_RECURSIVE,
3181               "the hierarchy type is not defined");
3182
3183   if (hierarchy == SURF_ROUTING_BASE) {
3184
3185     route = (route_t) data;
3186     links = route->link_list;
3187
3188   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
3189
3190     e_route = (route_extended_t) data;
3191     xbt_assert(e_route->src_gateway
3192                 && e_route->dst_gateway, "bad gateway, is null");
3193     links = e_route->generic_route.link_list;
3194
3195     /* remeber not erase the gateway names */
3196     new_e_route->src_gateway = strdup(e_route->src_gateway);
3197     new_e_route->dst_gateway = strdup(e_route->dst_gateway);
3198   }
3199
3200   links_id = new_e_route->generic_route.link_list;
3201
3202   xbt_dynar_foreach(links, cpt, link_name) {
3203
3204     void *link =
3205                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
3206     if (link) {
3207       if (order)
3208         xbt_dynar_push(links_id, &link);
3209       else
3210         xbt_dynar_unshift(links_id, &link);
3211     } else
3212       THROWF(mismatch_error, 0, "Link %s not found", link_name);
3213   }
3214
3215   return new_e_route;
3216 }
3217
3218 static void generic_free_route(route_t route)
3219 {
3220   if (route) {
3221     xbt_dynar_free(&(route->link_list));
3222     xbt_free(route);
3223   }
3224 }
3225
3226 static void generic_free_extended_route(route_extended_t e_route)
3227 {
3228   if (e_route) {
3229     xbt_dynar_free(&(e_route->generic_route.link_list));
3230     if (e_route->src_gateway)
3231       xbt_free(e_route->src_gateway);
3232     if (e_route->dst_gateway)
3233       xbt_free(e_route->dst_gateway);
3234     xbt_free(e_route);
3235   }
3236 }
3237
3238 static routing_component_t generic_as_exist(routing_component_t find_from,
3239                                             routing_component_t to_find)
3240 {
3241   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
3242   xbt_dict_cursor_t cursor = NULL;
3243   char *key;
3244   int found = 0;
3245   routing_component_t elem;
3246   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
3247     if (to_find == elem || generic_as_exist(elem, to_find)) {
3248       found = 1;
3249       break;
3250     }
3251   }
3252   if (found)
3253     return to_find;
3254   return NULL;
3255 }
3256
3257 static routing_component_t
3258 generic_autonomous_system_exist(routing_component_t rc, char *element)
3259 {
3260   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
3261   routing_component_t element_as, result, elem;
3262   xbt_dict_cursor_t cursor = NULL;
3263   char *key;
3264   element_as = ((network_element_info_t)
3265                 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
3266   result = ((routing_component_t) - 1);
3267   if (element_as != rc)
3268     result = generic_as_exist(rc, element_as);
3269
3270   int found = 0;
3271   if (result) {
3272     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
3273       found = !strcmp(elem->name, element);
3274       if (found)
3275         break;
3276     }
3277     if (found)
3278       return element_as;
3279   }
3280   return NULL;
3281 }
3282
3283 static routing_component_t
3284 generic_processing_units_exist(routing_component_t rc, char *element)
3285 {
3286   routing_component_t element_as;
3287   element_as = ((network_element_info_t)
3288                 xbt_lib_get_or_null(host_lib,
3289                  element, ROUTING_HOST_LEVEL))->rc_component;
3290   if (element_as == rc)
3291     return element_as;
3292   return generic_as_exist(rc, element_as);
3293 }
3294
3295 static void generic_src_dst_check(routing_component_t rc, const char *src,
3296                                   const char *dst)
3297 {
3298
3299   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
3300   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
3301   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
3302   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
3303
3304   if(src_data == NULL || dst_data == NULL)
3305           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
3306                      src, dst, rc->name);
3307
3308   routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
3309   routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
3310
3311   if(src_as != dst_as)
3312           xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
3313               src, src_as->name, dst, dst_as->name);
3314   if(rc != dst_as)
3315          xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
3316      src,dst,src_as->name, dst_as->name,rc->name);
3317 }
3318
3319 static void routing_parse_Sconfig(void)
3320 {
3321   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
3322 }
3323
3324 static void routing_parse_Econfig(void)
3325 {
3326   xbt_dict_cursor_t cursor = NULL;
3327   char *key;
3328   char *elem;
3329   char *cfg;
3330   xbt_dict_foreach(current_property_set, cursor, key, elem) {
3331           cfg = bprintf("%s:%s",key,elem);
3332           if(xbt_cfg_is_default_value(_surf_cfg_set, key))
3333                   xbt_cfg_set_parse(_surf_cfg_set, cfg);
3334           else
3335                   XBT_INFO("The custom configuration '%s' is already define by user!",key);
3336           free(cfg);
3337   }
3338   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
3339 }
3340
3341 static void routing_parse_Scluster(void)
3342 {
3343   static int AX_ptr = 0;
3344
3345   char *cluster_id = A_surfxml_cluster_id;
3346   char *cluster_prefix = A_surfxml_cluster_prefix;
3347   char *cluster_suffix = A_surfxml_cluster_suffix;
3348   char *cluster_radical = A_surfxml_cluster_radical;
3349   char *cluster_power = A_surfxml_cluster_power;
3350   char *cluster_core = A_surfxml_cluster_core;
3351   char *cluster_bw = A_surfxml_cluster_bw;
3352   char *cluster_lat = A_surfxml_cluster_lat;
3353   char *temp_cluster_bw = NULL;
3354   char *temp_cluster_lat = NULL;
3355   char *temp_cluster_power = NULL;
3356   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
3357   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
3358   char *cluster_availability_file = A_surfxml_cluster_availability_file;
3359   char *cluster_state_file = A_surfxml_cluster_state_file;
3360   char *host_id, *groups, *link_id = NULL;
3361   char *router_id, *link_router, *link_backbone;
3362   char *availability_file = xbt_strdup(cluster_availability_file);
3363   char *state_file = xbt_strdup(cluster_state_file);
3364
3365   if(xbt_dict_size(patterns)==0)
3366           patterns = xbt_dict_new();
3367
3368   xbt_dict_set(patterns,"id",cluster_id,NULL);
3369   xbt_dict_set(patterns,"prefix",cluster_prefix,NULL);
3370   xbt_dict_set(patterns,"suffix",cluster_suffix,NULL);
3371
3372 #ifdef HAVE_PCRE_LIB
3373   char *route_src_dst;
3374 #endif
3375   unsigned int iter;
3376   int start, end, i;
3377   xbt_dynar_t radical_elements;
3378   xbt_dynar_t radical_ends;
3379   int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
3380   int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
3381
3382 #ifndef HAVE_PCRE_LIB
3383   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
3384   char *route_src, *route_dst;
3385   int j;
3386 #endif
3387
3388   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
3389   static unsigned int surfxml_buffer_stack_stack[1024];
3390
3391   surfxml_buffer_stack_stack[0] = 0;
3392
3393   surfxml_bufferstack_push(1);
3394
3395   SURFXML_BUFFER_SET(AS_id, cluster_id);
3396 #ifdef HAVE_PCRE_LIB
3397   SURFXML_BUFFER_SET(AS_routing, "RuleBased");
3398   XBT_DEBUG("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
3399 #else
3400   SURFXML_BUFFER_SET(AS_routing, "Full");
3401   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
3402 #endif
3403   SURFXML_START_TAG(AS);
3404
3405   radical_elements = xbt_str_split(cluster_radical, ",");
3406   xbt_dynar_foreach(radical_elements, iter, groups) {
3407     radical_ends = xbt_str_split(groups, "-");
3408     switch (xbt_dynar_length(radical_ends)) {
3409     case 1:
3410       surf_parse_get_int(&start,
3411                          xbt_dynar_get_as(radical_ends, 0, char *));
3412       host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
3413 #ifndef HAVE_PCRE_LIB
3414       xbt_dynar_push_as(tab_elements_num, int, start);
3415 #endif
3416       link_id = bprintf("%s_link_%d", cluster_id, start);
3417
3418       xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
3419       temp_cluster_power = xbt_strdup(cluster_power);
3420       temp_cluster_power = replace_random_parameter(temp_cluster_power);
3421       XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, temp_cluster_power);
3422       A_surfxml_host_state = A_surfxml_host_state_ON;
3423       SURFXML_BUFFER_SET(host_id, host_id);
3424       SURFXML_BUFFER_SET(host_power, temp_cluster_power);
3425       SURFXML_BUFFER_SET(host_core, cluster_core);
3426       SURFXML_BUFFER_SET(host_availability, "1.0");
3427       SURFXML_BUFFER_SET(host_coordinates, "");
3428       xbt_free(availability_file);
3429       availability_file = xbt_strdup(cluster_availability_file);
3430       xbt_free(state_file);
3431       state_file = xbt_strdup(cluster_state_file);
3432       XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(availability_file,patterns));
3433       XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(state_file,patterns));
3434       SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(availability_file,patterns));
3435       SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(state_file,patterns));
3436       XBT_DEBUG("</host>");
3437       SURFXML_START_TAG(host);
3438       SURFXML_END_TAG(host);
3439
3440
3441       temp_cluster_bw = xbt_strdup(cluster_bw);
3442       temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
3443       temp_cluster_lat = xbt_strdup(cluster_lat);
3444       temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
3445       XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,temp_cluster_bw, cluster_lat);
3446       A_surfxml_link_state = A_surfxml_link_state_ON;
3447       A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3448       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3449           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3450       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3451           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3452       SURFXML_BUFFER_SET(link_id, link_id);
3453       SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
3454       SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
3455       SURFXML_BUFFER_SET(link_bandwidth_file, "");
3456       SURFXML_BUFFER_SET(link_latency_file, "");
3457       SURFXML_BUFFER_SET(link_state_file, "");
3458       SURFXML_START_TAG(link);
3459       SURFXML_END_TAG(link);
3460
3461       xbt_free(temp_cluster_bw);
3462       xbt_free(temp_cluster_lat);
3463       xbt_free(temp_cluster_power);
3464       free(link_id);
3465       free(host_id);
3466       break;
3467
3468     case 2:
3469
3470       surf_parse_get_int(&start,
3471                          xbt_dynar_get_as(radical_ends, 0, char *));
3472       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3473       for (i = start; i <= end; i++) {
3474         host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
3475 #ifndef HAVE_PCRE_LIB
3476         xbt_dynar_push_as(tab_elements_num, int, i);
3477 #endif
3478         link_id = bprintf("%s_link_%d", cluster_id, i);
3479
3480         xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
3481         temp_cluster_power = xbt_strdup(cluster_power);
3482         temp_cluster_power = replace_random_parameter(temp_cluster_power);
3483         XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, temp_cluster_power);
3484         A_surfxml_host_state = A_surfxml_host_state_ON;
3485         SURFXML_BUFFER_SET(host_id, host_id);
3486         SURFXML_BUFFER_SET(host_power, temp_cluster_power);
3487         SURFXML_BUFFER_SET(host_core, cluster_core);
3488         SURFXML_BUFFER_SET(host_availability, "1.0");
3489         SURFXML_BUFFER_SET(host_coordinates, "");
3490         xbt_free(availability_file);
3491         availability_file = xbt_strdup(cluster_availability_file);
3492         xbt_free(state_file);
3493         state_file = xbt_strdup(cluster_state_file);
3494         XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(availability_file,patterns));
3495         XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(state_file,patterns));
3496         SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(availability_file,patterns));
3497         SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(state_file,patterns));
3498         XBT_DEBUG("</host>");
3499         SURFXML_START_TAG(host);
3500         SURFXML_END_TAG(host);
3501
3502         xbt_free(temp_cluster_power);
3503
3504         temp_cluster_bw = xbt_strdup(cluster_bw);
3505         temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
3506         temp_cluster_lat = xbt_strdup(cluster_lat);
3507         temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
3508         XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,temp_cluster_bw, cluster_lat);
3509         A_surfxml_link_state = A_surfxml_link_state_ON;
3510         A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3511         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3512             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3513         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3514             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3515         SURFXML_BUFFER_SET(link_id, link_id);
3516         SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
3517         SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
3518         SURFXML_BUFFER_SET(link_bandwidth_file, "");
3519         SURFXML_BUFFER_SET(link_latency_file, "");
3520         SURFXML_BUFFER_SET(link_state_file, "");
3521         SURFXML_START_TAG(link);
3522         SURFXML_END_TAG(link);
3523
3524         xbt_free(temp_cluster_bw);
3525         xbt_free(temp_cluster_lat);
3526         free(link_id);
3527         free(host_id);
3528       }
3529       break;
3530
3531     default:
3532       XBT_DEBUG("Malformed radical");
3533     }
3534
3535     xbt_dynar_free(&radical_ends);
3536   }
3537   xbt_dynar_free(&radical_elements);
3538
3539   XBT_DEBUG(" ");
3540   router_id =
3541       bprintf("%s%s_router%s", cluster_prefix, cluster_id,
3542               cluster_suffix);
3543   link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
3544   link_backbone = bprintf("%s_backbone", cluster_id);
3545
3546   XBT_DEBUG("<router id=\"%s\"/>", router_id);
3547   SURFXML_BUFFER_SET(router_id, router_id);
3548   SURFXML_BUFFER_SET(router_coordinates, "");
3549   SURFXML_START_TAG(router);
3550   SURFXML_END_TAG(router);
3551
3552   //TODO
3553   xbt_dict_set(patterns, "radical", xbt_strdup("_router"), xbt_free);
3554   temp_cluster_bw = xbt_strdup(cluster_bw);
3555   temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
3556   temp_cluster_lat = xbt_strdup(cluster_lat);
3557   temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
3558   XBT_DEBUG("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_router,temp_cluster_bw, temp_cluster_lat);
3559   A_surfxml_link_state = A_surfxml_link_state_ON;
3560   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3561   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3562   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3563   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3564   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3565   SURFXML_BUFFER_SET(link_id, link_router);
3566   SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
3567   SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
3568   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3569   SURFXML_BUFFER_SET(link_latency_file, "");
3570   SURFXML_BUFFER_SET(link_state_file, "");
3571   SURFXML_START_TAG(link);
3572   SURFXML_END_TAG(link);
3573
3574   xbt_free(temp_cluster_bw);
3575   xbt_free(temp_cluster_lat);
3576
3577   XBT_DEBUG("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bb_bw, cluster_bb_lat);
3578   A_surfxml_link_state = A_surfxml_link_state_ON;
3579   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3580   if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
3581   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3582   SURFXML_BUFFER_SET(link_id, link_backbone);
3583   SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
3584   SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
3585   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3586   SURFXML_BUFFER_SET(link_latency_file, "");
3587   SURFXML_BUFFER_SET(link_state_file, "");
3588   SURFXML_START_TAG(link);
3589   SURFXML_END_TAG(link);
3590
3591   XBT_DEBUG(" ");
3592
3593 #ifdef HAVE_PCRE_LIB
3594   char *new_suffix = xbt_strdup("");
3595
3596   radical_elements = xbt_str_split(cluster_suffix, ".");
3597   xbt_dynar_foreach(radical_elements, iter, groups) {
3598     if (strcmp(groups, "")) {
3599       char *old_suffix = new_suffix;
3600       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
3601       free(old_suffix);
3602     }
3603   }
3604   route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
3605   xbt_dynar_free(&radical_elements);
3606   free(new_suffix);
3607
3608   char *pcre_link_src = bprintf("%s_link_$1src", cluster_id);
3609   char *pcre_link_backbone = bprintf("%s_backbone", cluster_id);
3610   char *pcre_link_dst = bprintf("%s_link_$1dst", cluster_id);
3611
3612   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
3613   XBT_DEBUG("symmetrical=\"NO\">");
3614   SURFXML_BUFFER_SET(route_src, route_src_dst);
3615   SURFXML_BUFFER_SET(route_dst, route_src_dst);
3616   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
3617   SURFXML_START_TAG(route);
3618
3619   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_src);
3620   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_src);
3621   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3622   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3623   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3624   SURFXML_START_TAG(link_ctn);
3625   SURFXML_END_TAG(link_ctn);
3626
3627   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
3628   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
3629   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3630   SURFXML_START_TAG(link_ctn);
3631   SURFXML_END_TAG(link_ctn);
3632
3633   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_dst);
3634   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_dst);
3635   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3636   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3637   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3638   SURFXML_START_TAG(link_ctn);
3639   SURFXML_END_TAG(link_ctn);
3640
3641   XBT_DEBUG("</route>");
3642   SURFXML_END_TAG(route);
3643
3644   free(pcre_link_dst);
3645   free(pcre_link_backbone);
3646   free(pcre_link_src);
3647   free(route_src_dst);
3648 #else
3649   for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
3650     for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
3651       if (i == xbt_dynar_length(tab_elements_num)) {
3652         route_src = router_id;
3653       } else {
3654         route_src =
3655             bprintf("%s%d%s", cluster_prefix,
3656                     xbt_dynar_get_as(tab_elements_num, i, int),
3657                     cluster_suffix);
3658       }
3659
3660       if (j == xbt_dynar_length(tab_elements_num)) {
3661         route_dst = router_id;
3662       } else {
3663         route_dst =
3664             bprintf("%s%d%s", cluster_prefix,
3665                     xbt_dynar_get_as(tab_elements_num, j, int),
3666                     cluster_suffix);
3667       }
3668
3669       XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src, route_dst);
3670       XBT_DEBUG("symmetrical=\"NO\">");
3671       SURFXML_BUFFER_SET(route_src, route_src);
3672       SURFXML_BUFFER_SET(route_dst, route_dst);
3673       A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
3674       SURFXML_START_TAG(route);
3675
3676       if (i == xbt_dynar_length(tab_elements_num)) {
3677         route_src = link_router;
3678       } else {
3679         route_src =
3680             bprintf("%s_link_%d", cluster_id,
3681                     xbt_dynar_get_as(tab_elements_num, i, int));
3682       }
3683
3684       if (j == xbt_dynar_length(tab_elements_num)) {
3685         route_dst = link_router;
3686       } else {
3687         route_dst =
3688             bprintf("%s_link_%d", cluster_id,
3689                     xbt_dynar_get_as(tab_elements_num, j, int));
3690       }
3691
3692       XBT_DEBUG("<link_ctn\tid=\"%s\"/>", route_src);
3693       SURFXML_BUFFER_SET(link_ctn_id, route_src);
3694       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3695       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3696       {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3697       SURFXML_START_TAG(link_ctn);
3698       SURFXML_END_TAG(link_ctn);
3699
3700       XBT_DEBUG("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3701       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3702       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3703       SURFXML_START_TAG(link_ctn);
3704       SURFXML_END_TAG(link_ctn);
3705
3706       XBT_DEBUG("<link_ctn\tid=\"%s\"/>", route_dst);
3707       SURFXML_BUFFER_SET(link_ctn_id, route_dst);
3708       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3709       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3710       {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3711       SURFXML_START_TAG(link_ctn);
3712       SURFXML_END_TAG(link_ctn);
3713
3714       XBT_DEBUG("</route>");
3715       SURFXML_END_TAG(route);
3716     }
3717   }
3718   xbt_dynar_free(&tab_elements_num);
3719
3720 #endif
3721
3722   free(router_id);
3723   free(link_backbone);
3724   free(link_router);
3725   xbt_dict_free(&patterns);
3726   free(availability_file);
3727   free(state_file);
3728
3729   XBT_DEBUG("</AS>");
3730   SURFXML_END_TAG(AS);
3731   XBT_DEBUG(" ");
3732
3733   surfxml_bufferstack_pop(1);
3734 }
3735 /*
3736  * This function take a string and replace parameters from patterns dict.
3737  * It returns the new value.
3738  */
3739 static char* replace_random_parameter(char * string)
3740 {
3741   char *test_string = NULL;
3742
3743   if(xbt_dict_size(random_value)==0)
3744     return string;
3745
3746   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
3747   test_string = bprintf("${%s}", string);
3748   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
3749
3750   if (strcmp(test_string,"")) { //if not empty, keep this value.
3751     xbt_free(string);
3752     string = test_string;
3753   } //In other case take old value (without ${})
3754   else
3755         free(test_string);
3756   return string;
3757 }
3758
3759 static void clean_dict_random(void)
3760 {
3761         xbt_dict_free(&random_value);
3762         xbt_dict_free(&patterns);
3763 }
3764
3765 static void routing_parse_Speer(void)
3766 {
3767   static int AX_ptr = 0;
3768
3769   char *peer_id = A_surfxml_peer_id;
3770   char *peer_power = A_surfxml_peer_power;
3771   char *peer_bw_in = A_surfxml_peer_bw_in;
3772   char *peer_bw_out = A_surfxml_peer_bw_out;
3773   char *peer_lat = A_surfxml_peer_lat;
3774   char *peer_coord = A_surfxml_peer_coordinates;
3775   char *peer_state_file = A_surfxml_peer_state_file;
3776   char *peer_availability_file = A_surfxml_peer_availability_file;
3777
3778   char *host_id = NULL;
3779   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
3780
3781   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
3782   static unsigned int surfxml_buffer_stack_stack[1024];
3783
3784   surfxml_buffer_stack_stack[0] = 0;
3785
3786   surfxml_bufferstack_push(1);
3787
3788   SURFXML_BUFFER_SET(AS_id, peer_id);
3789
3790   SURFXML_BUFFER_SET(AS_routing, "Full");
3791   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", peer_id);
3792
3793   SURFXML_START_TAG(AS);
3794
3795   XBT_DEBUG(" ");
3796   host_id = bprintf("peer_%s", peer_id);
3797   router_id = bprintf("router_%s", peer_id);
3798   link_id_up = bprintf("link_%s_up", peer_id);
3799   link_id_down = bprintf("link_%s_down", peer_id);
3800
3801   link_router = bprintf("%s_link_router", peer_id);
3802   link_backbone = bprintf("%s_backbone", peer_id);
3803
3804   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, peer_power);
3805   A_surfxml_host_state = A_surfxml_host_state_ON;
3806   SURFXML_BUFFER_SET(host_id, host_id);
3807   SURFXML_BUFFER_SET(host_power, peer_power);
3808   SURFXML_BUFFER_SET(host_availability, "1.0");
3809   SURFXML_BUFFER_SET(host_availability_file, peer_availability_file);
3810   SURFXML_BUFFER_SET(host_state_file, peer_state_file);
3811   SURFXML_BUFFER_SET(host_coordinates, "");
3812   SURFXML_START_TAG(host);
3813   SURFXML_END_TAG(host);
3814
3815   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, peer_coord);
3816   SURFXML_BUFFER_SET(router_id, router_id);
3817   SURFXML_BUFFER_SET(router_coordinates, peer_coord);
3818   SURFXML_START_TAG(router);
3819   SURFXML_END_TAG(router);
3820
3821   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_up, peer_bw_in, peer_lat);
3822   A_surfxml_link_state = A_surfxml_link_state_ON;
3823   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3824   SURFXML_BUFFER_SET(link_id, link_id_up);
3825   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_in);
3826   SURFXML_BUFFER_SET(link_latency, peer_lat);
3827   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3828   SURFXML_BUFFER_SET(link_latency_file, "");
3829   SURFXML_BUFFER_SET(link_state_file, "");
3830   SURFXML_START_TAG(link);
3831   SURFXML_END_TAG(link);
3832
3833   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_down, peer_bw_out, peer_lat);
3834   A_surfxml_link_state = A_surfxml_link_state_ON;
3835   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3836   SURFXML_BUFFER_SET(link_id, link_id_down);
3837   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_out);
3838   SURFXML_BUFFER_SET(link_latency, peer_lat);
3839   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3840   SURFXML_BUFFER_SET(link_latency_file, "");
3841   SURFXML_BUFFER_SET(link_state_file, "");
3842   SURFXML_START_TAG(link);
3843   SURFXML_END_TAG(link);
3844
3845   XBT_DEBUG(" ");
3846
3847   // begin here
3848   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
3849   XBT_DEBUG("symmetrical=\"NO\">");
3850   SURFXML_BUFFER_SET(route_src, host_id);
3851   SURFXML_BUFFER_SET(route_dst, router_id);
3852   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
3853   SURFXML_START_TAG(route);
3854
3855   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
3856   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
3857   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3858   SURFXML_START_TAG(link_ctn);
3859   SURFXML_END_TAG(link_ctn);
3860
3861   XBT_DEBUG("</route>");
3862   SURFXML_END_TAG(route);
3863
3864   //Opposite Route
3865   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
3866   XBT_DEBUG("symmetrical=\"NO\">");
3867   SURFXML_BUFFER_SET(route_src, router_id);
3868   SURFXML_BUFFER_SET(route_dst, host_id);
3869   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
3870   SURFXML_START_TAG(route);
3871
3872   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
3873   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
3874   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3875   SURFXML_START_TAG(link_ctn);
3876   SURFXML_END_TAG(link_ctn);
3877
3878   XBT_DEBUG("</route>");
3879   SURFXML_END_TAG(route);
3880
3881   XBT_DEBUG("</AS>");
3882   SURFXML_END_TAG(AS);
3883   XBT_DEBUG(" ");
3884
3885   //xbt_dynar_free(&tab_elements_num);
3886         free(host_id);
3887         free(router_id);
3888         free(link_router);
3889         free(link_backbone);
3890         free(link_id_up);
3891         free(link_id_down);
3892   surfxml_bufferstack_pop(1);
3893 }
3894
3895 static void routing_parse_Srandom(void)
3896 {
3897           double mean, std, min, max, seed;
3898           char *random_id = A_surfxml_random_id;
3899           char *random_radical = A_surfxml_random_radical;
3900           char *rd_name = NULL;
3901           char *rd_value;
3902           surf_parse_get_double(&mean,A_surfxml_random_mean);
3903           surf_parse_get_double(&std,A_surfxml_random_std_deviation);
3904           surf_parse_get_double(&min,A_surfxml_random_min);
3905           surf_parse_get_double(&max,A_surfxml_random_max);
3906           surf_parse_get_double(&seed,A_surfxml_random_seed);
3907
3908           double res = 0;
3909           int i = 0;
3910           random_data_t random = xbt_new0(s_random_data_t, 1);
3911           char *tmpbuf;
3912
3913           xbt_dynar_t radical_elements;
3914           unsigned int iter;
3915           char *groups;
3916           int start, end;
3917           xbt_dynar_t radical_ends;
3918
3919           random->generator = A_surfxml_random_generator;
3920           random->seed = seed;
3921           random->min = min;
3922           random->max = max;
3923
3924           /* Check user stupidities */
3925           if (max < min)
3926             THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
3927           if (mean < min)
3928             THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
3929                    min);
3930           if (mean > max)
3931             THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
3932                    max);
3933
3934           /* normalize the mean and standard deviation before storing */
3935           random->mean = (mean - min) / (max - min);
3936           random->std = std / (max - min);
3937
3938           if (random->mean * (1 - random->mean) < random->std * random->std)
3939             THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
3940                    random->mean, random->std);
3941
3942           XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
3943           random_id,
3944           random->min,
3945           random->max,
3946           random->mean,
3947           random->std,
3948           random->generator,
3949           random->seed,
3950           random_radical);
3951
3952           if(xbt_dict_size(random_value)==0)
3953                   random_value = xbt_dict_new();
3954
3955           if(!strcmp(random_radical,""))
3956           {
3957                   res = random_generate(random);
3958                   rd_value = bprintf("%f",res);
3959                   xbt_dict_set(random_value, random_id, rd_value, free);
3960           }
3961           else
3962           {
3963                   radical_elements = xbt_str_split(random_radical, ",");
3964                   xbt_dynar_foreach(radical_elements, iter, groups) {
3965                         radical_ends = xbt_str_split(groups, "-");
3966                         switch (xbt_dynar_length(radical_ends)) {
3967                         case 1:
3968                                           xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
3969                                           res = random_generate(random);
3970                                           tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
3971                                           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
3972                                           xbt_free(tmpbuf);
3973                                           break;
3974
3975                         case 2:   surf_parse_get_int(&start,
3976                                                                                  xbt_dynar_get_as(radical_ends, 0, char *));
3977                                           surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3978                                           for (i = start; i <= end; i++) {
3979                                                   xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
3980                                                   res = random_generate(random);
3981                           tmpbuf = bprintf("%s%d",random_id,i);
3982                                                   xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
3983                           xbt_free(tmpbuf);
3984                                           }
3985                                           break;
3986                         default:
3987                                 XBT_INFO("Malformed radical");
3988                         }
3989                         res = random_generate(random);
3990                         rd_name  = bprintf("%s_router",random_id);
3991                         rd_value = bprintf("%f",res);
3992                         xbt_dict_set(random_value, rd_name, rd_value, free);
3993
3994                         xbt_dynar_free(&radical_ends);
3995                   }
3996                   free(rd_name);
3997                   xbt_dynar_free(&radical_elements);
3998           }
3999 }
4000
4001 static void routing_parse_Erandom(void)
4002 {
4003         xbt_dict_cursor_t cursor = NULL;
4004         char *key;
4005         char *elem;
4006
4007         xbt_dict_foreach(random_value, cursor, key, elem) {
4008           XBT_DEBUG("%s = %s",key,elem);
4009         }
4010
4011 }
4012
4013
4014 /*
4015  * New methods to init the routing model component from the lua script
4016  */
4017
4018 /*
4019  * calling parse_S_AS_lua with lua values
4020  */
4021 void routing_AS_init(const char *AS_id, const char *AS_routing)
4022 {
4023   parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
4024 }
4025
4026 /*
4027  * calling parse_E_AS_lua to fisnish the creation of routing component
4028  */
4029 void routing_AS_end(const char *AS_id)
4030 {
4031   parse_E_AS_lua((char *) AS_id);
4032 }
4033
4034 /*
4035  * add a host to the network element list
4036  */
4037
4038 void routing_add_host(const char *host_id)
4039 {
4040   parse_S_host_lua((char *) host_id, (char*)""); // FIXME propagate coordinate system to lua
4041 }
4042
4043 /*
4044  * Set a new link on the actual list of link for a route or ASroute
4045  */
4046 void routing_add_link(const char *link_id)
4047 {
4048   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
4049 }
4050
4051 /*
4052  *Set the endpoints for a route
4053  */
4054 void routing_set_route(const char *src_id, const char *dst_id)
4055 {
4056   parse_S_route_new_and_endpoints_lua(src_id, dst_id);
4057 }
4058
4059 /*
4060  * Store the route by calling parse_E_route_store_route
4061  */
4062 void routing_store_route(void)
4063 {
4064   parse_E_route_store_route();
4065 }