📄 topo_gen.cc
字号:
//// topo_gen.cc : Topology generation program// author : Fabio Silva//// Copyright (C) 2000-2005 by the University of Southern California// $Id: topo_gen.cc,v 1.2 2005/05/10 04:12:06 fstann Exp $//// This program is free software; you can redistribute it and/or// modify it under the terms of the GNU General Public License,// version 2, as published by the Free Software Foundation.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License along// with this program; if not, write to the Free Software Foundation, Inc.,// 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.//#include "topo_gen.hh"double Distance(double x1, double y1, double x2, double y2){ return sqrt((x1 - x2) * (x1 -x2) + (y1 - y2) * (y1 - y2));}int TopoGen::findClosest(double x, double y){ NodeList::iterator node_iterator; NodeInfo *node_entry; double closest_distance; double current_distance; int closest; closest = -1; closest_distance = Distance(0, 0, MAX_X, MAX_Y); for (node_iterator = nodes_.begin(); node_iterator != nodes_.end(); ++node_iterator){ node_entry = *node_iterator; current_distance = Distance(x, y, node_entry->node_x_, node_entry->node_y_); // Is this node closest to what we have ? if (current_distance > closest_distance) continue; // Yes, keep track of it closest_distance = current_distance; closest = node_entry->node_id_; } return closest;}NodeInfo * TopoGen::findNode(NodeList *nodes, int id){ NodeList::iterator node_iterator; NodeInfo *node_entry; for (node_iterator = nodes->begin(); node_iterator != nodes->end(); ++node_iterator){ node_entry = *node_iterator; if (node_entry->node_id_ == id) return node_entry; } // Node cannot be found ! return NULL;}bool TopoGen::deleteNode(int id){ NodeList::iterator node_iterator; NodeInfo *node_entry; for (node_iterator = nodes_.begin(); node_iterator != nodes_.end(); ++node_iterator){ node_entry = *node_iterator; if (node_entry->node_id_ == id){ // Found it ! node_iterator = nodes_.erase(node_iterator); return true; } } return false;}void TopoGen::createNodes(){ NodeInfo *node; double x, y; // Create nodes for (int i = 1; i <= number_of_nodes_; i++){ x = 1.0 * size_x_ * (GetRand() * 1.0 / RAND_MAX); y = 1.0 * size_y_ * (GetRand() * 1.0 / RAND_MAX); // Add this node to the list node = new NodeInfo(i, x, y); nodes_.push_back(node); // Also add a copy to the topology list node = new NodeInfo(i, x, y); topology_.push_back(node); }}bool TopoGen::pickClusteredSinks(double x, double y){ return pickClusteredNodes(&sinks_, number_of_sinks_, x, y);}bool TopoGen::pickClusteredSources(double x, double y){ return pickClusteredNodes(&sources_, number_of_sources_, x, y);}bool TopoGen::pickClusteredNodes(NodeList *destination_list, int num_nodes, double x, double y){ NodeInfo *node; int node_id; for (int i = 0; i < num_nodes; i++){ node_id = findClosest(x, y); node = findNode(&nodes_, node_id); if (deleteNode(node_id)) destination_list->push_back(node); else return false; } return true;}bool TopoGen::pickRandomSinks(){ return pickRandomNodes(&sinks_, number_of_sinks_);}bool TopoGen::pickRandomSources(){ return pickRandomNodes(&sources_, number_of_sources_);}bool TopoGen::pickRandomNodes(NodeList *destination_list, int num_nodes){ NodeList::iterator node_iterator; NodeInfo *node_entry; // Pick the first num_nodes from nodes_ for (int i = 0; i < num_nodes; i++){ node_iterator = nodes_.begin(); // We must still have nodes available in the nodes_ list if (node_iterator == nodes_.end()) return false; node_entry = *node_iterator; // Delete node from the nodes_ list node_iterator = nodes_.erase(node_iterator); // Add node to the destination_list destination_list->push_back(node_entry); } return true;}void TopoGen::printConfig(){ NodeList::iterator node_iterator; NodeInfo *node_entry; char topology_filename[100]; char output_filename[100]; char custom_output_filename[100]; FILE *topology_file, *output_file, *custom_output_file; // Open output files with user specified name if provided if (output_filename_){ strcpy(output_filename, output_filename_); // Create a second file name 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); } } else{ // Otherwise automatically generate file names // 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); } } // Open the files strcat(output_filename, ".sim"); output_file = fopen(output_filename, "w"); if (!output_file){ fprintf(stderr, "Cannot create %s\n", output_filename); exit(-1); } if (use_custom_filter_){ strcat(custom_output_filename, ".sim"); custom_output_file = fopen(custom_output_filename, "w"); if (!custom_output_file){ fprintf(stderr, "Cannot create %s\n", custom_output_filename); exit(-1); } } // Print Configuration Parameters to the "normal" file fprintf(output_file, "# This is an topo_gen generated file\n#\n"); fprintf(output_file, "# Seed: %ld, Nodes: %d, Sinks: %d, Sources: %d\n", seed_, number_of_nodes_, number_of_sinks_, number_of_sources_); fprintf(output_file, "# Simulation Area: %d x %d, Send rate: %d\n", size_x_, size_y_, send_rate_); fprintf(output_file, "# Mode: %d, Sinks(%.2f, %.2f), Sources(%.2f, %.2f)\n", mode_, a_x_, a_y_, b_x_, b_y_); // First we start with the top level topo_gen stuff fprintf(output_file, "\n#Number of nodes\nnum-nodes = %d;\n\n", number_of_nodes_); fprintf(output_file, "#Field size\nfield-size = (%d, %d);\n\n", size_x_, size_y_); fprintf(output_file, "#Simulate MoteNIC\n"); fprintf(output_file, "sim-component = \"sim/sim_radio -m %s -p %d\";\n\n", SIM_RADIO_MODEL, SIM_RADIO_POWER); fprintf(output_file, "node default {\n"); fprintf(output_file, "\tpower = on;\n"); if (mode_ == TPP) fprintf(output_file, "\temruntab = " TPP_DEFAULT_TAB ";\n"); else if (mode_ == OPP) fprintf(output_file, "\temruntab = " OPP_DEFAULT_TAB ";\n"); else fprintf(output_file, "\temruntab = " PUSH_DEFAULT_TAB ";\n"); fprintf(output_file, "}\n\n"); // Print sinks fprintf(output_file, "#Sinks\n"); for (node_iterator = sinks_.begin(); node_iterator != sinks_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(output_file, "node %d {\n", node_entry->node_id_); fprintf(output_file, "\tposition = (%.2f, %.2f);\n", node_entry->node_x_, node_entry->node_y_); if (mode_ == TPP) fprintf(output_file, "\temruntab = " TPP_SINK_TAB ";\n"); else if (mode_ == OPP) fprintf(output_file, "\temruntab = " OPP_SINK_TAB ";\n"); else fprintf(output_file, "\temruntab = " PUSH_SINK_TAB ";\n"); fprintf(output_file, "}\n\n"); } // Print sources fprintf(output_file, "#Sources\n"); for (node_iterator = sources_.begin(); node_iterator != sources_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(output_file, "node %d {\n", node_entry->node_id_); fprintf(output_file, "\tposition = (%.2f, %.2f);\n", node_entry->node_x_, node_entry->node_y_); if (mode_ == TPP){ if(send_lambda_ > 0) fprintf(output_file, "\temruntab = \"" TPP_SOURCE_TAB " %.4f\";\n", send_lambda_); else fprintf(output_file, "\temruntab = " TPP_SOURCE_TAB ";\n"); } else if (mode_ == OPP){ if(send_lambda_ > 0) fprintf(output_file, "\temruntab = \"" OPP_SOURCE_TAB " %.4f\";\n", send_lambda_); else fprintf(output_file, "\temruntab = " OPP_SOURCE_TAB ";\n"); } else{ if(send_lambda_ > 0) fprintf(output_file, "\temruntab = \"" PUSH_SOURCE_TAB " %.4f\";\n", send_lambda_); else fprintf(output_file, "\temruntab = " PUSH_SOURCE_TAB ";\n"); } fprintf(output_file, "}\n\n"); } // Print other nodes fprintf(output_file, "#Intermediate Nodes\n"); for (node_iterator = nodes_.begin(); node_iterator != nodes_.end(); node_iterator++){ node_entry = *node_iterator; fprintf(output_file, "node %d {\n", node_entry->node_id_); fprintf(output_file, "\tposition = (%.2f, %.2f);\n", node_entry->node_x_, node_entry->node_y_); fprintf(output_file, "}\n\n"); } // Print Stop time fprintf(output_file, "time %dm %ds {\n", EXPERIMENT_RUN_MINUTES, EXPERIMENT_RUN_SECONDS); fprintf(output_file, "\thalt;\n"); fprintf(output_file, "}\n"); if (output_file != stdout) fclose(output_file); /////////////////////////// Custom Filter ////////////////////////// // Print to the "custom filter" file if (use_custom_filter_){ // Print Configuration Parameters fprintf(custom_output_file, "# This is an topo_gen generated file\n#\n"); fprintf(custom_output_file, "# Seed: %ld, Nodes: %d, Sinks: %d, Sources: %d\n", seed_, number_of_nodes_, number_of_sinks_, number_of_sources_); fprintf(custom_output_file, "# Simulation Area: %d x %d, Send rate: %d\n", size_x_, size_y_, send_rate_); fprintf(custom_output_file, "# Mode: %d, Sinks(%.2f, %.2f), Sources(%.2f, %.2f)\n", mode_, a_x_, a_y_, b_x_, b_y_); // First we start with the top level topo_gen stuff fprintf(custom_output_file, "\n#Number of nodes\nnum-nodes = %d;\n\n", number_of_nodes_); fprintf(custom_output_file, "#Field size\nfield-size = (%d, %d);\n\n", size_x_, size_y_); fprintf(custom_output_file, "#Simulate MoteNIC\n"); fprintf(custom_output_file, "sim-component = \"sim/sim_radio -m %s -p %d\";\n\n", SIM_RADIO_MODEL, SIM_RADIO_POWER); fprintf(custom_output_file, "node default {\n"); fprintf(custom_output_file, "\tpower = on;\n"); if (mode_ == TPP) fprintf(custom_output_file, "\temruntab = " TPP_CUSTOM_DEFAULT_TAB ";\n"); else if (mode_ == OPP) fprintf(custom_output_file, "\temruntab = " OPP_CUSTOM_DEFAULT_TAB ";\n"); else fprintf(custom_output_file, "\temruntab = " PUSH_CUSTOM_DEFAULT_TAB ";\n"); fprintf(custom_output_file, "}\n\n"); // Print sinks fprintf(custom_output_file, "#Sinks\n"); for (node_iterator = sinks_.begin(); node_iterator != sinks_.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_); if (mode_ == TPP) fprintf(custom_output_file, "\temruntab = " TPP_CUSTOM_SINK_TAB ";\n"); else if (mode_ == OPP) fprintf(custom_output_file, "\temruntab = " OPP_CUSTOM_SINK_TAB ";\n"); else fprintf(custom_output_file, "\temruntab = " PUSH_CUSTOM_SINK_TAB ";\n"); fprintf(custom_output_file, "}\n\n"); } // Print sources fprintf(custom_output_file, "#Sources\n"); for (node_iterator = sources_.begin(); node_iterator != sources_.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_); if (mode_ == TPP){ if(send_lambda_ > 0) fprintf(custom_output_file, "\temruntab = \"" TPP_CUSTOM_SOURCE_TAB " %.4f\";\n", send_lambda_); else fprintf(custom_output_file, "\temruntab = " TPP_CUSTOM_SOURCE_TAB ";\n"); } else if (mode_ == OPP){ if(send_lambda_ > 0) fprintf(custom_output_file, "\temruntab = \"" OPP_CUSTOM_SOURCE_TAB " %.4f\";\n", send_lambda_); else fprintf(custom_output_file, "\temruntab = " OPP_CUSTOM_SOURCE_TAB ";\n"); } else{ if(send_lambda_ > 0) fprintf(custom_output_file, "\temruntab = \"" PUSH_CUSTOM_SOURCE_TAB " %.4f\";\n", send_lambda_); else fprintf(custom_output_file, "\temruntab = " PUSH_CUSTOM_SOURCE_TAB ";\n"); } fprintf(custom_output_file, "}\n\n"); } // Print other nodes fprintf(custom_output_file, "#Intermediate Nodes\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -