void print_shortest_path(INFO_OF_NODE info, int i, int src) { if(info[i].parent != src) { print_shortest_path(info, info[i].parent, src); printf("-->%d", i); } else printf("%d-->%d", src, i); }
/******************************************************************************* * @fn void *compute_routes_thread( void *rp_tables ) * * @brief Thread that takes care of routing * ****************************************************************************/ void *compute_routes_thread( void *rp_tables ) { static uint32_t index; uint8_t node_index; uint8_t *route_table = &((uint8_t*)rp_tables)[0]; uint8_t *power_table = &((uint8_t*)rp_tables)[MAX_DEVICES]; // loop forever for (;;) { // Block until next table is ready pthread_mutex_lock ( &mutex_route_start ); // Assuming rssi_table has been updated clean_table( rssi_table ); add_links_from_table( rssi_table ); // Run dijkstra's algorithm with 0 being the access point dijkstra( AP_NODE_ID, c_factor ); // Display shortest paths and update energies for( node_index = 1; node_index < (MAX_DEVICES + 1); node_index++ ) { compute_shortest_path( node_index ); print_shortest_path( node_index ); } // Compute routing table compute_rp_tables( route_table, link_powers ); // Debug memcpy( previous_powers_debug, previous_powers, sizeof(previous_powers) ); memcpy( route_table_debug, route_table, sizeof(route_table_debug) ); // Compute power table compute_required_powers( link_powers, power_table ); print_rssi_table(); print_node_energy( AP_NODE_ID, fp_energies ); index++; printf("\nRound %d\n", index); // Block until next table is ready pthread_mutex_unlock ( &mutex_route_done ); } return NULL; }
void dijkstra(WEIGHT weight, int len, int src) { INFO_OF_NODE info; init_node_info(info, len, src); HEAP heap; int i; for(i = 0; i < len; ++i) heap[i] = i; build_min_heap(heap, info, len); int rear = len; while(rear > 0) { int q = heap[0]; info[q].color = BLACK; heap[0] = heap[--rear]; heap[rear] = q; keep_heap_property(heap, info, 0, rear); int i; for(i = 0; i < len; ++i) { //cause dijkstra can't support negative weight. if(weight[q][i] > 0 && info[i].color == WHITE) { relax_edge(q, i, heap, info, weight); } } } for(i = 0; i < len; ++i) { print_shortest_path(info, i, src); if(info[i].path_len == LONG_MAX) printf(" can't reach.\n"); else printf(" weight of path = %d\n", info[i].path_len); } }