📄 topo_gen.cc
字号:
for (node_iterator = nodes_.begin(); node_iterator != nodes_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(custom_output_file, "node %d {\n", node_entry->node_id_); fprintf(custom_output_file, "\tposition = (%.2f, %.2f);\n", node_entry->node_x_, node_entry->node_y_); fprintf(custom_output_file, "}\n\n"); } // Print Stop time fprintf(custom_output_file, "time %dm %ds {\n", EXPERIMENT_RUN_MINUTES, EXPERIMENT_RUN_SECONDS); fprintf(custom_output_file, "\thalt;\n"); fprintf(custom_output_file, "}\n"); if (custom_output_file != stdout) fclose(custom_output_file); } /////////////////////////// Custom Filter ////////////////////////// if(gen_topo_files_){ // Output nodes topology file sprintf(topology_filename, "nodes-%ld.dat", seed_); topology_file = fopen(topology_filename, "w"); if (topology_file){ for (node_iterator = nodes_.begin(); node_iterator != nodes_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(topology_file, "%.2f, %.2f\n", node_entry->node_x_, node_entry->node_y_); } fclose(topology_file); } // Output sinks topology file sprintf(topology_filename, "sinks-%ld.dat", seed_); topology_file = fopen(topology_filename, "w"); if (topology_file){ for (node_iterator = sinks_.begin(); node_iterator != sinks_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(topology_file, "%.2f, %.2f\n", node_entry->node_x_, node_entry->node_y_); } fclose(topology_file); } // Output sources topology file sprintf(topology_filename, "sources-%ld.dat", seed_); topology_file = fopen(topology_filename, "w"); if (topology_file){ for (node_iterator = sources_.begin(); node_iterator != sources_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(topology_file, "%.2f, %.2f\n", node_entry->node_x_, node_entry->node_y_); } fclose(topology_file); } }}//// printTrafficFile//// This routine outputs the actual ns-2 traffic file.//void TopoGen::printTrafficFile(){ NodeList::iterator node_iterator; NodeInfo *node_entry; char output_filename[100]; char custom_output_filename[100]; FILE *output_file, *custom_output_file; double start_time = 0.000; int tmp_id; // Open output files with user specified name if provided if (output_filename_){ strcpy(output_filename, output_filename_); // Open a second output file if we are using a custom filter if (use_custom_filter_){ strcpy(custom_output_filename, output_filename_); strcat(custom_output_filename, "_"); strcat(custom_output_filename, CUSTOM_FILTER_NAME); } } // Otherwise automatically generate file names else{ // Changed to include send rate in the file name if (mode_ == TPP){ if (filenames_with_send_rate_) sprintf(output_filename, "topo-tpp-%d-%d-%d-%d-run-%d", number_of_nodes_, number_of_sinks_, number_of_sources_, send_rate_, number_of_runs_); else sprintf(output_filename, "topo-tpp-%d-%d-%d-run-%d", number_of_nodes_, number_of_sinks_, number_of_sources_, number_of_runs_); } else if (mode_ == OPP){ if (filenames_with_send_rate_) sprintf(output_filename, "topo-opp-%d-%d-%d-%d-run-%d", number_of_nodes_, number_of_sinks_, number_of_sources_, send_rate_, number_of_runs_); else sprintf(output_filename, "topo-opp-%d-%d-%d-run-%d", number_of_nodes_, number_of_sinks_, number_of_sources_, number_of_runs_); } else{ if (filenames_with_send_rate_) sprintf(output_filename, "topo-push-%d-%d-%d-%d-run-%d", number_of_nodes_, number_of_sinks_, number_of_sources_, send_rate_, number_of_runs_); else sprintf(output_filename, "topo-push-%d-%d-%d-run-%d", number_of_nodes_, number_of_sinks_, number_of_sources_, number_of_runs_); } // Now concantenate the custom string if use_custom_filter // Now add the file prefix if (use_custom_filter_){ strcpy(custom_output_filename, output_filename); strcat(custom_output_filename, "_"); strcat(custom_output_filename, CUSTOM_FILTER_NAME); } } strcat(output_filename, ".tcl"); output_file = fopen(output_filename, "w"); if (!output_file){ fprintf(stderr, "Cannot create %s\n", output_filename); exit(-1); } printf("printTrafficFile: opened traffic file: %s\n", output_filename); if (use_custom_filter_){ strcat(custom_output_filename, ".tcl"); custom_output_file = fopen(custom_output_filename, "w"); if (!custom_output_file){ fprintf(stderr, "Cannot create %s\n", custom_output_filename); exit(-1); } printf("printTrafficFile: opened custom traffic file: %s\n", custom_output_filename); } // Print Configuration Parameters fprintf(output_file, "# This is a topo_gen generated ns-2 file\n#\n"); fprintf(output_file, "# Nodes: %d, Sinks: %d, Sources: %d\n", number_of_nodes_, number_of_sinks_, number_of_sources_); fprintf(output_file, "# Simulation Area: %d x %d \n", size_x_, size_y_ ); fprintf(output_file, "# Mode: %d, Sinks(%.2f, %.2f), Sources(%.2f, %.2f)\n", mode_, a_x_, a_y_, b_x_, b_y_); fprintf(output_file, "#Number of nodes = %d;\n", number_of_nodes_); fprintf(output_file, "#Field size = (%d, %d);\n\n", size_x_, size_y_); // Print basic ns commands for all nodes fprintf(output_file, "# Generate the nodes - the diffusion filter will be added to\n"); fprintf(output_file, "# each node as specified by node-config in your primary tcl script\n\n"); for (node_iterator = topology_.begin(); node_iterator != topology_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(output_file, "$node_(%d) set X_ %f \n", node_entry->node_id_, node_entry->node_x_); fprintf(output_file, "$node_(%d) set Y_ %f \n", node_entry->node_id_, node_entry->node_y_); fprintf(output_file, "$node_(%d) set Z_ 0.000000\n", node_entry->node_id_); fprintf(output_file, "\n"); } // Print sinks fprintf(output_file, "#Diffusion Sink Applications\n\n"); tmp_id = 1; for (node_iterator = sinks_.begin(); node_iterator != sinks_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(output_file, "#Diffusion sink %d application\n", tmp_id); fprintf(output_file, "set snk_(%d) [new Application/DiffApp/%sReceiver]\n", tmp_id, CUSTOM_FILTER_NAME); fprintf(output_file, "$ns_ attach-diffapp $node_(%d) $snk_(%d)\n", node_entry->node_id_, tmp_id); fprintf(output_file, "$ns_ at %3.3f \"$snk_(%d) subscribe\"\n\n", start_time, tmp_id); tmp_id += 1; start_time += .010; } // Print sources fprintf(output_file, "#Diffusion Source Applications\n\n"); tmp_id = 1; for (node_iterator = sources_.begin(); node_iterator != sources_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(output_file, "#Diffusion source %d application\n", tmp_id); fprintf(output_file, "set src_(%d) [new Application/DiffApp/%sSender]\n", tmp_id, CUSTOM_FILTER_NAME); fprintf(output_file, "$ns_ attach-diffapp $node_(%d) $src_(%d)\n", node_entry->node_id_, tmp_id); fprintf(output_file, "$ns_ at %3.3f \"$src_(%d) sourceId %d\"\n", start_time, tmp_id, tmp_id); start_time += .010; fprintf(output_file, "$ns_ at %3.3f \"$src_(%d) publish\"\n\n", start_time, tmp_id); tmp_id += 1; start_time += .990; } fclose(output_file); ////////////////////// custom filter printfs //////////////////////// if (use_custom_filter_){ // Print Configuration Parameters fprintf(custom_output_file, "# This is an ns_topo generated custom filter file\n#\n"); fprintf(custom_output_file, "# Nodes: %d, Sinks: %d, Sources: %d\n", number_of_nodes_, number_of_sinks_, number_of_sources_); fprintf(custom_output_file, "# Simulation Area: %d x %d \n", size_x_, size_y_ ); fprintf(custom_output_file, "# Mode: %d, Sinks(%.2f, %.2f), Sources(%.2f, %.2f)\n", mode_, a_x_, a_y_, b_x_, b_y_); fprintf(custom_output_file, "#Number of nodes = %d;\n", number_of_nodes_); fprintf(custom_output_file, "#Field size = (%d, %d);\n\n", size_x_, size_y_); // Print basic ns commands for all nodes fprintf(output_file, "# Generate the nodes - the diffusion filter will be added to\n"); fprintf(output_file, "# each node as specified by node-config in your primary tcl script\n\n"); for (node_iterator = topology_.begin(); node_iterator != topology_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(custom_output_file, "$node_(%d) set X_ %f \n", node_entry->node_id_, node_entry->node_x_); fprintf(custom_output_file, "$node_(%d) set Y_ %f \n", node_entry->node_id_, node_entry->node_y_); fprintf(custom_output_file, "$node_(%d) set Z_ 0.000000\n", node_entry->node_id_); fprintf(custom_output_file, "\n"); } fprintf (custom_output_file, "# Add a Custom Filter to each node\n\n"); for (node_iterator = topology_.begin(); node_iterator != topology_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(custom_output_file, "#Add a Custom Filter to node %d;\n", node_entry->node_id_); fprintf(custom_output_file, "set flt_(%d) [new Application/DiffApp/%sFilter]\n", node_entry->node_id_, CUSTOM_FILTER_NAME); fprintf(custom_output_file, "$ns_ attach-diffapp $node_(%d) $flt_(%d)\n", node_entry->node_id_, node_entry->node_id_); fprintf(custom_output_file, "$ns_ at %3.3f \"$flt_(%d) start\"\n\n", start_time, node_entry->node_id_); start_time += .010; } // Print sinks fprintf(custom_output_file, "#Diffusion Sink Applications\n\n"); tmp_id = 1; for (node_iterator = sinks_.begin(); node_iterator != sinks_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(custom_output_file, "#Diffusion sink %d application\n", tmp_id); fprintf(custom_output_file, "set snk_(%d) [new Application/DiffApp/%sReceiver]\n", tmp_id, CUSTOM_FILTER_NAME); fprintf(custom_output_file, "$ns_ attach-diffapp $node_(%d) $snk_(%d)\n", node_entry->node_id_, tmp_id); fprintf(custom_output_file, "$ns_ at %3.3f \"$snk_(%d) subscribe\"\n\n", start_time, tmp_id); tmp_id += 1; start_time += .010; } // Print sources fprintf(custom_output_file, "#Diffusion Source Applications\n\n"); tmp_id = 1; for (node_iterator = sources_.begin(); node_iterator != sources_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(custom_output_file, "#Diffusion source %d application\n", tmp_id); fprintf(custom_output_file, "set src_(%d) [new Application/DiffApp/%sSender]\n", tmp_id, CUSTOM_FILTER_NAME); fprintf(custom_output_file, "$ns_ attach-diffapp $node_(%d) $src_(%d)\n", node_entry->node_id_, tmp_id); fprintf(custom_output_file, "$ns_ at %3.3f \"$src_(%d) sourceId %d\"\n", start_time, tmp_id, tmp_id); start_time += .010; fprintf(custom_output_file, "$ns_ at %3.3f \"$src_(%d) publish\"\n\n", start_time, tmp_id); tmp_id += 1; start_time += .990; } fclose(custom_output_file); } printf("printTrafficFile: done!\n");}void TopoGen::statsConfig(){ NodeList::iterator node_iterator; NodeInfo *node_entry; int topology_diameter; int current_diameter; fprintf(stdout, "Checking topology...\n"); topology_diameter = 0; // Go through all sinks for (node_iterator = sinks_.begin(); node_iterator != sinks_.end(); node_iterator++){ node_entry = *node_iterator; current_diameter = statsNode(node_entry->node_id_); if (current_diameter > topology_diameter) topology_diameter = current_diameter; } fprintf(stdout, "Network diameter is : %d\n", topology_diameter);}int TopoGen::statsNode(int32_t node_id){ NodeList::iterator node_iterator; TopoList::iterator topo_list_itr; CostList::iterator cost_list_itr; TopoList *to_process; TopoList *in_processing; CostList processed; NodeInfo *node_entry; CostInfo *cost_entry; int current_diameter, max_diameter, current_cost; bool already_processed; double node_x, node_y; int32_t current_node; // Insert initial node in the to_process list to_process = new TopoList; to_process->push_back(node_id); current_cost = 0; // Start node has cost 0 cost_entry = new CostInfo(node_id, 0); processed.push_back(cost_entry); // Process topology while(to_process->size()){ // Move nodes from to_porcess to in_processing in_processing = to_process; to_process = new TopoList; // Increase cost current_cost++; // Process all nodes in the in_processing list for (topo_list_itr = in_processing->begin(); topo_list_itr != in_processing->end();){ // Get node and delete it from the list current_node = *topo_list_itr; topo_list_itr = in_processing->erase(topo_list_itr); node_entry = findNode(&topology_, current_node); if (!node_entry){ fprintf(stderr, "Cannot find node !\n"); exit(-1); } node_x = node_entry->node_x_; node_y = node_entry->node_y_; for (node_iterator = topology_.begin(); node_iterator != topology_.end(); node_iterator++){ node_entry = *node_iterator; // Check if the node is within radio range of node_id if (Distance(node_x, node_y, node_entry->node_x_, node_entry->node_y_) < COMMUNICATION_RANGE){ // We can reach this guy, check if it's already in the CostList already_processed = false; for (cost_list_itr = processed.begin(); cost_list_itr != processed.end(); cost_list_itr++){ cost_entry = *cost_list_itr; // Compare node ids if (cost_entry->node_id_ == node_entry->node_id_){ already_processed = true; break; } } if (!already_processed){ // Insert this node in the processed and to_process lists to_process->push_back(node_entry->node_id_); cost_entry = new CostInfo(node_entry->node_id_, current_cost); processed.push_back(cost_entry); } } // if Distance... } // for node_iterator } // for topo_list_itr if (in_processing->size()){ fprintf(stderr, "in_processing list not empty... size %d !\n", in_processing->size()); exit(-1); } in_processing->clear(); delete in_processing; } // while loop // Clean up to_process->clear(); delete to_process; max_diameter = 0; for (node_iterator = sources_.begin(); node_iterator != sources_.end(); node_iterator++){ // Check source's cost node_entry = *node_iterator; current_diameter = 0; for (cost_list_itr = processed.begin(); cost_list_itr != processed.end(); cost_list_itr++){ cost_entry = *cost_list_itr; if (cost_entry->node_id_ == node_entry->node_id_){ current_diameter = cost_entry->cost_; break; } } if (current_diameter == 0){ fprintf(stderr, "Source %d couldn't be found !\n", node_entry->node_id_); exit(-1); } if (max_diameter < current_diameter) max_diameter = current_diameter; } // Done, clean up now for (cost_list_itr = processed.begin(); cost_list_itr != processed.end(); cost_list_itr++){ delete (*cost_list_itr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -