Logo AND Algorithmique Numérique Distribuée

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