Logo AND Algorithmique Numérique Distribuée

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