Logo AND Algorithmique Numérique Distribuée

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