Logo AND Algorithmique Numérique Distribuée

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