Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e508683260dc4d797732d59b6cec04b776166caf
[simgrid.git] / src / surf / surf_routing.c
1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <float.h>
8 #include "surf_private.h"
9 #include "xbt/dynar.h"
10 #include "xbt/str.h"
11 #include "xbt/config.h"
12 #include "xbt/graph.h"
13 #include "xbt/set.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route,surf,"Routing part of surf");
16
17
18 // FIXME: the next two lines cant be comented without fix the code in network.c file.
19 // routing_t used_routing = NULL;
20 // xbt_dict_t onelink_routes = NULL;
21
22 // 
23 // /* Prototypes of each model */
24 // static void routing_model_full_create(size_t size_of_link,void *loopback);
25 // // static void routing_model_floyd_create(size_t size_of_link, void*loopback);
26 // // static void routing_model_dijkstra_create(size_t size_of_link, void*loopback);
27 // // static void routing_model_dijkstracache_create(size_t size_of_link, void*loopback);
28 // //static void routing_model_none_create(size_t size_of_link, void*loopback);
29 // 
30 // /* Definition of each model */
31 // struct model_type {
32 //   const char *name;
33 //   const char *desc;
34 //   void (*fun)(size_t,void*);
35 // };
36 // 
37 // struct model_type models[] =
38 // { {"Full", "Full routing data (fast, large memory requirements, fully expressive)", routing_model_full_create },
39 // //   {"Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)", routing_model_floyd_create },
40 // //   {"Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)", routing_model_dijkstra_create },
41 // //   {"DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)", routing_model_dijkstracache_create },
42 // //  {"none", "No routing (usable with Constant network only)", routing_model_none_create },
43 //     {NULL,NULL,NULL}};
44     
45 ////////////////////////////////////////////////////////////////////////////////
46 // HERE START THE NEW CODE
47 ////////////////////////////////////////////////////////////////////////////////
48
49 //...... DEBUG ONLY .... //
50 static void print_tree(routing_component_t rc);
51 static void print_global();
52 static void print_AS_start(void);
53 static void print_AS_end(void);
54 static void print_host(void);
55 static void print_link(void);
56 static void print_route(void);
57 static void print_ctn(void);
58 static void DEGUB_exit(void);
59
60 ////////////////////////////////////////////////////////////////////////////////
61 // HERE START THE NEW CODE
62 ////////////////////////////////////////////////////////////////////////////////
63
64 /* Global vars */
65 routing_global_t global_routing = NULL;
66 routing_component_t current_routing = NULL;
67 model_type_t current_routing_model = NULL;
68
69 /* Prototypes of each model */
70 static void* model_full_create(void); /* create structures for full routing model */
71 static void  model_full_load(void);   /* load parse functions for full routing model */
72 static void  model_full_unload(void); /* unload parse functions for full routing model */
73 static void  model_full_end(void);    /* finalize the creation of full routing model */
74
75 static void* model_floyd_create(void); /* create structures for floyd routing model */
76 static void  model_floyd_load(void);   /* load parse functions for floyd routing model */
77 static void  model_floyd_unload(void); /* unload parse functions for floyd routing model */
78 static void  model_floyd_end(void);    /* finalize the creation of floyd routing model */
79
80 static void* model_dijkstra_create(void); /* create structures for dijkstra routing model */
81 static void  model_dijkstra_load(void);   /* load parse functions for dijkstra routing model */
82 static void  model_dijkstra_unload(void); /* unload parse functions for dijkstra routing model */
83 static void  model_dijkstra_end(void);    /* finalize the creation of dijkstra routing model */
84
85 static void* model_dijkstracache_create(void); /* create structures for dijkstracache routing model */
86 static void  model_dijkstracache_load(void);   /* load parse functions for dijkstracache routing model */
87 static void  model_dijkstracache_unload(void); /* unload parse functions for dijkstracache routing model */
88 static void  model_dijkstracache_end(void);    /* finalize the creation of dijkstracache routing model */
89
90 static void* model_none_create(void); /* none routing model */
91 static void  model_none_load(void);   /* none routing model */
92 static void  model_none_unload(void); /* none routing model */
93 static void  model_none_end(void);    /* none routing model */
94
95 /* Table of routing models */
96 /* must be finish with null and carefull if change de order */
97
98 #define SURF_MODEL_FULL          0
99 #define SURF_MODEL_FLOYD         1
100 #define SURF_MODEL_DIJKSTRA      2
101 #define SURF_MODEL_DIJKSTRACACHE 3
102 #define SURF_MODEL_NONE          4
103
104 struct s_model_type routing_models[] =
105 { {"Full", "Full routing data (fast, large memory requirements, fully expressive)",
106   model_full_create, model_full_load, model_full_unload, model_full_end },  
107   {"Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
108   model_floyd_create, model_floyd_load, model_floyd_unload, model_floyd_end },
109   {"Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
110   model_dijkstra_create ,model_dijkstra_load, model_dijkstra_unload, model_dijkstra_end },
111   {"DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
112   model_dijkstracache_create, model_dijkstracache_load, model_dijkstracache_unload, model_dijkstracache_end },
113   {"none", "No routing (usable with Constant network only)",
114   model_none_create, model_none_load, model_none_unload, model_none_end },
115   {NULL,NULL,NULL,NULL,NULL,NULL}};
116
117   
118 /* global parse functions */
119
120 /**
121  * \brief Add a "host" to the network element list
122  *
123  * Allows find a "host" in any routing component
124  */
125 static void  parse_S_host(void) {
126   xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_host_id),
127       "The host \"%s\" already exist",A_surfxml_host_id);
128   xbt_dict_set(global_routing->where_network_elements,A_surfxml_host_id,(void*)current_routing,NULL); 
129 }
130
131 /**
132  * \brief Add a "router" to the network element list
133  *
134  * Allows find a "router" in any routing component
135  */
136 static void parse_S_router(void) {
137   xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_router_id),
138       "The router \"%s\" already exist",A_surfxml_router_id);
139   xbt_dict_set(global_routing->where_network_elements,A_surfxml_router_id,(void*)current_routing,NULL); 
140 }
141
142 /**
143  * \brief Add a "gateway" to the network element list
144  *
145  * Allows find a "gateway" in any routing component
146  */
147 static void parse_S_gateway(void) {
148   xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_gateway_id),
149       "The gateway \"%s\" already exist",A_surfxml_gateway_id);
150   xbt_dict_set(global_routing->where_network_elements,A_surfxml_gateway_id,(void*)current_routing,NULL); 
151 }
152
153 /**
154  * \brief Make a new routing component
155  *
156  * Detect the routing model type of the routing component, make the new structure and
157  * set the parsing functions to allows parsing the part of the routing tree
158  */
159 static void parse_S_AS(void) { 
160   routing_component_t new_routing;
161   model_type_t model = NULL;
162   char* wanted = A_surfxml_AS_routing;
163   int cpt;
164   /* seach the routing model */
165   for (cpt=0;routing_models[cpt].name;cpt++)
166     if (!strcmp(wanted,routing_models[cpt].name))
167           model = &routing_models[cpt];
168   /* if its not exist, error */
169   if( model == NULL ) {
170     fprintf(stderr,"Routing model %s not found. Existing models:\n",wanted);
171     for (cpt=0;routing_models[cpt].name;cpt++)
172       if (!strcmp(wanted,routing_models[cpt].name))
173         fprintf(stderr,"   %s: %s\n",routing_models[cpt].name,routing_models[cpt].desc);
174     exit(1);
175   }
176
177   /* make a new routing component */
178   new_routing = (routing_component_t)(*(model->create))();
179   new_routing->routing = model;
180   new_routing->name = xbt_strdup(A_surfxml_AS_id);
181   new_routing->routing_sons = xbt_dict_new();
182   INFO2("Routing %s for AS %s",A_surfxml_AS_routing,A_surfxml_AS_id);
183   
184   if( current_routing == NULL && global_routing->root == NULL )
185   { /* it is the first one */
186     new_routing->routing_father = NULL;
187     global_routing->root = new_routing;
188   }
189   else if( current_routing != NULL && global_routing->root != NULL )
190   {  /* it is a part of the tree */
191     new_routing->routing_father = current_routing;
192     xbt_assert1(!xbt_dict_get_or_null(current_routing->routing_sons,A_surfxml_AS_id),
193            "The AS \"%s\" already exist",A_surfxml_AS_id);
194     xbt_dict_set(current_routing->routing_sons,A_surfxml_AS_id,(void*)new_routing,NULL);
195     /* unload the prev parse rules */
196     (*(current_routing->routing->unload))();
197   }
198   else
199   {
200     THROW0(arg_error,0,"All defined components must be belong to a AS");
201   }
202   /* set the new parse rules */
203   (*(new_routing->routing->load))();
204   /* set the new current component of the tree */
205   current_routing = new_routing;
206 }
207
208 /**
209  * \brief Finish the creation of a new routing component
210  *
211  * When you finish to read the routing component, other structures must be created. 
212  * the "end" method allow to do that for any routing model type
213  */
214 static void parse_E_AS(void) {
215
216   if( current_routing == NULL ) {
217     THROW1(arg_error,0,"Close AS(%s), that never open",A_surfxml_AS_id);
218   } else {
219       (*(current_routing->routing->unload))();
220       (*(current_routing->routing->end))();
221       current_routing = current_routing->routing_father;
222       if( current_routing != NULL )
223         (*(current_routing->routing->load))();
224   }
225 }
226
227 /**
228  * \brief Recursive function for add the differents AS to global dict
229  *
230  * This fuction is call by "parse_E_platform_add_parse_AS". It allow to add the 
231  * AS or routing components, to the where_network_elements dictionary. In the same
232  * way as "parse_S_host", "parse_S_router" and "parse_S_gateway" do.
233  */
234 static void _add_parse_AS(routing_component_t rc) {
235   xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,rc->name),
236      "The AS \"%s\" already exist",rc->name);
237   xbt_dict_set(global_routing->where_network_elements,rc->name,rc->routing_father,NULL);
238   xbt_dict_cursor_t cursor = NULL;
239   char *key;
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 count_src, count_dst;
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   unsigned 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(count_src=index_src;count_src>=0;count_src--) {
357       current_src = xbt_dynar_get_ptr(path_src,count_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(count_dst=index_dst;count_dst>=0;count_dst--) {
377       current_dst = xbt_dynar_get_ptr(path_dst,count_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, *link_name, *src_name, *dst_name,  *src_gw_name, *dst_gw_name;
678   const char* sep = "#";
679   network_element_t src_id, dst_id, ne;
680   route_extended_t new_e_route;
681   unsigned 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, *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, 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   return NULL;
1290 }
1291
1292 static void  model_dijkstra_load() {
1293 }
1294
1295 static void  model_dijkstra_unload() {
1296 }
1297
1298 static void  model_dijkstra_end() {
1299 }
1300
1301 static void* model_dijkstracache_create() {
1302   return NULL;
1303 }
1304
1305 static void  model_dijkstracache_load() {
1306 }
1307
1308 static void  model_dijkstracache_unload() {
1309 }
1310
1311 static void  model_dijkstracache_end() {
1312 }
1313
1314
1315 /* ************************************************************************** */
1316 /* ******************************* NO ROUTING ******************************* */
1317
1318 /* Routing model structure */
1319 typedef struct {
1320   s_routing_component_t generic_routing;
1321 } s_routing_component_none_t,*routing_component_none_t;
1322
1323 /* Business methods */
1324 static route_extended_t none_get_route(routing_component_t rc, const char* src,const char* dst){
1325   return NULL;
1326 }
1327 static void none_finalize(routing_component_t rc) {
1328   xbt_free(rc);
1329 }
1330
1331 /* Creation routing model functions */
1332 static void* model_none_create() {
1333   routing_component_none_t new_component =  xbt_new0(s_routing_component_none_t,1);
1334   new_component->generic_routing.get_route = none_get_route;
1335   new_component->generic_routing.finalize = none_finalize;
1336   return new_component;
1337 }
1338
1339 static void  model_none_load() {}
1340 static void  model_none_unload() {}
1341 static void  model_none_end() {}
1342
1343 /* ************************************************** */
1344 /* ********** PATERN FOR NEW ROUTING **************** */
1345
1346 /* The minimal configuration of a new routing model need the next functions,
1347  * also you need to set at the start of the file, the new model in the model
1348  * list. Remember keep the null ending of the list.
1349  */
1350 /* Routing model structure */
1351 typedef struct {
1352   s_routing_component_t generic_routing;
1353   /* things that your routing model need */
1354 } s_routing_component_NEW_t,*routing_component_NEW_t;
1355
1356 /* Parse routing model functions */
1357 static void NEW_parse_S_host(void) {} /* example*/
1358
1359 /* Business methods */
1360 static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;} /* mandatory */
1361 static void NEW_finalize(routing_component_t rc) {} /* mandatory */
1362
1363 /* Creation routing model functions */
1364 static void* model_NEW_create() {return NULL;} /* mandatory */
1365 static void  model_NEW_load() {}   /* mandatory */
1366 static void  model_NEW_unload() {} /* mandatory */
1367 static void  model_NEW_end() {}    /* mandatory */
1368
1369 /* ************************************************************************** */
1370 /* ************************* GENERIC PARSE FUNCTIONS ************************ */ 
1371
1372 static void generic_parse_S_route_new_and_endpoints(void) {
1373   if( src != NULL && dst != NULL && link_list != NULL )
1374     THROW2(arg_error,0,"Route between %s to %s can not be defined",A_surfxml_route_src,A_surfxml_route_dst);
1375   src = A_surfxml_route_src;
1376   dst = A_surfxml_route_dst;
1377   gw_src = A_surfxml_route_gw_src;
1378   gw_dst = A_surfxml_route_gw_dst;
1379   link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
1380 }
1381
1382 static void generic_E_link_c_ctn_new_elem(void) {
1383   char *val;
1384   val = xbt_strdup(A_surfxml_link_c_ctn_id);
1385   xbt_dynar_push(link_list, &val);
1386 }
1387
1388 static void generic_parse_E_route_store_route(void) {
1389   char *route_name;
1390   xbt_dict_t parse_routes;
1391   if( current_routing->routing == &(routing_models[SURF_MODEL_FULL]) )
1392       parse_routes = ((routing_component_full_t)current_routing)->parse_routes;
1393   else if( current_routing->routing == &(routing_models[SURF_MODEL_FLOYD]) )
1394       parse_routes = ((routing_component_floyd_t)current_routing)->parse_routes; 
1395   else
1396       xbt_assert0(0, "Invalid model for call \"generic_parse_E_route_store_route\"");
1397   route_name = bprintf("%s#%s#%s#%s",src,dst,gw_src,gw_dst);
1398   xbt_assert2(xbt_dynar_length(link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);   
1399   xbt_assert3(!xbt_dict_get_or_null(parse_routes,route_name),
1400     "The route between \"%s\" and \"%s\" already exist (\"%s\")",src,dst,route_name);
1401   xbt_dict_set(parse_routes, route_name, link_list, NULL);
1402   free(route_name);
1403   src = NULL;
1404   dst = NULL;
1405   link_list = NULL;
1406 }
1407
1408
1409 ////////////////////////////////////////////////////////////////////////////////
1410 // HERE FINISH THE NEW CODE
1411 ////////////////////////////////////////////////////////////////////////////////
1412
1413 //...... DEBUG ONLY .... //
1414 static void print_tree(routing_component_t rc) {
1415   printf("(%s %s)\n",rc->name,rc->routing->name);
1416   printf("  ");
1417   xbt_dict_cursor_t cursor = NULL;
1418   char *key;
1419   int *val;  
1420   if( rc->routing == &(routing_models[SURF_MODEL_FULL]) )
1421   { xbt_dict_foreach(((routing_component_full_t)rc)->to_index, cursor, key, val) { printf("<%s-%d> ",key,*val); } }
1422   else if( rc->routing == &(routing_models[SURF_MODEL_FLOYD]) )
1423   { xbt_dict_foreach(((routing_component_floyd_t)rc)->predecessor_to_index, cursor, key, val) { printf("<%s-%d> ",key,*val); } 
1424     xbt_dict_foreach(((routing_component_floyd_t)rc)->AS_to_index, cursor, key, val) { printf("<%s-%d> ",key,*val); } }
1425   else
1426       xbt_assert0(0, "Invalid model for call \"print_tree\"");
1427   printf("\n");  
1428   routing_component_t elem;
1429   xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
1430     printf("<--\n");  
1431     print_tree(elem);
1432     printf("-->\n");
1433   }
1434 }
1435
1436 //...... DEBUG ONLY .... //
1437 static void print_global() {
1438   xbt_dict_cursor_t cursor = NULL;
1439   char *key;
1440   routing_component_t elem;  
1441   xbt_dict_foreach(global_routing->where_network_elements, cursor, key, elem) {
1442     printf("<%s>\n",key);
1443   }
1444 }
1445
1446 //...... DEBUG ONLY .... //
1447 static void print_AS_start(void) { printf("AS!!! %s y se rutea \"%s\"\n",A_surfxml_AS_id,A_surfxml_AS_routing); }
1448 static void print_AS_end(void) { printf("AS!!! %s\n",A_surfxml_AS_id); }
1449 static void print_host(void) { printf("host!!! %s\n",A_surfxml_host_id); }
1450 static void print_link(void) { printf("link!!! %s\n",A_surfxml_link_id); }
1451 static void print_route(void) { printf("route!!! %s a %s\n",A_surfxml_route_src,A_surfxml_route_dst); }
1452 static void print_ctn(void) { printf("ctn!!! %s\n",A_surfxml_link_c_ctn_id); }
1453
1454 //...... DEBUG ONLY .... //
1455 static void DEGUB_exit(void) {
1456   
1457   printf("-------- print tree elements -----\n");
1458   print_tree(global_routing->root);
1459   printf("----------------------------------\n\n");
1460   
1461   printf("-------- network_elements --------\n");
1462   print_global();
1463   printf("----------------------------------\n\n");
1464   
1465 //   char* names[] = { "11.A","11.B","11.C","121.A","121.B","122.A","13.A","13.B"};
1466 //   int i,j,total = 8;
1467
1468 //   printf("-- print all the example routes --\n");
1469 //   char* names[] = { "11.A","11.B","11.C"};
1470 //   int i,j,total = 3;
1471
1472 //   char* names[] = { "141.A","141.B","143.A","143.B"};
1473 //   int i,j,total = 4;
1474
1475 //   char* names[] = { "142.A","142.B","142.C" };
1476 //   int i,j,total = 3;
1477
1478 //   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"};
1479 //   int i,j,total = 3+4+8+3;
1480
1481    const char* names[] = { "11.A","11.B","11.C",
1482                            "121.A","121.B",
1483                            "122.A",
1484                            "13.A","13.B",
1485                            "141.A","141.B",
1486                            "142.A","142.B","142.C",
1487                            "143.A","143.B",
1488                            "15.A","15.B","15.C"};
1489    unsigned int i,j,total = 3+2+1+2+2+3+2+3;
1490
1491   printf("-- print all the example routes --\n");
1492   xbt_dynar_t links;
1493   void* link;
1494   for(i=0;i<total;i++) {
1495     for(j=0;j<total;j++) {
1496       links = (*(global_routing->get_route))(names[i],names[j]);
1497       printf("route from %s to %s >>> ",names[i],names[j]);
1498       unsigned int cpt=0;
1499       xbt_dynar_foreach(links, cpt, link) {
1500         s_surf_resource_t* generic_resource = link;
1501         printf(" %s",generic_resource->name);
1502       }
1503       printf("\n");
1504     }
1505   }
1506   
1507   printf("----------------------------------\n\n");
1508   
1509   printf("---------- call finalize ---------\n");
1510   (*(global_routing->finalize))();
1511   printf("----------------------------------\n");
1512   
1513   //exit(0); 
1514 }
1515
1516 ////////////////////////////////////////////////////////////////////////////////
1517 // HERE END THE NEW CODE
1518 ////////////////////////////////////////////////////////////////////////////////
1519
1520 // /* ************************************************************************** */
1521 // /* *************************** FULL ROUTING ********************************* */
1522 // typedef struct {
1523 //   s_routing_t generic_routing;
1524 //   xbt_dynar_t *routing_table;
1525 //   void *loopback;
1526 //   size_t size_of_link;
1527 // } s_routing_full_t,*routing_full_t;
1528 // 
1529 // #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
1530 // #define HOST2ROUTER(id) ((id)+(2<<29))
1531 // #define ROUTER2HOST(id) ((id)-(2>>29))
1532 // #define ISROUTER(id) ((id)>=(2<<29))
1533 // 
1534 // /*
1535 //  * Free the onelink routes
1536 //  */
1537 // static void onelink_route_elem_free(void *e) {
1538 //   s_onelink_t tmp = (s_onelink_t)e;
1539 //   if(tmp) {
1540 //     free(tmp);
1541 //   }
1542 // }
1543 // 
1544 // /*
1545 //  * Parsing
1546 //  */
1547 // static void routing_full_parse_Shost(void) {
1548 //   int *val = xbt_malloc(sizeof(int));
1549 //   DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
1550 //   *val = used_routing->host_count++;
1551 //   xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
1552 // #ifdef HAVE_TRACING
1553 //   TRACE_surf_host_define_id (A_surfxml_host_id, *val);
1554 // #endif
1555 // }
1556 // 
1557 // static void routing_full_parse_Srouter(void) {
1558 //      int *val = xbt_malloc(sizeof(int));
1559 //   DEBUG3("Seen router %s (%d -> #%d)",A_surfxml_router_id,used_routing->router_count,
1560 //              HOST2ROUTER(used_routing->router_count));
1561 //   *val = HOST2ROUTER(used_routing->router_count++);
1562 //   xbt_dict_set(used_routing->host_id,A_surfxml_router_id,val,xbt_free);
1563 // #ifdef HAVE_TRACING
1564 //   TRACE_surf_host_define_id (A_surfxml_host_id, *val);
1565 //   TRACE_surf_host_declaration (A_surfxml_host_id, 0);
1566 // #endif
1567 // }
1568 // 
1569 // static int src_id = -1;
1570 // static int dst_id = -1;
1571 // static void routing_full_parse_Sroute_set_endpoints(void)
1572 // {
1573 //   src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
1574 //   dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
1575 //   DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
1576 //   route_action = A_surfxml_route_action;
1577 // }
1578 // 
1579 // static void routing_full_parse_Eroute(void)
1580 // {
1581 //   char *name;
1582 //   if (src_id != -1 && dst_id != -1) {
1583 //     name = bprintf("%x#%x", src_id, dst_id);
1584 //     manage_route(route_table, name, route_action, 0);
1585 //     free(name);
1586 //   }
1587 // }
1588 // 
1589 // /* Cluster tag functions */
1590 // 
1591 // static void routing_full_parse_change_cpu_data(const char *hostName,
1592 //                                   const char *surfxml_host_power,
1593 //                                   const char *surfxml_host_availability,
1594 //                                   const char *surfxml_host_availability_file,
1595 //                                   const char *surfxml_host_state_file)
1596 // {
1597 //   int AX_ptr = 0;
1598 // 
1599 //   SURFXML_BUFFER_SET(host_id, hostName);
1600 //   SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower */ );
1601 //   SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
1602 //   SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
1603 //   SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
1604 // }
1605 // 
1606 // static void routing_full_parse_change_link_data(const char *linkName,
1607 //                                    const char *surfxml_link_bandwidth,
1608 //                                    const char *surfxml_link_bandwidth_file,
1609 //                                    const char *surfxml_link_latency,
1610 //                                    const char *surfxml_link_latency_file,
1611 //                                    const char *surfxml_link_state_file)
1612 // {
1613 //   int AX_ptr = 0;
1614 // 
1615 //   SURFXML_BUFFER_SET(link_id, linkName);
1616 //   SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
1617 //   SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
1618 //   SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
1619 //   SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
1620 //   SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
1621 // }
1622 // 
1623 // static void routing_full_parse_Scluster(void)
1624 // {
1625 //   static int AX_ptr = 0;
1626 // 
1627 //   char *cluster_id = A_surfxml_cluster_id;
1628 //   char *cluster_prefix = A_surfxml_cluster_prefix;
1629 //   char *cluster_suffix = A_surfxml_cluster_suffix;
1630 //   char *cluster_radical = A_surfxml_cluster_radical;
1631 //   char *cluster_power = A_surfxml_cluster_power;
1632 //   char *cluster_bw = A_surfxml_cluster_bw;
1633 //   char *cluster_lat = A_surfxml_cluster_lat;
1634 //   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
1635 //   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
1636 //   char *backbone_name;
1637 //   unsigned int it1,it2;
1638 //   char *name1,*name2;
1639 //   xbt_dynar_t names = NULL;
1640 //   surfxml_bufferstack_push(1);
1641 // 
1642 //   /* Make set a set to parse the prefix/suffix/radical into a neat list of names */
1643 //   DEBUG4("Make <set id='%s' prefix='%s' suffix='%s' radical='%s'>",
1644 //       cluster_id,cluster_prefix,cluster_suffix,cluster_radical);
1645 //   SURFXML_BUFFER_SET(set_id, cluster_id);
1646 //   SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1647 //   SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1648 //   SURFXML_BUFFER_SET(set_radical, cluster_radical);
1649 // 
1650 //   SURFXML_START_TAG(set);
1651 //   SURFXML_END_TAG(set);
1652 // 
1653 //   names = xbt_dict_get(set_list,cluster_id);
1654 // 
1655 //   xbt_dynar_foreach(names,it1,name1) {
1656 //     /* create the host */
1657 //     routing_full_parse_change_cpu_data(name1, cluster_power, "1.0", "", "");
1658 //     A_surfxml_host_state = A_surfxml_host_state_ON;
1659 // 
1660 //     SURFXML_START_TAG(host);
1661 //     SURFXML_END_TAG(host);
1662 // 
1663 //     /* Here comes the link */
1664 //     routing_full_parse_change_link_data(name1, cluster_bw, "", cluster_lat, "", "");
1665 //     A_surfxml_link_state = A_surfxml_link_state_ON;
1666 //     A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1667 // 
1668 //     SURFXML_START_TAG(link);
1669 //     SURFXML_END_TAG(link);
1670 //   }
1671 // 
1672 //   /* Make backbone link */
1673 //   backbone_name = bprintf("%s_bb", cluster_id);
1674 //   routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
1675 //                          "");
1676 //   A_surfxml_link_state = A_surfxml_link_state_ON;
1677 //   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1678 // 
1679 //   SURFXML_START_TAG(link);
1680 //   SURFXML_END_TAG(link);
1681 // 
1682 //   /* And now the internal routes */
1683 //   xbt_dynar_foreach(names,it1,name1) {
1684 //     xbt_dynar_foreach(names,it2,name2) {
1685 //       if (strcmp(name1,name2)) {
1686 //         A_surfxml_route_action = A_surfxml_route_action_POSTPEND;
1687 //         SURFXML_BUFFER_SET(route_src,name1);
1688 //         SURFXML_BUFFER_SET(route_dst,name2);
1689 //         SURFXML_START_TAG(route); {
1690 //           /* FIXME: name1 link is added by error about 20 lines below, so don't add it here
1691 //           SURFXML_BUFFER_SET(link_c_ctn_id, name1);
1692 //           SURFXML_START_TAG(link_c_ctn);
1693 //           SURFXML_END_TAG(link_c_ctn);
1694 //            */
1695 //           SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1696 //           SURFXML_START_TAG(link_c_ctn);
1697 //           SURFXML_END_TAG(link_c_ctn);
1698 // 
1699 //           SURFXML_BUFFER_SET(link_c_ctn_id, name2);
1700 //           SURFXML_START_TAG(link_c_ctn);
1701 //           SURFXML_END_TAG(link_c_ctn);
1702 // 
1703 //         } SURFXML_END_TAG(route);
1704 //       }
1705 //     }
1706 //   }
1707 // 
1708 //   /* Make route multi with the outside world, i.e. cluster->$* */
1709 // 
1710 //   /* FIXME
1711 //    * This also adds an elements to the routes within the cluster,
1712 //    * and I guess it's wrong, but since this element is commented out in the above
1713 //    * code creating the internal routes, we're good.
1714 //    * To fix it, I'd say that we need a way to understand "$*-${cluster_id}" as "whole world, but the guys in that cluster"
1715 //    * But for that, we need to install a real expression parser for src/dst attributes
1716 //    *
1717 //    * FIXME
1718 //    * This also adds a dumb element (the private link) in place of the loopback. Then, since
1719 //    * the loopback is added only if no link to self already exist, this fails.
1720 //    * That's really dumb.
1721 //    *
1722 //    * FIXME
1723 //    * It seems to me that it does not add the backbone to the path to outside world...
1724 //    */
1725 //   SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1726 //   SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
1727 //   A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1728 //   A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_PREPEND;
1729 // 
1730 //   SURFXML_START_TAG(route_c_multi);
1731 // 
1732 //   SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
1733 // 
1734 //   SURFXML_START_TAG(link_c_ctn);
1735 //   SURFXML_END_TAG(link_c_ctn);
1736 // 
1737 //   SURFXML_END_TAG(route_c_multi);
1738 // 
1739 //   free(backbone_name);
1740 // 
1741 //   /* Restore buff */
1742 //   surfxml_bufferstack_pop(1);
1743 // }
1744 // 
1745 // 
1746 // static void routing_full_parse_end(void) {
1747 //   routing_full_t routing = (routing_full_t) used_routing;
1748 //   int nb_link = 0;
1749 //   unsigned int cpt = 0;
1750 //   xbt_dict_cursor_t cursor = NULL;
1751 //   char *key, *data, *end;
1752 //   const char *sep = "#";
1753 //   xbt_dynar_t links, keys;
1754 //   char *link_name = NULL;
1755 //   int i,j;
1756 // 
1757 //   int host_count = routing->generic_routing.host_count;
1758 // 
1759 //   /* Create the routing table */
1760 //   routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
1761 //   for (i=0;i<host_count;i++)
1762 //     for (j=0;j<host_count;j++)
1763 //       ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
1764 // 
1765 //   /* Put the routes in position */
1766 //   xbt_dict_foreach(route_table, cursor, key, data) {
1767 //     nb_link = 0;
1768 //     links = (xbt_dynar_t) data;
1769 //     keys = xbt_str_split_str(key, sep);
1770 // 
1771 //     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
1772 //     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
1773 //     xbt_dynar_free(&keys);
1774 // 
1775 //     if(xbt_dynar_length(links) == 1){
1776 //       s_onelink_t new_link = (s_onelink_t) xbt_malloc0(sizeof(s_onelink));
1777 //       new_link->src_id = src_id;
1778 //       new_link->dst_id = dst_id;
1779 //       link_name = xbt_dynar_getfirst_as(links, char*);
1780 //       new_link->link_ptr = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1781 //       DEBUG3("Adding onelink route from (#%d) to (#%d), link_name %s",src_id, dst_id, link_name);
1782 //       xbt_dict_set(onelink_routes, link_name, (void *)new_link, onelink_route_elem_free);
1783 // #ifdef HAVE_TRACING
1784 //       TRACE_surf_link_save_endpoints (link_name, src_id, dst_id);
1785 // #endif
1786 //     }
1787 // 
1788 //     if(ISROUTER(src_id) || ISROUTER(dst_id)) {
1789 //                              DEBUG2("There is route with a router here: (%d ,%d)",src_id,dst_id);
1790 //                              /* Check there is only one link in the route and store the information */
1791 //                              continue;
1792 //     }
1793 // 
1794 //     DEBUG4("Handle %d %d (from %d hosts): %ld links",
1795 //         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
1796 //     xbt_dynar_foreach(links, cpt, link_name) {
1797 //       void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1798 //       if (link)
1799 //         xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
1800 //       else
1801 //         THROW1(mismatch_error,0,"Link %s not found", link_name);
1802 //     }
1803 //   }
1804 // 
1805 //   /* Add the loopback if needed */
1806 //   for (i = 0; i < host_count; i++)
1807 //     if (!xbt_dynar_length(ROUTE_FULL(i, i)))
1808 //       xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
1809 // 
1810 //   /* Shrink the dynar routes (save unused slots) */
1811 //   for (i=0;i<host_count;i++)
1812 //     for (j=0;j<host_count;j++)
1813 //       xbt_dynar_shrink(ROUTE_FULL(i,j),0);
1814 // }
1815 // 
1816 // /*
1817 //  * Business methods
1818 //  */
1819 // static xbt_dynar_t routing_full_get_route(int src,int dst) {
1820 //   xbt_assert0(!(ISROUTER(src) || ISROUTER(dst)), "Ask for route \"from\" or \"to\" a router node");
1821 //   return ROUTE_FULL(src,dst);
1822 // }
1823 // 
1824 // static xbt_dict_t routing_full_get_onelink_routes(void){
1825 //   return onelink_routes;
1826 // }
1827 // 
1828 // static int routing_full_is_router(int id){
1829 //      return ISROUTER(id);
1830 // }
1831 // 
1832 // static void routing_full_finalize(void) {
1833 //   routing_full_t routing = (routing_full_t)used_routing;
1834 //   int i,j;
1835 // 
1836 //   if (routing) {
1837 //     for (i = 0; i < used_routing->host_count; i++)
1838 //       for (j = 0; j < used_routing->host_count; j++)
1839 //         xbt_dynar_free(&ROUTE_FULL(i, j));
1840 //     free(routing->routing_table);
1841 //     xbt_dict_free(&used_routing->host_id);
1842 //     xbt_dict_free(&onelink_routes);
1843 //     free(routing);
1844 //     routing=NULL;
1845 //   }
1846 // }
1847 // 
1848 // static void routing_model_full_create(size_t size_of_link,void *loopback) {
1849 //   /* initialize our structure */
1850 //   routing_full_t routing = xbt_new0(s_routing_full_t,1);
1851 //   routing->generic_routing.name = "Full";
1852 //   routing->generic_routing.host_count = 0;
1853 //   routing->generic_routing.get_route = routing_full_get_route;
1854 //   routing->generic_routing.get_onelink_routes = routing_full_get_onelink_routes;
1855 //   routing->generic_routing.is_router = routing_full_is_router;
1856 //   routing->generic_routing.finalize = routing_full_finalize;
1857 // 
1858 //   routing->size_of_link = size_of_link;
1859 //   routing->loopback = loopback;
1860 // 
1861 //   /* Set it in position */
1862 //   used_routing = (routing_t) routing;
1863 // 
1864 //   /* Set the dict for onehop routes */
1865 //   onelink_routes =  xbt_dict_new();
1866 // 
1867 //   routing->generic_routing.host_id = xbt_dict_new();
1868 //   
1869 //   /* Setup the parsing callbacks we need */
1870 // //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1871 // //   surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
1872 // //   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
1873 // //   surfxml_add_callback(STag_surfxml_route_cb_list, &routing_full_parse_Sroute_set_endpoints);
1874 // //   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
1875 // //   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_full_parse_Scluster);
1876 // 
1877 // //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1878 // //   surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
1879 //   
1880 // }
1881
1882 /* ************************************************************************** */
1883
1884 // static void routing_shortest_path_parse_Scluster(void)
1885 // {
1886 //   static int AX_ptr = 0;
1887 // 
1888 //   char *cluster_id = A_surfxml_cluster_id;
1889 //   char *cluster_prefix = A_surfxml_cluster_prefix;
1890 //   char *cluster_suffix = A_surfxml_cluster_suffix;
1891 //   char *cluster_radical = A_surfxml_cluster_radical;
1892 //   char *cluster_power = A_surfxml_cluster_power;
1893 //   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
1894 //   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
1895 //   char *backbone_name;
1896 // 
1897 //   surfxml_bufferstack_push(1);
1898 // 
1899 //   /* Make set */
1900 //   SURFXML_BUFFER_SET(set_id, cluster_id);
1901 //   SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1902 //   SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1903 //   SURFXML_BUFFER_SET(set_radical, cluster_radical);
1904 // 
1905 //   SURFXML_START_TAG(set);
1906 //   SURFXML_END_TAG(set);
1907 // 
1908 //   /* Make foreach */
1909 //   SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
1910 // 
1911 //   SURFXML_START_TAG(foreach);
1912 // 
1913 //   /* Make host for the foreach */
1914 //   routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
1915 //   A_surfxml_host_state = A_surfxml_host_state_ON;
1916 // 
1917 //   SURFXML_START_TAG(host);
1918 //   SURFXML_END_TAG(host);
1919 // 
1920 //   SURFXML_END_TAG(foreach);
1921 // 
1922 //   /* Make backbone link */
1923 //   backbone_name = bprintf("%s_bb", cluster_id);
1924 //   routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
1925 //                          "");
1926 //   A_surfxml_link_state = A_surfxml_link_state_ON;
1927 //   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1928 // 
1929 //   SURFXML_START_TAG(link);
1930 //   SURFXML_END_TAG(link);
1931 // 
1932 //   free(backbone_name);
1933 // 
1934 //   /* Restore buff */
1935 //   surfxml_bufferstack_pop(1);
1936 // }
1937 // 
1938 // /* ************************************************************************** */
1939 // /* *************************** FLOYD ROUTING ********************************* */
1940 // typedef struct {
1941 //   s_routing_t generic_routing;
1942 //   int *predecessor_table;
1943 //   void** link_table;
1944 //   xbt_dynar_t last_route;
1945 //   void *loopback;
1946 //   size_t size_of_link;
1947 // } s_routing_floyd_t,*routing_floyd_t;
1948 // 
1949 // #define FLOYD_COST(i,j) cost_table[(i)+(j)*(used_routing)->host_count]
1950 // #define FLOYD_PRED(i,j) ((routing_floyd_t)used_routing)->predecessor_table[(i)+(j)*(used_routing)->host_count]
1951 // #define FLOYD_LINK(i,j) ((routing_floyd_t)used_routing)->link_table[(i)+(j)*(used_routing)->host_count]
1952 // 
1953 // static void routing_floyd_parse_end(void) {
1954 // 
1955 //   routing_floyd_t routing = (routing_floyd_t) used_routing;
1956 //   int nb_link = 0;
1957 //   void* link_list = NULL;
1958 //   double * cost_table;
1959 //   xbt_dict_cursor_t cursor = NULL;
1960 //   char *key,*data, *end;
1961 //   const char *sep = "#";
1962 //   xbt_dynar_t links, keys;
1963 // 
1964 //   unsigned int i,j;
1965 //   unsigned int a,b,c;
1966 //   int host_count = routing->generic_routing.host_count;
1967 //   char * link_name = NULL;
1968 //   void * link = NULL;
1969 // 
1970 //   /* Create Cost, Predecessor and Link tables */
1971 //   cost_table = xbt_new0(double, host_count * host_count); //link cost from host to host
1972 //   routing->predecessor_table = xbt_new0(int, host_count*host_count); //predecessor host numbers
1973 //   routing->link_table = xbt_new0(void*,host_count*host_count); //actual link between src and dst
1974 //   routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
1975 // 
1976 //   /* Initialize costs and predecessors*/
1977 //   for(i = 0; i<host_count;i++)
1978 //     for(j = 0; j<host_count;j++) {
1979 //         FLOYD_COST(i,j) = DBL_MAX;
1980 //         FLOYD_PRED(i,j) = -1;
1981 //     }
1982 // 
1983 //    /* Put the routes in position */
1984 //   xbt_dict_foreach(route_table, cursor, key, data) {
1985 //     nb_link = 0;
1986 //     links = (xbt_dynar_t)data;
1987 //     keys = xbt_str_split_str(key, sep);
1988 // 
1989 //     
1990 //     src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
1991 //     dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
1992 //     xbt_dynar_free(&keys);
1993 //  
1994 //     DEBUG4("Handle %d %d (from %d hosts): %ld links",
1995 //         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
1996 //     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);
1997 //     
1998 //     link_name = xbt_dynar_getfirst_as(links, char*);
1999 //     link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2000 // 
2001 //     if (link)
2002 //       link_list = link;
2003 //     else
2004 //       THROW1(mismatch_error,0,"Link %s not found", link_name);
2005 //     
2006 // 
2007 //     FLOYD_LINK(src_id,dst_id) = link_list;
2008 //     FLOYD_PRED(src_id, dst_id) = src_id;
2009 // 
2010 //     //link cost
2011 //     FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
2012 // 
2013 //   }
2014 // 
2015 //     /* Add the loopback if needed */
2016 //   for (i = 0; i < host_count; i++)
2017 //     if (!FLOYD_PRED(i, i)) {
2018 //       FLOYD_PRED(i, i) = i;
2019 //       FLOYD_COST(i, i) = 1;
2020 //       FLOYD_LINK(i, i) = routing->loopback;
2021 //     }
2022 // 
2023 // 
2024 //   //Calculate path costs 
2025 // 
2026 //   for(c=0;c<host_count;c++) {
2027 //     for(a=0;a<host_count;a++) {
2028 //       for(b=0;b<host_count;b++) {
2029 //         if(FLOYD_COST(a,c) < DBL_MAX && FLOYD_COST(c,b) < DBL_MAX) {
2030 //           if(FLOYD_COST(a,b) == DBL_MAX || (FLOYD_COST(a,c)+FLOYD_COST(c,b) < FLOYD_COST(a,b))) {
2031 //             FLOYD_COST(a,b) = FLOYD_COST(a,c)+FLOYD_COST(c,b);
2032 //             FLOYD_PRED(a,b) = FLOYD_PRED(c,b);
2033 //           }
2034 //         }
2035 //       }
2036 //     }
2037 //   }
2038 // 
2039 //   //cleanup
2040 //   free(cost_table);
2041 // }
2042 // 
2043 // /*
2044 //  * Business methods
2045 //  */
2046 // static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
2047 // 
2048 //   routing_floyd_t routing = (routing_floyd_t) used_routing;
2049 // 
2050 //   int pred = dst_id;
2051 //   int prev_pred = 0;
2052 // 
2053 //   xbt_dynar_reset(routing->last_route);
2054 // 
2055 //   do {
2056 //     prev_pred = pred;
2057 //     pred = FLOYD_PRED(src_id, pred);
2058 // 
2059 //     if(pred == -1) // if no pred in route -> no route to host
2060 //         break;
2061 // 
2062 //     xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
2063 // 
2064 //   } while(pred != src_id);
2065 // 
2066 //   xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
2067 // 
2068 //   return routing->last_route;
2069 // }
2070 // 
2071 // static void routing_floyd_finalize(void) {
2072 //   routing_floyd_t routing = (routing_floyd_t)used_routing;
2073 // 
2074 //   if (routing) {
2075 //     free(routing->link_table);
2076 //     free(routing->predecessor_table);
2077 //     xbt_dynar_free(&routing->last_route);
2078 //     xbt_dict_free(&used_routing->host_id);
2079 //     free(routing);
2080 //     routing=NULL;
2081 //   }
2082 // }
2083 // 
2084 // static xbt_dict_t routing_floyd_get_onelink_routes(void){
2085 //   xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model Floyd");
2086 // }
2087 // 
2088 // static int routing_floyd_is_router(int id){
2089 //   xbt_assert0(0,"The get_is_router feature is not supported in routing model Floyd");
2090 // }
2091 // 
2092 // static void routing_model_floyd_create(size_t size_of_link,void *loopback) {
2093 //   /* initialize our structure */
2094 //   routing_floyd_t routing = xbt_new0(s_routing_floyd_t,1);
2095 //   routing->generic_routing.name = "Floyd";
2096 //   routing->generic_routing.host_count = 0;
2097 //   routing->generic_routing.host_id = xbt_dict_new();
2098 //   routing->generic_routing.get_route = routing_floyd_get_route;
2099 //   routing->generic_routing.get_onelink_routes = routing_floyd_get_onelink_routes;
2100 //   routing->generic_routing.is_router = routing_floyd_is_router;
2101 //   routing->generic_routing.finalize = routing_floyd_finalize;
2102 //   routing->size_of_link = size_of_link;
2103 //   routing->loopback = loopback;
2104 // 
2105 //   /* Set it in position */
2106 //   used_routing = (routing_t) routing;
2107 //   
2108 //   /* Setup the parsing callbacks we need */
2109 //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2110 //   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_floyd_parse_end);
2111 //   surfxml_add_callback(STag_surfxml_route_cb_list, 
2112 //       &routing_full_parse_Sroute_set_endpoints);
2113 //   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
2114 //   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
2115 //   
2116 // }
2117 // 
2118 // /* ************************************************************************** */
2119 // /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
2120 // typedef struct {
2121 //   s_routing_t generic_routing;
2122 //   xbt_graph_t route_graph;
2123 //   xbt_dict_t graph_node_map;
2124 //   xbt_dict_t route_cache;
2125 //   xbt_dynar_t last_route;
2126 //   int cached;
2127 //   void *loopback;
2128 //   size_t size_of_link;
2129 // } s_routing_dijkstra_t,*routing_dijkstra_t;
2130 // 
2131 // 
2132 // typedef struct graph_node_data {
2133 //   int id; 
2134 //   int graph_id; //used for caching internal graph id's
2135 // } s_graph_node_data_t, * graph_node_data_t;
2136 // 
2137 // typedef struct graph_node_map_element {
2138 //   xbt_node_t node;
2139 // } s_graph_node_map_element_t, * graph_node_map_element_t;
2140 // 
2141 // typedef struct route_cache_element {
2142 //   int * pred_arr;
2143 //   int size;
2144 // } s_route_cache_element_t, * route_cache_element_t;  
2145 // 
2146 // /*
2147 //  * Free functions
2148 //  */
2149 // static void route_cache_elem_free(void *e) {
2150 //   route_cache_element_t elm=(route_cache_element_t)e;
2151 // 
2152 //   if (elm) {
2153 //     free(elm->pred_arr);
2154 //     free(elm);
2155 //   }
2156 // }
2157 // 
2158 // static void graph_node_map_elem_free(void *e) {
2159 //   graph_node_map_element_t elm = (graph_node_map_element_t)e;
2160 // 
2161 //   if(elm) {
2162 //     free(elm);
2163 //   }
2164 // }
2165 // 
2166 // /*
2167 //  * Utility functions
2168 // */
2169 // static xbt_node_t route_graph_new_node(int id, int graph_id) {
2170 //   xbt_node_t node = NULL;
2171 //   graph_node_data_t data = NULL;
2172 //   graph_node_map_element_t elm = NULL;
2173 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2174 // 
2175 //   data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
2176 //   data->id = id;
2177 //   data->graph_id = graph_id;
2178 //   node = xbt_graph_new_node(routing->route_graph, data);
2179 // 
2180 //   elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
2181 //   elm->node = node;
2182 //   xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
2183 // 
2184 //   return node;
2185 // }
2186 // 
2187 // static graph_node_map_element_t graph_node_map_search(int id) {
2188 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2189 // 
2190 //   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));
2191 // 
2192 //   return elm;
2193 // }
2194 // 
2195 // /*
2196 //  * Parsing
2197 //  */
2198 // static void route_new_dijkstra(int src_id, int dst_id, void* link) {
2199 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2200 // 
2201 //   xbt_node_t src = NULL;
2202 //   xbt_node_t dst = NULL;
2203 //   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));
2204 //   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));
2205 // 
2206 //   if(src_elm)
2207 //     src = src_elm->node;
2208 // 
2209 //   if(dst_elm)
2210 //     dst = dst_elm->node;
2211 // 
2212 //   //add nodes if they don't exist in the graph
2213 //   if(src_id == dst_id && src == NULL && dst == NULL) {
2214 //     src = route_graph_new_node(src_id, -1);
2215 //     dst = src;
2216 //   } else {
2217 //     if(src == NULL) {
2218 //       src = route_graph_new_node(src_id, -1);
2219 //     }
2220 //      
2221 //     if(dst == NULL) {
2222 //       dst = route_graph_new_node(dst_id, -1);
2223 //     }
2224 //   }
2225 // 
2226 //   //add link as edge to graph
2227 //   xbt_graph_new_edge(routing->route_graph, src, dst, link);
2228 //   
2229 // }
2230 // 
2231 // static void add_loopback_dijkstra(void) {
2232 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2233 // 
2234 //      xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2235 //      
2236 //      xbt_node_t node = NULL;
2237 //      unsigned int cursor2;
2238 //      xbt_dynar_foreach(nodes, cursor2, node) {
2239 //              xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node); 
2240 //              xbt_edge_t edge = NULL;
2241 //              unsigned int cursor;
2242 //      
2243 //              int found = 0;
2244 //              xbt_dynar_foreach(out_edges, cursor, edge) {
2245 //                      xbt_node_t other_node = xbt_graph_edge_get_target(edge);
2246 //                      if(other_node == node) {
2247 //                              found = 1;
2248 //                              break;
2249 //                      }
2250 //              }
2251 // 
2252 //              if(!found)
2253 //                      xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
2254 //      }
2255 // }
2256 // 
2257 // static void routing_dijkstra_parse_end(void) {
2258 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2259 //   int nb_link = 0;
2260 //   xbt_dict_cursor_t cursor = NULL;
2261 //   char *key, *data, *end;
2262 //   const char *sep = "#";
2263 //   xbt_dynar_t links, keys;
2264 //   char* link_name = NULL;
2265 //   void* link = NULL;
2266 //   xbt_node_t node = NULL;
2267 //   unsigned int cursor2;
2268 //   xbt_dynar_t nodes = NULL;
2269 //   /* Create the topology graph */
2270 //   routing->route_graph = xbt_graph_new_graph(1, NULL);
2271 //   routing->graph_node_map = xbt_dict_new();
2272 //   routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
2273 //   if(routing->cached)
2274 //     routing->route_cache = xbt_dict_new();
2275 // 
2276 // 
2277 //   /* Put the routes in position */
2278 //   xbt_dict_foreach(route_table, cursor, key, data) {
2279 //     nb_link = 0;
2280 //     links = (xbt_dynar_t) data;
2281 //     keys = xbt_str_split_str(key, sep);
2282 // 
2283 //     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
2284 //     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
2285 //     xbt_dynar_free(&keys);
2286 // 
2287 //     DEBUG4("Handle %d %d (from %d hosts): %ld links",
2288 //         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
2289 // 
2290 //     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);
2291 // 
2292 //     link_name = xbt_dynar_getfirst_as(links, char*);
2293 //     link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2294 //     if (link)
2295 //       route_new_dijkstra(src_id,dst_id,link);
2296 //     else
2297 //       THROW1(mismatch_error,0,"Link %s not found", link_name);
2298 //     
2299 //   }
2300 // 
2301 //   /* Add the loopback if needed */
2302 //   add_loopback_dijkstra();
2303 // 
2304 //   /* initialize graph indexes in nodes after graph has been built */
2305 //   nodes = xbt_graph_get_nodes(routing->route_graph);
2306 // 
2307 //   xbt_dynar_foreach(nodes, cursor2, node) {
2308 //     graph_node_data_t data = xbt_graph_node_get_data(node);
2309 //     data->graph_id = cursor2;
2310 //   }
2311 // 
2312 // }
2313 // 
2314 // /*
2315 //  * Business methods
2316 //  */
2317 // static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
2318 // 
2319 //   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2320 //   int * pred_arr = NULL;
2321 //   int src_node_id = 0;
2322 //   int dst_node_id = 0;
2323 //   int * nodeid = NULL;
2324 //   int v;
2325 //   int size = 0;
2326 //   void * link = NULL;
2327 //   route_cache_element_t elm = NULL;
2328 //   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2329 // 
2330 //   /*Use the graph_node id mapping set to quickly find the nodes */
2331 //   graph_node_map_element_t src_elm = graph_node_map_search(src_id);
2332 //   graph_node_map_element_t dst_elm = graph_node_map_search(dst_id);
2333 //   xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", src_id, dst_id);
2334 //   src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
2335 //   dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
2336 // 
2337 //   if(routing->cached) {
2338 //     /*check if there is a cached predecessor list avail */
2339 //     elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
2340 //   }
2341 // 
2342 //   if(elm) { //cached mode and cache hit
2343 //     pred_arr = elm->pred_arr;
2344 //   } else { //not cached mode or cache miss
2345 //     double * cost_arr = NULL;
2346 //     xbt_heap_t pqueue = NULL;
2347 //     int i = 0;
2348 // 
2349 //     int nr_nodes = xbt_dynar_length(nodes);
2350 //     cost_arr = xbt_new0(double, nr_nodes); //link cost from src to other hosts
2351 //     pred_arr = xbt_new0(int, nr_nodes); //predecessors in path from src
2352 //     pqueue = xbt_heap_new(nr_nodes, free);
2353 // 
2354 //     //initialize
2355 //     cost_arr[src_node_id] = 0.0;
2356 // 
2357 //     for(i = 0; i < nr_nodes; i++) {
2358 //       if(i != src_node_id) {
2359 //         cost_arr[i] = DBL_MAX;
2360 //       }
2361 // 
2362 //       pred_arr[i] = 0;
2363 // 
2364 //       //initialize priority queue
2365 //       nodeid = xbt_new0(int, 1);
2366 //       *nodeid = i;
2367 //       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
2368 // 
2369 //     }
2370 // 
2371 //     // apply dijkstra using the indexes from the graph's node array
2372 //     while(xbt_heap_size(pqueue) > 0) {
2373 //       int * v_id = xbt_heap_pop(pqueue);
2374 //       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
2375 //       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node); 
2376 //       xbt_edge_t edge = NULL;
2377 //       unsigned int cursor;
2378 // 
2379 //       xbt_dynar_foreach(out_edges, cursor, edge) {
2380 //         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
2381 //         graph_node_data_t data = xbt_graph_node_get_data(u_node);
2382 //         int u_id = data->graph_id;
2383 //         int cost_v_u = 1; //fixed link cost for now
2384 // 
2385 //         if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
2386 //           pred_arr[u_id] = *v_id;
2387 //           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
2388 //           nodeid = xbt_new0(int, 1);
2389 //           *nodeid = u_id;
2390 //           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
2391 //         }
2392 //       }
2393 // 
2394 //       //free item popped from pqueue
2395 //       free(v_id);
2396 //     }
2397 // 
2398 //     free(cost_arr);
2399 //     xbt_heap_free(pqueue);
2400 // 
2401 //   }
2402 // 
2403 //   //compose route path with links
2404 //   xbt_dynar_reset(routing->last_route);
2405 // 
2406 //   for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
2407 //     xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
2408 //     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
2409 //     xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
2410 // 
2411 //     xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
2412 // 
2413 //     link = xbt_graph_edge_get_data(edge);
2414 //     xbt_dynar_unshift(routing->last_route, &link);
2415 //     size++;
2416 //   }
2417 // 
2418 // 
2419 //   if(routing->cached && elm == NULL) {
2420 //     //add to predecessor list of the current src-host to cache
2421 //     elm = xbt_new0(struct route_cache_element, sizeof(struct route_cache_element));
2422 //     elm->pred_arr = pred_arr;
2423 //     elm->size = size;
2424 //     xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
2425 //   }
2426 // 
2427 //   if(!routing->cached)
2428 //     free(pred_arr);
2429 // 
2430 //   return routing->last_route;
2431 // }
2432 // 
2433 // 
2434 // static void routing_dijkstra_finalize(void) {
2435 //   routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
2436 // 
2437 //   if (routing) {
2438 //     xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
2439 //     xbt_dict_free(&routing->graph_node_map);
2440 //     if(routing->cached)
2441 //       xbt_dict_free(&routing->route_cache);
2442 //     xbt_dynar_free(&routing->last_route);
2443 //     xbt_dict_free(&used_routing->host_id);
2444 //     free(routing);
2445 //     routing=NULL;
2446 //   }
2447 // }
2448 // 
2449 // static xbt_dict_t routing_dijkstraboth_get_onelink_routes(void){
2450 //   xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model dijkstraboth");
2451 // }
2452 // 
2453 // static int routing_dijkstraboth_is_router(int id){
2454 //   xbt_assert0(0,"The get_is_router feature is not supported in routing model dijkstraboth");
2455 // }
2456 // 
2457 // /*
2458 //  *
2459 //  */
2460 // static void routing_model_dijkstraboth_create(size_t size_of_link,void *loopback, int cached) {
2461 //   /* initialize our structure */
2462 //   routing_dijkstra_t routing = xbt_new0(s_routing_dijkstra_t,1);
2463 //   routing->generic_routing.name = "Dijkstra";
2464 //   routing->generic_routing.host_count = 0;
2465 //   routing->generic_routing.get_route = routing_dijkstra_get_route;
2466 //   routing->generic_routing.get_onelink_routes = routing_dijkstraboth_get_onelink_routes;
2467 //   routing->generic_routing.is_router = routing_dijkstraboth_is_router;
2468 //   routing->generic_routing.finalize = routing_dijkstra_finalize;
2469 //   routing->size_of_link = size_of_link;
2470 //   routing->loopback = loopback;
2471 //   routing->cached = cached;
2472 // 
2473 //   /* Set it in position */
2474 //   used_routing = (routing_t) routing;
2475 // 
2476 //   /* Setup the parsing callbacks we need */
2477 //   routing->generic_routing.host_id = xbt_dict_new();
2478 //   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2479 //   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_dijkstra_parse_end);
2480 //   surfxml_add_callback(STag_surfxml_route_cb_list,
2481 //       &routing_full_parse_Sroute_set_endpoints);
2482 //   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
2483 //   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
2484 // }
2485 // 
2486 // static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
2487 //   routing_model_dijkstraboth_create(size_of_link, loopback, 0);
2488 // }
2489 // 
2490 // static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
2491 //   routing_model_dijkstraboth_create(size_of_link, loopback, 1);
2492 // }
2493
2494 /* ************************************************** */
2495 /* ********** NO ROUTING **************************** */
2496
2497
2498 // static void routing_none_finalize(void) {
2499 //   if (used_routing) {
2500 //     xbt_dict_free(&used_routing->host_id);
2501 //     free(used_routing);
2502 //     used_routing=NULL;
2503 //   }
2504 // }
2505 // 
2506 // static void routing_model_none_create(size_t size_of_link,void *loopback) {
2507 //   routing_t routing = xbt_new0(s_routing_t,1);
2508 //   INFO0("Null routing");
2509 //   routing->name = "none";
2510 //   routing->host_count = 0;
2511 //   routing->host_id = xbt_dict_new();
2512 //   routing->get_onelink_routes = NULL;
2513 //   routing->is_router = NULL;
2514 //   routing->get_route = NULL;
2515 // 
2516 //   routing->finalize = routing_none_finalize;
2517 // 
2518 //   /* Set it in position */
2519 //   used_routing = (routing_t) routing;
2520 // }