+ int *src_id, *dst_id;
+
+ if(modeltype==&routing_models[SURF_MODEL_FULL]) {
+ _parse_routes = ((routing_component_full_t)rc)->parse_routes;
+ _to_index = ((routing_component_full_t)rc)->to_index;
+
+ } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
+ _parse_routes = ((routing_component_floyd_t)rc)->parse_routes;
+ _to_index = ((routing_component_floyd_t)rc)->to_index;
+
+ } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
+ modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
+ _parse_routes = ((routing_component_dijkstra_t)rc)->parse_routes;
+ _to_index = ((routing_component_dijkstra_t)rc)->to_index;
+
+ } else xbt_die("\"generic_set_route\" not support");
+
+ src_id = xbt_dict_get_or_null(_to_index, src);
+ dst_id = xbt_dict_get_or_null(_to_index, dst);
+
+ xbt_assert2(src_id&&dst_id,"Network elements %s or %s not found", src, dst);
+ route_name = bprintf("%d#%d",*src_id,*dst_id);
+
+ xbt_assert2(xbt_dynar_length(route->link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);
+ xbt_assert2(!xbt_dict_get_or_null(_parse_routes,route_name),
+ "The route between \"%s\" and \"%s\" already exist",src,dst);
+
+ xbt_dict_set(_parse_routes, route_name, route, NULL);
+ xbt_free(route_name);
+}
+
+static void generic_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {
+ DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",src,e_route->src_gateway,dst,e_route->dst_gateway);
+ model_type_t modeltype = rc->routing;
+ xbt_dict_t _parse_routes;
+ xbt_dict_t _to_index;
+ char *route_name;
+ int *src_id, *dst_id;
+
+ if(modeltype==&routing_models[SURF_MODEL_FULL]) {
+ _parse_routes = ((routing_component_full_t)rc)->parse_routes;
+ _to_index = ((routing_component_full_t)rc)->to_index;
+
+ } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
+ _parse_routes = ((routing_component_floyd_t)rc)->parse_routes;
+ _to_index = ((routing_component_floyd_t)rc)->to_index;
+
+ } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
+ modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
+ _parse_routes = ((routing_component_dijkstra_t)rc)->parse_routes;
+ _to_index = ((routing_component_dijkstra_t)rc)->to_index;
+
+ } else xbt_die("\"generic_set_route\" not support");
+
+ src_id = xbt_dict_get_or_null(_to_index, src);
+ dst_id = xbt_dict_get_or_null(_to_index, dst);
+
+ xbt_assert2(src_id&&dst_id,"Network elements %s or %s not found", src, dst);
+ route_name = bprintf("%d#%d",*src_id,*dst_id);
+
+ 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);
+ xbt_assert4(!xbt_dict_get_or_null(_parse_routes,route_name),
+ "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",src,e_route->src_gateway,dst,e_route->dst_gateway);
+
+ xbt_dict_set(_parse_routes, route_name, e_route, NULL);
+ xbt_free(route_name);
+}
+
+static void generic_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {
+ DEBUG2("Load bypassRoute from \"%s\" to \"%s\"",src,dst);
+ model_type_t modeltype = rc->routing;
+ xbt_dict_t dict_bypassRoutes;
+ char *route_name;
+ if(modeltype==&routing_models[SURF_MODEL_FULL]) {
+ dict_bypassRoutes = ((routing_component_full_t)rc)->bypassRoutes;
+ } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
+ dict_bypassRoutes = ((routing_component_floyd_t)rc)->bypassRoutes;
+ } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
+ modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
+ dict_bypassRoutes = ((routing_component_dijkstra_t)rc)->bypassRoutes;
+ } else xbt_die("\"generic_set_bypassroute\" not support");
+ route_name = bprintf("%s#%s",src,dst);
+ 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);
+ xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes,route_name),
+ "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",src,e_route->src_gateway,dst,e_route->dst_gateway);
+
+ route_extended_t new_e_route = generic_new_extended_route(SURF_ROUTING_RECURSIVE,e_route,0);
+ xbt_dynar_free( &(e_route->generic_route.link_list) );
+ xbt_free(e_route);
+
+ xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, (void(*)(void*))generic_free_extended_route );
+ xbt_free(route_name);
+}
+
+/* ************************************************************************** */
+/* *********************** GENERIC BUSINESS METHODS ************************* */
+
+static route_extended_t generic_get_bypassroute(routing_component_t rc, const char* src, const char* dst) {
+ model_type_t modeltype = rc->routing;
+ xbt_dict_t dict_bypassRoutes;
+
+ if(modeltype==&routing_models[SURF_MODEL_FULL]) {
+ dict_bypassRoutes = ((routing_component_full_t)rc)->bypassRoutes;
+ } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
+ dict_bypassRoutes = ((routing_component_floyd_t)rc)->bypassRoutes;
+ } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
+ modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
+ dict_bypassRoutes = ((routing_component_dijkstra_t)rc)->bypassRoutes;
+ } else xbt_die("\"generic_set_bypassroute\" not support");
+
+
+ routing_component_t src_as, dst_as;
+ int index_src, index_dst;
+ xbt_dynar_t path_src = NULL;
+ xbt_dynar_t path_dst = NULL;
+ routing_component_t current = NULL;
+ routing_component_t* current_src = NULL;
+ routing_component_t* current_dst = NULL;
+
+ /* (1) find the as where the src and dst are located */
+ src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
+ dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst);
+ xbt_assert2(src_as&&dst_as, "Ask for route \"from\"(%s) or \"to\"(%s) no found",src,dst);
+
+ /* (2) find the path to the root routing component */
+ path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
+ current = src_as;
+ while( current != NULL ) {
+ xbt_dynar_push(path_src,¤t);
+ current = current->routing_father;
+ }
+ path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
+ current = dst_as;
+ while( current != NULL ) {
+ xbt_dynar_push(path_dst,¤t);
+ current = current->routing_father;
+ }
+
+ /* (3) find the common father */
+ index_src = (path_src->used)-1;
+ index_dst = (path_dst->used)-1;
+ current_src = xbt_dynar_get_ptr(path_src,index_src);
+ current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
+ while( index_src >= 0 && index_dst >= 0 && *current_src == *current_dst ) {
+ routing_component_t *tmp_src,*tmp_dst;
+ tmp_src = xbt_dynar_pop_ptr(path_src);
+ tmp_dst = xbt_dynar_pop_ptr(path_dst);
+ index_src--;
+ index_dst--;
+ current_src = xbt_dynar_get_ptr(path_src,index_src);
+ current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
+ }
+
+ int max_index_src = (path_src->used)-1;
+ int max_index_dst = (path_dst->used)-1;
+
+ int max_index = max(max_index_src,max_index_dst);
+ int i, max;
+
+ route_extended_t e_route_bypass = NULL;
+
+ for( max=0;max<=max_index;max++)
+ {
+ for(i=0;i<max;i++)
+ {
+ if( i <= max_index_src && max <= max_index_dst ) {
+ char* route_name = bprintf("%s#%s",
+ (*(routing_component_t*)(xbt_dynar_get_ptr(path_src,i)))->name,
+ (*(routing_component_t*)(xbt_dynar_get_ptr(path_dst,max)))->name);
+ e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes,route_name);
+ xbt_free(route_name);
+ }
+ if( e_route_bypass ) break;
+ if( max <= max_index_src && i <= max_index_dst ) {
+ char* route_name = bprintf("%s#%s",
+ (*(routing_component_t*)(xbt_dynar_get_ptr(path_src,max)))->name,
+ (*(routing_component_t*)(xbt_dynar_get_ptr(path_dst,i)))->name);
+ e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes,route_name);
+ xbt_free(route_name);
+ }
+ if( e_route_bypass ) break;
+ }
+
+ if( e_route_bypass ) break;
+
+ if( max <= max_index_src && max <= max_index_dst ) {
+ char* route_name = bprintf("%s#%s",
+ (*(routing_component_t*)(xbt_dynar_get_ptr(path_src,max)))->name,
+ (*(routing_component_t*)(xbt_dynar_get_ptr(path_dst,max)))->name);
+ e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes,route_name);
+ xbt_free(route_name);
+ }
+ if( e_route_bypass ) break;
+ }
+
+ xbt_dynar_free(&path_src);
+ xbt_dynar_free(&path_dst);
+
+ route_extended_t new_e_route = NULL;
+
+ if(e_route_bypass) {
+ void* link;
+ unsigned int cpt=0;
+ new_e_route = xbt_new0(s_route_extended_t,1);
+ new_e_route->src_gateway = e_route_bypass->src_gateway;
+ new_e_route->dst_gateway = e_route_bypass->dst_gateway;
+ new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
+ xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
+ xbt_dynar_push(new_e_route->generic_route.link_list,&link);
+ }
+ }
+
+ return new_e_route;
+}
+
+/* ************************************************************************** */
+/* ************************* GENERIC AUX FUNCTIONS ************************** */
+
+static route_extended_t generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy, void* data, int order) {
+
+ char *link_name;
+ route_extended_t e_route, new_e_route;
+ route_t route;
+ unsigned int cpt;
+ xbt_dynar_t links, links_id;
+
+ new_e_route = xbt_new0(s_route_extended_t,1);
+ new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
+ new_e_route->src_gateway = NULL;
+ new_e_route->dst_gateway = NULL;
+
+ xbt_assert0(hierarchy == SURF_ROUTING_BASE || hierarchy == SURF_ROUTING_RECURSIVE,
+ "the hierarchy type is not defined");
+
+ if(hierarchy == SURF_ROUTING_BASE ) {
+
+ route = (route_t)data;
+ links = route->link_list;
+
+ } else if(hierarchy == SURF_ROUTING_RECURSIVE ) {
+
+ e_route = (route_extended_t)data;
+ xbt_assert0(e_route->src_gateway&&e_route->dst_gateway,"bad gateway, is null");
+ links = e_route->generic_route.link_list;
+
+ /* remeber not erase the gateway names */
+ new_e_route->src_gateway = e_route->src_gateway;
+ new_e_route->dst_gateway = e_route->dst_gateway;
+ }
+
+ links_id = new_e_route->generic_route.link_list;
+
+ xbt_dynar_foreach(links, cpt, link_name) {
+
+ void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
+ if (link)
+ {
+ if( order )
+ xbt_dynar_push(links_id,&link);
+ else
+ xbt_dynar_unshift(links_id,&link);
+ }
+ else
+ THROW1(mismatch_error,0,"Link %s not found", link_name);
+ }
+
+ return new_e_route;
+}
+
+static void generic_free_extended_route(route_extended_t e_route) {
+ if(e_route) {
+ xbt_dynar_free(&(e_route->generic_route.link_list));
+ if(e_route->src_gateway) xbt_free(e_route->src_gateway);
+ if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
+ xbt_free(e_route);
+ }
+}
+
+static routing_component_t generic_as_exist(routing_component_t find_from, routing_component_t to_find) {
+ xbt_dict_cursor_t cursor = NULL;
+ char *key;
+ int found=0;
+ routing_component_t elem;
+ xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
+ if( to_find == elem || generic_as_exist(elem,to_find) ){
+ found=1;
+ break;
+ }
+ }
+ if(found) return to_find;
+ return NULL;
+}
+
+static routing_component_t generic_autonomous_system_exist(routing_component_t rc, char* element) {
+ routing_component_t element_as, result, elem;
+ xbt_dict_cursor_t cursor = NULL;
+ char *key;
+ element_as = xbt_dict_get_or_null(global_routing->where_network_elements,element);
+ result = ((routing_component_t)-1);
+ if(element_as!=rc)
+ result = generic_as_exist(rc,element_as);
+
+ if(result)
+ {
+ xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
+ if( !strcmp(elem->name,element) ) return element_as;
+ }
+ }
+ return NULL;