A Discrete-Event Network Simulator
API
orbis-topology-reader.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Universita' di Firenze, Italy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tommaso Pecorella (tommaso.pecorella@unifi.it)
19  * Author: Valerio Sartini (valesar@gmail.com)
20  */
21 
22 #include <fstream>
23 #include <cstdlib>
24 #include <iostream>
25 #include <sstream>
26 
27 #include "ns3/log.h"
28 #include "orbis-topology-reader.h"
29 
30 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("OrbisTopologyReader");
34 
35 NS_OBJECT_ENSURE_REGISTERED (OrbisTopologyReader);
36 
38 {
39  static TypeId tid = TypeId ("ns3::OrbisTopologyReader")
41  .SetGroupName ("TopologyReader")
42  .AddConstructor<OrbisTopologyReader> ()
43  ;
44  return tid;
45 }
46 
48 {
49  NS_LOG_FUNCTION (this);
50 }
51 
53 {
54  NS_LOG_FUNCTION (this);
55 }
56 
59 {
60  std::ifstream topgen;
61  topgen.open (GetFileName ().c_str ());
62  std::map<std::string, Ptr<Node> > nodeMap;
64 
65  if ( !topgen.is_open () )
66  {
67  return nodes;
68  }
69 
70  std::string from;
71  std::string to;
72  std::istringstream lineBuffer;
73  std::string line;
74 
75  int linksNumber = 0;
76  int nodesNumber = 0;
77 
78  while (!topgen.eof ())
79  {
80  line.clear ();
81  lineBuffer.clear ();
82  from.clear ();
83  to.clear ();
84 
85  getline (topgen,line);
86  lineBuffer.str (line);
87  lineBuffer >> from;
88  lineBuffer >> to;
89 
90  if ( (!from.empty ()) && (!to.empty ()) )
91  {
92  NS_LOG_INFO ( linksNumber << " From: " << from << " to: " << to );
93  if ( nodeMap[from] == 0 )
94  {
95  Ptr<Node> tmpNode = CreateObject<Node> ();
96  nodeMap[from] = tmpNode;
97  nodes.Add (tmpNode);
98  nodesNumber++;
99  }
100 
101  if (nodeMap[to] == 0)
102  {
103  Ptr<Node> tmpNode = CreateObject<Node> ();
104  nodeMap[to] = tmpNode;
105  nodes.Add (tmpNode);
106  nodesNumber++;
107  }
108 
109  Link link ( nodeMap[from], from, nodeMap[to], to );
110  AddLink (link);
111 
112  linksNumber++;
113  }
114  }
115  NS_LOG_INFO ("Orbis topology created with " << nodesNumber << " nodes and " << linksNumber << " links");
116  topgen.close ();
117 
118  return nodes;
119 }
120 
121 } /* namespace ns3 */
122 
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
std::string GetFileName(void) const
Returns the input file name.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
void AddLink(Link link)
Adds a link to the topology.
tuple nodes
Definition: first.py:25
Interface for input file readers management.
Topology file reader (Orbis-format type).
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
static TypeId GetTypeId(void)
Get the type ID.
virtual NodeContainer Read(void)
Main topology reading function.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914