From: Richard Whitehouse Date: Sat, 16 Apr 2011 14:56:33 +0000 (+0100) Subject: Allow a network topology to be loaded from a file X-Git-Url: https://git.richardwhiuk.com/?a=commitdiff_plain;h=3288d3c1974252a47e68f757acfc82fbea7b9ee2;p=ns-moose.git Allow a network topology to be loaded from a file --- diff --git a/src/topology/model/topology.cc b/src/topology/model/topology.cc index f4e4a10..cda3dfb 100644 --- a/src/topology/model/topology.cc +++ b/src/topology/model/topology.cc @@ -21,10 +21,84 @@ #include "topology.h" #include "ns3/log.h" +#include + NS_LOG_COMPONENT_DEFINE ("Topology"); namespace ns3 { +Topology::Topology(){ + +} + +Topology::Topology(std::istream& file){ + + std::string line; + + std::getline(file, line); + if(line != "ns-moose"){ + throw new std::runtime_error("Invalid Network Topology File"); + } + + unsigned long type; + file >> type; + if(type != 1){ + throw new std::runtime_error("Invalid Network Topology File Type"); + } + + unsigned long version; + file >> version; + if(version != 1){ + throw new std::runtime_error("Invalid Network Topology File Version"); + } + + file >> hosts; + if(hosts < 1){ + throw new std::runtime_error("Network Topology contains no hosts."); + } + + file >> bridges; + if(bridges < 1){ + throw new std::runtime_error("Network Topology contains no bridges."); + } + + while(file.good() && !file.eof()){ + unsigned long source; + unsigned long destination; + file >> source; + file >> destination; + if(file.good()){ + if(source > bridges){ // Source is a host + source -= bridges; + if(source > hosts){ + throw new std::runtime_error("Invalid Link in Topology"); + } + if(destination > bridges){ // Dest is a host + throw new std::runtime_error("Host-Host Link in Topology"); + } + hostLinks[source] = destination; + } else { // Source is a bridge + if(destination > bridges){ // Source is a host + destination -= bridges; + if(destination > hosts){ + throw new std::runtime_error("Invalid Host Link"); + } + hostLinks[destination] = source; + } else { + bridgeLinks.insert(Topology::BridgeLink(source, destination)); + } + } + } + } + + std::cout << "Valid Network Topology File" << std::endl; + +} + +Topology::~Topology(){ + +} + bool Topology::BridgeLinkCompare::operator()( BridgeLink const &lhs, BridgeLink const &rhs) { if(lhs.first < lhs.first){ if(rhs.first < rhs.second){ diff --git a/src/topology/model/topology.h b/src/topology/model/topology.h index af65141..11ef56e 100644 --- a/src/topology/model/topology.h +++ b/src/topology/model/topology.h @@ -21,6 +21,7 @@ #ifndef TOPOLOGY_H #define TOPOLOGY_H +#include #include #include @@ -35,6 +36,11 @@ namespace ns3 { struct Topology { public: + + Topology(); + Topology(std::istream& file); + ~Topology(); + long bridges; long hosts; typedef std::map HostLinks;