Logo AND Algorithmique Numérique Distribuée

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