Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
format changes
[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 #include <float.h>
8 #include "surf_private.h"
9 #include "xbt/dynar.h"
10 #include "xbt/str.h"
11 #include "xbt/config.h"
12 #include "xbt/graph.h"
13 #include "xbt/set.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route,surf,"Routing part of surf");
16     
17 ////////////////////////////////////////////////////////////////////////////////
18 // HERE START THE NEW CODE
19 ////////////////////////////////////////////////////////////////////////////////
20
21 //...... DEBUG ONLY .... //
22 static void print_tree(routing_component_t rc);
23 static void print_global(void);
24 static void print_AS_start(void);
25 static void print_AS_end(void);
26 static void print_host(void);
27 static void print_link(void);
28 static void print_route(void);
29 static void print_ctn(void);
30 static void DEBUG_exit(void);
31
32 ////////////////////////////////////////////////////////////////////////////////
33 // HERE START THE NEW CODE
34 ////////////////////////////////////////////////////////////////////////////////
35
36 /* Global vars */
37 routing_global_t global_routing = NULL;
38 routing_component_t current_routing = NULL;
39 model_type_t current_routing_model = NULL;
40
41 /* Prototypes of each model */
42 static void* model_full_create(void); /* create structures for full routing model */
43 static void  model_full_load(void);   /* load parse functions for full routing model */
44 static void  model_full_unload(void); /* unload parse functions for full routing model */
45 static void  model_full_end(void);    /* finalize the creation of full routing model */
46
47 static void* model_floyd_create(void); /* create structures for floyd routing model */
48 static void  model_floyd_load(void);   /* load parse functions for floyd routing model */
49 static void  model_floyd_unload(void); /* unload parse functions for floyd routing model */
50 static void  model_floyd_end(void);    /* finalize the creation of floyd routing model */
51
52 static void* model_dijkstra_both_create(int cached); /* create by calling dijkstra or dijkstracache */
53 static void* model_dijkstra_create(void);      /* create structures for dijkstra routing model */
54 static void* model_dijkstracache_create(void); /* create structures for dijkstracache routing model */
55 static void  model_dijkstra_both_load(void);   /* load parse functions for dijkstra routing model */
56 static void  model_dijkstra_both_unload(void); /* unload parse functions for dijkstra routing model */
57 static void  model_dijkstra_both_end(void);    /* finalize the creation of dijkstra 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 /* Table of routing models */
65 // typedef enum {
66 //   SURF_MODEL_FULL = 0,
67 //   SURF_MODEL_FLOYD,
68 //   SURF_MODEL_DIJKSTRA,
69 //   SURF_MODEL_DIJKSTRACACHE,
70 //   SURF_MODEL_NONE
71 // } e_surf_model_type_t;
72
73 /* this lines are olny for replace use like index in the model table */
74 #define SURF_MODEL_FULL           0
75 #define SURF_MODEL_FLOYD          1
76 #define SURF_MODEL_DIJKSTRA       2
77 #define SURF_MODEL_DIJKSTRACACHE  3
78 #define SURF_MODEL_NONE           4
79
80 /* must be finish with null and carefull if change de order */
81 struct s_model_type routing_models[] =
82 { {"Full", "Full routing data (fast, large memory requirements, fully expressive)",
83   model_full_create, model_full_load, model_full_unload, model_full_end },  
84   {"Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
85   model_floyd_create, model_floyd_load, model_floyd_unload, model_floyd_end },
86   {"Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
87   model_dijkstra_create ,model_dijkstra_both_load, model_dijkstra_both_unload, model_dijkstra_both_end },
88   {"DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
89   model_dijkstracache_create, model_dijkstra_both_load, model_dijkstra_both_unload, model_dijkstra_both_end },
90   {"none", "No routing (usable with Constant network only)",
91   model_none_create, model_none_load, model_none_unload, model_none_end },
92   {NULL,NULL,NULL,NULL,NULL,NULL}};
93
94 /* ************************************************************************** */
95 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
96
97 static void generic_set_processing_units(routing_component_t rc, const char* name);
98 static void generic_set_autonomous_system(routing_component_t rc, const char* name);
99 static void generic_set_route(routing_component_t rc, const char* src, const char* dst, route_t route);
100 static void generic_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route);
101 static void generic_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route);
102
103 /* ************************************************************************** */
104 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
105
106 static route_extended_t generic_get_bypassroute(routing_component_t rc, const char* src, const char* dst);
107
108 /* ************************************************************************** */
109 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
110
111 static route_extended_t generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy, void* data, int order);
112 static void generic_free_extended_route(route_extended_t e_route);
113 static routing_component_t generic_autonomous_system_exist(routing_component_t rc, char* element);
114 static routing_component_t generic_processing_units_exist(routing_component_t rc, char* element);
115 static void generic_src_dst_check(routing_component_t rc, const char* src, const char* dst);
116
117 /* ************************************************************************** */
118 /* **************************** GLOBAL FUNCTIONS **************************** */
119   
120 /* global parse functions */
121
122 static char* src = NULL;    /* temporary store the source name of a route */
123 static char* dst = NULL;    /* temporary store the destination name of a route */
124 static char* gw_src = NULL; /* temporary store the gateway source name of a route */
125 static char* gw_dst = NULL; /* temporary store the gateway destination name of a route */
126 static xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */ 
127
128 /**
129  * \brief Add a "host" to the network element list
130  */
131 static void  parse_S_host(void) {
132   if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_BASE;
133   xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_host_id),
134       "Reading a host, processing unit \"%s\" already exist",A_surfxml_host_id);
135   xbt_assert1(current_routing->set_processing_units,
136       "no defined method \"set_processing_units\" in \"%s\"",current_routing->name);
137   (*(current_routing->set_processing_units))(current_routing,A_surfxml_host_id);
138   xbt_dict_set(global_routing->where_network_elements,A_surfxml_host_id,(void*)current_routing,NULL); 
139 }
140
141 /**
142  * \brief Add a "router" to the network element list
143  */
144 static void parse_S_router(void) {
145   if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_BASE;
146   xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_router_id),
147       "Reading a router, processing unit \"%s\" already exist",A_surfxml_router_id);
148   xbt_assert1(current_routing->set_processing_units,
149       "no defined method \"set_processing_units\" in \"%s\"",current_routing->name);
150   (*(current_routing->set_processing_units))(current_routing,A_surfxml_router_id);
151   xbt_dict_set(global_routing->where_network_elements,A_surfxml_router_id,(void*)current_routing,NULL); 
152 }
153
154 /**
155  * \brief Set the endponints for a route
156  */
157 static void parse_S_route_new_and_endpoints(void) {
158   if( src != NULL && dst != NULL && link_list != NULL )
159     THROW2(arg_error,0,"Route between %s to %s can not be defined",A_surfxml_route_src,A_surfxml_route_dst);
160   src = A_surfxml_route_src;
161   dst = A_surfxml_route_dst;
162   link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
163 }
164
165 /**
166  * \brief Set the endponints and gateways for a ASroute
167  */
168 static void parse_S_ASroute_new_and_endpoints(void) {
169   if( src != NULL && dst != NULL && link_list != NULL )
170     THROW2(arg_error,0,"Route between %s to %s can not be defined",A_surfxml_ASroute_src,A_surfxml_ASroute_dst);
171   src = A_surfxml_ASroute_src;
172   dst = A_surfxml_ASroute_dst;
173   gw_src = A_surfxml_ASroute_gw_src;
174   gw_dst = A_surfxml_ASroute_gw_dst;
175   link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
176 }
177
178 /**
179  * \brief Set the endponints for a bypassRoute
180  */
181 static void parse_S_bypassRoute_new_and_endpoints(void) {
182   if( src != NULL && dst != NULL && link_list != NULL )
183     THROW2(arg_error,0,"Bypass Route between %s to %s can not be defined",A_surfxml_bypassRoute_src,A_surfxml_bypassRoute_dst);
184   src = A_surfxml_bypassRoute_src;
185   dst = A_surfxml_bypassRoute_dst;
186   gw_src = A_surfxml_bypassRoute_gw_src;
187   gw_dst = A_surfxml_bypassRoute_gw_dst;
188   link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
189 }
190
191 /**
192  * \brief Set a new link on the actual list of link for a route or ASroute
193  */
194 static void parse_E_link_c_ctn_new_elem(void) {
195   char *val;
196   val = xbt_strdup(A_surfxml_link_c_ctn_id);
197   xbt_dynar_push(link_list, &val);
198 }
199
200 /**
201  * \brief Store the route by calling the set_route function of the current routing component
202  */
203 static void parse_E_route_store_route(void) {
204   route_t route = xbt_new0(s_route_t,1);
205   route->link_list = link_list;
206   //TODO: NULL?
207   xbt_assert1(generic_processing_units_exist(current_routing,src),"the \"%s\" processing units gateway does not exist",src);
208   xbt_assert1(generic_processing_units_exist(current_routing,dst),"the \"%s\" processing units gateway does not exist",dst);
209   xbt_assert1(current_routing->set_route,"no defined method \"set_route\" in \"%s\"",current_routing->name);
210   (*(current_routing->set_route))(current_routing,src,dst,route);
211   link_list = NULL;
212   src = NULL;
213   dst = NULL;
214 }
215
216 /**
217  * \brief Store the ASroute by calling the set_ASroute function of the current routing component
218  */
219 static void parse_E_ASroute_store_route(void) {
220   route_extended_t e_route = xbt_new0(s_route_extended_t,1);
221   e_route->generic_route.link_list = link_list;
222   e_route->src_gateway = xbt_strdup(gw_src);
223   e_route->dst_gateway = xbt_strdup(gw_dst);
224   //TODO: NULL?
225   xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
226   xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
227   xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
228   xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
229   xbt_assert1(current_routing->set_ASroute,"no defined method \"set_ASroute\" in \"%s\"",current_routing->name);
230   (*(current_routing->set_ASroute))(current_routing,src,dst,e_route);
231   link_list = NULL;
232   src = NULL;
233   dst = NULL;
234   gw_src = NULL;
235   gw_dst = NULL;
236 }
237
238 /**
239  * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
240  */
241 static void parse_E_bypassRoute_store_route(void) {
242   route_extended_t e_route = xbt_new0(s_route_extended_t,1);
243   e_route->generic_route.link_list = link_list;
244   e_route->src_gateway = xbt_strdup(gw_src);
245   e_route->dst_gateway = xbt_strdup(gw_dst);
246   //TODO: NULL?
247   xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
248   xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
249   xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
250   xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
251   xbt_assert1(current_routing->set_bypassroute,"no defined method \"set_bypassroute\" in \"%s\"",current_routing->name);
252   (*(current_routing->set_bypassroute))(current_routing,src,dst,e_route);
253   link_list = NULL;
254   src = NULL;
255   dst = NULL;
256   gw_src = NULL;
257   gw_dst = NULL;
258 }
259
260 /**
261  * \brief Make a new routing component
262  *
263  * Detect the routing model type of the routing component, make the new structure and
264  * set the parsing functions to allows parsing the part of the routing tree
265  */
266 static void parse_S_AS(void) { 
267   routing_component_t new_routing;
268   model_type_t model = NULL;
269   char* wanted = A_surfxml_AS_routing;
270   int cpt;
271   /* seach the routing model */
272   for (cpt=0;routing_models[cpt].name;cpt++)
273     if (!strcmp(wanted,routing_models[cpt].name))
274           model = &routing_models[cpt];
275   /* if its not exist, error */
276   if( model == NULL ) {
277     fprintf(stderr,"Routing model %s not found. Existing models:\n",wanted);
278     for (cpt=0;routing_models[cpt].name;cpt++)
279       if (!strcmp(wanted,routing_models[cpt].name))
280         fprintf(stderr,"   %s: %s\n",routing_models[cpt].name,routing_models[cpt].desc);
281     xbt_die(NULL);
282   }
283
284   /* make a new routing component */
285   new_routing = (routing_component_t)(*(model->create))();
286   new_routing->routing = model;
287   new_routing->hierarchy = SURF_ROUTING_NULL;
288   new_routing->name = xbt_strdup(A_surfxml_AS_id);
289   new_routing->routing_sons = xbt_dict_new();
290   INFO2("Routing %s for AS %s",A_surfxml_AS_routing,A_surfxml_AS_id);
291   
292   if( current_routing == NULL && global_routing->root == NULL ){
293     
294     /* it is the first one */
295     new_routing->routing_father = NULL;
296     global_routing->root = new_routing;
297     
298   } else if( current_routing != NULL && global_routing->root != NULL ) { 
299     
300     xbt_assert1(!xbt_dict_get_or_null(current_routing->routing_sons,A_surfxml_AS_id),
301            "The AS \"%s\" already exist",A_surfxml_AS_id);
302      /* it is a part of the tree */
303     new_routing->routing_father = current_routing;
304     /* set the father behavior */
305     if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
306     /* add to the sons dictionary */
307     xbt_dict_set(current_routing->routing_sons,A_surfxml_AS_id,(void*)new_routing,NULL);
308     /* add to the father element list */
309     (*(current_routing->set_autonomous_system))(current_routing,A_surfxml_AS_id);
310     /* unload the prev parse rules */
311     (*(current_routing->routing->unload))();
312     
313   } else {
314     THROW0(arg_error,0,"All defined components must be belong to a AS");
315   }
316   /* set the new parse rules */
317   (*(new_routing->routing->load))();
318   /* set the new current component of the tree */
319   current_routing = new_routing;
320 }
321
322 /**
323  * \brief Finish the creation of a new routing component
324  *
325  * When you finish to read the routing component, other structures must be created. 
326  * the "end" method allow to do that for any routing model type
327  */
328 static void parse_E_AS(void) {
329
330   if( current_routing == NULL ) {
331     THROW1(arg_error,0,"Close AS(%s), that never open",A_surfxml_AS_id);
332   } else {
333       xbt_dict_set(global_routing->where_network_elements,current_routing->name,current_routing->routing_father,NULL);
334       (*(current_routing->routing->unload))();
335       (*(current_routing->routing->end))();
336       current_routing = current_routing->routing_father;
337       if( current_routing != NULL )
338         (*(current_routing->routing->load))();
339   }
340 }
341
342 /* Aux Business methods */
343
344 /**
345  * \brief Get the AS father and the first elements of the chain
346  *
347  * \param src the source host name 
348  * \param dst the destination host name
349  * 
350  * Get the common father of the to processing units, and the first different 
351  * father in the chain
352  */
353 static xbt_dynar_t elements_father(const char* src,const char* dst) {
354   
355   xbt_assert0(src&&dst,"bad parameters for \"elements_father\" method");
356   
357   xbt_dynar_t result = xbt_dynar_new(sizeof(char*), NULL); // &xbt_free_ref
358   
359   routing_component_t src_as, dst_as;
360   int index_src, index_dst, index_father_src, index_father_dst;
361   xbt_dynar_t path_src = NULL;
362   xbt_dynar_t path_dst = NULL;
363   routing_component_t current = NULL;
364   routing_component_t* current_src = NULL;
365   routing_component_t* current_dst = NULL;
366   routing_component_t* father = NULL;
367   
368   /* (1) find the as where the src and dst are located */
369   src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
370   dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst); 
371   xbt_assert2(src_as&&dst_as, "Ask for route \"from\"(%s) or \"to\"(%s) no found",src,dst);
372   
373   /* (2) find the path to the root routing component */
374   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
375   current = src_as; 
376   while( current != NULL ) {
377     xbt_dynar_push(path_src,&current);
378     current = current->routing_father;
379   }
380   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
381   current = dst_as; 
382   while( current != NULL ) {
383     xbt_dynar_push(path_dst,&current);
384     current = current->routing_father;
385   }
386   
387   /* (3) find the common father */
388   index_src  = (path_src->used)-1;
389   index_dst  = (path_dst->used)-1;
390   current_src = xbt_dynar_get_ptr(path_src,index_src);
391   current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
392   while( index_src >= 0 && index_dst >= 0 && *current_src == *current_dst ) {
393     current_src = xbt_dynar_get_ptr(path_src,index_src);
394     current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
395     index_src--;
396     index_dst--;
397   }
398   index_src++;
399   index_dst++;
400   current_src = xbt_dynar_get_ptr(path_src,index_src);
401   current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
402
403   /* (4) they are not in the same routing component, make the path */
404   index_father_src = index_src+1;
405   index_father_dst = index_dst+1;
406    
407   if(*current_src==*current_dst)
408     father = current_src;
409   else
410     father = xbt_dynar_get_ptr(path_src,index_father_src);
411   
412   /* (5) result generation */
413   xbt_dynar_push(result,father); // first same the father of src and dst
414   xbt_dynar_push(result,current_src); // second the first different father of src 
415   xbt_dynar_push(result,current_dst); // three  the first different father of dst
416   
417   xbt_dynar_free(&path_src);
418   xbt_dynar_free(&path_dst);
419   
420   return result;
421 }
422
423 /* Global Business methods */
424
425 /**
426  * \brief Recursive function for get_route
427  *
428  * \param src the source host name 
429  * \param dst the destination host name
430  * 
431  * This fuction is call by "get_route". It allow to walk through the 
432  * routing components tree.
433  */
434 static route_extended_t _get_route(const char* src,const char* dst) {
435   
436   void* link;
437   unsigned int cpt=0;
438   
439   DEBUG2("Solve route  \"%s\" to \"%s\"",src,dst);
440      
441   xbt_assert0(src&&dst,"bad parameters for \"_get_route\" method");
442   
443   route_extended_t e_route, e_route_cnt, e_route_src, e_route_dst;
444   
445   xbt_dynar_t elem_father_list = elements_father(src,dst);
446   
447   routing_component_t common_father = xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
448   routing_component_t src_father    = xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
449   routing_component_t dst_father    = xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
450   
451   e_route = xbt_new0(s_route_extended_t,1);
452   e_route->src_gateway = NULL;
453   e_route->dst_gateway = NULL;
454   e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
455   
456   if(src_father==dst_father) { /* SURF_ROUTING_BASE */
457     
458     if( strcmp(src,dst) ){
459       e_route_cnt = (*(common_father->get_route))(common_father,src,dst);
460       xbt_assert2(e_route_cnt,"no route between \"%s\" and \"%s\"",src,dst);
461       xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
462         xbt_dynar_push(e_route->generic_route.link_list,&link);
463       }
464       xbt_dynar_free( &(e_route_cnt->generic_route.link_list) );
465       xbt_free(e_route_cnt);
466     }
467     
468   } else { /* SURF_ROUTING_RECURSIVE */
469
470     route_extended_t e_route_bypass = (*(common_father->get_bypass_route))(common_father,src,dst);
471     
472     if(e_route_bypass)
473       e_route_cnt = e_route_bypass;    
474     else
475       e_route_cnt = (*(common_father->get_route))(common_father,src_father->name,dst_father->name);
476     
477     xbt_assert2(e_route_cnt,"no route between \"%s\" and \"%s\"",src_father->name,dst_father->name);
478
479     xbt_assert2( (e_route_cnt->src_gateway==NULL) == (e_route_cnt->dst_gateway==NULL) ,
480       "bad gateway for route between \"%s\" and \"%s\"",src,dst);
481
482     if( src != e_route_cnt->src_gateway ) {
483       e_route_src = _get_route(src,e_route_cnt->src_gateway);
484       xbt_assert2(e_route_src,"no route between \"%s\" and \"%s\"",src,e_route_cnt->src_gateway);
485       xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
486         xbt_dynar_push(e_route->generic_route.link_list,&link);
487       }
488     }
489     
490     xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
491       xbt_dynar_push(e_route->generic_route.link_list,&link);
492     }
493     
494     if( e_route_cnt->dst_gateway != dst ) {
495       e_route_dst = _get_route(e_route_cnt->dst_gateway,dst);
496       xbt_assert2(e_route_dst,"no route between \"%s\" and \"%s\"",e_route_cnt->dst_gateway,dst);
497       xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
498         xbt_dynar_push(e_route->generic_route.link_list,&link);
499       }
500     }
501     
502     e_route->src_gateway = e_route_cnt->src_gateway;
503     e_route->dst_gateway = e_route_cnt->dst_gateway;
504
505     xbt_dynar_free( &(e_route_src->generic_route.link_list) );
506     xbt_free(e_route_src);
507     xbt_dynar_free( &(e_route_cnt->generic_route.link_list) );
508     xbt_free(e_route_cnt);
509     xbt_dynar_free( &(e_route_dst->generic_route.link_list) );
510     xbt_free(e_route_dst);
511   }
512   
513   xbt_dynar_free(&elem_father_list);
514   
515   return e_route; 
516 }
517
518 /**
519  * \brief Generic method: find a route between hosts
520  *
521  * \param src the source host name 
522  * \param dst the destination host name
523  * 
524  * walk through the routing components tree and find a route between hosts
525  * by calling the differents "get_route" functions in each routing component
526  */
527 static xbt_dynar_t get_route(const char* src,const char* dst) {
528   
529   if(global_routing->last_route) xbt_dynar_free( &(global_routing->last_route) );
530   
531   route_extended_t e_route;
532   xbt_dynar_t elem_father_list = elements_father(src,dst);
533   routing_component_t common_father = xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
534   
535   if(strcmp(src,dst))
536     e_route = _get_route(src,dst);
537   else
538     e_route = (*(common_father->get_route))(common_father,src,dst);
539   
540   xbt_assert2(e_route,"no route between \"%s\" and \"%s\"",src,dst);
541   global_routing->last_route = e_route->generic_route.link_list;
542  
543   xbt_free(e_route);
544   xbt_dynar_free(&elem_father_list);
545   
546   if( xbt_dynar_length(global_routing->last_route)==0 )
547     return NULL;
548   else
549     return global_routing->last_route;
550 }
551
552 /**
553  * \brief Recursive function for finalize
554  *
555  * \param rc the source host name 
556  * 
557  * This fuction is call by "finalize". It allow to finalize the 
558  * AS or routing components. It delete all the structures.
559  */
560 static void _finalize(routing_component_t rc) {
561   if(rc) {
562     xbt_dict_cursor_t cursor = NULL;
563     char *key;
564     routing_component_t elem;
565     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
566       _finalize(elem);
567     }
568     xbt_dict_t tmp_sons = rc->routing_sons;
569     char* tmp_name = rc->name;
570     xbt_dict_free(&tmp_sons);
571     xbt_free(tmp_name);
572     xbt_assert1(rc->finalize,"no defined method \"finalize\" in \"%s\"",current_routing->name);
573     (*(rc->finalize))(rc);
574   }
575 }
576
577 /**
578  * \brief Generic method: delete all the routing structures
579  * 
580  * walk through the routing components tree and delete the structures
581  * by calling the differents "finalize" functions in each routing component
582  */
583 static void finalize(void) {
584   /* delete recursibly all the tree */  
585   _finalize(global_routing->root);
586   /* delete "where" dict */
587   xbt_dict_free(&(global_routing->where_network_elements));
588   /* delete last_route */
589   xbt_dynar_free(&(global_routing->last_route));
590   /* delete global routing structure */
591   xbt_free(global_routing);
592 }
593
594 /**
595  * \brief Generic method: create the global routing schema
596  * 
597  * Make a global routing structure and set all the parsing functions.
598  */
599 void routing_model_create(size_t size_of_links, void* loopback) {
600   
601   /* config the uniq global routing */
602   global_routing = xbt_new0(s_routing_global_t,1);
603   global_routing->where_network_elements = xbt_dict_new();
604   global_routing->root = NULL;
605   global_routing->get_route = get_route;
606   global_routing->finalize = finalize;
607   global_routing->loopback = loopback;
608   global_routing->size_of_link = size_of_links;
609   global_routing->last_route = xbt_dynar_new(size_of_links, NULL);
610   
611   /* no current routing at moment */
612   current_routing = NULL;
613
614   /* parse generic elements */
615   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host);
616   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
617
618   surfxml_add_callback(STag_surfxml_route_cb_list, &parse_S_route_new_and_endpoints);
619   surfxml_add_callback(STag_surfxml_ASroute_cb_list, &parse_S_ASroute_new_and_endpoints);
620   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list, &parse_S_bypassRoute_new_and_endpoints);
621   
622   surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_E_link_c_ctn_new_elem);
623   
624   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_E_route_store_route);
625   surfxml_add_callback(ETag_surfxml_ASroute_cb_list, &parse_E_ASroute_store_route);
626   surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list, &parse_E_bypassRoute_store_route);
627   
628   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS);
629   surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS);
630   
631   /* DEBUG ONLY */  
632   //surfxml_add_callback(ETag_surfxml_platform_cb_list, &DEBUG_exit);
633   
634   // TODO checked if ANY AS are duplicated
635 }
636
637 /* ************************************************************************** */
638 /* *************************** FULL ROUTING ********************************* */
639
640 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
641
642 /* Routing model structure */
643
644 typedef struct {
645   s_routing_component_t generic_routing;
646   xbt_dict_t parse_routes; /* store data during the parse process */
647   xbt_dict_t to_index; /* char* -> network_element_t */
648   xbt_dict_t bypassRoutes;
649   route_extended_t *routing_table;
650 } s_routing_component_full_t,*routing_component_full_t;
651
652 /* Business methods */
653
654 static route_extended_t full_get_route(routing_component_t rc, const char* src,const char* dst) {
655   xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
656   
657   /* set utils vars */
658   routing_component_full_t routing = (routing_component_full_t)rc;
659   int table_size = xbt_dict_length(routing->to_index);
660   
661   generic_src_dst_check(rc,src,dst);
662   int *src_id = xbt_dict_get_or_null(routing->to_index,src);
663   int *dst_id = xbt_dict_get_or_null(routing->to_index,dst);
664   xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",src,dst); 
665
666   route_extended_t e_route = NULL;
667   route_extended_t new_e_route = NULL;
668   void* link;
669   unsigned int cpt=0;
670
671   e_route = TO_ROUTE_FULL(*src_id,*dst_id);
672   
673   if(e_route) {
674     new_e_route = xbt_new0(s_route_extended_t,1);
675     new_e_route->src_gateway = e_route->src_gateway;
676     new_e_route->dst_gateway = e_route->dst_gateway;
677     new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
678     xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
679       xbt_dynar_push(new_e_route->generic_route.link_list,&link);
680     }
681   }
682   return new_e_route;
683 }
684
685 static void full_finalize(routing_component_t rc) {
686   routing_component_full_t routing = (routing_component_full_t)rc;
687   int table_size = xbt_dict_length(routing->to_index);
688   int i,j;
689   if (routing) {
690     /* Delete routing table */
691     for (i=0;i<table_size;i++)
692       for (j=0;j<table_size;j++)
693         generic_free_extended_route(TO_ROUTE_FULL(i,j));
694     xbt_free(routing->routing_table);
695     /* Delete bypass dict */
696     xbt_dict_free(&routing->bypassRoutes);
697     /* Delete index dict */
698     xbt_dict_free(&(routing->to_index));
699     /* Delete structure */
700     xbt_free(rc);
701   }
702 }
703
704 /* Creation routing model functions */
705
706 static void* model_full_create(void) {
707   routing_component_full_t new_component =  xbt_new0(s_routing_component_full_t,1);
708   new_component->generic_routing.set_processing_units = generic_set_processing_units;
709   new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
710   new_component->generic_routing.set_route = generic_set_route;
711   new_component->generic_routing.set_ASroute = generic_set_ASroute;
712   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
713   new_component->generic_routing.get_route = full_get_route;
714   new_component->generic_routing.get_bypass_route = generic_get_bypassroute;
715   new_component->generic_routing.finalize = full_finalize;
716   new_component->to_index = xbt_dict_new();
717   new_component->bypassRoutes = xbt_dict_new();
718   new_component->parse_routes = xbt_dict_new();
719   return new_component;
720 }
721
722 static void model_full_load(void) {
723  /* use "surfxml_add_callback" to add a parse function call */
724 }
725
726 static void model_full_unload(void) {
727  /* use "surfxml_del_callback" to remove a parse function call */
728 }
729
730 static void  model_full_end(void) {
731   
732   char *key, *end; //*src_name, *dst_name
733   const char* sep = "#";
734   int src_id, dst_id;
735   unsigned int i, j;
736   route_t route;
737   route_extended_t e_route;
738   void* data;
739   
740   xbt_dict_cursor_t cursor = NULL;
741   xbt_dynar_t keys = NULL;
742   
743   /* set utils vars */
744   routing_component_full_t routing = ((routing_component_full_t)current_routing);
745   int table_size = xbt_dict_length(routing->to_index);
746   
747   /* Create the routing table */
748   routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
749   
750   /* Put the routes in position */
751   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
752     keys = xbt_str_split_str(key, sep);
753     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
754     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
755     TO_ROUTE_FULL(src_id,dst_id) = generic_new_extended_route(current_routing->hierarchy,data,1);
756      xbt_dynar_free(&keys);
757    }
758
759    /* delete the parse table */
760   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
761     route = (route_t)data;
762     xbt_dynar_free(&(route->link_list));
763     xbt_free(data);
764   }
765       
766   /* delete parse dict */
767   xbt_dict_free(&(routing->parse_routes));
768
769   /* Add the loopback if needed */
770   if(current_routing->hierarchy == SURF_ROUTING_BASE) {
771     for (i = 0; i < table_size; i++) {
772       e_route = TO_ROUTE_FULL(i, i);
773       if(!e_route) { // && !xbt_dynar_length(e_route->generic_route.link_list)
774         e_route = xbt_new0(s_route_extended_t,1);
775         e_route->src_gateway = NULL;
776         e_route->dst_gateway = NULL;
777         e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
778         xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
779         TO_ROUTE_FULL(i, i) = e_route;
780       }
781     }
782   }
783
784   /* Shrink the dynar routes (save unused slots) */
785   for (i=0;i<table_size;i++)
786     for (j=0;j<table_size;j++)
787       if(TO_ROUTE_FULL(i,j))
788         xbt_dynar_shrink(TO_ROUTE_FULL(i,j)->generic_route.link_list,0);
789 }
790
791 /* ************************************************************************** */
792 /* *************************** FLOYD ROUTING ******************************** */
793
794 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
795 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
796 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
797
798 /* Routing model structure */
799
800 typedef struct {
801   s_routing_component_t generic_routing;
802   /* vars for calculate the floyd algorith. */
803   int *predecessor_table;
804   route_extended_t *link_table; /* char* -> int* */
805   xbt_dict_t to_index;
806   xbt_dict_t bypassRoutes;
807   /* store data during the parse process */  
808   xbt_dict_t parse_routes; 
809 } s_routing_component_floyd_t,*routing_component_floyd_t;
810
811 /* Business methods */
812
813 static route_extended_t floyd_get_route(routing_component_t rc, const char* src,const char* dst) {
814   xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
815   
816   /* set utils vars */
817   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
818   int table_size = xbt_dict_length(routing->to_index);
819   
820   generic_src_dst_check(rc,src,dst);
821   int *src_id = xbt_dict_get_or_null(routing->to_index,src);
822   int *dst_id = xbt_dict_get_or_null(routing->to_index,dst);
823   xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",src,dst); 
824   
825   /* create a result route */
826   route_extended_t new_e_route = xbt_new0(s_route_extended_t,1);
827   new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
828   new_e_route->src_gateway = NULL;
829   new_e_route->dst_gateway = NULL;
830   
831   int first = 1;
832   int pred = *dst_id;
833   int prev_pred = 0;
834   char *gw_src,*gw_dst, *prev_gw_src,*prev_gw_dst, *first_gw;
835   unsigned int cpt;
836   void* link;
837   xbt_dynar_t links;
838   
839   do {
840     prev_pred = pred;
841     pred = TO_FLOYD_PRED(*src_id, pred);
842     if(pred == -1) // if no pred in route -> no route to host
843       break;      
844     xbt_assert2(TO_FLOYD_LINK(pred,prev_pred),"Invalid link for the route between \"%s\" or \"%s\"", src, dst);
845     
846     prev_gw_src = gw_src;
847     prev_gw_dst = gw_dst;
848     
849     route_extended_t e_route = TO_FLOYD_LINK(pred,prev_pred);
850     gw_src = e_route->src_gateway;
851     gw_dst = e_route->dst_gateway;
852     
853     if(first) first_gw = gw_dst;
854     
855     if(rc->hierarchy == SURF_ROUTING_RECURSIVE && !first && strcmp(gw_dst,prev_gw_src)) {
856       //routing_component_t src_as = xbt_dict_get_or_null(global_routing->where_network_elements,gw_dst);
857       //routing_component_t dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,prev_gw_src);
858       //xbt_assert4(src_as==dst_as,"bad routing, differents AS gateways in route \"%s\" to \"%s\" (\"%s\"-\"%s\")",src,dst,gw_dst,prev_gw_src);
859       //route_extended_t e_route_as_to_as = (*(src_as->get_route))(src_as,gw_dst,prev_gw_src);
860       // TODO: - check for error - is possible to generate a route like =  as----as----as
861       xbt_dynar_t e_route_as_to_as = (*(global_routing->get_route))(gw_dst,prev_gw_src);
862       xbt_assert2(e_route_as_to_as,"no route between \"%s\" and \"%s\"",gw_dst,prev_gw_src);
863       //links = e_route_as_to_as->generic_route.link_list;
864       links = e_route_as_to_as;
865       int pos = 0;
866       xbt_dynar_foreach(links, cpt, link) {
867         xbt_dynar_insert_at(new_e_route->generic_route.link_list,pos,&link);
868         pos++;
869       }
870 //       xbt_dynar_free(&(e_route_as_to_as->generic_route.link_list));
871 //       xbt_free(e_route_as_to_as);
872     }
873     
874     links = e_route->generic_route.link_list;
875     xbt_dynar_foreach(links, cpt, link) {
876       xbt_dynar_unshift(new_e_route->generic_route.link_list,&link);
877     }
878     first=0;
879     
880   } while(pred != *src_id);
881   xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")", *src_id, *dst_id,src,dst);
882   
883   if(rc->hierarchy == SURF_ROUTING_RECURSIVE) {
884     new_e_route->src_gateway = gw_src;
885     new_e_route->dst_gateway = first_gw;
886   }
887   
888   return new_e_route;
889 }
890
891 static void floyd_finalize(routing_component_t rc) {
892    routing_component_floyd_t routing = (routing_component_floyd_t)rc;
893   int i,j,table_size;
894   if (routing) {
895     table_size = xbt_dict_length(routing->to_index);
896     /* Delete link_table */
897     for (i=0;i<table_size;i++)
898       for (j=0;j<table_size;j++)
899         generic_free_extended_route(TO_FLOYD_LINK(i,j));
900     xbt_free(routing->link_table);
901     /* Delete bypass dict */
902     xbt_dict_free(&routing->bypassRoutes);
903     /* Delete index dict */
904     xbt_dict_free(&(routing->to_index));
905     /* Delete dictionary index dict, predecessor and links table */
906     xbt_free(routing->predecessor_table);
907     /* Delete structure */
908     xbt_free(rc);
909   }
910 }
911
912 static void* model_floyd_create(void) {
913   routing_component_floyd_t new_component = xbt_new0(s_routing_component_floyd_t,1);
914   new_component->generic_routing.set_processing_units = generic_set_processing_units;
915   new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
916   new_component->generic_routing.set_route = generic_set_route;
917   new_component->generic_routing.set_ASroute = generic_set_ASroute;
918   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
919   new_component->generic_routing.get_route = floyd_get_route;
920   new_component->generic_routing.get_bypass_route = generic_get_bypassroute;
921   new_component->generic_routing.finalize = floyd_finalize;
922   new_component->to_index = xbt_dict_new();
923   new_component->bypassRoutes = xbt_dict_new();
924   new_component->parse_routes = xbt_dict_new();
925   return new_component;
926 }
927
928 static void  model_floyd_load(void) {
929  /* use "surfxml_add_callback" to add a parse function call */
930 }
931
932 static void  model_floyd_unload(void) {
933  /* use "surfxml_del_callback" to remove a parse function call */
934 }
935
936 static void  model_floyd_end(void) {
937   
938   routing_component_floyd_t routing = ((routing_component_floyd_t)current_routing);
939   xbt_dict_cursor_t cursor = NULL;
940   double * cost_table;
941   char *key,*data, *end; //*src_name, *dst_name;
942   const char *sep = "#";
943   xbt_dynar_t keys;
944   int src_id, dst_id;
945   unsigned int i,j,a,b,c;
946
947   /* set the size of inicial table */
948   int table_size = xbt_dict_length(routing->to_index);
949   
950   /* Create Cost, Predecessor and Link tables */
951   cost_table = xbt_new0(double, table_size*table_size); //link cost from host to host
952   routing->predecessor_table = xbt_new0(int, table_size*table_size); //predecessor host numbers
953   routing->link_table = xbt_new0(route_extended_t, table_size*table_size); //actual link between src and dst
954
955   /* Initialize costs and predecessors*/
956   for(i = 0; i<table_size;i++)
957     for(j = 0; j<table_size;j++) {
958         TO_FLOYD_COST(i,j) = DBL_MAX;
959         TO_FLOYD_PRED(i,j) = -1;
960         TO_FLOYD_LINK(i,j) = NULL; // FIXED by david
961     }
962
963    /* Put the routes in position */
964   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
965     keys = xbt_str_split_str(key, sep);
966     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
967     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
968     TO_FLOYD_LINK(src_id,dst_id) = generic_new_extended_route(current_routing->hierarchy,data,0);
969     TO_FLOYD_PRED(src_id,dst_id) = src_id;
970     //link cost
971     TO_FLOYD_COST(src_id,dst_id) = ((TO_FLOYD_LINK(src_id,dst_id))->generic_route.link_list)->used; // assume 1 for now // TODO DAVID REDO
972     xbt_dynar_free(&keys);
973   }
974
975   /* Add the loopback if needed */
976   if(current_routing->hierarchy == SURF_ROUTING_BASE) {
977     for (i = 0; i < table_size; i++) {
978       route_extended_t e_route = TO_FLOYD_LINK(i, i);
979       if(!e_route) { // && !xbt_dynar_length(e_route->generic_route.link_list)
980         e_route = xbt_new0(s_route_extended_t,1);
981         e_route->src_gateway = NULL;
982         e_route->dst_gateway = NULL;
983         e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
984         xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
985         TO_FLOYD_LINK(i,i) = e_route;
986         TO_FLOYD_PRED(i,i) = i;
987         TO_FLOYD_COST(i,i) = 1;
988       }
989     }
990   }
991   //Calculate path costs 
992   for(c=0;c<table_size;c++) {
993     for(a=0;a<table_size;a++) {
994       for(b=0;b<table_size;b++) {
995         if(TO_FLOYD_COST(a,c) < DBL_MAX && TO_FLOYD_COST(c,b) < DBL_MAX) {
996           if(TO_FLOYD_COST(a,b) == DBL_MAX || 
997             (TO_FLOYD_COST(a,c)+TO_FLOYD_COST(c,b) < TO_FLOYD_COST(a,b))) {
998             TO_FLOYD_COST(a,b) = TO_FLOYD_COST(a,c)+TO_FLOYD_COST(c,b);
999             TO_FLOYD_PRED(a,b) = TO_FLOYD_PRED(c,b);
1000           }
1001         }
1002       }
1003     }
1004   }
1005
1006    /* delete the parse table */
1007   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1008     route_t route = (route_t)data;
1009     xbt_dynar_free(&(route->link_list));
1010     xbt_free(data);
1011   }
1012   
1013   /* delete parse dict */
1014   xbt_dict_free(&(routing->parse_routes));
1015
1016   //cleanup
1017   xbt_free(cost_table);
1018 }
1019
1020 /* ************************************************************************** */
1021 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1022
1023 typedef struct {
1024   s_routing_component_t generic_routing;
1025   xbt_dict_t to_index;
1026   xbt_dict_t bypassRoutes;
1027   xbt_graph_t route_graph;   /* xbt_graph */
1028   xbt_dict_t graph_node_map; /* map */
1029   xbt_dict_t route_cache;    /* use in cache mode */
1030   int cached;
1031   xbt_dict_t parse_routes;
1032 } s_routing_component_dijkstra_t,*routing_component_dijkstra_t;
1033
1034
1035 typedef struct graph_node_data {
1036   int id; 
1037   int graph_id; //used for caching internal graph id's
1038 } s_graph_node_data_t, * graph_node_data_t;
1039
1040 typedef struct graph_node_map_element {
1041   xbt_node_t node;
1042 } s_graph_node_map_element_t, * graph_node_map_element_t;
1043
1044 typedef struct route_cache_element {
1045   int * pred_arr;
1046   int size;
1047 } s_route_cache_element_t, * route_cache_element_t;     
1048
1049 /* Free functions */
1050
1051 static void route_cache_elem_free(void *e) {
1052   route_cache_element_t elm=(route_cache_element_t)e;
1053   if (elm) {
1054     xbt_free(elm->pred_arr);
1055     xbt_free(elm);
1056   }
1057 }
1058
1059 static void graph_node_map_elem_free(void *e) {
1060   graph_node_map_element_t elm = (graph_node_map_element_t)e;
1061   if(elm) {
1062     xbt_free(elm);
1063   }
1064 }
1065
1066 static void graph_edge_data_free(void *e) {
1067   route_extended_t e_route = (route_extended_t)e;
1068   if(e_route) {
1069     xbt_dynar_free(&(e_route->generic_route.link_list));
1070     if(e_route->src_gateway) xbt_free(e_route->src_gateway);
1071     if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
1072     xbt_free(e_route);
1073   }
1074 }
1075
1076 /* Utility functions */
1077
1078 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc, int id, int graph_id) {
1079   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1080   xbt_node_t node = NULL;
1081   graph_node_data_t data = NULL;
1082   graph_node_map_element_t elm = NULL;
1083
1084   data = xbt_new0(struct graph_node_data, 1); // FIXED by david
1085   data->id = id;
1086   data->graph_id = graph_id;
1087   node = xbt_graph_new_node(routing->route_graph, data);
1088
1089   elm = xbt_new0(struct graph_node_map_element, 1); // FIXED by david
1090   elm->node = node;
1091   xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
1092
1093   return node;
1094 }
1095  
1096 static graph_node_map_element_t graph_node_map_search(routing_component_dijkstra_t rc, int id) {
1097   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1098   graph_node_map_element_t elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&id), sizeof(int));
1099   return elm;
1100 }
1101
1102 /* Parsing */
1103
1104 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id, int dst_id, route_extended_t e_route        ) {
1105   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1106
1107   xbt_node_t src = NULL;
1108   xbt_node_t dst = NULL;
1109   graph_node_map_element_t src_elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&src_id), sizeof(int));
1110   graph_node_map_element_t dst_elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&dst_id), sizeof(int));
1111
1112   if(src_elm)
1113     src = src_elm->node;
1114
1115   if(dst_elm)
1116     dst = dst_elm->node;
1117
1118   //add nodes if they don't exist in the graph
1119   if(src_id == dst_id && src == NULL && dst == NULL) {
1120     src = route_graph_new_node(rc,src_id, -1);
1121     dst = src;
1122   } else {
1123     if(src == NULL) {
1124       src = route_graph_new_node(rc,src_id, -1);
1125     }
1126     if(dst == NULL) {
1127       dst = route_graph_new_node(rc,dst_id, -1);
1128     }
1129   }
1130
1131   //add link as edge to graph
1132   xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1133 }
1134
1135 static void add_loopback_dijkstra(routing_component_dijkstra_t rc) {
1136   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1137
1138   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1139
1140   xbt_node_t node = NULL;
1141   unsigned int cursor2;
1142   xbt_dynar_foreach(nodes, cursor2, node) {
1143     xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node); 
1144     xbt_edge_t edge = NULL;
1145     unsigned int cursor;
1146
1147     int found = 0;
1148     xbt_dynar_foreach(out_edges, cursor, edge) {
1149       xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1150       if(other_node == node) {
1151         found = 1;
1152         break;
1153       }
1154     }
1155
1156     if(!found) {
1157       route_extended_t e_route = xbt_new0(s_route_extended_t,1);
1158       e_route->src_gateway = NULL;
1159       e_route->dst_gateway = NULL;
1160       e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1161       xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
1162       xbt_graph_new_edge(routing->route_graph, node, node, e_route);
1163     }
1164   }
1165 }
1166
1167 /* Business methods */
1168
1169 static route_extended_t dijkstra_get_route(routing_component_t rc, const char* src,const char* dst) {
1170   xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
1171   
1172   /* set utils vars */
1173   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1174   
1175   generic_src_dst_check(rc,src,dst);
1176   int *src_id = xbt_dict_get_or_null(routing->to_index,src);
1177   int *dst_id = xbt_dict_get_or_null(routing->to_index,dst);
1178   xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",src,dst); 
1179   
1180   /* create a result route */
1181   route_extended_t new_e_route = xbt_new0(s_route_extended_t,1);
1182   new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1183   new_e_route->src_gateway = NULL;
1184   new_e_route->dst_gateway = NULL;
1185   
1186   int *pred_arr = NULL;
1187   int src_node_id = 0;
1188   int dst_node_id = 0;
1189   int * nodeid = NULL;
1190   int v;
1191   route_extended_t e_route;
1192   int size = 0;
1193   unsigned int cpt;
1194   void* link;
1195   xbt_dynar_t links = NULL;
1196   route_cache_element_t elm = NULL;
1197   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1198   
1199   /* Use the graph_node id mapping set to quickly find the nodes */
1200   graph_node_map_element_t src_elm = graph_node_map_search(routing,*src_id);
1201   graph_node_map_element_t dst_elm = graph_node_map_search(routing,*dst_id);
1202   xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", *src_id, *dst_id);
1203   src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
1204   dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
1205
1206   /* if the src and dst are the same */ // FIXED by david
1207   if( src_node_id == dst_node_id ) {
1208     
1209     xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
1210     xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
1211     xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
1212
1213     xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id, *dst_id);
1214     
1215     e_route = (route_extended_t)xbt_graph_edge_get_data(edge);
1216
1217     links = e_route->generic_route.link_list;
1218     xbt_dynar_foreach(links, cpt, link) {
1219       xbt_dynar_unshift(new_e_route->generic_route.link_list,&link);
1220     }
1221   
1222     return new_e_route;
1223   }
1224   
1225   if(routing->cached) {
1226     /*check if there is a cached predecessor list avail */
1227     elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
1228   }
1229
1230   if(elm) { //cached mode and cache hit
1231     pred_arr = elm->pred_arr;
1232   } else { //not cached mode or cache miss
1233     double * cost_arr = NULL;
1234     xbt_heap_t pqueue = NULL;
1235     int i = 0;
1236
1237     int nr_nodes = xbt_dynar_length(nodes);
1238     cost_arr = xbt_new0(double, nr_nodes); // link cost from src to other hosts
1239     pred_arr = xbt_new0(int, nr_nodes);    // predecessors in path from src
1240     pqueue = xbt_heap_new(nr_nodes, xbt_free);
1241
1242     //initialize
1243     cost_arr[src_node_id] = 0.0;
1244
1245     for(i = 0; i < nr_nodes; i++) {
1246       if(i != src_node_id) {
1247         cost_arr[i] = DBL_MAX;
1248       }
1249
1250       pred_arr[i] = 0;
1251
1252       //initialize priority queue
1253       nodeid = xbt_new0(int, 1);
1254       *nodeid = i;
1255       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
1256
1257     }
1258
1259     // apply dijkstra using the indexes from the graph's node array
1260     while(xbt_heap_size(pqueue) > 0) {
1261       int * v_id = xbt_heap_pop(pqueue);
1262       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
1263       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node); 
1264       xbt_edge_t edge = NULL;
1265       unsigned int cursor;
1266
1267       xbt_dynar_foreach(out_edges, cursor, edge) {
1268         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
1269         graph_node_data_t data = xbt_graph_node_get_data(u_node);
1270         int u_id = data->graph_id;
1271         route_extended_t tmp_e_route = (route_extended_t)xbt_graph_edge_get_data(edge); // added TODO DAVID REDO
1272         int cost_v_u = (tmp_e_route->generic_route.link_list)->used; //fixed link cost for now // TODO DAVID REDO
1273
1274         if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
1275           pred_arr[u_id] = *v_id;
1276           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
1277           nodeid = xbt_new0(int, 1);
1278           *nodeid = u_id;
1279           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
1280         }
1281       }
1282
1283       //free item popped from pqueue
1284       xbt_free(v_id);
1285     }
1286
1287     xbt_free(cost_arr);
1288     xbt_heap_free(pqueue);
1289   }
1290   
1291   //compose route path with links
1292   char *gw_src,*gw_dst, *prev_gw_src,*prev_gw_dst, *first_gw;
1293   
1294   for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
1295     xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
1296     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
1297     xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
1298
1299     xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id, *dst_id);
1300     
1301     prev_gw_src = gw_src;
1302     prev_gw_dst = gw_dst;
1303     
1304     e_route = (route_extended_t)xbt_graph_edge_get_data(edge);
1305     gw_src = e_route->src_gateway;
1306     gw_dst = e_route->dst_gateway;
1307     
1308     if(v==dst_node_id) first_gw = gw_dst;
1309     
1310     if(rc->hierarchy == SURF_ROUTING_RECURSIVE && v!=dst_node_id && strcmp(gw_dst,prev_gw_src)) {
1311       //routing_component_t src_as = xbt_dict_get_or_null(global_routing->where_network_elements,gw_dst);
1312       //routing_component_t dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,prev_gw_src);
1313       //xbt_assert4(src_as==dst_as,"bad routing, differents AS gateways in route \"%s\" to \"%s\" (\"%s\"-\"%s\")",src,dst,gw_dst,prev_gw_src);
1314       //route_extended_t e_route_as_to_as = (*(src_as->get_route))(src_as,gw_dst,prev_gw_src);
1315       // TODO: - check for error - is possible to generate a route like =  as----as----as
1316       xbt_dynar_t e_route_as_to_as = (*(global_routing->get_route))(gw_dst,prev_gw_src);
1317       xbt_assert2(e_route_as_to_as,"no route between \"%s\" and \"%s\"",gw_dst,prev_gw_src);
1318       //links = e_route_as_to_as->generic_route.link_list;
1319       links = e_route_as_to_as;
1320       int pos = 0;
1321       xbt_dynar_foreach(links, cpt, link) {
1322         xbt_dynar_insert_at(new_e_route->generic_route.link_list,pos,&link);
1323         pos++;
1324       }
1325 //       xbt_dynar_free(&(e_route_as_to_as->generic_route.link_list));
1326 //       xbt_free(e_route_as_to_as);
1327     }
1328     
1329     links = e_route->generic_route.link_list;
1330     xbt_dynar_foreach(links, cpt, link) {
1331       xbt_dynar_unshift(new_e_route->generic_route.link_list,&link);
1332     }
1333     size++;
1334   }
1335
1336   if(rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1337     new_e_route->src_gateway = gw_src;
1338     new_e_route->dst_gateway = first_gw;
1339   }
1340
1341   if(routing->cached && elm == NULL) {
1342     //add to predecessor list of the current src-host to cache
1343     elm = xbt_new0(struct route_cache_element, 1); // FIXED by david
1344     elm->pred_arr = pred_arr;
1345     elm->size = size;
1346     xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
1347   }
1348
1349   if(!routing->cached)
1350     xbt_free(pred_arr);
1351   
1352   return new_e_route;
1353 }
1354
1355 static void dijkstra_finalize(routing_component_t rc) {
1356   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1357
1358   if (routing) {
1359     xbt_graph_free_graph(routing->route_graph, &xbt_free, &graph_edge_data_free, &xbt_free);
1360     xbt_dict_free(&routing->graph_node_map);
1361     if(routing->cached)
1362       xbt_dict_free(&routing->route_cache);
1363     /* Delete bypass dict */
1364     xbt_dict_free(&routing->bypassRoutes);
1365     /* Delete index dict */
1366     xbt_dict_free(&(routing->to_index));
1367     /* Delete structure */
1368     xbt_free(routing);
1369   }
1370 }
1371
1372 /* Creation routing model functions */
1373
1374 static void* model_dijkstra_both_create(int cached) {
1375   routing_component_dijkstra_t new_component = xbt_new0(s_routing_component_dijkstra_t,1);
1376   new_component->generic_routing.set_processing_units = generic_set_processing_units;
1377   new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
1378   new_component->generic_routing.set_route = generic_set_route;
1379   new_component->generic_routing.set_ASroute = generic_set_ASroute;
1380   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1381   new_component->generic_routing.get_route = dijkstra_get_route;
1382   new_component->generic_routing.get_bypass_route = generic_get_bypassroute;
1383   new_component->generic_routing.finalize = dijkstra_finalize;
1384   new_component->cached = cached;
1385   new_component->to_index = xbt_dict_new();
1386   new_component->bypassRoutes = xbt_dict_new();
1387   new_component->parse_routes = xbt_dict_new();
1388   return new_component;
1389 }
1390
1391 static void* model_dijkstra_create(void) {
1392   return model_dijkstra_both_create(0);
1393 }
1394
1395 static void* model_dijkstracache_create(void) {
1396   return model_dijkstra_both_create(1);
1397 }
1398
1399 static void  model_dijkstra_both_load(void) {
1400  /* use "surfxml_add_callback" to add a parse function call */
1401 }
1402
1403 static void  model_dijkstra_both_unload(void) {
1404  /* use "surfxml_del_callback" to remove a parse function call */
1405 }
1406
1407 static void  model_dijkstra_both_end(void) {
1408   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) current_routing;
1409    xbt_dict_cursor_t cursor = NULL;
1410    char *key, *data, *end;
1411    const char *sep = "#";
1412    xbt_dynar_t keys;
1413   xbt_node_t node = NULL;
1414   unsigned int cursor2;
1415   xbt_dynar_t nodes = NULL;
1416   int src_id, dst_id;
1417   route_t route;
1418   
1419   /* Create the topology graph */
1420   routing->route_graph = xbt_graph_new_graph(1, NULL);
1421   routing->graph_node_map = xbt_dict_new();
1422   
1423   if(routing->cached)
1424     routing->route_cache = xbt_dict_new();
1425
1426   /* Put the routes in position */
1427   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1428     keys = xbt_str_split_str(key, sep);
1429     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1430     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1431     route_extended_t e_route = generic_new_extended_route(current_routing->hierarchy,data,0);
1432     route_new_dijkstra(routing,src_id,dst_id,e_route);
1433     xbt_dynar_free(&keys);
1434   }
1435
1436   /* delete the parse table */
1437   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1438     route = (route_t)data;
1439     xbt_dynar_free(&(route->link_list));
1440     xbt_free(data);
1441   }
1442       
1443   /* delete parse dict */
1444   xbt_dict_free(&(routing->parse_routes));
1445   
1446   /* Add the loopback if needed */
1447   if(current_routing->hierarchy == SURF_ROUTING_BASE)
1448     add_loopback_dijkstra(routing);
1449
1450   /* initialize graph indexes in nodes after graph has been built */
1451   nodes = xbt_graph_get_nodes(routing->route_graph);
1452
1453   xbt_dynar_foreach(nodes, cursor2, node) {
1454     graph_node_data_t data = xbt_graph_node_get_data(node);
1455     data->graph_id = cursor2;
1456   }
1457   
1458 }
1459
1460 /* ************************************************************************** */
1461 /* ******************************* NO ROUTING ******************************* */
1462
1463 /* Routing model structure */
1464 typedef struct {
1465   s_routing_component_t generic_routing;
1466 } s_routing_component_none_t,*routing_component_none_t;
1467
1468 /* Business methods */
1469 static route_extended_t none_get_route(routing_component_t rc, const char* src,const char* dst){
1470   return NULL;
1471 }
1472 static route_extended_t none_get_bypass_route(routing_component_t rc, const char* src,const char* dst){
1473   return NULL;
1474 }
1475 static void none_finalize(routing_component_t rc) {
1476   xbt_free(rc);
1477 }
1478
1479 static void none_set_processing_units(routing_component_t rc, const char* name) {}
1480 static void none_set_autonomous_system(routing_component_t rc, const char* name) {}
1481
1482 /* Creation routing model functions */
1483 static void* model_none_create(void) {
1484   routing_component_none_t new_component =  xbt_new0(s_routing_component_none_t,1);
1485   new_component->generic_routing.set_processing_units = none_set_processing_units;
1486   new_component->generic_routing.set_autonomous_system = none_set_autonomous_system;
1487   new_component->generic_routing.set_route = NULL;
1488   new_component->generic_routing.set_ASroute = NULL;
1489   new_component->generic_routing.set_bypassroute = NULL;
1490   new_component->generic_routing.get_route = none_get_route;
1491   new_component->generic_routing.get_bypass_route = none_get_bypass_route;
1492   new_component->generic_routing.finalize = none_finalize;
1493   return new_component;
1494 }
1495
1496 static void  model_none_load(void) {}
1497 static void  model_none_unload(void) {}
1498 static void  model_none_end(void) {}
1499
1500 /* ************************************************** */
1501 /* ********** PATERN FOR NEW ROUTING **************** */
1502
1503 /* The minimal configuration of a new routing model need the next functions,
1504  * also you need to set at the start of the file, the new model in the model
1505  * list. Remember keep the null ending of the list.
1506  */
1507 /*** Routing model structure ***/
1508 typedef struct {
1509   s_routing_component_t generic_routing;
1510   /* things that your routing model need */
1511 } s_routing_component_NEW_t,*routing_component_NEW_t;
1512
1513 /*** Parse routing model functions ***/
1514 static void model_NEW_set_processing_units(routing_component_t rc, const char* name) {}
1515 static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
1516 static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
1517 static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
1518 static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
1519
1520 /*** Business methods ***/
1521 static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
1522 static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
1523 static void NEW_finalize(routing_component_t rc) {}
1524
1525 // /*** Creation routing model functions ***/
1526 static void* model_NEW_create(void) {
1527   routing_component_full_t new_component =  xbt_new0(s_routing_component_full_t,1);
1528   new_component->generic_routing.set_processing_units = model_NEW_set_processing_units;
1529   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
1530   new_component->generic_routing.set_route = model_NEW_set_route;
1531   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
1532   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
1533   new_component->generic_routing.get_route = NEW_get_route;
1534   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
1535   new_component->generic_routing.finalize = NEW_finalize;
1536   /* initialization of internal structures */
1537   return new_component;
1538 } /* mandatory */
1539 static void  model_NEW_load(void) {}   /* mandatory */
1540 static void  model_NEW_unload(void) {} /* mandatory */
1541 static void  model_NEW_end(void) {}    /* mandatory */
1542
1543 /* ************************************************************************** */
1544 /* ************************* GENERIC PARSE FUNCTIONS ************************ */ 
1545
1546 static void generic_set_processing_units(routing_component_t rc, const char* name) {
1547   DEBUG1("Load process unit \"%s\"",name);
1548   model_type_t modeltype = rc->routing;
1549   int *id = xbt_new0(int,1); // xbt_malloc(sizeof(int)); ?
1550   xbt_dict_t _to_index;
1551   if(modeltype==&routing_models[SURF_MODEL_FULL])
1552     _to_index = ((routing_component_full_t)rc)->to_index;
1553   
1554   else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1555     _to_index = ((routing_component_floyd_t)rc)->to_index;
1556   
1557   else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
1558           modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE])
1559     _to_index = ((routing_component_dijkstra_t)rc)->to_index;
1560   
1561   else xbt_die("\"generic_set_processing_units\" not support");
1562   *id = xbt_dict_length(_to_index);
1563   xbt_dict_set(_to_index,name,id,xbt_free);
1564 }
1565
1566 static void generic_set_autonomous_system(routing_component_t rc, const char* name) {
1567   DEBUG1("Load Autonomous system \"%s\"",name);
1568   model_type_t modeltype = rc->routing;
1569   int *id = xbt_new0(int,1); // xbt_malloc(sizeof(int)); ?
1570   xbt_dict_t _to_index;
1571   if(modeltype==&routing_models[SURF_MODEL_FULL])
1572     _to_index = ((routing_component_full_t)rc)->to_index;
1573   
1574   else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1575     _to_index = ((routing_component_floyd_t)rc)->to_index;
1576   
1577   else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
1578           modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE])
1579     _to_index = ((routing_component_dijkstra_t)rc)->to_index;
1580   
1581   else xbt_die("\"generic_set_autonomous_system\" not support");
1582   *id = xbt_dict_length(_to_index);
1583   xbt_dict_set(_to_index,name,id,xbt_free);
1584 }
1585
1586 static void generic_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {
1587   DEBUG2("Load Route from \"%s\" to \"%s\"",src,dst);
1588   model_type_t modeltype = rc->routing;
1589   xbt_dict_t _parse_routes;
1590   xbt_dict_t _to_index;
1591   char *route_name;
1592   int *src_id, *dst_id;
1593   
1594   if(modeltype==&routing_models[SURF_MODEL_FULL]) {
1595     _parse_routes = ((routing_component_full_t)rc)->parse_routes;
1596     _to_index = ((routing_component_full_t)rc)->to_index;
1597     
1598   } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
1599     _parse_routes = ((routing_component_floyd_t)rc)->parse_routes;
1600     _to_index = ((routing_component_floyd_t)rc)->to_index;
1601     
1602   } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
1603           modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
1604     _parse_routes = ((routing_component_dijkstra_t)rc)->parse_routes;
1605     _to_index = ((routing_component_dijkstra_t)rc)->to_index;
1606   
1607   } else xbt_die("\"generic_set_route\" not support");
1608
1609   src_id = xbt_dict_get_or_null(_to_index, src);
1610   dst_id = xbt_dict_get_or_null(_to_index, dst);
1611   
1612   xbt_assert2(src_id&&dst_id,"Network elements %s or %s not found", src, dst);
1613   route_name = bprintf("%d#%d",*src_id,*dst_id);
1614   
1615   xbt_assert2(xbt_dynar_length(route->link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);   
1616   xbt_assert2(!xbt_dict_get_or_null(_parse_routes,route_name),
1617     "The route between \"%s\" and \"%s\" already exist",src,dst);
1618
1619   xbt_dict_set(_parse_routes, route_name, route, NULL);
1620   xbt_free(route_name);
1621 }
1622
1623 static void generic_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {
1624   DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",src,e_route->src_gateway,dst,e_route->dst_gateway);
1625   model_type_t modeltype = rc->routing;
1626   xbt_dict_t _parse_routes;
1627   xbt_dict_t _to_index;
1628   char *route_name;
1629   int *src_id, *dst_id;
1630   
1631   if(modeltype==&routing_models[SURF_MODEL_FULL]) {
1632     _parse_routes = ((routing_component_full_t)rc)->parse_routes;
1633     _to_index = ((routing_component_full_t)rc)->to_index;
1634     
1635   } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
1636     _parse_routes = ((routing_component_floyd_t)rc)->parse_routes;
1637     _to_index = ((routing_component_floyd_t)rc)->to_index;
1638     
1639   } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
1640           modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
1641     _parse_routes = ((routing_component_dijkstra_t)rc)->parse_routes;
1642     _to_index = ((routing_component_dijkstra_t)rc)->to_index;
1643   
1644   } else xbt_die("\"generic_set_route\" not support");
1645   
1646   src_id = xbt_dict_get_or_null(_to_index, src);
1647   dst_id = xbt_dict_get_or_null(_to_index, dst);
1648   
1649   xbt_assert2(src_id&&dst_id,"Network elements %s or %s not found", src, dst);
1650   route_name = bprintf("%d#%d",*src_id,*dst_id);
1651   
1652   xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);   
1653   xbt_assert4(!xbt_dict_get_or_null(_parse_routes,route_name),
1654     "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",src,e_route->src_gateway,dst,e_route->dst_gateway);
1655     
1656   xbt_dict_set(_parse_routes, route_name, e_route, NULL);
1657   xbt_free(route_name);
1658 }
1659
1660 static void generic_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {
1661   DEBUG2("Load bypassRoute from \"%s\" to \"%s\"",src,dst);
1662   model_type_t modeltype = rc->routing;
1663   xbt_dict_t dict_bypassRoutes;
1664   char *route_name;
1665   if(modeltype==&routing_models[SURF_MODEL_FULL]) {
1666     dict_bypassRoutes = ((routing_component_full_t)rc)->bypassRoutes;
1667   } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
1668     dict_bypassRoutes = ((routing_component_floyd_t)rc)->bypassRoutes;
1669   } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
1670           modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
1671     dict_bypassRoutes = ((routing_component_dijkstra_t)rc)->bypassRoutes;
1672   } else xbt_die("\"generic_set_bypassroute\" not support");
1673   route_name = bprintf("%s#%s",src,dst);
1674   xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);
1675   xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes,route_name),
1676     "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",src,e_route->src_gateway,dst,e_route->dst_gateway);
1677     
1678   route_extended_t new_e_route = generic_new_extended_route(SURF_ROUTING_RECURSIVE,e_route,0);
1679   xbt_dynar_free( &(e_route->generic_route.link_list) );
1680   xbt_free(e_route);
1681   
1682   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, (void(*)(void*))generic_free_extended_route ); 
1683   xbt_free(route_name);
1684 }
1685
1686 /* ************************************************************************** */
1687 /* *********************** GENERIC BUSINESS METHODS ************************* */
1688
1689 static route_extended_t generic_get_bypassroute(routing_component_t rc, const char* src, const char* dst) {
1690   model_type_t modeltype = rc->routing;
1691   xbt_dict_t dict_bypassRoutes;
1692   
1693   if(modeltype==&routing_models[SURF_MODEL_FULL]) {
1694     dict_bypassRoutes = ((routing_component_full_t)rc)->bypassRoutes;
1695   } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
1696     dict_bypassRoutes = ((routing_component_floyd_t)rc)->bypassRoutes;
1697   } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
1698           modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
1699     dict_bypassRoutes = ((routing_component_dijkstra_t)rc)->bypassRoutes;
1700   } else xbt_die("\"generic_set_bypassroute\" not support");
1701  
1702
1703   routing_component_t src_as, dst_as;
1704   int index_src, index_dst;
1705   xbt_dynar_t path_src = NULL;
1706   xbt_dynar_t path_dst = NULL;
1707   routing_component_t current = NULL;
1708   routing_component_t* current_src = NULL;
1709   routing_component_t* current_dst = NULL;
1710
1711   /* (1) find the as where the src and dst are located */
1712   src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
1713   dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst); 
1714   xbt_assert2(src_as&&dst_as, "Ask for route \"from\"(%s) or \"to\"(%s) no found",src,dst);
1715  
1716   /* (2) find the path to the root routing component */
1717   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1718   current = src_as; 
1719   while( current != NULL ) {
1720     xbt_dynar_push(path_src,&current);
1721     current = current->routing_father;
1722   }
1723   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1724   current = dst_as; 
1725   while( current != NULL ) {
1726     xbt_dynar_push(path_dst,&current);
1727     current = current->routing_father;
1728   }
1729
1730   /* (3) find the common father */
1731   index_src  = (path_src->used)-1;
1732   index_dst  = (path_dst->used)-1;
1733   current_src = xbt_dynar_get_ptr(path_src,index_src);
1734   current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
1735   while( index_src >= 0 && index_dst >= 0 && *current_src == *current_dst ) {
1736     routing_component_t *tmp_src,*tmp_dst;
1737     tmp_src = xbt_dynar_pop_ptr(path_src);
1738     tmp_dst = xbt_dynar_pop_ptr(path_dst);
1739     index_src--;
1740     index_dst--;
1741     current_src = xbt_dynar_get_ptr(path_src,index_src);
1742     current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
1743   }
1744   
1745   int max_index_src = (path_src->used)-1;
1746   int max_index_dst = (path_dst->used)-1;
1747   
1748   int max_index = max(max_index_src,max_index_dst);
1749   int i, max;
1750  
1751  route_extended_t e_route_bypass = NULL;
1752  
1753   for( max=0;max<=max_index;max++)
1754   {
1755     for(i=0;i<max;i++)
1756     {
1757       if( i <= max_index_src  && max <= max_index_dst ) {
1758         char* route_name = bprintf("%s#%s",
1759                                    (*(routing_component_t*)(xbt_dynar_get_ptr(path_src,i)))->name,
1760                                    (*(routing_component_t*)(xbt_dynar_get_ptr(path_dst,max)))->name);
1761         e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes,route_name);
1762         xbt_free(route_name);
1763       }
1764       if( e_route_bypass ) break;
1765       if( max <= max_index_src  && i <= max_index_dst ) {
1766         char* route_name = bprintf("%s#%s",
1767                                    (*(routing_component_t*)(xbt_dynar_get_ptr(path_src,max)))->name,
1768                                    (*(routing_component_t*)(xbt_dynar_get_ptr(path_dst,i)))->name);
1769         e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes,route_name);
1770         xbt_free(route_name);
1771       }
1772       if( e_route_bypass ) break;
1773     }
1774     
1775     if( e_route_bypass ) break;
1776      
1777     if( max <= max_index_src  && max <= max_index_dst ) {
1778         char* route_name = bprintf("%s#%s",
1779                                    (*(routing_component_t*)(xbt_dynar_get_ptr(path_src,max)))->name,
1780                                    (*(routing_component_t*)(xbt_dynar_get_ptr(path_dst,max)))->name);
1781         e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes,route_name);
1782         xbt_free(route_name);
1783       }
1784     if( e_route_bypass ) break;
1785   }
1786
1787   xbt_dynar_free(&path_src);
1788   xbt_dynar_free(&path_dst);
1789   
1790   route_extended_t new_e_route = NULL;
1791   
1792   if(e_route_bypass) {
1793     void* link;
1794     unsigned int cpt=0;
1795     new_e_route = xbt_new0(s_route_extended_t,1);
1796     new_e_route->src_gateway = e_route_bypass->src_gateway;
1797     new_e_route->dst_gateway = e_route_bypass->dst_gateway;
1798     new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1799     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1800       xbt_dynar_push(new_e_route->generic_route.link_list,&link);
1801     }
1802   }
1803
1804   return new_e_route;
1805 }
1806
1807 /* ************************************************************************** */
1808 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1809
1810 static route_extended_t generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy, void* data, int order) {
1811   
1812   char *link_name;
1813   route_extended_t e_route, new_e_route;
1814   route_t route;
1815   unsigned int cpt;
1816   xbt_dynar_t links, links_id;
1817   
1818   new_e_route = xbt_new0(s_route_extended_t,1);
1819   new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1820   new_e_route->src_gateway = NULL;
1821   new_e_route->dst_gateway = NULL;
1822
1823   xbt_assert0(hierarchy == SURF_ROUTING_BASE || hierarchy == SURF_ROUTING_RECURSIVE,
1824       "the hierarchy type is not defined");
1825   
1826   if(hierarchy == SURF_ROUTING_BASE ) {
1827     
1828     route = (route_t)data;
1829     links = route->link_list;
1830     
1831   } else if(hierarchy == SURF_ROUTING_RECURSIVE ) {
1832
1833     e_route = (route_extended_t)data;
1834     xbt_assert0(e_route->src_gateway&&e_route->dst_gateway,"bad gateway, is null");
1835     links = e_route->generic_route.link_list;
1836     
1837     /* remeber not erase the gateway names */
1838     new_e_route->src_gateway = e_route->src_gateway;
1839     new_e_route->dst_gateway = e_route->dst_gateway;
1840   }
1841   
1842   links_id = new_e_route->generic_route.link_list;
1843   
1844   xbt_dynar_foreach(links, cpt, link_name) {
1845     
1846     void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1847     if (link)
1848     {
1849       if( order )
1850         xbt_dynar_push(links_id,&link);
1851       else
1852         xbt_dynar_unshift(links_id,&link);
1853     }
1854     else
1855       THROW1(mismatch_error,0,"Link %s not found", link_name);
1856   }
1857  
1858   return new_e_route;
1859 }
1860
1861 static void generic_free_extended_route(route_extended_t e_route) {
1862   if(e_route) {
1863     xbt_dynar_free(&(e_route->generic_route.link_list));
1864     if(e_route->src_gateway) xbt_free(e_route->src_gateway);
1865     if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
1866     xbt_free(e_route);
1867   }
1868 }
1869
1870 static routing_component_t generic_as_exist(routing_component_t find_from, routing_component_t to_find) {
1871   xbt_dict_cursor_t cursor = NULL;
1872   char *key;
1873   int found=0;
1874   routing_component_t elem;
1875   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1876     if( to_find == elem || generic_as_exist(elem,to_find) ){
1877       found=1;
1878       break;
1879     }
1880   }
1881   if(found) return to_find;
1882   return NULL;
1883 }
1884
1885 static routing_component_t generic_autonomous_system_exist(routing_component_t rc, char* element) {
1886   routing_component_t element_as, result, elem;
1887   xbt_dict_cursor_t cursor = NULL;
1888   char *key;
1889   element_as = xbt_dict_get_or_null(global_routing->where_network_elements,element);
1890   result = ((routing_component_t)-1);
1891   if(element_as!=rc)
1892     result = generic_as_exist(rc,element_as);
1893   
1894   if(result)
1895   {
1896     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1897       if( !strcmp(elem->name,element) ) return element_as;
1898     }
1899   }
1900   return NULL;
1901 }
1902
1903 static routing_component_t generic_processing_units_exist(routing_component_t rc, char* element) {
1904   routing_component_t element_as;
1905   element_as = xbt_dict_get_or_null(global_routing->where_network_elements,element);
1906   if(element_as==rc) return element_as;
1907   return generic_as_exist(rc,element_as);
1908 }
1909
1910 static void generic_src_dst_check(routing_component_t rc, const char* src, const char* dst) {
1911   
1912   routing_component_t src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
1913   routing_component_t dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst);
1914    
1915   xbt_assert3(src_as != NULL && dst_as  != NULL, 
1916       "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",src,dst,rc->name);
1917   xbt_assert4(src_as == dst_as,
1918       "The src(%s in %s) and dst(%s in %s) are in differents AS",src,src_as->name,dst,dst_as->name);
1919   xbt_assert2(rc == dst_as,
1920       "The routing component of src and dst is not the same as the network elements belong (%s==%s)",rc->name,dst_as->name);
1921 }
1922         
1923 ////////////////////////////////////////////////////////////////////////////////
1924 // HERE FINISH THE NEW CODE
1925 ////////////////////////////////////////////////////////////////////////////////
1926
1927 //...... DEBUG ONLY .... //
1928 static void print_tree(routing_component_t rc) {
1929   printf("(%s %s)\n",rc->name,rc->routing->name);
1930   printf("  ");
1931   xbt_dict_cursor_t cursor = NULL;
1932   char *key;
1933   int *val;  
1934   if( rc->routing == &(routing_models[SURF_MODEL_FULL]) )
1935   { xbt_dict_foreach(((routing_component_full_t)rc)->to_index, cursor, key, val) { printf("<%s-%d> ",key,*val); } }
1936   else if( rc->routing == &(routing_models[SURF_MODEL_FLOYD]) )
1937   { xbt_dict_foreach(((routing_component_floyd_t)rc)->to_index, cursor, key, val) { printf("<%s-%d> ",key,*val); } 
1938   }
1939   else
1940       xbt_assert0(0, "Invalid model for call \"print_tree\"");
1941   printf("\n");  
1942   routing_component_t elem;
1943   xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
1944     printf("<--\n");  
1945     print_tree(elem);
1946     printf("-->\n");
1947   }
1948 }
1949
1950 //...... DEBUG ONLY .... //
1951 static void print_global() {
1952   xbt_dict_cursor_t cursor = NULL;
1953   char *key;
1954   routing_component_t elem;  
1955   xbt_dict_foreach(global_routing->where_network_elements, cursor, key, elem) { printf("<%s>\n",key); }
1956 }
1957
1958 //...... DEBUG ONLY .... //
1959 static void print_AS_start(void) { printf("AS!!! %s y se rutea \"%s\"\n",A_surfxml_AS_id,A_surfxml_AS_routing); }
1960 static void print_AS_end(void)   { printf("AS!!! %s\n",A_surfxml_AS_id); }
1961 static void print_host(void)     { printf("host!!! %s\n",A_surfxml_host_id); }
1962 static void print_link(void)     { printf("link!!! %s\n",A_surfxml_link_id); }
1963 static void print_route(void)    { printf("route!!! %s a %s\n",A_surfxml_route_src,A_surfxml_route_dst); }
1964 static void print_ctn(void)      { printf("ctn!!! %s\n",A_surfxml_link_c_ctn_id); }
1965
1966 //...... DEBUG ONLY .... //
1967 static void DEBUG_exit(void) {
1968   
1969 //   printf("-------- print tree elements -----\n");
1970 //   print_tree(global_routing->root);
1971 //   printf("----------------------------------\n\n");
1972 //   
1973 //   printf("-------- network_elements --------\n");
1974 //   print_global();
1975 //   printf("----------------------------------\n\n");
1976   
1977 //   const char* names[] = { "11.A","11.B","11.C","121.A","121.B","122.A","13.A","13.B"};
1978 //   int i,j,total = 8;
1979
1980 //   printf("-- print all the example routes --\n");
1981 //   const char* names[] = { "11.A","11.B","11.C"};
1982 //   int i,j,total = 3;
1983
1984 //   const char* names[] = { "141.A","141.B","143.A","143.B"};
1985 //   int i,j,total = 4;
1986
1987 //   const char* names[] = { "15.A","15.B","15.C" };
1988 //   int i,j,total = 3;
1989
1990 //   const char* names[] = { "142.A","142.B","142.C", "11.A","11.B","11.C","141.A","141.B","143.A","143.B","11.A","11.B","11.C","121.A","121.B","122.A","13.A","13.B"};
1991 //   int i,j,total = 3+4+8+3;
1992
1993 //   const char* names[] = { "142.A","142.B","142.C" };
1994 //   int i,j,total = 3;
1995
1996    const char* names[] = { "11.A","11.B","11.C",
1997                            "121.A","121.B",
1998                            "122.A",
1999                            "13.A","13.B",
2000                            "141.A","141.B",
2001                            "142.A","142.B","142.C",
2002                            "143.A","143.B",
2003                            "15.A","15.B","15.C"};
2004    unsigned int i,j,total = 3+2+1+2+2+3+2+3;
2005
2006 // const char* names[] = { "141.A","143.A" };
2007 // int i,j,total = 2;
2008    
2009   printf("-- print all the example routes --\n");
2010   xbt_dynar_t links;
2011   void* link;
2012   for(i=0;i<total;i++) {
2013     for(j=0;j<total;j++) {
2014       printf("route from %s to %s >>>\n ",names[i],names[j]);
2015       links = (*(global_routing->get_route))(names[i],names[j]);
2016       printf(">>>");
2017       unsigned int cpt=0;
2018       xbt_dynar_foreach(links, cpt, link) {
2019         s_surf_resource_t* generic_resource = link;
2020         printf(" %s",generic_resource->name);
2021       }
2022       printf("\n");
2023     }
2024   }
2025   printf("----------------------------------\n\n");
2026   
2027   printf("---------- call finalize ---------\n");
2028   (*(global_routing->finalize))();
2029   printf("----------------------------------\n");
2030   
2031   exit(0); 
2032 }
2033
2034 ////////////////////////////////////////////////////////////////////////////////
2035 // HERE END THE NEW CODE
2036 ////////////////////////////////////////////////////////////////////////////////
2037
2038
2039 // /* ************************************************************************** */
2040 // /* *************************** FULL ROUTING ********************************* */
2041 // typedef struct {
2042 //   s_routing_t generic_routing;
2043 //   xbt_dynar_t *routing_table;
2044 //   void *loopback;
2045 //   size_t size_of_link;
2046 // } s_routing_full_t,*routing_full_t;
2047 // 
2048 // #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
2049 // #define HOST2ROUTER(id) ((id)+(2<<29))
2050 // #define ROUTER2HOST(id) ((id)-(2>>29))
2051 // #define ISROUTER(id) ((id)>=(2<<29))
2052 // 
2053 // /*
2054 //  * Free the onelink routes
2055 //  */
2056 // static void onelink_route_elem_free(void *e) {
2057 //   s_onelink_t tmp = (s_onelink_t)e;
2058 //   if(tmp) {
2059 //     free(tmp);
2060 //   }
2061 // }
2062 // 
2063 // /*
2064 //  * Parsing
2065 //  */
2066 // static void routing_full_parse_Shost(void) {
2067 //   int *val = xbt_malloc(sizeof(int));
2068 //   DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
2069 //   *val = used_routing->host_count++;
2070 //   xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
2071 // #ifdef HAVE_TRACING
2072 //   TRACE_surf_host_define_id (A_surfxml_host_id, *val);
2073 // #endif
2074 // }
2075 // 
2076 // static void routing_full_parse_Srouter(void) {
2077 //      int *val = xbt_malloc(sizeof(int));
2078 //   DEBUG3("Seen router %s (%d -> #%d)",A_surfxml_router_id,used_routing->router_count,
2079 //              HOST2ROUTER(used_routing->router_count));
2080 //   *val = HOST2ROUTER(used_routing->router_count++);
2081 //   xbt_dict_set(used_routing->host_id,A_surfxml_router_id,val,xbt_free);
2082 // #ifdef HAVE_TRACING
2083 //   TRACE_surf_host_define_id (A_surfxml_host_id, *val);
2084 //   TRACE_surf_host_declaration (A_surfxml_host_id, 0);
2085 // #endif
2086 // }
2087 // 
2088 // static int src_id = -1;
2089 // static int dst_id = -1;
2090 // static void routing_full_parse_Sroute_set_endpoints(void)
2091 // {
2092 //   src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
2093 //   dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
2094 //   DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
2095 //   route_action = A_surfxml_route_action;
2096 // }
2097 // 
2098 // static void routing_full_parse_Eroute(void)
2099 // {
2100 //   char *name;
2101 //   if (src_id != -1 && dst_id != -1) {
2102 //     name = bprintf("%x#%x", src_id, dst_id);
2103 //     manage_route(route_table, name, route_action, 0);
2104 //     free(name);
2105 //   }
2106 // }
2107 // 
2108 // /* Cluster tag functions */
2109 // 
2110 // static void routing_full_parse_change_cpu_data(const char *hostName,
2111 //                                   const char *surfxml_host_power,
2112 //                                   const char *surfxml_host_availability,
2113 //                                   const char *surfxml_host_availability_file,
2114 //                                   const char *surfxml_host_state_file)
2115 // {
2116 //   int AX_ptr = 0;
2117 // 
2118 //   SURFXML_BUFFER_SET(host_id, hostName);
2119 //   SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower */ );
2120 //   SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
2121 //   SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
2122 //   SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
2123 // }
2124 // 
2125 // static void routing_full_parse_change_link_data(const char *linkName,
2126 //                                    const char *surfxml_link_bandwidth,
2127 //                                    const char *surfxml_link_bandwidth_file,
2128 //                                    const char *surfxml_link_latency,
2129 //                                    const char *surfxml_link_latency_file,
2130 //                                    const char *surfxml_link_state_file)
2131 // {
2132 //   int AX_ptr = 0;
2133 // 
2134 //   SURFXML_BUFFER_SET(link_id, linkName);
2135 //   SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
2136 //   SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
2137 //   SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
2138 //   SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
2139 //   SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
2140 // }
2141 // 
2142 // static void routing_full_parse_Scluster(void)
2143 // {
2144 //   static int AX_ptr = 0;
2145 // 
2146 //   char *cluster_id = A_surfxml_cluster_id;
2147 //   char *cluster_prefix = A_surfxml_cluster_prefix;
2148 //   char *cluster_suffix = A_surfxml_cluster_suffix;
2149 //   char *cluster_radical = A_surfxml_cluster_radical;
2150 //   char *cluster_power = A_surfxml_cluster_power;
2151 //   char *cluster_bw = A_surfxml_cluster_bw;
2152 //   char *cluster_lat = A_surfxml_cluster_lat;
2153 //   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
2154 //   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
2155 //   char *backbone_name;
2156 //   unsigned int it1,it2;
2157 //   char *name1,*name2;
2158 //   xbt_dynar_t names = NULL;
2159 //   surfxml_bufferstack_push(1);
2160 // 
2161 //   /* Make set a set to parse the prefix/suffix/radical into a neat list of names */
2162 //   DEBUG4("Make <set id='%s' prefix='%s' suffix='%s' radical='%s'>",
2163 //       cluster_id,cluster_prefix,cluster_suffix,cluster_radical);
2164 //   SURFXML_BUFFER_SET(set_id, cluster_id);
2165 //   SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
2166 //   SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
2167 //   SURFXML_BUFFER_SET(set_radical, cluster_radical);
2168 // 
2169 //   SURFXML_START_TAG(set);
2170 //   SURFXML_END_TAG(set);
2171 // 
2172 //   names = xbt_dict_get(set_list,cluster_id);
2173 // 
2174 //   xbt_dynar_foreach(names,it1,name1) {
2175 //     /* create the host */
2176 //     routing_full_parse_change_cpu_data(name1, cluster_power, "1.0", "", "");
2177 //     A_surfxml_host_state = A_surfxml_host_state_ON;
2178 // 
2179 //     SURFXML_START_TAG(host);
2180 //     SURFXML_END_TAG(host);
2181 // 
2182 //     /* Here comes the link */
2183 //     routing_full_parse_change_link_data(name1, cluster_bw, "", cluster_lat, "", "");
2184 //     A_surfxml_link_state = A_surfxml_link_state_ON;
2185 //     A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
2186 // 
2187 //     SURFXML_START_TAG(link);
2188 //     SURFXML_END_TAG(link);
2189 //   }
2190 // 
2191 //   /* Make backbone link */
2192 //   backbone_name = bprintf("%s_bb", cluster_id);
2193 //   routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
2194 //                          "");
2195 //   A_surfxml_link_state = A_surfxml_link_state_ON;
2196 //   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
2197 // 
2198 //   SURFXML_START_TAG(link);
2199 //   SURFXML_END_TAG(link);
2200 // 
2201 //   /* And now the internal routes */
2202 //   xbt_dynar_foreach(names,it1,name1) {
2203 //     xbt_dynar_foreach(names,it2,name2) {
2204 //       if (strcmp(name1,name2)) {
2205 //         A_surfxml_route_action = A_surfxml_route_action_POSTPEND;
2206 //         SURFXML_BUFFER_SET(route_src,name1);
2207 //         SURFXML_BUFFER_SET(route_dst,name2);
2208 //         SURFXML_START_TAG(route); {
2209 //           /* FIXME: name1 link is added by error about 20 lines below, so don't add it here
2210 //           SURFXML_BUFFER_SET(link_c_ctn_id, name1);
2211 //           SURFXML_START_TAG(link_c_ctn);
2212 //           SURFXML_END_TAG(link_c_ctn);
2213 //            */
2214 //           SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
2215 //           SURFXML_START_TAG(link_c_ctn);
2216 //           SURFXML_END_TAG(link_c_ctn);
2217 // 
2218 //           SURFXML_BUFFER_SET(link_c_ctn_id, name2);
2219 //           SURFXML_START_TAG(link_c_ctn);
2220 //           SURFXML_END_TAG(link_c_ctn);
2221 // 
2222 //         } SURFXML_END_TAG(route);
2223 //       }
2224 //     }
2225 //   }
2226 // 
2227 //   /* Make route multi with the outside world, i.e. cluster->$* */
2228 // 
2229 //   /* FIXME
2230 //    * This also adds an elements to the routes within the cluster,
2231 //    * and I guess it's wrong, but since this element is commented out in the above
2232 //    * code creating the internal routes, we're good.
2233 //    * To fix it, I'd say that we need a way to understand "$*-${cluster_id}" as "whole world, but the guys in that cluster"
2234 //    * But for that, we need to install a real expression parser for src/dst attributes
2235 //    *
2236 //    * FIXME
2237 //    * This also adds a dumb element (the private link) in place of the loopback. Then, since
2238 //    * the loopback is added only if no link to self already exist, this fails.
2239 //    * That's really dumb.
2240 //    *
2241 //    * FIXME
2242 //    * It seems to me that it does not add the backbone to the path to outside world...
2243 //    */
2244 //   SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
2245 //   SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
2246 //   A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
2247 //   A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_PREPEND;
2248 // 
2249 //   SURFXML_START_TAG(route_c_multi);
2250 // 
2251 //   SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
2252 // 
2253 //   SURFXML_START_TAG(link_c_ctn);
2254 //   SURFXML_END_TAG(link_c_ctn);
2255 // 
2256 //   SURFXML_END_TAG(route_c_multi);
2257 // 
2258 //   free(backbone_name);
2259 // 
2260 //   /* Restore buff */
2261 //   surfxml_bufferstack_pop(1);
2262 // }
2263 // 
2264 // 
2265 // static void routing_full_parse_end(void) {
2266 //   routing_full_t routing = (routing_full_t) used_routing;
2267 //   int nb_link = 0;
2268 //   unsigned int cpt = 0;
2269 //   xbt_dict_cursor_t cursor = NULL;
2270 //   char *key, *data, *end;
2271 //   const char *sep = "#";
2272 //   xbt_dynar_t links, keys;
2273 //   char *link_name = NULL;
2274 //   int i,j;
2275 // 
2276 //   int host_count = routing->generic_routing.host_count;
2277 // 
2278 //   /* Create the routing table */
2279 //   routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
2280 //   for (i=0;i<host_count;i++)
2281 //     for (j=0;j<host_count;j++)
2282 //       ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
2283 // 
2284 //   /* Put the routes in position */
2285 //   xbt_dict_foreach(route_table, cursor, key, data) {
2286 //     nb_link = 0;
2287 //     links = (xbt_dynar_t) data;
2288 //     keys = xbt_str_split_str(key, sep);
2289 // 
2290 //     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
2291 //     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
2292 //     xbt_dynar_free(&keys);
2293 // 
2294 //     if(xbt_dynar_length(links) == 1){
2295 //       s_onelink_t new_link = (s_onelink_t) xbt_malloc0(sizeof(s_onelink));
2296 //       new_link->src_id = src_id;
2297 //       new_link->dst_id = dst_id;
2298 //       link_name = xbt_dynar_getfirst_as(links, char*);
2299 //       new_link->link_ptr = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2300 //       DEBUG3("Adding onelink route from (#%d) to (#%d), link_name %s",src_id, dst_id, link_name);
2301 //       xbt_dict_set(onelink_routes, link_name, (void *)new_link, onelink_route_elem_free);
2302 // #ifdef HAVE_TRACING
2303 //       TRACE_surf_link_save_endpoints (link_name, src_id, dst_id);
2304 // #endif
2305 //     }
2306 // 
2307 //     if(ISROUTER(src_id) || ISROUTER(dst_id)) {
2308 //                              DEBUG2("There is route with a router here: (%d ,%d)",src_id,dst_id);
2309 //                              /* Check there is only one link in the route and store the information */
2310 //                              continue;
2311 //     }
2312 // 
2313 //     DEBUG4("Handle %d %d (from %d hosts): %ld links",
2314 //         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
2315 //     xbt_dynar_foreach(links, cpt, link_name) {
2316 //       void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2317 //       if (link)
2318 //         xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
2319 //       else
2320 //         THROW1(mismatch_error,0,"Link %s not found", link_name);
2321 //     }
2322 //   }
2323 // 
2324 //   /* Add the loopback if needed */
2325 //   for (i = 0; i < host_count; i++)
2326 //     if (!xbt_dynar_length(ROUTE_FULL(i, i)))
2327 //       xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
2328 // 
2329 //   /* Shrink the dynar routes (save unused slots) */
2330 //   for (i=0;i<host_count;i++)
2331 //     for (j=0;j<host_count;j++)
2332 //       xbt_dynar_shrink(ROUTE_FULL(i,j),0);
2333 // }
2334 // 
2335 // /*
2336 //  * Business methods
2337 //  */
2338 // static xbt_dynar_t routing_full_get_route(int src,int dst) {
2339 //   xbt_assert0(!(ISROUTER(src) || ISROUTER(dst)), "Ask for route \"from\" or \"to\" a router node");
2340 //   return ROUTE_FULL(src,dst);
2341 // }
2342 // 
2343 // static xbt_dict_t routing_full_get_onelink_routes(void){
2344 //   return onelink_routes;
2345 // }
2346 // 
2347 // static int routing_full_is_router(int id){
2348 //      return ISROUTER(id);
2349 // }
2350 // 
2351 // static void routing_full_finalize(void) {
2352 //   routing_full_t routing = (routing_full_t)used_routing;
2353 //   int i,j;
2354 // 
2355 //   if (routing) {
2356 //     for (i = 0; i < used_routing->host_count; i++)
2357 //       for (j = 0; j < used_routing->host_count; j++)
2358 //         xbt_dynar_free(&ROUTE_FULL(i, j));
2359 //     free(routing->routing_table);
2360 //     xbt_dict_free(&used_routing->host_id);
2361 //     xbt_dict_free(&onelink_routes);
2362 //     free(routing);
2363 //     routing=NULL;
2364 //   }
2365 // }
2366 // 
2367 // static void routing_model_full_create(size_t size_of_link,void *loopback) {
2368 //   /* initialize our structure */
2369 //   routing_full_t routing = xbt_new0(s_routing_full_t,1);
2370 //   routing->generic_routing.name = "Full";
2371 //   routing->generic_routing.host_count = 0;
2372 //   routing->generic_routing.get_route = routing_full_get_route;
2373 //   routing->generic_routing.get_onelink_routes = routing_full_get_onelink_routes;
2374 //   routing->generic_routing.is_router = routing_full_is_router;
2375 //   routing->generic_routing.finalize = routing_full_finalize;
2376 // 
2377 //   routing->size_of_link = size_of_link;
2378 //   routing->loopback = loopback;
2379 // 
2380 //   /* Set it in position */
2381 //   used_routing = (routing_t) routing;
2382 // 
2383 //   /* Set the dict for onehop routes */
2384 //   onelink_routes =  xbt_dict_new();
2385 // 
2386 //   routing->generic_routing.host_id = xbt_dict_new();
2387 //   
2388 //   /* Setup the parsing callbacks we need */
2389 // //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2390 // //   surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
2391 // //   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
2392 // //   surfxml_add_callback(STag_surfxml_route_cb_list, &routing_full_parse_Sroute_set_endpoints);
2393 // //   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
2394 // //   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_full_parse_Scluster);
2395 // 
2396 // //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2397 // //   surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
2398 //   
2399 // }
2400
2401 /* ************************************************************************** */
2402
2403 // static void routing_shortest_path_parse_Scluster(void)
2404 // {
2405 //   static int AX_ptr = 0;
2406 // 
2407 //   char *cluster_id = A_surfxml_cluster_id;
2408 //   char *cluster_prefix = A_surfxml_cluster_prefix;
2409 //   char *cluster_suffix = A_surfxml_cluster_suffix;
2410 //   char *cluster_radical = A_surfxml_cluster_radical;
2411 //   char *cluster_power = A_surfxml_cluster_power;
2412 //   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
2413 //   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
2414 //   char *backbone_name;
2415 // 
2416 //   surfxml_bufferstack_push(1);
2417 // 
2418 //   /* Make set */
2419 //   SURFXML_BUFFER_SET(set_id, cluster_id);
2420 //   SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
2421 //   SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
2422 //   SURFXML_BUFFER_SET(set_radical, cluster_radical);
2423 // 
2424 //   SURFXML_START_TAG(set);
2425 //   SURFXML_END_TAG(set);
2426 // 
2427 //   /* Make foreach */
2428 //   SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
2429 // 
2430 //   SURFXML_START_TAG(foreach);
2431 // 
2432 //   /* Make host for the foreach */
2433 //   routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
2434 //   A_surfxml_host_state = A_surfxml_host_state_ON;
2435 // 
2436 //   SURFXML_START_TAG(host);
2437 //   SURFXML_END_TAG(host);
2438 // 
2439 //   SURFXML_END_TAG(foreach);
2440 // 
2441 //   /* Make backbone link */
2442 //   backbone_name = bprintf("%s_bb", cluster_id);
2443 //   routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
2444 //                          "");
2445 //   A_surfxml_link_state = A_surfxml_link_state_ON;
2446 //   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
2447 // 
2448 //   SURFXML_START_TAG(link);
2449 //   SURFXML_END_TAG(link);
2450 // 
2451 //   free(backbone_name);
2452 // 
2453 //   /* Restore buff */
2454 //   surfxml_bufferstack_pop(1);
2455 // }
2456 // 
2457 // /* ************************************************************************** */
2458 // /* *************************** FLOYD ROUTING ********************************* */
2459 // typedef struct {
2460 //   s_routing_t generic_routing;
2461 //   int *predecessor_table;
2462 //   void** link_table;
2463 //   xbt_dynar_t last_route;
2464 //   void *loopback;
2465 //   size_t size_of_link;
2466 // } s_routing_floyd_t,*routing_floyd_t;
2467 // 
2468 // #define FLOYD_COST(i,j) cost_table[(i)+(j)*(used_routing)->host_count]
2469 // #define FLOYD_PRED(i,j) ((routing_floyd_t)used_routing)->predecessor_table[(i)+(j)*(used_routing)->host_count]
2470 // #define FLOYD_LINK(i,j) ((routing_floyd_t)used_routing)->link_table[(i)+(j)*(used_routing)->host_count]
2471 // 
2472 // static void routing_floyd_parse_end(void) {
2473 // 
2474 //   routing_floyd_t routing = (routing_floyd_t) used_routing;
2475 //   int nb_link = 0;
2476 //   void* link_list = NULL;
2477 //   double * cost_table;
2478 //   xbt_dict_cursor_t cursor = NULL;
2479 //   char *key,*data, *end;
2480 //   const char *sep = "#";
2481 //   xbt_dynar_t links, keys;
2482 // 
2483 //   unsigned int i,j;
2484 //   unsigned int a,b,c;
2485 //   int host_count = routing->generic_routing.host_count;
2486 //   char * link_name = NULL;
2487 //   void * link = NULL;
2488 // 
2489 //   /* Create Cost, Predecessor and Link tables */
2490 //   cost_table = xbt_new0(double, host_count * host_count); //link cost from host to host
2491 //   routing->predecessor_table = xbt_new0(int, host_count*host_count); //predecessor host numbers
2492 //   routing->link_table = xbt_new0(void*,host_count*host_count); //actual link between src and dst
2493 //   routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
2494 // 
2495 //   /* Initialize costs and predecessors*/
2496 //   for(i = 0; i<host_count;i++)
2497 //     for(j = 0; j<host_count;j++) {
2498 //         FLOYD_COST(i,j) = DBL_MAX;
2499 //         FLOYD_PRED(i,j) = -1;
2500 //     }
2501 // 
2502 //    /* Put the routes in position */
2503 //   xbt_dict_foreach(route_table, cursor, key, data) {
2504 //     nb_link = 0;
2505 //     links = (xbt_dynar_t)data;
2506 //     keys = xbt_str_split_str(key, sep);
2507 // 
2508 //     
2509 //     src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
2510 //     dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
2511 //     xbt_dynar_free(&keys);
2512 //  
2513 //     DEBUG4("Handle %d %d (from %d hosts): %ld links",
2514 //         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
2515 //     xbt_assert3(xbt_dynar_length(links) == 1, "%ld links in route between host %d and %d, should be 1", xbt_dynar_length(links), src_id, dst_id);
2516 //     
2517 //     link_name = xbt_dynar_getfirst_as(links, char*);
2518 //     link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2519 // 
2520 //     if (link)
2521 //       link_list = link;
2522 //     else
2523 //       THROW1(mismatch_error,0,"Link %s not found", link_name);
2524 //     
2525 // 
2526 //     FLOYD_LINK(src_id,dst_id) = link_list;
2527 //     FLOYD_PRED(src_id, dst_id) = src_id;
2528 // 
2529 //     //link cost
2530 //     FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
2531 // 
2532 //   }
2533 // 
2534 //     /* Add the loopback if needed */
2535 //   for (i = 0; i < host_count; i++)
2536 //     if (!FLOYD_PRED(i, i)) {
2537 //       FLOYD_PRED(i, i) = i;
2538 //       FLOYD_COST(i, i) = 1;
2539 //       FLOYD_LINK(i, i) = routing->loopback;
2540 //     }
2541 // 
2542 // 
2543 //   //Calculate path costs 
2544 // 
2545 //   for(c=0;c<host_count;c++) {
2546 //     for(a=0;a<host_count;a++) {
2547 //       for(b=0;b<host_count;b++) {
2548 //         if(FLOYD_COST(a,c) < DBL_MAX && FLOYD_COST(c,b) < DBL_MAX) {
2549 //           if(FLOYD_COST(a,b) == DBL_MAX || (FLOYD_COST(a,c)+FLOYD_COST(c,b) < FLOYD_COST(a,b))) {
2550 //             FLOYD_COST(a,b) = FLOYD_COST(a,c)+FLOYD_COST(c,b);
2551 //             FLOYD_PRED(a,b) = FLOYD_PRED(c,b);
2552 //           }
2553 //         }
2554 //       }
2555 //     }
2556 //   }
2557 // 
2558 //   //cleanup
2559 //   free(cost_table);
2560 // }
2561 // 
2562 // /*
2563 //  * Business methods
2564 //  */
2565 // static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
2566 // 
2567 //   routing_floyd_t routing = (routing_floyd_t) used_routing;
2568 // 
2569 //   int pred = dst_id;
2570 //   int prev_pred = 0;
2571 // 
2572 //   xbt_dynar_reset(routing->last_route);
2573 // 
2574 //   do {
2575 //     prev_pred = pred;
2576 //     pred = FLOYD_PRED(src_id, pred);
2577 // 
2578 //     if(pred == -1) // if no pred in route -> no route to host
2579 //         break;
2580 // 
2581 //     xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
2582 // 
2583 //   } while(pred != src_id);
2584 // 
2585 //   xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
2586 // 
2587 //   return routing->last_route;
2588 // }
2589 // 
2590 // static void routing_floyd_finalize(void) {
2591 //   routing_floyd_t routing = (routing_floyd_t)used_routing;
2592 // 
2593 //   if (routing) {
2594 //     free(routing->link_table);
2595 //     free(routing->predecessor_table);
2596 //     xbt_dynar_free(&routing->last_route);
2597 //     xbt_dict_free(&used_routing->host_id);
2598 //     free(routing);
2599 //     routing=NULL;
2600 //   }
2601 // }
2602 // 
2603 // static xbt_dict_t routing_floyd_get_onelink_routes(void){
2604 //   xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model Floyd");
2605 // }
2606 // 
2607 // static int routing_floyd_is_router(int id){
2608 //   xbt_assert0(0,"The get_is_router feature is not supported in routing model Floyd");
2609 // }
2610 // 
2611 // static void routing_model_floyd_create(size_t size_of_link,void *loopback) {
2612 //   /* initialize our structure */
2613 //   routing_floyd_t routing = xbt_new0(s_routing_floyd_t,1);
2614 //   routing->generic_routing.name = "Floyd";
2615 //   routing->generic_routing.host_count = 0;
2616 //   routing->generic_routing.host_id = xbt_dict_new();
2617 //   routing->generic_routing.get_route = routing_floyd_get_route;
2618 //   routing->generic_routing.get_onelink_routes = routing_floyd_get_onelink_routes;
2619 //   routing->generic_routing.is_router = routing_floyd_is_router;
2620 //   routing->generic_routing.finalize = routing_floyd_finalize;
2621 //   routing->size_of_link = size_of_link;
2622 //   routing->loopback = loopback;
2623 // 
2624 //   /* Set it in position */
2625 //   used_routing = (routing_t) routing;
2626 //   
2627 //   /* Setup the parsing callbacks we need */
2628 //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2629 //   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_floyd_parse_end);
2630 //   surfxml_add_callback(STag_surfxml_route_cb_list, 
2631 //       &routing_full_parse_Sroute_set_endpoints);
2632 //   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
2633 //   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
2634 //   
2635 // }
2636 // 
2637 // /* ************************************************************************** */
2638 // /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
2639 // typedef struct {
2640 //   s_routing_t generic_routing;
2641 //   xbt_graph_t route_graph;
2642 //   xbt_dict_t graph_node_map;
2643 //   xbt_dict_t route_cache;
2644 //   xbt_dynar_t last_route;
2645 //   int cached;
2646 //   void *loopback;
2647 //   size_t size_of_link;
2648 // } s_routing_dijkstra_t,*routing_dijkstra_t;
2649 // 
2650 // 
2651 // typedef struct graph_node_data {
2652 //   int id; 
2653 //   int graph_id; //used for caching internal graph id's
2654 // } s_graph_node_data_t, * graph_node_data_t;
2655 // 
2656 // typedef struct graph_node_map_element {
2657 //   xbt_node_t node;
2658 // } s_graph_node_map_element_t, * graph_node_map_element_t;
2659 // 
2660 // typedef struct route_cache_element {
2661 //   int * pred_arr;
2662 //   int size;
2663 // } s_route_cache_element_t, * route_cache_element_t;  
2664 // 
2665 // /*
2666 //  * Free functions
2667 //  */
2668 // static void route_cache_elem_free(void *e) {
2669 //   route_cache_element_t elm=(route_cache_element_t)e;
2670 // 
2671 //   if (elm) {
2672 //     free(elm->pred_arr);
2673 //     free(elm);
2674 //   }
2675 // }
2676 // 
2677 // static void graph_node_map_elem_free(void *e) {
2678 //   graph_node_map_element_t elm = (graph_node_map_element_t)e;
2679 // 
2680 //   if(elm) {
2681 //     free(elm);
2682 //   }
2683 // }
2684 // 
2685 // /*
2686 //  * Utility functions
2687 // */
2688 // static xbt_node_t route_graph_new_node(int id, int graph_id) {
2689 //   xbt_node_t node = NULL;
2690 //   graph_node_data_t data = NULL;
2691 //   graph_node_map_element_t elm = NULL;
2692 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2693 // 
2694 //   data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
2695 //   data->id = id;
2696 //   data->graph_id = graph_id;
2697 //   node = xbt_graph_new_node(routing->route_graph, data);
2698 // 
2699 //   elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
2700 //   elm->node = node;
2701 //   xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
2702 // 
2703 //   return node;
2704 // }
2705 // 
2706 // static graph_node_map_element_t graph_node_map_search(int id) {
2707 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2708 // 
2709 //   graph_node_map_element_t elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&id), sizeof(int));
2710 // 
2711 //   return elm;
2712 // }
2713 // 
2714 // /*
2715 //  * Parsing
2716 //  */
2717 // static void route_new_dijkstra(int src_id, int dst_id, void* link) {
2718 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2719 // 
2720 //   xbt_node_t src = NULL;
2721 //   xbt_node_t dst = NULL;
2722 //   graph_node_map_element_t src_elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&src_id), sizeof(int));
2723 //   graph_node_map_element_t dst_elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&dst_id), sizeof(int));
2724 // 
2725 //   if(src_elm)
2726 //     src = src_elm->node;
2727 // 
2728 //   if(dst_elm)
2729 //     dst = dst_elm->node;
2730 // 
2731 //   //add nodes if they don't exist in the graph
2732 //   if(src_id == dst_id && src == NULL && dst == NULL) {
2733 //     src = route_graph_new_node(src_id, -1);
2734 //     dst = src;
2735 //   } else {
2736 //     if(src == NULL) {
2737 //       src = route_graph_new_node(src_id, -1);
2738 //     }
2739 //      
2740 //     if(dst == NULL) {
2741 //       dst = route_graph_new_node(dst_id, -1);
2742 //     }
2743 //   }
2744 // 
2745 //   //add link as edge to graph
2746 //   xbt_graph_new_edge(routing->route_graph, src, dst, link);
2747 //   
2748 // }
2749 // 
2750 // static void add_loopback_dijkstra(void) {
2751 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2752 // 
2753 //      xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2754 //      
2755 //      xbt_node_t node = NULL;
2756 //      unsigned int cursor2;
2757 //      xbt_dynar_foreach(nodes, cursor2, node) {
2758 //              xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node); 
2759 //              xbt_edge_t edge = NULL;
2760 //              unsigned int cursor;
2761 //      
2762 //              int found = 0;
2763 //              xbt_dynar_foreach(out_edges, cursor, edge) {
2764 //                      xbt_node_t other_node = xbt_graph_edge_get_target(edge);
2765 //                      if(other_node == node) {
2766 //                              found = 1;
2767 //                              break;
2768 //                      }
2769 //              }
2770 // 
2771 //              if(!found)
2772 //                      xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
2773 //      }
2774 // }
2775 // 
2776 // static void routing_dijkstra_parse_end(void) {
2777 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2778 //   int nb_link = 0;
2779 //   xbt_dict_cursor_t cursor = NULL;
2780 //   char *key, *data, *end;
2781 //   const char *sep = "#";
2782 //   xbt_dynar_t links, keys;
2783 //   char* link_name = NULL;
2784 //   void* link = NULL;
2785 //   xbt_node_t node = NULL;
2786 //   unsigned int cursor2;
2787 //   xbt_dynar_t nodes = NULL;
2788 //   /* Create the topology graph */
2789 //   routing->route_graph = xbt_graph_new_graph(1, NULL);
2790 //   routing->graph_node_map = xbt_dict_new();
2791 //   routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
2792 //   if(routing->cached)
2793 //     routing->route_cache = xbt_dict_new();
2794 // 
2795 // 
2796 //   /* Put the routes in position */
2797 //   xbt_dict_foreach(route_table, cursor, key, data) {
2798 //     nb_link = 0;
2799 //     links = (xbt_dynar_t) data;
2800 //     keys = xbt_str_split_str(key, sep);
2801 // 
2802 //     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
2803 //     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
2804 //     xbt_dynar_free(&keys);
2805 // 
2806 //     DEBUG4("Handle %d %d (from %d hosts): %ld links",
2807 //         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
2808 // 
2809 //     xbt_assert3(xbt_dynar_length(links) == 1, "%ld links in route between host %d and %d, should be 1", xbt_dynar_length(links), src_id, dst_id);
2810 // 
2811 //     link_name = xbt_dynar_getfirst_as(links, char*);
2812 //     link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2813 //     if (link)
2814 //       route_new_dijkstra(src_id,dst_id,link);
2815 //     else
2816 //       THROW1(mismatch_error,0,"Link %s not found", link_name);
2817 //     
2818 //   }
2819 // 
2820 //   /* Add the loopback if needed */
2821 //   add_loopback_dijkstra();
2822 // 
2823 //   /* initialize graph indexes in nodes after graph has been built */
2824 //   nodes = xbt_graph_get_nodes(routing->route_graph);
2825 // 
2826 //   xbt_dynar_foreach(nodes, cursor2, node) {
2827 //     graph_node_data_t data = xbt_graph_node_get_data(node);
2828 //     data->graph_id = cursor2;
2829 //   }
2830 // 
2831 // }
2832 // 
2833 // /*
2834 //  * Business methods
2835 //  */
2836 // static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
2837 // 
2838 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2839 //   int * pred_arr = NULL;
2840 //   int src_node_id = 0;
2841 //   int dst_node_id = 0;
2842 //   int * nodeid = NULL;
2843 //   int v;
2844 //   int size = 0;
2845 //   void * link = NULL;
2846 //   route_cache_element_t elm = NULL;
2847 //   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2848 // 
2849 //   /*Use the graph_node id mapping set to quickly find the nodes */
2850 //   graph_node_map_element_t src_elm = graph_node_map_search(src_id);
2851 //   graph_node_map_element_t dst_elm = graph_node_map_search(dst_id);
2852 //   xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", src_id, dst_id);
2853 //   src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
2854 //   dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
2855 // 
2856 //   if(routing->cached) {
2857 //     /*check if there is a cached predecessor list avail */
2858 //     elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
2859 //   }
2860 // 
2861 //   if(elm) { //cached mode and cache hit
2862 //     pred_arr = elm->pred_arr;
2863 //   } else { //not cached mode or cache miss
2864 //     double * cost_arr = NULL;
2865 //     xbt_heap_t pqueue = NULL;
2866 //     int i = 0;
2867 // 
2868 //     int nr_nodes = xbt_dynar_length(nodes);
2869 //     cost_arr = xbt_new0(double, nr_nodes); //link cost from src to other hosts
2870 //     pred_arr = xbt_new0(int, nr_nodes); //predecessors in path from src
2871 //     pqueue = xbt_heap_new(nr_nodes, free);
2872 // 
2873 //     //initialize
2874 //     cost_arr[src_node_id] = 0.0;
2875 // 
2876 //     for(i = 0; i < nr_nodes; i++) {
2877 //       if(i != src_node_id) {
2878 //         cost_arr[i] = DBL_MAX;
2879 //       }
2880 // 
2881 //       pred_arr[i] = 0;
2882 // 
2883 //       //initialize priority queue
2884 //       nodeid = xbt_new0(int, 1);
2885 //       *nodeid = i;
2886 //       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
2887 // 
2888 //     }
2889 // 
2890 //     // apply dijkstra using the indexes from the graph's node array
2891 //     while(xbt_heap_size(pqueue) > 0) {
2892 //       int * v_id = xbt_heap_pop(pqueue);
2893 //       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
2894 //       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node); 
2895 //       xbt_edge_t edge = NULL;
2896 //       unsigned int cursor;
2897 // 
2898 //       xbt_dynar_foreach(out_edges, cursor, edge) {
2899 //         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
2900 //         graph_node_data_t data = xbt_graph_node_get_data(u_node);
2901 //         int u_id = data->graph_id;
2902 //         int cost_v_u = 1; //fixed link cost for now
2903 // 
2904 //         if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
2905 //           pred_arr[u_id] = *v_id;
2906 //           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
2907 //           nodeid = xbt_new0(int, 1);
2908 //           *nodeid = u_id;
2909 //           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
2910 //         }
2911 //       }
2912 // 
2913 //       //free item popped from pqueue
2914 //       free(v_id);
2915 //     }
2916 // 
2917 //     free(cost_arr);
2918 //     xbt_heap_free(pqueue);
2919 // 
2920 //   }
2921 // 
2922 //   //compose route path with links
2923 //   xbt_dynar_reset(routing->last_route);
2924 // 
2925 //   for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
2926 //     xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
2927 //     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
2928 //     xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
2929 // 
2930 //     xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
2931 // 
2932 //     link = xbt_graph_edge_get_data(edge);
2933 //     xbt_dynar_unshift(routing->last_route, &link);
2934 //     size++;
2935 //   }
2936 // 
2937 // 
2938 //   if(routing->cached && elm == NULL) {
2939 //     //add to predecessor list of the current src-host to cache
2940 //     elm = xbt_new0(struct route_cache_element, sizeof(struct route_cache_element));
2941 //     elm->pred_arr = pred_arr;
2942 //     elm->size = size;
2943 //     xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
2944 //   }
2945 // 
2946 //   if(!routing->cached)
2947 //     free(pred_arr);
2948 // 
2949 //   return routing->last_route;
2950 // }
2951 // 
2952 // 
2953 // static void routing_dijkstra_finalize(void) {
2954 //   routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
2955 // 
2956 //   if (routing) {
2957 //     xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
2958 //     xbt_dict_free(&routing->graph_node_map);
2959 //     if(routing->cached)
2960 //       xbt_dict_free(&routing->route_cache);
2961 //     xbt_dynar_free(&routing->last_route);
2962 //     xbt_dict_free(&used_routing->host_id);
2963 //     free(routing);
2964 //     routing=NULL;
2965 //   }
2966 // }
2967 // 
2968 // static xbt_dict_t routing_dijkstraboth_get_onelink_routes(void){
2969 //   xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model dijkstraboth");
2970 // }
2971 // 
2972 // static int routing_dijkstraboth_is_router(int id){
2973 //   xbt_assert0(0,"The get_is_router feature is not supported in routing model dijkstraboth");
2974 // }
2975 // 
2976 // /*
2977 //  *
2978 //  */
2979 // static void routing_model_dijkstraboth_create(size_t size_of_link,void *loopback, int cached) {
2980 //   /* initialize our structure */
2981 //   routing_dijkstra_t routing = xbt_new0(s_routing_dijkstra_t,1);
2982 //   routing->generic_routing.name = "Dijkstra";
2983 //   routing->generic_routing.host_count = 0;
2984 //   routing->generic_routing.get_route = routing_dijkstra_get_route;
2985 //   routing->generic_routing.get_onelink_routes = routing_dijkstraboth_get_onelink_routes;
2986 //   routing->generic_routing.is_router = routing_dijkstraboth_is_router;
2987 //   routing->generic_routing.finalize = routing_dijkstra_finalize;
2988 //   routing->size_of_link = size_of_link;
2989 //   routing->loopback = loopback;
2990 //   routing->cached = cached;
2991 // 
2992 //   /* Set it in position */
2993 //   used_routing = (routing_t) routing;
2994 // 
2995 //   /* Setup the parsing callbacks we need */
2996 //   routing->generic_routing.host_id = xbt_dict_new();
2997 //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2998 //   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_dijkstra_parse_end);
2999 //   surfxml_add_callback(STag_surfxml_route_cb_list,
3000 //       &routing_full_parse_Sroute_set_endpoints);
3001 //   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
3002 //   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
3003 // }
3004 // 
3005 // static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
3006 //   routing_model_dijkstraboth_create(size_of_link, loopback, 0);
3007 // }
3008 // 
3009 // static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
3010 //   routing_model_dijkstraboth_create(size_of_link, loopback, 1);
3011 // }
3012
3013 /* ************************************************** */
3014 /* ********** NO ROUTING **************************** */
3015
3016
3017 // static void routing_none_finalize(void) {
3018 //   if (used_routing) {
3019 //     xbt_dict_free(&used_routing->host_id);
3020 //     free(used_routing);
3021 //     used_routing=NULL;
3022 //   }
3023 // }
3024 // 
3025 // static void routing_model_none_create(size_t size_of_link,void *loopback) {
3026 //   routing_t routing = xbt_new0(s_routing_t,1);
3027 //   INFO0("Null routing");
3028 //   routing->name = "none";
3029 //   routing->host_count = 0;
3030 //   routing->host_id = xbt_dict_new();
3031 //   routing->get_onelink_routes = NULL;
3032 //   routing->is_router = NULL;
3033 //   routing->get_route = NULL;
3034 // 
3035 //   routing->finalize = routing_none_finalize;
3036 // 
3037 //   /* Set it in position */
3038 //   used_routing = (routing_t) routing;
3039 // }