+ // TODO: MAKE A FUNCTION FOR GENERIC CHECK
+ 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_assert3(src_as != NULL && dst_as != NULL, "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",src,dst,rc->name);
+ xbt_assert4(src_as == dst_as, "The src(%s in %s) and dst(%s in %s) are in differents AS",src,src_as->name,dst,dst_as->name);
+ xbt_assert2(rc == dst_as, "The routing component of src and dst is not the same as the network elements belong (%s==%s)",rc->name,dst_as->name);
+
+ src_id = xbt_dict_get_or_null(routing->to_index,src);
+ dst_id = xbt_dict_get_or_null(routing->to_index,dst);
+ xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",src,dst);
+
+ /*Use the graph_node id mapping set to quickly find the nodes */
+ graph_node_map_element_t src_elm = graph_node_map_search(routing,*src_id);
+ graph_node_map_element_t dst_elm = graph_node_map_search(routing,*dst_id);
+ xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", *src_id, *dst_id);
+ src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
+ dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
+
+ /* if the src and dst are the same */ // FIXED by david
+ if( src_node_id == dst_node_id ) {
+
+ xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
+ xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
+ xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
+
+ xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id, *dst_id);
+
+ e_route = (route_extended_t)xbt_graph_edge_get_data(edge);
+
+ links = e_route->generic_route.link_list;
+ xbt_dynar_foreach(links, cpt, link) {
+ xbt_dynar_push(new_e_route->generic_route.link_list,&link);
+ }
+
+ return new_e_route;
+ }
+
+ if(routing->cached) {
+ /*check if there is a cached predecessor list avail */
+ elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
+ }
+
+ if(elm) { //cached mode and cache hit
+ pred_arr = elm->pred_arr;
+ } else { //not cached mode or cache miss
+ double * cost_arr = NULL;
+ xbt_heap_t pqueue = NULL;
+ int i = 0;
+
+ int nr_nodes = xbt_dynar_length(nodes);
+ cost_arr = xbt_new0(double, nr_nodes); // link cost from src to other hosts
+ pred_arr = xbt_new0(int, nr_nodes); // predecessors in path from src
+ pqueue = xbt_heap_new(nr_nodes, xbt_free);
+
+ //initialize
+ cost_arr[src_node_id] = 0.0;
+
+ for(i = 0; i < nr_nodes; i++) {
+ if(i != src_node_id) {
+ cost_arr[i] = DBL_MAX;
+ }
+
+ pred_arr[i] = 0;
+
+ //initialize priority queue
+ nodeid = xbt_new0(int, 1);
+ *nodeid = i;
+ xbt_heap_push(pqueue, nodeid, cost_arr[i]);
+
+ }
+
+ // apply dijkstra using the indexes from the graph's node array
+ while(xbt_heap_size(pqueue) > 0) {
+ int * v_id = xbt_heap_pop(pqueue);
+ xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
+ xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
+ xbt_edge_t edge = NULL;
+ unsigned int cursor;
+
+ xbt_dynar_foreach(out_edges, cursor, edge) {
+ xbt_node_t u_node = xbt_graph_edge_get_target(edge);
+ graph_node_data_t data = xbt_graph_node_get_data(u_node);
+ int u_id = data->graph_id;
+ int cost_v_u = 1; //fixed link cost for now // TODO DAVID REDO
+
+ if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
+ pred_arr[u_id] = *v_id;
+ cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
+ nodeid = xbt_new0(int, 1);
+ *nodeid = u_id;
+ xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
+ }
+ }
+
+ //free item popped from pqueue
+ xbt_free(v_id);
+ }
+
+ xbt_free(cost_arr);
+ xbt_heap_free(pqueue);
+
+ }
+
+ //compose route path with links
+ for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
+ xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
+ xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
+ xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
+
+ xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id, *dst_id);
+
+ e_route = (route_extended_t)xbt_graph_edge_get_data(edge);
+
+ if(v==dst_node_id)
+ new_e_route->src_gateway=e_route->src_gateway;
+ new_e_route->dst_gateway=e_route->dst_gateway;
+
+ links = e_route->generic_route.link_list;
+ xbt_dynar_foreach(links, cpt, link) {
+ xbt_dynar_push(new_e_route->generic_route.link_list,&link);
+ }
+ size++;
+ }
+
+ if(routing->cached && elm == NULL) {
+ //add to predecessor list of the current src-host to cache
+ elm = xbt_new0(struct route_cache_element, 1); // FIXED by david
+ elm->pred_arr = pred_arr;
+ elm->size = size;
+ xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
+ }
+
+ if(!routing->cached)
+ xbt_free(pred_arr);
+
+ return new_e_route;