Logo AND Algorithmique Numérique Distribuée

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