Logo AND Algorithmique Numérique Distribuée

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