Allow a network topology to be loaded from a file
authorRichard Whitehouse <github@richardwhiuk.com>
Sat, 16 Apr 2011 14:56:33 +0000 (15:56 +0100)
committerRichard Whitehouse <github@richardwhiuk.com>
Sat, 16 Apr 2011 14:56:33 +0000 (15:56 +0100)
src/topology/model/topology.cc
src/topology/model/topology.h

index f4e4a10a1dd481d6602f942a839b8ac835f54af1..cda3dfbd5f728def7d1213ae2287a3cd0edb4ef0 100644 (file)
 #include "topology.h"
 #include "ns3/log.h"
 
+#include <stdexcept>
+
 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){
index af65141dede7af9d779e457176c387b203b32a8a..11ef56ecaf4308a9a0fa62ce08c4632f4b404e25 100644 (file)
@@ -21,6 +21,7 @@
 #ifndef TOPOLOGY_H
 #define TOPOLOGY_H
 
+#include <fstream>
 #include <map>
 #include <set>
 
@@ -35,6 +36,11 @@ namespace ns3 {
 struct Topology {
        
 public:
+
+       Topology();
+       Topology(std::istream& file);
+       ~Topology();
+
        long bridges;
        long hosts;
        typedef std::map<long,long> HostLinks;