Logo AND Algorithmique Numérique Distribuée

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