Logo AND Algorithmique Numérique Distribuée

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