Logo AND Algorithmique Numérique Distribuée

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