From 1233c18fb41d17de7cd1c623f540411ebe856d67 Mon Sep 17 00:00:00 2001 From: Richard Whitehouse Date: Sun, 17 Apr 2011 00:12:09 +0100 Subject: [PATCH] Generate network topologies --- src/simulation/generator.cc | 79 ++++++++++++++++++++++++++++++++++ src/simulation/wscript | 6 +++ src/topology/model/topology.cc | 15 +++++++ src/topology/model/topology.h | 2 + 4 files changed, 102 insertions(+) create mode 100644 src/simulation/generator.cc diff --git a/src/simulation/generator.cc b/src/simulation/generator.cc new file mode 100644 index 0000000..a540fa8 --- /dev/null +++ b/src/simulation/generator.cc @@ -0,0 +1,79 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * 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 + * + * Author: Richard Whitehouse + */ + +#include "ns3/core-module.h" +#include "ns3/topology-module.h" + +#include +#include +#include + +using namespace ns3; + +NS_LOG_COMPONENT_DEFINE ("MooseGenerator"); + +int main (int argc, char *argv[]) +{ + +try { + + std::string type; + unsigned long size; + unsigned long hosts; + std::string file; + + CommandLine cmd; // Allow CommandLine args + cmd.AddValue("file", "Output file", file); + cmd.AddValue("type", "Network Topology (cube|mesh|torus)", type); + cmd.AddValue("size", "Network size", size); + cmd.AddValue("hosts", "Hosts per switch", hosts); + cmd.Parse (argc, argv); + + Topology t; + + if(hosts == 0){ + throw new std::runtime_error("Invalid number of hosts"); + } + + if(size < 2){ + throw new std::runtime_error("Invalid network size"); + } + + if(type == "cube"){ + t = CubeTopologyHelper::Create(hosts, size); + } else if(type == "mesh"){ + t = MeshTopologyHelper::Create(hosts, size); + } else if(type == "torus"){ + t = TorusTopologyHelper::Create(hosts, size); + } else { + throw new std::runtime_error("Unknown network topology type"); + } + + std::ofstream stream(file.c_str(), std::ios_base::out | std::ios_base::trunc); + + stream << t; + +} catch(std::runtime_error* e) { + std::cerr << e->what() << std::endl; + return 1; +} + +return 0; + +} + diff --git a/src/simulation/wscript b/src/simulation/wscript index 5cb063b..3f614c4 100644 --- a/src/simulation/wscript +++ b/src/simulation/wscript @@ -10,6 +10,12 @@ def build(bld): headers.module = 'simulation' headers.source = [] + + obj = bld.create_ns3_program('generator', ['bridge', 'internet-stack']) + + obj.source = 'generator.cc' + + obj = bld.create_ns3_program('simulation', ['bridge', 'internet-stack']) obj.source = 'simulation.cc' diff --git a/src/topology/model/topology.cc b/src/topology/model/topology.cc index 7ffd858..b2b8bcc 100644 --- a/src/topology/model/topology.cc +++ b/src/topology/model/topology.cc @@ -27,6 +27,21 @@ NS_LOG_COMPONENT_DEFINE ("Topology"); namespace ns3 { +std::ostream& operator<<(std::ostream& file, ns3::Topology t){ + file << "ns-moose" << std::endl; + file << 1 << std::endl; + file << 1 << std::endl; + file << t.hosts << std::endl; + file << t.bridges << std::endl; + for(Topology::BridgeLinks::iterator it = t.bridgeLinks.begin(); it != t.bridgeLinks.end(); it ++){ + file << it->first << '\t' << it->second << std::endl; + } + for(Topology::HostLinks::iterator it = t.hostLinks.begin(); it != t.hostLinks.end(); it ++){ + file << (it->first + t.bridges) << '\t' << it->second << std::endl; + } + return file; +} + Topology::Topology(){ } diff --git a/src/topology/model/topology.h b/src/topology/model/topology.h index 11ef56e..2f6af0b 100644 --- a/src/topology/model/topology.h +++ b/src/topology/model/topology.h @@ -60,6 +60,8 @@ public: }; +std::ostream& operator<<(std::ostream&, ns3::Topology); + } #endif -- 2.34.1