Logo AND Algorithmique Numérique Distribuée

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