Logo AND Algorithmique Numérique Distribuée

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