Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
added the floyd algorithm first version
[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 ////////////////////////////////////////////////////////////////////////////////
19 // HERE START THE NEW CODE
20 ////////////////////////////////////////////////////////////////////////////////
21
22 //...... DEBUG ONLY .... //
23 static void print_tree(routing_component_t rc);
24 static void print_global();
25 static void print_AS_start(void);
26 static void print_AS_end(void);
27 static void print_host(void);
28 static void print_link(void);
29 static void print_route(void);
30 static void print_ctn(void);
31 static void DEBUG_exit(void);
32
33 ////////////////////////////////////////////////////////////////////////////////
34 // HERE START THE NEW CODE
35 ////////////////////////////////////////////////////////////////////////////////
36
37 /* Global vars */
38 routing_global_t global_routing = NULL;
39 routing_component_t current_routing = NULL;
40 model_type_t current_routing_model = NULL;
41
42 /* Prototypes of each model */
43 static void* model_full_create(void); /* create structures for full routing model */
44 static void  model_full_load(void);   /* load parse functions for full routing model */
45 static void  model_full_unload(void); /* unload parse functions for full routing model */
46 static void  model_full_end(void);    /* finalize the creation of full routing model */
47
48 static void* model_floyd_create(void); /* create structures for floyd routing model */
49 static void  model_floyd_load(void);   /* load parse functions for floyd routing model */
50 static void  model_floyd_unload(void); /* unload parse functions for floyd routing model */
51 static void  model_floyd_end(void);    /* finalize the creation of floyd routing model */
52
53 static void* model_dijkstra_create(void); /* create structures for dijkstra routing model */
54 static void  model_dijkstra_load(void);   /* load parse functions for dijkstra routing model */
55 static void  model_dijkstra_unload(void); /* unload parse functions for dijkstra routing model */
56 static void  model_dijkstra_end(void);    /* finalize the creation of dijkstra routing model */
57
58 static void* model_dijkstracache_create(void); /* create structures for dijkstracache routing model */
59 static void  model_dijkstracache_load(void);   /* load parse functions for dijkstracache routing model */
60 static void  model_dijkstracache_unload(void); /* unload parse functions for dijkstracache routing model */
61 static void  model_dijkstracache_end(void);    /* finalize the creation of dijkstracache routing model */
62
63 static void* model_none_create(void); /* none routing model */
64 static void  model_none_load(void);   /* none routing model */
65 static void  model_none_unload(void); /* none routing model */
66 static void  model_none_end(void);    /* none routing model */
67
68 /* Table of routing models */
69 // typedef enum {
70 //   SURF_MODEL_FULL = 0,
71 //   SURF_MODEL_FLOYD,
72 //   SURF_MODEL_DIJKSTRA,
73 //   SURF_MODEL_DIJKSTRACACHE,
74 //   SURF_MODEL_NONE
75 // } e_surf_model_type_t;
76
77 #define SURF_MODEL_FULL           0
78 #define SURF_MODEL_FLOYD          1
79 #define SURF_MODEL_DIJKSTRA       2
80 #define SURF_MODEL_DIJKSTRACACHE  3
81 #define SURF_MODEL_NONE           4
82
83 /* must be finish with null and carefull if change de order */
84 struct s_model_type routing_models[] =
85 { {"Full", "Full routing data (fast, large memory requirements, fully expressive)",
86   model_full_create, model_full_load, model_full_unload, model_full_end },  
87   {"Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
88   model_floyd_create, model_floyd_load, model_floyd_unload, model_floyd_end },
89   {"Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
90   model_dijkstra_create ,model_dijkstra_load, model_dijkstra_unload, model_dijkstra_end },
91   {"DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
92   model_dijkstracache_create, model_dijkstracache_load, model_dijkstracache_unload, model_dijkstracache_end },
93   {"none", "No routing (usable with Constant network only)",
94   model_none_create, model_none_load, model_none_unload, model_none_end },
95   {NULL,NULL,NULL,NULL,NULL,NULL}};
96
97 /* global parse functions */
98
99 static char* src = NULL;    /* temporary store the source name of a route */
100 static char* dst = NULL;    /* temporary store the destination name of a route */
101 static char* gw_src = NULL; /* temporary store the gateway source name of a route */
102 static char* gw_dst = NULL; /* temporary store the gateway destination name of a route */
103 static xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */ 
104
105 /**
106  * \brief Add a "host" to the network element list
107  */
108 static void  parse_S_host(void) {
109   if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_BASE;
110   xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_host_id),
111       "Reading a host, processing unit \"%s\" already exist",A_surfxml_host_id);
112 // FIXME: checked by parser
113 //   xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
114 //       "Bad declaration of processing unit \"%s\"",A_surfxml_host_id);
115   (*(current_routing->set_processing_units))(current_routing,A_surfxml_host_id);
116   xbt_dict_set(global_routing->where_network_elements,A_surfxml_host_id,(void*)current_routing,NULL); 
117 }
118
119 /**
120  * \brief Add a "router" to the network element list
121  */
122 static void parse_S_router(void) {
123   if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_BASE;
124   xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_router_id),
125       "Reading a router, processing unit \"%s\" already exist",A_surfxml_router_id);
126 // FIXME: checked by parser
127 //   xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
128 //       "Bad declaration of processing unit \"%s\"",A_surfxml_host_id);
129   (*(current_routing->set_processing_units))(current_routing,A_surfxml_router_id);
130   xbt_dict_set(global_routing->where_network_elements,A_surfxml_router_id,(void*)current_routing,NULL); 
131 }
132
133 /**
134  * \brief Set the endponints for a route
135  */
136 static void parse_S_route_new_and_endpoints(void) {
137   // FIXME: checked by parser
138 //   xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
139 //     "Bad declaration of route in \"%s\"",current_routing->name);
140   if( src != NULL && dst != NULL && link_list != NULL )
141     THROW2(arg_error,0,"Route between %s to %s can not be defined",A_surfxml_route_src,A_surfxml_route_dst);
142   src = A_surfxml_route_src;
143   dst = A_surfxml_route_dst;
144   link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
145 }
146
147 /**
148  * \brief Set the endponints and gateways for a ASroute
149  */
150 static void parse_S_ASroute_new_and_endpoints(void) {
151 // FIXME: checked by parser
152 //   xbt_assert1(current_routing->hierarchy==SURF_ROUTING_RECURSIVE,
153 //     "Bad declaration of ASroute in \"%s\"",current_routing->name);
154   if( src != NULL && dst != NULL && link_list != NULL )
155     THROW2(arg_error,0,"Route between %s to %s can not be defined",A_surfxml_ASroute_src,A_surfxml_ASroute_dst);
156   src = A_surfxml_ASroute_src;
157   dst = A_surfxml_ASroute_dst;
158   gw_src = A_surfxml_ASroute_gw_src;
159   gw_dst = A_surfxml_ASroute_gw_dst;
160   link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
161 }
162
163 /**
164  * \brief Set a new link on the actual list of link for a route or ASroute
165  */
166 static void parse_E_link_c_ctn_new_elem(void) {
167   char *val;
168   val = xbt_strdup(A_surfxml_link_c_ctn_id);
169   xbt_dynar_push(link_list, &val);
170 }
171
172 /**
173  * \brief Store de route by calling the set_route function of the current routing component
174  */
175 static void parse_E_route_store_route(void) {
176 // FIXME: checked by parser
177 //   xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
178 //     "Bad declaration of route in \"%s\"",current_routing->name);
179   route_t route = xbt_new(s_route_t,1);
180   route->link_list = link_list;
181   // TODO check if are correct
182   (*(current_routing->set_route))(current_routing,src,dst,route);
183   link_list = NULL;
184   src = NULL;
185   dst = NULL;
186 }
187
188 /**
189  * \brief Store de ASroute by calling the set_ASroute function of the current routing component
190  */
191 static void parse_E_ASroute_store_route(void) {
192 // FIXME: checked by parser
193 //   xbt_assert1(current_routing->hierarchy==SURF_ROUTING_RECURSIVE,
194 //     "Bad declaration of ASroute in \"%s\"",current_routing->name);
195   route_extended_t e_route = xbt_new(s_route_extended_t,1);
196   e_route->generic_route.link_list = link_list;
197   e_route->src_gateway = xbt_strdup(gw_src);
198   e_route->dst_gateway = xbt_strdup(gw_dst);
199   // TODO check if are correct
200   (*(current_routing->set_ASroute))(current_routing,src,dst,e_route);
201   link_list = NULL;
202   src = NULL;
203   dst = NULL;
204   gw_src = NULL;
205   gw_dst = NULL;
206 }
207
208 /**
209  * \brief Make a new routing component
210  *
211  * Detect the routing model type of the routing component, make the new structure and
212  * set the parsing functions to allows parsing the part of the routing tree
213  */
214 static void parse_S_AS(void) { 
215   routing_component_t new_routing;
216   model_type_t model = NULL;
217   char* wanted = A_surfxml_AS_routing;
218   int cpt;
219   /* seach the routing model */
220   for (cpt=0;routing_models[cpt].name;cpt++)
221     if (!strcmp(wanted,routing_models[cpt].name))
222           model = &routing_models[cpt];
223   /* if its not exist, error */
224   if( model == NULL ) {
225     fprintf(stderr,"Routing model %s not found. Existing models:\n",wanted);
226     for (cpt=0;routing_models[cpt].name;cpt++)
227       if (!strcmp(wanted,routing_models[cpt].name))
228         fprintf(stderr,"   %s: %s\n",routing_models[cpt].name,routing_models[cpt].desc);
229     xbt_die(NULL);
230   }
231
232   /* make a new routing component */
233   new_routing = (routing_component_t)(*(model->create))();
234   
235   /* FIXME: for now, if I forget to declare */  
236   xbt_assert1( new_routing->set_processing_units,
237        "Bad routing type, \"set_processing_units\" is not declared for \"%s\"",A_surfxml_AS_id);
238   xbt_assert1( new_routing->set_autonomous_system,
239        "Bad routing type, \"set_autonomous_system\" is not declared for \"%s\"",A_surfxml_AS_id);
240   xbt_assert1( new_routing->set_route,
241        "Bad routing type, \"set_route\" is not declared for \"%s\"",A_surfxml_AS_id);
242   xbt_assert1( new_routing->set_ASroute,
243        "Bad routing type, \"set_ASroute\" is not declared for \"%s\"",A_surfxml_AS_id);
244   xbt_assert1( new_routing->finalize,
245        "Bad routing type, \"finalize\" is not declared for \"%s\"",A_surfxml_AS_id);
246        
247   new_routing->routing = model;
248   new_routing->hierarchy = SURF_ROUTING_NULL;
249   new_routing->name = xbt_strdup(A_surfxml_AS_id);
250   new_routing->routing_sons = xbt_dict_new();
251   INFO2("Routing %s for AS %s",A_surfxml_AS_routing,A_surfxml_AS_id);
252   
253   if( current_routing == NULL && global_routing->root == NULL ){
254     
255     /* it is the first one */
256     new_routing->routing_father = NULL;
257     global_routing->root = new_routing;
258     
259   } else if( current_routing != NULL && global_routing->root != NULL ) { 
260     
261     xbt_assert1(!xbt_dict_get_or_null(current_routing->routing_sons,A_surfxml_AS_id),
262            "The AS \"%s\" already exist",A_surfxml_AS_id);
263      /* it is a part of the tree */
264     new_routing->routing_father = current_routing;
265     /* set the father behavior */
266     if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
267 // FIXME: checked by parser
268 //     xbt_assert1(current_routing->hierarchy==SURF_ROUTING_RECURSIVE,
269 //       "Bad declaration of AS \"%s\"",A_surfxml_AS_id);
270     /* add to the sons dictionary */
271     xbt_dict_set(current_routing->routing_sons,A_surfxml_AS_id,(void*)new_routing,NULL);
272     /* add to the father element list */
273     (*(current_routing->set_autonomous_system))(current_routing,A_surfxml_AS_id);
274     /* unload the prev parse rules */
275     (*(current_routing->routing->unload))();
276     
277   } else {
278     THROW0(arg_error,0,"All defined components must be belong to a AS");
279   }
280   /* set the new parse rules */
281   (*(new_routing->routing->load))();
282   /* set the new current component of the tree */
283   current_routing = new_routing;
284 }
285
286 /**
287  * \brief Finish the creation of a new routing component
288  *
289  * When you finish to read the routing component, other structures must be created. 
290  * the "end" method allow to do that for any routing model type
291  */
292 static void parse_E_AS(void) {
293
294   if( current_routing == NULL ) {
295     THROW1(arg_error,0,"Close AS(%s), that never open",A_surfxml_AS_id);
296   } else {
297       (*(current_routing->routing->unload))();
298       (*(current_routing->routing->end))();
299       current_routing = current_routing->routing_father;
300       if( current_routing != NULL )
301         (*(current_routing->routing->load))();
302   }
303 }
304
305 /**
306  * \brief Recursive function for add the differents AS to global dict of network element
307  *
308  * This fuction is call by "parse_E_platform_add_parse_AS". It allow to add the 
309  * AS or routing components, to the where_network_elements dictionary. In the same
310  * way as "parse_S_host", "parse_S_router" and "parse_S_gateway" do.
311  */
312 static void _add_parse_AS(routing_component_t rc) {
313   xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,rc->name),
314      "The AS \"%s\" already exist",rc->name);
315   xbt_dict_set(global_routing->where_network_elements,rc->name,rc->routing_father,NULL);
316   xbt_dict_cursor_t cursor = NULL;
317   char *key;
318   routing_component_t elem;
319   xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
320     _add_parse_AS(elem);
321   } 
322 }
323
324 /**
325  * \brief Add all "AS" to the global dict of network element
326  *
327  * Allows find a "AS" in any routing component
328  */
329 static void parse_E_platform_add_parse_AS(void) {
330   _add_parse_AS(global_routing->root);
331 }
332
333 /* Aux Business methods */
334
335 /**
336  * \brief Get the AS father and the first elements of the chain
337  *
338  * \param src the source host name 
339  * \param dst the destination host name
340  * 
341  * Get the common father of the to processing units, and the first different 
342  * father in the chain
343  */
344 static xbt_dynar_t elements_father(const char* src,const char* dst) {
345   
346   xbt_assert0(src&&dst,"bad parameters for \"elements_father\" method");
347   
348   xbt_dynar_t result = xbt_dynar_new(sizeof(char*), NULL); // &xbt_free_ref
349   
350   routing_component_t src_as, dst_as;
351   int index_src, index_dst, index_father_src, index_father_dst;
352   xbt_dynar_t path_src = NULL;
353   xbt_dynar_t path_dst = NULL;
354   routing_component_t current = NULL;
355   routing_component_t* current_src = NULL;
356   routing_component_t* current_dst = NULL;
357   routing_component_t* father = NULL;
358   
359   /* (1) find the as where the src and dst are located */
360   src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
361   dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst); 
362   xbt_assert2(src_as&&dst_as, "Ask for route \"from\"(%s) or \"to\"(%s) no found",src,dst);
363   
364   /* (2) find the path to the root routing component */
365   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
366   current = src_as; 
367   while( current != NULL ) {
368     xbt_dynar_push(path_src,&current);
369     current = current->routing_father;
370   }
371   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
372   current = dst_as; 
373   while( current != NULL ) {
374     xbt_dynar_push(path_dst,&current);
375     current = current->routing_father;
376   }
377   
378   /* (3) find the common father */
379   index_src  = (path_src->used)-1;
380   index_dst  = (path_dst->used)-1;
381   current_src = xbt_dynar_get_ptr(path_src,index_src);
382   current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
383   while( index_src >= 0 && index_dst >= 0 && *current_src == *current_dst ) {
384     current_src = xbt_dynar_get_ptr(path_src,index_src);
385     current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
386     index_src--;
387     index_dst--;
388   }
389   index_src++;
390   index_dst++;
391   current_src = xbt_dynar_get_ptr(path_src,index_src);
392   current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
393
394   /* (4) they are not in the same routing component, make the path */
395   index_father_src = index_src+1;
396   index_father_dst = index_dst+1;
397    
398   if(*current_src==*current_dst)
399     father = current_src;
400   else
401     father = xbt_dynar_get_ptr(path_src,index_father_src);
402   
403   /* (5) result generation */
404   xbt_dynar_push(result,father); // first same the father of src and dst
405   xbt_dynar_push(result,current_src); // second the first different father of src 
406   xbt_dynar_push(result,current_dst); // three  the first different father of dst
407   
408   xbt_dynar_free(&path_src);
409   xbt_dynar_free(&path_dst);
410   
411   return result;
412 }
413
414 /* Global Business methods */
415
416 /**
417  * \brief Recursive function for get_route
418  *
419  * \param src the source host name 
420  * \param dst the destination host name
421  * 
422  * This fuction is call by "get_route". It allow to walk through the 
423  * routing components tree.
424  */
425 static route_extended_t _get_route(const char* src,const char* dst) {
426   
427   void* link;
428   unsigned int cpt=0;
429   
430   DEBUG2("Solve route  \"%s\" to \"%s\"",src,dst);
431      
432   xbt_assert0(src&&dst,"bad parameters for \"_get_route\" method");
433   
434   route_extended_t e_route, e_route_cnt, e_route_src, e_route_dst;
435   
436   xbt_dynar_t elem_father_list = elements_father(src,dst);
437   
438   routing_component_t common_father = xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
439   routing_component_t src_father    = xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
440   routing_component_t dst_father    = xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
441   
442   e_route = xbt_new0(s_route_extended_t,1);
443   e_route->src_gateway = NULL;
444   e_route->dst_gateway = NULL;
445   e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
446   
447   if(src_father==dst_father) { /* SURF_ROUTING_BASE */
448     
449     if( strcmp(src,dst) ){
450       e_route_cnt = (*(common_father->get_route))(common_father,src,dst);
451       xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
452         xbt_dynar_push(e_route->generic_route.link_list,&link);
453       }
454       xbt_dynar_free( &(e_route_cnt->generic_route.link_list) );
455       xbt_free(e_route_cnt);
456     }
457     
458   } else { /* SURF_ROUTING_RECURSIVE */
459     
460       e_route_cnt = (*(common_father->get_route))(common_father,src_father->name,dst_father->name);
461       
462       xbt_assert2( (e_route_cnt->src_gateway==NULL) == (e_route_cnt->dst_gateway==NULL) ,
463         "bad gateway for route between \"%s\" and \"%s\"",src,dst);
464
465       if( src != e_route_cnt->src_gateway ) {
466         e_route_src = _get_route(src,e_route_cnt->src_gateway);
467         xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
468           xbt_dynar_push(e_route->generic_route.link_list,&link);
469         }
470       }
471       
472       xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
473         xbt_dynar_push(e_route->generic_route.link_list,&link);
474       }
475       
476       if( e_route_cnt->dst_gateway != dst ) {
477         e_route_dst = _get_route(e_route_cnt->dst_gateway,dst);
478         xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
479           xbt_dynar_push(e_route->generic_route.link_list,&link);
480         }
481       }
482       
483       e_route->src_gateway = e_route_cnt->src_gateway;
484       e_route->dst_gateway = e_route_cnt->dst_gateway;
485
486       xbt_dynar_free( &(e_route_src->generic_route.link_list) );
487       xbt_free(e_route_src);
488       xbt_dynar_free( &(e_route_cnt->generic_route.link_list) );
489       xbt_free(e_route_cnt);
490       xbt_dynar_free( &(e_route_dst->generic_route.link_list) );
491       xbt_free(e_route_dst);
492   }
493   
494   xbt_dynar_free(&elem_father_list);
495   
496   return e_route; 
497 }
498
499 /**
500  * \brief Generic method: find a route between hosts
501  *
502  * \param src the source host name 
503  * \param dst the destination host name
504  * 
505  * walk through the routing components tree and find a route between hosts
506  * by calling the differents "get_route" functions in each routing component
507  */
508 static xbt_dynar_t get_route(const char* src,const char* dst) {
509   
510   if(global_routing->last_route) xbt_dynar_free( &(global_routing->last_route) );
511   
512   route_extended_t e_route;
513   xbt_dynar_t elem_father_list = elements_father(src,dst);
514   routing_component_t common_father = xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
515   
516   if(strcmp(src,dst))
517     e_route = _get_route(src,dst);
518   else
519     e_route = (*(common_father->get_route))(common_father,src,dst);
520   
521   global_routing->last_route = e_route->generic_route.link_list;
522  
523   xbt_free(e_route);
524   xbt_dynar_free(&elem_father_list);
525   
526   if( xbt_dynar_length(global_routing->last_route)==0 )
527     return NULL;
528   else
529     return global_routing->last_route;
530 }
531
532 /**
533  * \brief Recursive function for finalize
534  *
535  * \param rc the source host name 
536  * 
537  * This fuction is call by "finalize". It allow to finalize the 
538  * AS or routing components. It delete all the structures.
539  */
540 static void _finalize(routing_component_t rc) {
541   if(rc) {
542     xbt_dict_cursor_t cursor = NULL;
543     char *key;
544     routing_component_t elem;
545     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
546       _finalize(elem);
547     }
548     xbt_dict_t tmp_sons = rc->routing_sons;
549     char* tmp_name = rc->name;
550     xbt_dict_free(&tmp_sons);
551     xbt_free(tmp_name);
552     (*(rc->finalize))(rc);
553   }
554 }
555
556 /**
557  * \brief Generic method: delete all the routing structures
558  * 
559  * walk through the routing components tree and delete the structures
560  * by calling the differents "finalize" functions in each routing component
561  */
562 static void finalize(void) {
563   /* delete recursibly all the tree */  
564   _finalize(global_routing->root);
565   /* delete "where" dict */
566   xbt_dict_free(&(global_routing->where_network_elements));
567   /* delete last_route */
568   xbt_dynar_free(&(global_routing->last_route));
569   /* delete global routing structure */
570   xbt_free(global_routing);
571 }
572
573 /**
574  * \brief Generic method: create the global routing schema
575  * 
576  * Make a global routing structure and set all the parsing functions.
577  */
578 void routing_model_create(size_t size_of_links, void* loopback) {
579   
580   /* config the uniq global routing */
581   global_routing = xbt_new0(s_routing_global_t,1);
582   global_routing->where_network_elements = xbt_dict_new();
583   global_routing->root = NULL;
584   global_routing->get_route = get_route;
585   global_routing->finalize = finalize;
586   global_routing->loopback = loopback;
587   global_routing->size_of_link = size_of_links;
588   global_routing->last_route = xbt_dynar_new(size_of_links, NULL);
589   
590   /* no current routing at moment */
591   current_routing = NULL;
592
593   /* parse generic elements */
594   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host);
595   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
596
597   surfxml_add_callback(STag_surfxml_route_cb_list, &parse_S_route_new_and_endpoints);
598   surfxml_add_callback(STag_surfxml_ASroute_cb_list, &parse_S_ASroute_new_and_endpoints);
599   
600   surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_E_link_c_ctn_new_elem);
601   
602   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_E_route_store_route);
603   surfxml_add_callback(ETag_surfxml_ASroute_cb_list, &parse_E_ASroute_store_route);
604   
605   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS);
606   surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS);
607   
608   /* set all the as in the global where table (recursive fuction) */
609   surfxml_add_callback(ETag_surfxml_platform_cb_list, &parse_E_platform_add_parse_AS);
610   
611   /* DEBUG ONLY */  
612   //surfxml_add_callback(ETag_surfxml_platform_cb_list, &DEBUG_exit);
613 }
614
615 /* ************************************************************************** */
616 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
617
618 static void generic_set_processing_units(routing_component_t rc, const char* name);
619 static void generic_set_autonomous_system(routing_component_t rc, const char* name);
620 static void generic_set_route(routing_component_t rc, const char* src, const char* dst, route_t route);
621 static void generic_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route);
622
623 /* ************************************************************************** */
624 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
625
626 static route_extended_t generic_new_extended_route(routing_component_t rc, void* data);
627
628 /* ************************************************************************** */
629 /* *************************** FULL ROUTING ********************************* */
630
631 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
632
633 /* Routing model structure */
634
635 typedef struct {
636   s_routing_component_t generic_routing;
637   xbt_dict_t parse_routes; /* store data during the parse process */
638   xbt_dict_t to_index; /* char* -> network_element_t */
639   route_extended_t *routing_table;
640 } s_routing_component_full_t,*routing_component_full_t;
641
642 /* Business methods */
643
644 static route_extended_t full_get_route(routing_component_t rc, const char* src,const char* dst) {
645
646   routing_component_full_t routing = (routing_component_full_t)rc;
647   int table_size = xbt_dict_length(routing->to_index);
648   
649   xbt_assert1(src && dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
650    
651   routing_component_t src_as, dst_as;
652   int *src_id,*dst_id;
653
654   src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
655   dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst);
656    
657   xbt_assert3(src_as != NULL && dst_as  != NULL, "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",src,dst,rc->name);
658   xbt_assert4(src_as == dst_as, "The src(%s in %s) and dst(%s in %s) are in differents AS",src,src_as->name,dst,dst_as->name);
659   xbt_assert2(rc == dst_as, "The routing component of src and dst is not the same as the network elements belong (%s==%s)",rc->name,dst_as->name);
660
661   src_id = xbt_dict_get(routing->to_index,src);
662   dst_id = xbt_dict_get(routing->to_index,dst);
663   xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",src,dst); 
664   
665   void* link;
666   unsigned int cpt=0;
667   route_extended_t new_e_route;
668   route_extended_t e_route = TO_ROUTE_FULL(*src_id,*dst_id);
669   if(e_route) {
670     new_e_route = xbt_new0(s_route_extended_t,1);
671     new_e_route->src_gateway = e_route->src_gateway;
672     new_e_route->dst_gateway = e_route->dst_gateway;
673     new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
674     xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
675       xbt_dynar_push(new_e_route->generic_route.link_list,&link);
676     }
677   } else {
678     new_e_route = NULL;
679   }
680     return new_e_route;
681 }
682
683 static void full_finalize(routing_component_t rc) {
684   routing_component_full_t routing = (routing_component_full_t)rc;
685   int table_size = xbt_dict_length(routing->to_index);
686   int i,j;
687   if (routing) {
688     /* Delete routing table */
689     for (i=0;i<table_size;i++) {
690       for (j=0;j<table_size;j++) {
691         route_extended_t e_route = TO_ROUTE_FULL(i,j);
692         if(e_route) {
693           xbt_dynar_free(&(e_route->generic_route.link_list));
694           if(e_route->src_gateway) xbt_free(e_route->src_gateway);
695           if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
696           xbt_free(e_route);
697         }
698       }
699     }
700     xbt_free(routing->routing_table);
701     /* Delete index dict */
702     xbt_dict_free(&(routing->to_index));
703     /* Delete structure */
704     xbt_free(rc);
705   }
706 }
707
708 /* Creation routing model functions */
709
710 static void* model_full_create() {
711   routing_component_full_t new_component =  xbt_new0(s_routing_component_full_t,1);
712   new_component->generic_routing.set_processing_units = generic_set_processing_units;
713   new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
714   new_component->generic_routing.set_route = generic_set_route;
715   new_component->generic_routing.set_ASroute = generic_set_ASroute;
716   new_component->generic_routing.get_route = full_get_route;
717   new_component->generic_routing.finalize = full_finalize;
718   new_component->to_index = xbt_dict_new();
719   new_component->parse_routes = xbt_dict_new();
720   return new_component;
721 }
722
723 static void model_full_load() {
724  /* use "surfxml_add_callback" to add a parse function call */
725 }
726
727 static void model_full_unload() {
728  /* use "surfxml_del_callback" to remove a parse function call */
729 }
730
731 static void  model_full_end() {
732   
733   char *key, *src_name, *dst_name;
734   const char* sep = "#";
735   int *src_id, *dst_id;
736   unsigned int i, j;
737   route_t route;
738   route_extended_t e_route;
739   void* data;
740   
741   xbt_dict_cursor_t cursor = NULL;
742   xbt_dynar_t keys = NULL;
743   
744   /* set utils vars */
745   routing_component_full_t routing = ((routing_component_full_t)current_routing);
746   int table_size = xbt_dict_length(routing->to_index);
747   
748   /* Create the routing table */
749   routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
750   
751   /* Put the routes in position */
752   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
753     
754     keys = xbt_str_split_str(key, sep);
755
756     src_name = xbt_dynar_get_as(keys, 0, char*);
757     dst_name = xbt_dynar_get_as(keys, 1, char*);
758     
759     src_id = xbt_dict_get_or_null(routing->to_index, src_name);
760     dst_id = xbt_dict_get_or_null(routing->to_index, dst_name);
761     
762     if (src_id == NULL || dst_id == NULL )
763       THROW2(mismatch_error,0,"Network elements %s or %s not found", src_name, dst_name);
764     
765     TO_ROUTE_FULL(*src_id,*dst_id) = generic_new_extended_route(current_routing,data);
766
767      xbt_dynar_free(&keys);
768    }
769
770    /* delete the parse table */
771   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
772     route = (route_t)data;
773     xbt_dynar_free(&(route->link_list));
774     xbt_free(data);
775   }
776       
777   /* delete parse dict */
778   xbt_dict_free(&(routing->parse_routes));
779
780   /* Add the loopback if needed */
781   for (i = 0; i < table_size; i++) {
782     e_route = TO_ROUTE_FULL(i, i);
783     if(!e_route) { // && !xbt_dynar_length(e_route->generic_route.link_list)
784       e_route = xbt_new0(s_route_extended_t,1);
785       e_route->src_gateway = NULL;
786       e_route->dst_gateway = NULL;
787       e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
788       xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
789       TO_ROUTE_FULL(i, i) = e_route;
790     }
791   }
792
793   /* Shrink the dynar routes (save unused slots) */
794   for (i=0;i<table_size;i++)
795     for (j=0;j<table_size;j++)
796       if(TO_ROUTE_FULL(i,j))
797         xbt_dynar_shrink(TO_ROUTE_FULL(i,j)->generic_route.link_list,0);
798 }
799
800 /* ************************************************************************** */
801 /* *************************** FLOYD ROUTING ******************************** */
802
803 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
804 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
805 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
806
807 /* Routing model structure */
808
809 typedef struct {
810   s_routing_component_t generic_routing;
811   /* vars for calculate the floyd algorith. */
812   int *predecessor_table;
813   route_extended_t *link_table; /* char* -> int* */
814   xbt_dict_t to_index;
815   /* store data during the parse process */  
816   xbt_dict_t parse_routes; 
817 } s_routing_component_floyd_t,*routing_component_floyd_t;
818
819 /* Business methods */
820
821 static route_extended_t floyd_get_route(routing_component_t rc, const char* src,const char* dst) {
822   
823   xbt_assert1(src && dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
824   
825   /* set utils vars */
826   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
827   int table_size = xbt_dict_length(routing->to_index);
828   
829   route_extended_t new_e_route = xbt_new0(s_route_extended_t,1);
830   new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
831   new_e_route->src_gateway = NULL;
832   new_e_route->dst_gateway = NULL;
833   
834   int *src_id = xbt_dict_get(routing->to_index,src);
835   int *dst_id = xbt_dict_get(routing->to_index,dst);
836   xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",src,dst); 
837
838   int first = 1;
839   int pred = *dst_id;
840   int prev_pred = 0;
841   char *gw_src,*gw_dst, *prev_gw_src,*prev_gw_dst, *first_gw;
842   unsigned int cpt;
843   void* link;
844   xbt_dynar_t links;
845   route_extended_t e_route_as_to_as;
846   do {
847     prev_pred = pred;
848     pred = TO_FLOYD_PRED(*src_id, pred);
849     if(pred == -1) // if no pred in route -> no route to host
850       break;      
851     xbt_assert2(TO_FLOYD_LINK(pred,prev_pred),"Invalid link for the route between \"%s\" or \"%s\"", src, dst);
852     
853     prev_gw_src = gw_src;
854     prev_gw_dst = gw_dst;
855     
856     route_extended_t e_route = TO_FLOYD_LINK(pred,prev_pred);
857     gw_src = e_route->src_gateway;
858     gw_dst = e_route->dst_gateway;
859     
860     if(first) first_gw = gw_src;
861     
862     if(rc->hierarchy == SURF_ROUTING_RECURSIVE && !first && strcmp(prev_gw_dst,gw_src)) {
863       
864       routing_component_t src_as = xbt_dict_get_or_null(global_routing->where_network_elements,prev_gw_dst);
865       routing_component_t dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,gw_src);
866       xbt_assert2(src_as==dst_as,"bad routing, differents AS gateways in route \"%s\" to \"%s\"",src,dst);
867       e_route_as_to_as = (*(src_as->get_route))(src_as,prev_gw_dst,gw_src);
868       links = e_route_as_to_as->generic_route.link_list;
869       xbt_dynar_foreach(links, cpt, link) {
870         xbt_dynar_push(new_e_route->generic_route.link_list,&link);
871       }
872     }
873     
874     links = e_route->generic_route.link_list;
875     xbt_dynar_foreach(links, cpt, link) {
876       xbt_dynar_push(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 = first_gw;
885     new_e_route->dst_gateway = gw_dst;
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         route_extended_t e_route = TO_FLOYD_LINK(i,j);
900         if(e_route) {
901           xbt_dynar_free(&(e_route->generic_route.link_list));
902           if(e_route->src_gateway) xbt_free(e_route->src_gateway);
903           if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
904           xbt_free(e_route);
905         }
906       }
907     }
908     xbt_free(routing->link_table);
909     /* Delete index dict */
910     xbt_dict_free(&(routing->to_index));
911     /* Delete dictionary index dict, predecessor and links table */
912     xbt_free(routing->predecessor_table);
913     /* Delete structure */
914     xbt_free(rc);
915   }
916 }
917
918 static void* model_floyd_create(void) {
919   routing_component_floyd_t new_component = xbt_new0(s_routing_component_floyd_t,1);
920   new_component->generic_routing.set_processing_units = generic_set_processing_units;
921   new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
922   new_component->generic_routing.set_route = generic_set_route;
923   new_component->generic_routing.set_ASroute = generic_set_ASroute;
924   new_component->generic_routing.get_route = floyd_get_route;
925   new_component->generic_routing.finalize = floyd_finalize;
926   new_component->to_index = xbt_dict_new();
927   new_component->parse_routes = xbt_dict_new();
928   return new_component;
929 }
930
931 static void  model_floyd_load() {
932  /* use "surfxml_add_callback" to add a parse function call */
933 }
934
935 static void  model_floyd_unload() {
936  /* use "surfxml_del_callback" to remove a parse function call */
937 }
938
939 static void  model_floyd_end() {
940   
941   routing_component_floyd_t routing = ((routing_component_floyd_t)current_routing);
942   xbt_dict_cursor_t cursor = NULL;
943   double * cost_table;
944   char *key,*data, *src_name, *dst_name;
945   const char *sep = "#";
946   xbt_dynar_t keys;
947   int *src_id, *dst_id;
948   unsigned int i,j,a,b,c;
949
950   /* set the size of inicial table */
951   int table_size = xbt_dict_length(routing->to_index);
952   
953   /* Create Cost, Predecessor and Link tables */
954   cost_table = xbt_new0(double, table_size*table_size); //link cost from host to host
955   routing->predecessor_table = xbt_new0(int, table_size*table_size); //predecessor host numbers
956   routing->link_table = xbt_new0(route_extended_t, table_size*table_size); //actual link between src and dst
957
958   /* Initialize costs and predecessors*/
959   for(i = 0; i<table_size;i++)
960     for(j = 0; j<table_size;j++) {
961         TO_FLOYD_COST(i,j) = DBL_MAX;
962         TO_FLOYD_PRED(i,j) = -1;
963         TO_FLOYD_LINK(i,j) = NULL; // FIXED DAVID
964     }
965
966    /* Put the routes in position */
967   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
968     
969     keys = xbt_str_split_str(key, sep);
970
971     src_name = xbt_dynar_get_as(keys, 0, char*);
972     dst_name = xbt_dynar_get_as(keys, 1, char*);
973     
974     src_id = xbt_dict_get_or_null(routing->to_index, src_name);
975     dst_id = xbt_dict_get_or_null(routing->to_index, dst_name);
976     
977     if (src_id == NULL || dst_id == NULL )
978       THROW2(mismatch_error,0,"Network elements %s or %s not found", src_name, dst_name);
979
980     TO_FLOYD_LINK(*src_id,*dst_id) = generic_new_extended_route(current_routing,data);
981     TO_FLOYD_PRED(*src_id,*dst_id) = *src_id;
982     
983     //link cost
984     TO_FLOYD_COST(*src_id,*dst_id) = 1; // assume 1 for now
985     
986     xbt_dynar_free(&keys);
987   }
988
989   /* Add the loopback if needed */
990   for (i = 0; i < table_size; i++) {
991     route_extended_t e_route = TO_FLOYD_LINK(i, i);
992     if(!e_route) { // && !xbt_dynar_length(e_route->generic_route.link_list)
993       e_route = xbt_new0(s_route_extended_t,1);
994       e_route->src_gateway = NULL;
995       e_route->dst_gateway = NULL;
996       e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
997       xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
998       TO_FLOYD_LINK(i,i) = e_route;
999       TO_FLOYD_PRED(i,i) = i;
1000       TO_FLOYD_COST(i,i) = 1;
1001     }
1002   }
1003
1004   //Calculate path costs 
1005   for(c=0;c<table_size;c++) {
1006     for(a=0;a<table_size;a++) {
1007       for(b=0;b<table_size;b++) {
1008         if(TO_FLOYD_COST(a,c) < DBL_MAX && TO_FLOYD_COST(c,b) < DBL_MAX) {
1009           if(TO_FLOYD_COST(a,b) == DBL_MAX || 
1010             (TO_FLOYD_COST(a,c)+TO_FLOYD_COST(c,b) < TO_FLOYD_COST(a,b))) {
1011             TO_FLOYD_COST(a,b) = TO_FLOYD_COST(a,c)+TO_FLOYD_COST(c,b);
1012             TO_FLOYD_PRED(a,b) = TO_FLOYD_PRED(c,b);
1013           }
1014         }
1015       }
1016     }
1017   }
1018
1019    /* delete the parse table */
1020   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1021     route_t route = (route_t)data;
1022     xbt_dynar_free(&(route->link_list));
1023     xbt_free(data);
1024   }
1025   
1026   /* delete parse dict */
1027   xbt_dict_free(&(routing->parse_routes));
1028
1029   //cleanup
1030   free(cost_table);
1031 }
1032
1033 /* ************************************************************************** */
1034 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1035
1036 typedef struct {
1037   s_routing_component_t generic_routing;
1038   xbt_graph_t route_graph;
1039   xbt_dict_t graph_node_map;
1040   xbt_dict_t route_cache;
1041   xbt_dynar_t last_route;
1042   int cached;
1043 } s_routing_component_dijkstra_t,*routing_component_dijkstra_t;
1044
1045 /* Parse routing model functions */
1046
1047 /* Business methods */
1048
1049 /* Creation routing model functions */
1050
1051 static void* model_dijkstra_create() {
1052   return NULL;
1053 }
1054
1055 static void  model_dijkstra_load() {
1056 }
1057
1058 static void  model_dijkstra_unload() {
1059 }
1060
1061 static void  model_dijkstra_end() {
1062 }
1063
1064 static void* model_dijkstracache_create() {
1065   return NULL;
1066 }
1067
1068 static void  model_dijkstracache_load() {
1069 }
1070
1071 static void  model_dijkstracache_unload() {
1072 }
1073
1074 static void  model_dijkstracache_end() {
1075 }
1076
1077
1078 /* ************************************************************************** */
1079 /* ******************************* NO ROUTING ******************************* */
1080
1081 /* Routing model structure */
1082 typedef struct {
1083   s_routing_component_t generic_routing;
1084 } s_routing_component_none_t,*routing_component_none_t;
1085
1086 /* Business methods */
1087 static route_extended_t none_get_route(routing_component_t rc, const char* src,const char* dst){
1088   return NULL;
1089 }
1090 static void none_finalize(routing_component_t rc) {
1091   xbt_free(rc);
1092 }
1093
1094 /* Creation routing model functions */
1095 static void* model_none_create() {
1096   routing_component_none_t new_component =  xbt_new0(s_routing_component_none_t,1);
1097   new_component->generic_routing.set_processing_units = NULL;
1098   new_component->generic_routing.set_autonomous_system = NULL;
1099   new_component->generic_routing.set_route = NULL;
1100   new_component->generic_routing.set_ASroute = NULL;
1101   new_component->generic_routing.get_route = none_get_route;
1102   new_component->generic_routing.finalize = none_finalize;
1103   return new_component;
1104 }
1105
1106 static void  model_none_load() {}
1107 static void  model_none_unload() {}
1108 static void  model_none_end() {}
1109
1110 /* ************************************************** */
1111 /* ********** PATERN FOR NEW ROUTING **************** */
1112
1113 /* The minimal configuration of a new routing model need the next functions,
1114  * also you need to set at the start of the file, the new model in the model
1115  * list. Remember keep the null ending of the list.
1116  */
1117 /* Routing model structure */
1118 typedef struct {
1119   s_routing_component_t generic_routing;
1120   /* things that your routing model need */
1121 } s_routing_component_NEW_t,*routing_component_NEW_t;
1122
1123 /* Parse routing model functions */
1124 static void NEW_parse_S_host(void) {} /* example*/
1125
1126 /* Business methods */
1127 static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;} /* mandatory */
1128 static void NEW_finalize(routing_component_t rc) {} /* mandatory */
1129
1130 /* Creation routing model functions */
1131 static void* model_NEW_create() {return NULL;} /* mandatory */
1132 static void  model_NEW_load() {}   /* mandatory */
1133 static void  model_NEW_unload() {} /* mandatory */
1134 static void  model_NEW_end() {}    /* mandatory */
1135
1136 /* ************************************************************************** */
1137 /* ************************* GENERIC PARSE FUNCTIONS ************************ */ 
1138
1139 static void generic_set_processing_units(routing_component_t rc, const char* name) {
1140    DEBUG1("Full - Load process unit \"%s\"",name);
1141    model_type_t modeltype = rc->routing;
1142    int *id = xbt_new0(int,1); // xbt_malloc(sizeof(int)); ?
1143    xbt_dict_t index;
1144    if(modeltype==&routing_models[SURF_MODEL_FULL])
1145      index = ((routing_component_full_t)current_routing)->to_index;
1146    else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1147      index = ((routing_component_floyd_t)current_routing)->to_index;   
1148    else xbt_die("\"generic_set_processing_units\" not support");
1149    *id = xbt_dict_length(index);
1150    xbt_dict_set(index,name,id,xbt_free);
1151 }
1152
1153 static void generic_set_autonomous_system(routing_component_t rc, const char* name) {
1154    DEBUG1("Full - Load Autonomous system \"%s\"",name);
1155    model_type_t modeltype = rc->routing;
1156    int *id = xbt_new0(int,1); // xbt_malloc(sizeof(int)); ?
1157    xbt_dict_t index;
1158    if(modeltype==&routing_models[SURF_MODEL_FULL])
1159      index = ((routing_component_full_t)current_routing)->to_index;
1160    else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1161      index = ((routing_component_floyd_t)current_routing)->to_index;
1162    else xbt_die("\"generic_set_autonomous_system\" not support");
1163    *id = xbt_dict_length(index);
1164    xbt_dict_set(index,name,id,xbt_free);
1165 }
1166
1167 static void generic_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {
1168   DEBUG2("Full - Load Route from \"%s\" to \"%s\"",src,dst);
1169   model_type_t modeltype = rc->routing;
1170   xbt_dict_t parseroutes;
1171   if(modeltype==&routing_models[SURF_MODEL_FULL])
1172     parseroutes = ((routing_component_full_t)current_routing)->parse_routes;
1173   else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1174     parseroutes = ((routing_component_floyd_t)current_routing)->parse_routes;
1175   else xbt_die("\"generic_set_autonomous_system\" not support");
1176   char *route_name;
1177   route_name = bprintf("%s#%s",src,dst);
1178   xbt_assert2(xbt_dynar_length(link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);   
1179   xbt_assert2(!xbt_dict_get_or_null(parseroutes,route_name),
1180     "The route between \"%s\" and \"%s\" already exist",src,dst);
1181   xbt_dict_set(parseroutes, route_name, route, NULL);
1182   free(route_name);
1183 }
1184
1185 static void generic_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {
1186   DEBUG4("Full - Load ASroute from \"%s(%s)\" to \"%s(%s)\"",src,e_route->src_gateway,dst,e_route->dst_gateway);
1187   model_type_t modeltype = rc->routing;
1188   xbt_dict_t parseroutes;
1189   if(modeltype==&routing_models[SURF_MODEL_FULL])
1190     parseroutes = ((routing_component_full_t)current_routing)->parse_routes;
1191   else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1192     parseroutes = ((routing_component_floyd_t)current_routing)->parse_routes;
1193   else xbt_die("\"generic_set_autonomous_system\" not support");
1194   char *route_name;
1195   route_name = bprintf("%s#%s",src,dst);
1196   xbt_assert2(xbt_dynar_length(link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);   
1197   xbt_assert4(!xbt_dict_get_or_null(parseroutes,route_name),
1198     "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",src,e_route->src_gateway,dst,e_route->dst_gateway);
1199   xbt_dict_set(parseroutes, route_name, e_route, NULL);
1200   free(route_name);
1201 }
1202
1203 /* ************************************************************************** */
1204 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1205
1206 static route_extended_t generic_new_extended_route(routing_component_t rc, void* data) {
1207   
1208   char *link_name;
1209   route_extended_t e_route, new_e_route;
1210   route_t route;
1211   unsigned int cpt;
1212   xbt_dynar_t links, links_id;
1213   
1214   new_e_route = xbt_new0(s_route_extended_t,1);
1215   new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1216   new_e_route->src_gateway = NULL;
1217   new_e_route->dst_gateway = NULL;
1218
1219   xbt_assert0(rc->hierarchy == SURF_ROUTING_BASE || rc->hierarchy == SURF_ROUTING_RECURSIVE,
1220       "the hierarchy type is not defined");
1221   
1222   if(rc->hierarchy == SURF_ROUTING_BASE ) {
1223     
1224     route = (route_t)data;
1225     links = route->link_list;
1226     
1227   } else if(rc->hierarchy == SURF_ROUTING_RECURSIVE ) {
1228
1229     e_route = (route_extended_t)data;
1230     
1231     // FIXME: HERE CHECK THE GATEWAY!!!!!!!!!
1232     xbt_assert0(e_route->src_gateway&&e_route->dst_gateway,"bad gateway, is null");
1233     
1234     links = e_route->generic_route.link_list;
1235     
1236     /* remeber not erase the gateway names */
1237     new_e_route->src_gateway = e_route->src_gateway;
1238     new_e_route->dst_gateway = e_route->dst_gateway;
1239   }
1240   
1241   links_id = new_e_route->generic_route.link_list;
1242   
1243   xbt_dynar_foreach(links, cpt, link_name) {
1244     
1245     void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1246     if (link)
1247       xbt_dynar_push(links_id,&link);
1248     else
1249       THROW1(mismatch_error,0,"Link %s not found", link_name);
1250   }
1251  
1252   return new_e_route;
1253 }
1254
1255 ////////////////////////////////////////////////////////////////////////////////
1256 // HERE FINISH THE NEW CODE
1257 ////////////////////////////////////////////////////////////////////////////////
1258
1259 //...... DEBUG ONLY .... //
1260 static void print_tree(routing_component_t rc) {
1261   printf("(%s %s)\n",rc->name,rc->routing->name);
1262   printf("  ");
1263   xbt_dict_cursor_t cursor = NULL;
1264   char *key;
1265   int *val;  
1266   if( rc->routing == &(routing_models[SURF_MODEL_FULL]) )
1267   { xbt_dict_foreach(((routing_component_full_t)rc)->to_index, cursor, key, val) { printf("<%s-%d> ",key,*val); } }
1268   else if( rc->routing == &(routing_models[SURF_MODEL_FLOYD]) )
1269   { xbt_dict_foreach(((routing_component_floyd_t)rc)->to_index, cursor, key, val) { printf("<%s-%d> ",key,*val); } 
1270   }
1271   else
1272       xbt_assert0(0, "Invalid model for call \"print_tree\"");
1273   printf("\n");  
1274   routing_component_t elem;
1275   xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
1276     printf("<--\n");  
1277     print_tree(elem);
1278     printf("-->\n");
1279   }
1280 }
1281
1282 //...... DEBUG ONLY .... //
1283 static void print_global() {
1284   xbt_dict_cursor_t cursor = NULL;
1285   char *key;
1286   routing_component_t elem;  
1287   xbt_dict_foreach(global_routing->where_network_elements, cursor, key, elem) { printf("<%s>\n",key); }
1288 }
1289
1290 //...... DEBUG ONLY .... //
1291 static void print_AS_start(void) { printf("AS!!! %s y se rutea \"%s\"\n",A_surfxml_AS_id,A_surfxml_AS_routing); }
1292 static void print_AS_end(void)   { printf("AS!!! %s\n",A_surfxml_AS_id); }
1293 static void print_host(void)     { printf("host!!! %s\n",A_surfxml_host_id); }
1294 static void print_link(void)     { printf("link!!! %s\n",A_surfxml_link_id); }
1295 static void print_route(void)    { printf("route!!! %s a %s\n",A_surfxml_route_src,A_surfxml_route_dst); }
1296 static void print_ctn(void)      { printf("ctn!!! %s\n",A_surfxml_link_c_ctn_id); }
1297
1298 //...... DEBUG ONLY .... //
1299 static void DEBUG_exit(void) {
1300   
1301   printf("-------- print tree elements -----\n");
1302   print_tree(global_routing->root);
1303   printf("----------------------------------\n\n");
1304   
1305   printf("-------- network_elements --------\n");
1306   print_global();
1307   printf("----------------------------------\n\n");
1308   
1309 //   char* names[] = { "11.A","11.B","11.C","121.A","121.B","122.A","13.A","13.B"};
1310 //   int i,j,total = 8;
1311
1312 //   printf("-- print all the example routes --\n");
1313 //   char* names[] = { "11.A","11.B","11.C"};
1314 //   int i,j,total = 3;
1315
1316 //   char* names[] = { "141.A","141.B","143.A","143.B"};
1317 //   int i,j,total = 4;
1318
1319 //   char* names[] = { "142.A","142.B","142.C" };
1320 //   int i,j,total = 3;
1321
1322 //   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"};
1323 //   int i,j,total = 3+4+8+3;
1324
1325    const char* names[] = { "11.A","11.B","11.C",
1326                            "121.A","121.B",
1327                            "122.A",
1328                            "13.A","13.B",
1329                            "141.A","141.B",
1330                            "142.A","142.B","142.C",
1331                            "143.A","143.B",
1332                            "15.A","15.B","15.C"};
1333    unsigned int i,j,total = 3+2+1+2+2+3+2+3;
1334
1335   printf("-- print all the example routes --\n");
1336   xbt_dynar_t links;
1337   void* link;
1338   for(i=0;i<total;i++) {
1339     for(j=0;j<total;j++) {
1340       printf("route from %s to %s >>> ",names[i],names[j]);
1341       links = (*(global_routing->get_route))(names[i],names[j]);
1342       unsigned int cpt=0;
1343       xbt_dynar_foreach(links, cpt, link) {
1344         s_surf_resource_t* generic_resource = link;
1345         printf(" %s",generic_resource->name);
1346       }
1347       printf("\n");
1348     }
1349   }
1350   
1351   printf("----------------------------------\n\n");
1352   
1353   printf("---------- call finalize ---------\n");
1354   (*(global_routing->finalize))();
1355   printf("----------------------------------\n");
1356   
1357   exit(0); 
1358 }
1359
1360 ////////////////////////////////////////////////////////////////////////////////
1361 // HERE END THE NEW CODE
1362 ////////////////////////////////////////////////////////////////////////////////
1363
1364 // /* ************************************************************************** */
1365 // /* *************************** FULL ROUTING ********************************* */
1366 // typedef struct {
1367 //   s_routing_t generic_routing;
1368 //   xbt_dynar_t *routing_table;
1369 //   void *loopback;
1370 //   size_t size_of_link;
1371 // } s_routing_full_t,*routing_full_t;
1372 // 
1373 // #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
1374 // #define HOST2ROUTER(id) ((id)+(2<<29))
1375 // #define ROUTER2HOST(id) ((id)-(2>>29))
1376 // #define ISROUTER(id) ((id)>=(2<<29))
1377 // 
1378 // /*
1379 //  * Free the onelink routes
1380 //  */
1381 // static void onelink_route_elem_free(void *e) {
1382 //   s_onelink_t tmp = (s_onelink_t)e;
1383 //   if(tmp) {
1384 //     free(tmp);
1385 //   }
1386 // }
1387 // 
1388 // /*
1389 //  * Parsing
1390 //  */
1391 // static void routing_full_parse_Shost(void) {
1392 //   int *val = xbt_malloc(sizeof(int));
1393 //   DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
1394 //   *val = used_routing->host_count++;
1395 //   xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
1396 // #ifdef HAVE_TRACING
1397 //   TRACE_surf_host_define_id (A_surfxml_host_id, *val);
1398 // #endif
1399 // }
1400 // 
1401 // static void routing_full_parse_Srouter(void) {
1402 //      int *val = xbt_malloc(sizeof(int));
1403 //   DEBUG3("Seen router %s (%d -> #%d)",A_surfxml_router_id,used_routing->router_count,
1404 //              HOST2ROUTER(used_routing->router_count));
1405 //   *val = HOST2ROUTER(used_routing->router_count++);
1406 //   xbt_dict_set(used_routing->host_id,A_surfxml_router_id,val,xbt_free);
1407 // #ifdef HAVE_TRACING
1408 //   TRACE_surf_host_define_id (A_surfxml_host_id, *val);
1409 //   TRACE_surf_host_declaration (A_surfxml_host_id, 0);
1410 // #endif
1411 // }
1412 // 
1413 // static int src_id = -1;
1414 // static int dst_id = -1;
1415 // static void routing_full_parse_Sroute_set_endpoints(void)
1416 // {
1417 //   src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
1418 //   dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
1419 //   DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
1420 //   route_action = A_surfxml_route_action;
1421 // }
1422 // 
1423 // static void routing_full_parse_Eroute(void)
1424 // {
1425 //   char *name;
1426 //   if (src_id != -1 && dst_id != -1) {
1427 //     name = bprintf("%x#%x", src_id, dst_id);
1428 //     manage_route(route_table, name, route_action, 0);
1429 //     free(name);
1430 //   }
1431 // }
1432 // 
1433 // /* Cluster tag functions */
1434 // 
1435 // static void routing_full_parse_change_cpu_data(const char *hostName,
1436 //                                   const char *surfxml_host_power,
1437 //                                   const char *surfxml_host_availability,
1438 //                                   const char *surfxml_host_availability_file,
1439 //                                   const char *surfxml_host_state_file)
1440 // {
1441 //   int AX_ptr = 0;
1442 // 
1443 //   SURFXML_BUFFER_SET(host_id, hostName);
1444 //   SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower */ );
1445 //   SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
1446 //   SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
1447 //   SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
1448 // }
1449 // 
1450 // static void routing_full_parse_change_link_data(const char *linkName,
1451 //                                    const char *surfxml_link_bandwidth,
1452 //                                    const char *surfxml_link_bandwidth_file,
1453 //                                    const char *surfxml_link_latency,
1454 //                                    const char *surfxml_link_latency_file,
1455 //                                    const char *surfxml_link_state_file)
1456 // {
1457 //   int AX_ptr = 0;
1458 // 
1459 //   SURFXML_BUFFER_SET(link_id, linkName);
1460 //   SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
1461 //   SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
1462 //   SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
1463 //   SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
1464 //   SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
1465 // }
1466 // 
1467 // static void routing_full_parse_Scluster(void)
1468 // {
1469 //   static int AX_ptr = 0;
1470 // 
1471 //   char *cluster_id = A_surfxml_cluster_id;
1472 //   char *cluster_prefix = A_surfxml_cluster_prefix;
1473 //   char *cluster_suffix = A_surfxml_cluster_suffix;
1474 //   char *cluster_radical = A_surfxml_cluster_radical;
1475 //   char *cluster_power = A_surfxml_cluster_power;
1476 //   char *cluster_bw = A_surfxml_cluster_bw;
1477 //   char *cluster_lat = A_surfxml_cluster_lat;
1478 //   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
1479 //   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
1480 //   char *backbone_name;
1481 //   unsigned int it1,it2;
1482 //   char *name1,*name2;
1483 //   xbt_dynar_t names = NULL;
1484 //   surfxml_bufferstack_push(1);
1485 // 
1486 //   /* Make set a set to parse the prefix/suffix/radical into a neat list of names */
1487 //   DEBUG4("Make <set id='%s' prefix='%s' suffix='%s' radical='%s'>",
1488 //       cluster_id,cluster_prefix,cluster_suffix,cluster_radical);
1489 //   SURFXML_BUFFER_SET(set_id, cluster_id);
1490 //   SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1491 //   SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1492 //   SURFXML_BUFFER_SET(set_radical, cluster_radical);
1493 // 
1494 //   SURFXML_START_TAG(set);
1495 //   SURFXML_END_TAG(set);
1496 // 
1497 //   names = xbt_dict_get(set_list,cluster_id);
1498 // 
1499 //   xbt_dynar_foreach(names,it1,name1) {
1500 //     /* create the host */
1501 //     routing_full_parse_change_cpu_data(name1, cluster_power, "1.0", "", "");
1502 //     A_surfxml_host_state = A_surfxml_host_state_ON;
1503 // 
1504 //     SURFXML_START_TAG(host);
1505 //     SURFXML_END_TAG(host);
1506 // 
1507 //     /* Here comes the link */
1508 //     routing_full_parse_change_link_data(name1, cluster_bw, "", cluster_lat, "", "");
1509 //     A_surfxml_link_state = A_surfxml_link_state_ON;
1510 //     A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1511 // 
1512 //     SURFXML_START_TAG(link);
1513 //     SURFXML_END_TAG(link);
1514 //   }
1515 // 
1516 //   /* Make backbone link */
1517 //   backbone_name = bprintf("%s_bb", cluster_id);
1518 //   routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
1519 //                          "");
1520 //   A_surfxml_link_state = A_surfxml_link_state_ON;
1521 //   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1522 // 
1523 //   SURFXML_START_TAG(link);
1524 //   SURFXML_END_TAG(link);
1525 // 
1526 //   /* And now the internal routes */
1527 //   xbt_dynar_foreach(names,it1,name1) {
1528 //     xbt_dynar_foreach(names,it2,name2) {
1529 //       if (strcmp(name1,name2)) {
1530 //         A_surfxml_route_action = A_surfxml_route_action_POSTPEND;
1531 //         SURFXML_BUFFER_SET(route_src,name1);
1532 //         SURFXML_BUFFER_SET(route_dst,name2);
1533 //         SURFXML_START_TAG(route); {
1534 //           /* FIXME: name1 link is added by error about 20 lines below, so don't add it here
1535 //           SURFXML_BUFFER_SET(link_c_ctn_id, name1);
1536 //           SURFXML_START_TAG(link_c_ctn);
1537 //           SURFXML_END_TAG(link_c_ctn);
1538 //            */
1539 //           SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1540 //           SURFXML_START_TAG(link_c_ctn);
1541 //           SURFXML_END_TAG(link_c_ctn);
1542 // 
1543 //           SURFXML_BUFFER_SET(link_c_ctn_id, name2);
1544 //           SURFXML_START_TAG(link_c_ctn);
1545 //           SURFXML_END_TAG(link_c_ctn);
1546 // 
1547 //         } SURFXML_END_TAG(route);
1548 //       }
1549 //     }
1550 //   }
1551 // 
1552 //   /* Make route multi with the outside world, i.e. cluster->$* */
1553 // 
1554 //   /* FIXME
1555 //    * This also adds an elements to the routes within the cluster,
1556 //    * and I guess it's wrong, but since this element is commented out in the above
1557 //    * code creating the internal routes, we're good.
1558 //    * To fix it, I'd say that we need a way to understand "$*-${cluster_id}" as "whole world, but the guys in that cluster"
1559 //    * But for that, we need to install a real expression parser for src/dst attributes
1560 //    *
1561 //    * FIXME
1562 //    * This also adds a dumb element (the private link) in place of the loopback. Then, since
1563 //    * the loopback is added only if no link to self already exist, this fails.
1564 //    * That's really dumb.
1565 //    *
1566 //    * FIXME
1567 //    * It seems to me that it does not add the backbone to the path to outside world...
1568 //    */
1569 //   SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1570 //   SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
1571 //   A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1572 //   A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_PREPEND;
1573 // 
1574 //   SURFXML_START_TAG(route_c_multi);
1575 // 
1576 //   SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
1577 // 
1578 //   SURFXML_START_TAG(link_c_ctn);
1579 //   SURFXML_END_TAG(link_c_ctn);
1580 // 
1581 //   SURFXML_END_TAG(route_c_multi);
1582 // 
1583 //   free(backbone_name);
1584 // 
1585 //   /* Restore buff */
1586 //   surfxml_bufferstack_pop(1);
1587 // }
1588 // 
1589 // 
1590 // static void routing_full_parse_end(void) {
1591 //   routing_full_t routing = (routing_full_t) used_routing;
1592 //   int nb_link = 0;
1593 //   unsigned int cpt = 0;
1594 //   xbt_dict_cursor_t cursor = NULL;
1595 //   char *key, *data, *end;
1596 //   const char *sep = "#";
1597 //   xbt_dynar_t links, keys;
1598 //   char *link_name = NULL;
1599 //   int i,j;
1600 // 
1601 //   int host_count = routing->generic_routing.host_count;
1602 // 
1603 //   /* Create the routing table */
1604 //   routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
1605 //   for (i=0;i<host_count;i++)
1606 //     for (j=0;j<host_count;j++)
1607 //       ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
1608 // 
1609 //   /* Put the routes in position */
1610 //   xbt_dict_foreach(route_table, cursor, key, data) {
1611 //     nb_link = 0;
1612 //     links = (xbt_dynar_t) data;
1613 //     keys = xbt_str_split_str(key, sep);
1614 // 
1615 //     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
1616 //     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
1617 //     xbt_dynar_free(&keys);
1618 // 
1619 //     if(xbt_dynar_length(links) == 1){
1620 //       s_onelink_t new_link = (s_onelink_t) xbt_malloc0(sizeof(s_onelink));
1621 //       new_link->src_id = src_id;
1622 //       new_link->dst_id = dst_id;
1623 //       link_name = xbt_dynar_getfirst_as(links, char*);
1624 //       new_link->link_ptr = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1625 //       DEBUG3("Adding onelink route from (#%d) to (#%d), link_name %s",src_id, dst_id, link_name);
1626 //       xbt_dict_set(onelink_routes, link_name, (void *)new_link, onelink_route_elem_free);
1627 // #ifdef HAVE_TRACING
1628 //       TRACE_surf_link_save_endpoints (link_name, src_id, dst_id);
1629 // #endif
1630 //     }
1631 // 
1632 //     if(ISROUTER(src_id) || ISROUTER(dst_id)) {
1633 //                              DEBUG2("There is route with a router here: (%d ,%d)",src_id,dst_id);
1634 //                              /* Check there is only one link in the route and store the information */
1635 //                              continue;
1636 //     }
1637 // 
1638 //     DEBUG4("Handle %d %d (from %d hosts): %ld links",
1639 //         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
1640 //     xbt_dynar_foreach(links, cpt, link_name) {
1641 //       void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1642 //       if (link)
1643 //         xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
1644 //       else
1645 //         THROW1(mismatch_error,0,"Link %s not found", link_name);
1646 //     }
1647 //   }
1648 // 
1649 //   /* Add the loopback if needed */
1650 //   for (i = 0; i < host_count; i++)
1651 //     if (!xbt_dynar_length(ROUTE_FULL(i, i)))
1652 //       xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
1653 // 
1654 //   /* Shrink the dynar routes (save unused slots) */
1655 //   for (i=0;i<host_count;i++)
1656 //     for (j=0;j<host_count;j++)
1657 //       xbt_dynar_shrink(ROUTE_FULL(i,j),0);
1658 // }
1659 // 
1660 // /*
1661 //  * Business methods
1662 //  */
1663 // static xbt_dynar_t routing_full_get_route(int src,int dst) {
1664 //   xbt_assert0(!(ISROUTER(src) || ISROUTER(dst)), "Ask for route \"from\" or \"to\" a router node");
1665 //   return ROUTE_FULL(src,dst);
1666 // }
1667 // 
1668 // static xbt_dict_t routing_full_get_onelink_routes(void){
1669 //   return onelink_routes;
1670 // }
1671 // 
1672 // static int routing_full_is_router(int id){
1673 //      return ISROUTER(id);
1674 // }
1675 // 
1676 // static void routing_full_finalize(void) {
1677 //   routing_full_t routing = (routing_full_t)used_routing;
1678 //   int i,j;
1679 // 
1680 //   if (routing) {
1681 //     for (i = 0; i < used_routing->host_count; i++)
1682 //       for (j = 0; j < used_routing->host_count; j++)
1683 //         xbt_dynar_free(&ROUTE_FULL(i, j));
1684 //     free(routing->routing_table);
1685 //     xbt_dict_free(&used_routing->host_id);
1686 //     xbt_dict_free(&onelink_routes);
1687 //     free(routing);
1688 //     routing=NULL;
1689 //   }
1690 // }
1691 // 
1692 // static void routing_model_full_create(size_t size_of_link,void *loopback) {
1693 //   /* initialize our structure */
1694 //   routing_full_t routing = xbt_new0(s_routing_full_t,1);
1695 //   routing->generic_routing.name = "Full";
1696 //   routing->generic_routing.host_count = 0;
1697 //   routing->generic_routing.get_route = routing_full_get_route;
1698 //   routing->generic_routing.get_onelink_routes = routing_full_get_onelink_routes;
1699 //   routing->generic_routing.is_router = routing_full_is_router;
1700 //   routing->generic_routing.finalize = routing_full_finalize;
1701 // 
1702 //   routing->size_of_link = size_of_link;
1703 //   routing->loopback = loopback;
1704 // 
1705 //   /* Set it in position */
1706 //   used_routing = (routing_t) routing;
1707 // 
1708 //   /* Set the dict for onehop routes */
1709 //   onelink_routes =  xbt_dict_new();
1710 // 
1711 //   routing->generic_routing.host_id = xbt_dict_new();
1712 //   
1713 //   /* Setup the parsing callbacks we need */
1714 // //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1715 // //   surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
1716 // //   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
1717 // //   surfxml_add_callback(STag_surfxml_route_cb_list, &routing_full_parse_Sroute_set_endpoints);
1718 // //   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
1719 // //   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_full_parse_Scluster);
1720 // 
1721 // //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1722 // //   surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
1723 //   
1724 // }
1725
1726 /* ************************************************************************** */
1727
1728 // static void routing_shortest_path_parse_Scluster(void)
1729 // {
1730 //   static int AX_ptr = 0;
1731 // 
1732 //   char *cluster_id = A_surfxml_cluster_id;
1733 //   char *cluster_prefix = A_surfxml_cluster_prefix;
1734 //   char *cluster_suffix = A_surfxml_cluster_suffix;
1735 //   char *cluster_radical = A_surfxml_cluster_radical;
1736 //   char *cluster_power = A_surfxml_cluster_power;
1737 //   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
1738 //   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
1739 //   char *backbone_name;
1740 // 
1741 //   surfxml_bufferstack_push(1);
1742 // 
1743 //   /* Make set */
1744 //   SURFXML_BUFFER_SET(set_id, cluster_id);
1745 //   SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1746 //   SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1747 //   SURFXML_BUFFER_SET(set_radical, cluster_radical);
1748 // 
1749 //   SURFXML_START_TAG(set);
1750 //   SURFXML_END_TAG(set);
1751 // 
1752 //   /* Make foreach */
1753 //   SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
1754 // 
1755 //   SURFXML_START_TAG(foreach);
1756 // 
1757 //   /* Make host for the foreach */
1758 //   routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
1759 //   A_surfxml_host_state = A_surfxml_host_state_ON;
1760 // 
1761 //   SURFXML_START_TAG(host);
1762 //   SURFXML_END_TAG(host);
1763 // 
1764 //   SURFXML_END_TAG(foreach);
1765 // 
1766 //   /* Make backbone link */
1767 //   backbone_name = bprintf("%s_bb", cluster_id);
1768 //   routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
1769 //                          "");
1770 //   A_surfxml_link_state = A_surfxml_link_state_ON;
1771 //   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1772 // 
1773 //   SURFXML_START_TAG(link);
1774 //   SURFXML_END_TAG(link);
1775 // 
1776 //   free(backbone_name);
1777 // 
1778 //   /* Restore buff */
1779 //   surfxml_bufferstack_pop(1);
1780 // }
1781 // 
1782 // /* ************************************************************************** */
1783 // /* *************************** FLOYD ROUTING ********************************* */
1784 // typedef struct {
1785 //   s_routing_t generic_routing;
1786 //   int *predecessor_table;
1787 //   void** link_table;
1788 //   xbt_dynar_t last_route;
1789 //   void *loopback;
1790 //   size_t size_of_link;
1791 // } s_routing_floyd_t,*routing_floyd_t;
1792 // 
1793 // #define FLOYD_COST(i,j) cost_table[(i)+(j)*(used_routing)->host_count]
1794 // #define FLOYD_PRED(i,j) ((routing_floyd_t)used_routing)->predecessor_table[(i)+(j)*(used_routing)->host_count]
1795 // #define FLOYD_LINK(i,j) ((routing_floyd_t)used_routing)->link_table[(i)+(j)*(used_routing)->host_count]
1796 // 
1797 // static void routing_floyd_parse_end(void) {
1798 // 
1799 //   routing_floyd_t routing = (routing_floyd_t) used_routing;
1800 //   int nb_link = 0;
1801 //   void* link_list = NULL;
1802 //   double * cost_table;
1803 //   xbt_dict_cursor_t cursor = NULL;
1804 //   char *key,*data, *end;
1805 //   const char *sep = "#";
1806 //   xbt_dynar_t links, keys;
1807 // 
1808 //   unsigned int i,j;
1809 //   unsigned int a,b,c;
1810 //   int host_count = routing->generic_routing.host_count;
1811 //   char * link_name = NULL;
1812 //   void * link = NULL;
1813 // 
1814 //   /* Create Cost, Predecessor and Link tables */
1815 //   cost_table = xbt_new0(double, host_count * host_count); //link cost from host to host
1816 //   routing->predecessor_table = xbt_new0(int, host_count*host_count); //predecessor host numbers
1817 //   routing->link_table = xbt_new0(void*,host_count*host_count); //actual link between src and dst
1818 //   routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
1819 // 
1820 //   /* Initialize costs and predecessors*/
1821 //   for(i = 0; i<host_count;i++)
1822 //     for(j = 0; j<host_count;j++) {
1823 //         FLOYD_COST(i,j) = DBL_MAX;
1824 //         FLOYD_PRED(i,j) = -1;
1825 //     }
1826 // 
1827 //    /* Put the routes in position */
1828 //   xbt_dict_foreach(route_table, cursor, key, data) {
1829 //     nb_link = 0;
1830 //     links = (xbt_dynar_t)data;
1831 //     keys = xbt_str_split_str(key, sep);
1832 // 
1833 //     
1834 //     src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
1835 //     dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
1836 //     xbt_dynar_free(&keys);
1837 //  
1838 //     DEBUG4("Handle %d %d (from %d hosts): %ld links",
1839 //         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
1840 //     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);
1841 //     
1842 //     link_name = xbt_dynar_getfirst_as(links, char*);
1843 //     link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1844 // 
1845 //     if (link)
1846 //       link_list = link;
1847 //     else
1848 //       THROW1(mismatch_error,0,"Link %s not found", link_name);
1849 //     
1850 // 
1851 //     FLOYD_LINK(src_id,dst_id) = link_list;
1852 //     FLOYD_PRED(src_id, dst_id) = src_id;
1853 // 
1854 //     //link cost
1855 //     FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
1856 // 
1857 //   }
1858 // 
1859 //     /* Add the loopback if needed */
1860 //   for (i = 0; i < host_count; i++)
1861 //     if (!FLOYD_PRED(i, i)) {
1862 //       FLOYD_PRED(i, i) = i;
1863 //       FLOYD_COST(i, i) = 1;
1864 //       FLOYD_LINK(i, i) = routing->loopback;
1865 //     }
1866 // 
1867 // 
1868 //   //Calculate path costs 
1869 // 
1870 //   for(c=0;c<host_count;c++) {
1871 //     for(a=0;a<host_count;a++) {
1872 //       for(b=0;b<host_count;b++) {
1873 //         if(FLOYD_COST(a,c) < DBL_MAX && FLOYD_COST(c,b) < DBL_MAX) {
1874 //           if(FLOYD_COST(a,b) == DBL_MAX || (FLOYD_COST(a,c)+FLOYD_COST(c,b) < FLOYD_COST(a,b))) {
1875 //             FLOYD_COST(a,b) = FLOYD_COST(a,c)+FLOYD_COST(c,b);
1876 //             FLOYD_PRED(a,b) = FLOYD_PRED(c,b);
1877 //           }
1878 //         }
1879 //       }
1880 //     }
1881 //   }
1882 // 
1883 //   //cleanup
1884 //   free(cost_table);
1885 // }
1886 // 
1887 // /*
1888 //  * Business methods
1889 //  */
1890 // static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
1891 // 
1892 //   routing_floyd_t routing = (routing_floyd_t) used_routing;
1893 // 
1894 //   int pred = dst_id;
1895 //   int prev_pred = 0;
1896 // 
1897 //   xbt_dynar_reset(routing->last_route);
1898 // 
1899 //   do {
1900 //     prev_pred = pred;
1901 //     pred = FLOYD_PRED(src_id, pred);
1902 // 
1903 //     if(pred == -1) // if no pred in route -> no route to host
1904 //         break;
1905 // 
1906 //     xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
1907 // 
1908 //   } while(pred != src_id);
1909 // 
1910 //   xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
1911 // 
1912 //   return routing->last_route;
1913 // }
1914 // 
1915 // static void routing_floyd_finalize(void) {
1916 //   routing_floyd_t routing = (routing_floyd_t)used_routing;
1917 // 
1918 //   if (routing) {
1919 //     free(routing->link_table);
1920 //     free(routing->predecessor_table);
1921 //     xbt_dynar_free(&routing->last_route);
1922 //     xbt_dict_free(&used_routing->host_id);
1923 //     free(routing);
1924 //     routing=NULL;
1925 //   }
1926 // }
1927 // 
1928 // static xbt_dict_t routing_floyd_get_onelink_routes(void){
1929 //   xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model Floyd");
1930 // }
1931 // 
1932 // static int routing_floyd_is_router(int id){
1933 //   xbt_assert0(0,"The get_is_router feature is not supported in routing model Floyd");
1934 // }
1935 // 
1936 // static void routing_model_floyd_create(size_t size_of_link,void *loopback) {
1937 //   /* initialize our structure */
1938 //   routing_floyd_t routing = xbt_new0(s_routing_floyd_t,1);
1939 //   routing->generic_routing.name = "Floyd";
1940 //   routing->generic_routing.host_count = 0;
1941 //   routing->generic_routing.host_id = xbt_dict_new();
1942 //   routing->generic_routing.get_route = routing_floyd_get_route;
1943 //   routing->generic_routing.get_onelink_routes = routing_floyd_get_onelink_routes;
1944 //   routing->generic_routing.is_router = routing_floyd_is_router;
1945 //   routing->generic_routing.finalize = routing_floyd_finalize;
1946 //   routing->size_of_link = size_of_link;
1947 //   routing->loopback = loopback;
1948 // 
1949 //   /* Set it in position */
1950 //   used_routing = (routing_t) routing;
1951 //   
1952 //   /* Setup the parsing callbacks we need */
1953 //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1954 //   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_floyd_parse_end);
1955 //   surfxml_add_callback(STag_surfxml_route_cb_list, 
1956 //       &routing_full_parse_Sroute_set_endpoints);
1957 //   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
1958 //   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
1959 //   
1960 // }
1961 // 
1962 // /* ************************************************************************** */
1963 // /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1964 // typedef struct {
1965 //   s_routing_t generic_routing;
1966 //   xbt_graph_t route_graph;
1967 //   xbt_dict_t graph_node_map;
1968 //   xbt_dict_t route_cache;
1969 //   xbt_dynar_t last_route;
1970 //   int cached;
1971 //   void *loopback;
1972 //   size_t size_of_link;
1973 // } s_routing_dijkstra_t,*routing_dijkstra_t;
1974 // 
1975 // 
1976 // typedef struct graph_node_data {
1977 //   int id; 
1978 //   int graph_id; //used for caching internal graph id's
1979 // } s_graph_node_data_t, * graph_node_data_t;
1980 // 
1981 // typedef struct graph_node_map_element {
1982 //   xbt_node_t node;
1983 // } s_graph_node_map_element_t, * graph_node_map_element_t;
1984 // 
1985 // typedef struct route_cache_element {
1986 //   int * pred_arr;
1987 //   int size;
1988 // } s_route_cache_element_t, * route_cache_element_t;  
1989 // 
1990 // /*
1991 //  * Free functions
1992 //  */
1993 // static void route_cache_elem_free(void *e) {
1994 //   route_cache_element_t elm=(route_cache_element_t)e;
1995 // 
1996 //   if (elm) {
1997 //     free(elm->pred_arr);
1998 //     free(elm);
1999 //   }
2000 // }
2001 // 
2002 // static void graph_node_map_elem_free(void *e) {
2003 //   graph_node_map_element_t elm = (graph_node_map_element_t)e;
2004 // 
2005 //   if(elm) {
2006 //     free(elm);
2007 //   }
2008 // }
2009 // 
2010 // /*
2011 //  * Utility functions
2012 // */
2013 // static xbt_node_t route_graph_new_node(int id, int graph_id) {
2014 //   xbt_node_t node = NULL;
2015 //   graph_node_data_t data = NULL;
2016 //   graph_node_map_element_t elm = NULL;
2017 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2018 // 
2019 //   data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
2020 //   data->id = id;
2021 //   data->graph_id = graph_id;
2022 //   node = xbt_graph_new_node(routing->route_graph, data);
2023 // 
2024 //   elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
2025 //   elm->node = node;
2026 //   xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
2027 // 
2028 //   return node;
2029 // }
2030 // 
2031 // static graph_node_map_element_t graph_node_map_search(int id) {
2032 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2033 // 
2034 //   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));
2035 // 
2036 //   return elm;
2037 // }
2038 // 
2039 // /*
2040 //  * Parsing
2041 //  */
2042 // static void route_new_dijkstra(int src_id, int dst_id, void* link) {
2043 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2044 // 
2045 //   xbt_node_t src = NULL;
2046 //   xbt_node_t dst = NULL;
2047 //   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));
2048 //   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));
2049 // 
2050 //   if(src_elm)
2051 //     src = src_elm->node;
2052 // 
2053 //   if(dst_elm)
2054 //     dst = dst_elm->node;
2055 // 
2056 //   //add nodes if they don't exist in the graph
2057 //   if(src_id == dst_id && src == NULL && dst == NULL) {
2058 //     src = route_graph_new_node(src_id, -1);
2059 //     dst = src;
2060 //   } else {
2061 //     if(src == NULL) {
2062 //       src = route_graph_new_node(src_id, -1);
2063 //     }
2064 //      
2065 //     if(dst == NULL) {
2066 //       dst = route_graph_new_node(dst_id, -1);
2067 //     }
2068 //   }
2069 // 
2070 //   //add link as edge to graph
2071 //   xbt_graph_new_edge(routing->route_graph, src, dst, link);
2072 //   
2073 // }
2074 // 
2075 // static void add_loopback_dijkstra(void) {
2076 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2077 // 
2078 //      xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2079 //      
2080 //      xbt_node_t node = NULL;
2081 //      unsigned int cursor2;
2082 //      xbt_dynar_foreach(nodes, cursor2, node) {
2083 //              xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node); 
2084 //              xbt_edge_t edge = NULL;
2085 //              unsigned int cursor;
2086 //      
2087 //              int found = 0;
2088 //              xbt_dynar_foreach(out_edges, cursor, edge) {
2089 //                      xbt_node_t other_node = xbt_graph_edge_get_target(edge);
2090 //                      if(other_node == node) {
2091 //                              found = 1;
2092 //                              break;
2093 //                      }
2094 //              }
2095 // 
2096 //              if(!found)
2097 //                      xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
2098 //      }
2099 // }
2100 // 
2101 // static void routing_dijkstra_parse_end(void) {
2102 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2103 //   int nb_link = 0;
2104 //   xbt_dict_cursor_t cursor = NULL;
2105 //   char *key, *data, *end;
2106 //   const char *sep = "#";
2107 //   xbt_dynar_t links, keys;
2108 //   char* link_name = NULL;
2109 //   void* link = NULL;
2110 //   xbt_node_t node = NULL;
2111 //   unsigned int cursor2;
2112 //   xbt_dynar_t nodes = NULL;
2113 //   /* Create the topology graph */
2114 //   routing->route_graph = xbt_graph_new_graph(1, NULL);
2115 //   routing->graph_node_map = xbt_dict_new();
2116 //   routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
2117 //   if(routing->cached)
2118 //     routing->route_cache = xbt_dict_new();
2119 // 
2120 // 
2121 //   /* Put the routes in position */
2122 //   xbt_dict_foreach(route_table, cursor, key, data) {
2123 //     nb_link = 0;
2124 //     links = (xbt_dynar_t) data;
2125 //     keys = xbt_str_split_str(key, sep);
2126 // 
2127 //     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
2128 //     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
2129 //     xbt_dynar_free(&keys);
2130 // 
2131 //     DEBUG4("Handle %d %d (from %d hosts): %ld links",
2132 //         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
2133 // 
2134 //     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);
2135 // 
2136 //     link_name = xbt_dynar_getfirst_as(links, char*);
2137 //     link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2138 //     if (link)
2139 //       route_new_dijkstra(src_id,dst_id,link);
2140 //     else
2141 //       THROW1(mismatch_error,0,"Link %s not found", link_name);
2142 //     
2143 //   }
2144 // 
2145 //   /* Add the loopback if needed */
2146 //   add_loopback_dijkstra();
2147 // 
2148 //   /* initialize graph indexes in nodes after graph has been built */
2149 //   nodes = xbt_graph_get_nodes(routing->route_graph);
2150 // 
2151 //   xbt_dynar_foreach(nodes, cursor2, node) {
2152 //     graph_node_data_t data = xbt_graph_node_get_data(node);
2153 //     data->graph_id = cursor2;
2154 //   }
2155 // 
2156 // }
2157 // 
2158 // /*
2159 //  * Business methods
2160 //  */
2161 // static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
2162 // 
2163 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2164 //   int * pred_arr = NULL;
2165 //   int src_node_id = 0;
2166 //   int dst_node_id = 0;
2167 //   int * nodeid = NULL;
2168 //   int v;
2169 //   int size = 0;
2170 //   void * link = NULL;
2171 //   route_cache_element_t elm = NULL;
2172 //   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2173 // 
2174 //   /*Use the graph_node id mapping set to quickly find the nodes */
2175 //   graph_node_map_element_t src_elm = graph_node_map_search(src_id);
2176 //   graph_node_map_element_t dst_elm = graph_node_map_search(dst_id);
2177 //   xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", src_id, dst_id);
2178 //   src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
2179 //   dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
2180 // 
2181 //   if(routing->cached) {
2182 //     /*check if there is a cached predecessor list avail */
2183 //     elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
2184 //   }
2185 // 
2186 //   if(elm) { //cached mode and cache hit
2187 //     pred_arr = elm->pred_arr;
2188 //   } else { //not cached mode or cache miss
2189 //     double * cost_arr = NULL;
2190 //     xbt_heap_t pqueue = NULL;
2191 //     int i = 0;
2192 // 
2193 //     int nr_nodes = xbt_dynar_length(nodes);
2194 //     cost_arr = xbt_new0(double, nr_nodes); //link cost from src to other hosts
2195 //     pred_arr = xbt_new0(int, nr_nodes); //predecessors in path from src
2196 //     pqueue = xbt_heap_new(nr_nodes, free);
2197 // 
2198 //     //initialize
2199 //     cost_arr[src_node_id] = 0.0;
2200 // 
2201 //     for(i = 0; i < nr_nodes; i++) {
2202 //       if(i != src_node_id) {
2203 //         cost_arr[i] = DBL_MAX;
2204 //       }
2205 // 
2206 //       pred_arr[i] = 0;
2207 // 
2208 //       //initialize priority queue
2209 //       nodeid = xbt_new0(int, 1);
2210 //       *nodeid = i;
2211 //       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
2212 // 
2213 //     }
2214 // 
2215 //     // apply dijkstra using the indexes from the graph's node array
2216 //     while(xbt_heap_size(pqueue) > 0) {
2217 //       int * v_id = xbt_heap_pop(pqueue);
2218 //       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
2219 //       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node); 
2220 //       xbt_edge_t edge = NULL;
2221 //       unsigned int cursor;
2222 // 
2223 //       xbt_dynar_foreach(out_edges, cursor, edge) {
2224 //         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
2225 //         graph_node_data_t data = xbt_graph_node_get_data(u_node);
2226 //         int u_id = data->graph_id;
2227 //         int cost_v_u = 1; //fixed link cost for now
2228 // 
2229 //         if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
2230 //           pred_arr[u_id] = *v_id;
2231 //           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
2232 //           nodeid = xbt_new0(int, 1);
2233 //           *nodeid = u_id;
2234 //           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
2235 //         }
2236 //       }
2237 // 
2238 //       //free item popped from pqueue
2239 //       free(v_id);
2240 //     }
2241 // 
2242 //     free(cost_arr);
2243 //     xbt_heap_free(pqueue);
2244 // 
2245 //   }
2246 // 
2247 //   //compose route path with links
2248 //   xbt_dynar_reset(routing->last_route);
2249 // 
2250 //   for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
2251 //     xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
2252 //     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
2253 //     xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
2254 // 
2255 //     xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
2256 // 
2257 //     link = xbt_graph_edge_get_data(edge);
2258 //     xbt_dynar_unshift(routing->last_route, &link);
2259 //     size++;
2260 //   }
2261 // 
2262 // 
2263 //   if(routing->cached && elm == NULL) {
2264 //     //add to predecessor list of the current src-host to cache
2265 //     elm = xbt_new0(struct route_cache_element, sizeof(struct route_cache_element));
2266 //     elm->pred_arr = pred_arr;
2267 //     elm->size = size;
2268 //     xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
2269 //   }
2270 // 
2271 //   if(!routing->cached)
2272 //     free(pred_arr);
2273 // 
2274 //   return routing->last_route;
2275 // }
2276 // 
2277 // 
2278 // static void routing_dijkstra_finalize(void) {
2279 //   routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
2280 // 
2281 //   if (routing) {
2282 //     xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
2283 //     xbt_dict_free(&routing->graph_node_map);
2284 //     if(routing->cached)
2285 //       xbt_dict_free(&routing->route_cache);
2286 //     xbt_dynar_free(&routing->last_route);
2287 //     xbt_dict_free(&used_routing->host_id);
2288 //     free(routing);
2289 //     routing=NULL;
2290 //   }
2291 // }
2292 // 
2293 // static xbt_dict_t routing_dijkstraboth_get_onelink_routes(void){
2294 //   xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model dijkstraboth");
2295 // }
2296 // 
2297 // static int routing_dijkstraboth_is_router(int id){
2298 //   xbt_assert0(0,"The get_is_router feature is not supported in routing model dijkstraboth");
2299 // }
2300 // 
2301 // /*
2302 //  *
2303 //  */
2304 // static void routing_model_dijkstraboth_create(size_t size_of_link,void *loopback, int cached) {
2305 //   /* initialize our structure */
2306 //   routing_dijkstra_t routing = xbt_new0(s_routing_dijkstra_t,1);
2307 //   routing->generic_routing.name = "Dijkstra";
2308 //   routing->generic_routing.host_count = 0;
2309 //   routing->generic_routing.get_route = routing_dijkstra_get_route;
2310 //   routing->generic_routing.get_onelink_routes = routing_dijkstraboth_get_onelink_routes;
2311 //   routing->generic_routing.is_router = routing_dijkstraboth_is_router;
2312 //   routing->generic_routing.finalize = routing_dijkstra_finalize;
2313 //   routing->size_of_link = size_of_link;
2314 //   routing->loopback = loopback;
2315 //   routing->cached = cached;
2316 // 
2317 //   /* Set it in position */
2318 //   used_routing = (routing_t) routing;
2319 // 
2320 //   /* Setup the parsing callbacks we need */
2321 //   routing->generic_routing.host_id = xbt_dict_new();
2322 //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2323 //   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_dijkstra_parse_end);
2324 //   surfxml_add_callback(STag_surfxml_route_cb_list,
2325 //       &routing_full_parse_Sroute_set_endpoints);
2326 //   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
2327 //   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
2328 // }
2329 // 
2330 // static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
2331 //   routing_model_dijkstraboth_create(size_of_link, loopback, 0);
2332 // }
2333 // 
2334 // static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
2335 //   routing_model_dijkstraboth_create(size_of_link, loopback, 1);
2336 // }
2337
2338 /* ************************************************** */
2339 /* ********** NO ROUTING **************************** */
2340
2341
2342 // static void routing_none_finalize(void) {
2343 //   if (used_routing) {
2344 //     xbt_dict_free(&used_routing->host_id);
2345 //     free(used_routing);
2346 //     used_routing=NULL;
2347 //   }
2348 // }
2349 // 
2350 // static void routing_model_none_create(size_t size_of_link,void *loopback) {
2351 //   routing_t routing = xbt_new0(s_routing_t,1);
2352 //   INFO0("Null routing");
2353 //   routing->name = "none";
2354 //   routing->host_count = 0;
2355 //   routing->host_id = xbt_dict_new();
2356 //   routing->get_onelink_routes = NULL;
2357 //   routing->is_router = NULL;
2358 //   routing->get_route = NULL;
2359 // 
2360 //   routing->finalize = routing_none_finalize;
2361 // 
2362 //   /* Set it in position */
2363 //   used_routing = (routing_t) routing;
2364 // }