Logo AND Algorithmique Numérique Distribuée

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