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 <sstream>
25#include "ns3/node-container.h"
26#include "ns3/log.h"
28
29
36namespace ns3 {
37
38NS_LOG_COMPONENT_DEFINE ("OrbisTopologyReader");
39
40NS_OBJECT_ENSURE_REGISTERED (OrbisTopologyReader);
41
43{
44 static TypeId tid = TypeId ("ns3::OrbisTopologyReader")
46 .SetGroupName ("TopologyReader")
47 .AddConstructor<OrbisTopologyReader> ()
48 ;
49 return tid;
50}
51
53{
54 NS_LOG_FUNCTION (this);
55}
56
58{
59 NS_LOG_FUNCTION (this);
60}
61
64{
65 std::ifstream topgen;
66 topgen.open (GetFileName ().c_str ());
67 std::map<std::string, Ptr<Node> > nodeMap;
69
70 if ( !topgen.is_open () )
71 {
72 return nodes;
73 }
74
75 std::string from;
76 std::string to;
77 std::istringstream lineBuffer;
78 std::string line;
79
80 int linksNumber = 0;
81 int nodesNumber = 0;
82
83 while (!topgen.eof ())
84 {
85 line.clear ();
86 lineBuffer.clear ();
87 from.clear ();
88 to.clear ();
89
90 getline (topgen,line);
91 lineBuffer.str (line);
92 lineBuffer >> from;
93 lineBuffer >> to;
94
95 if ( (!from.empty ()) && (!to.empty ()) )
96 {
97 NS_LOG_INFO ( linksNumber << " From: " << from << " to: " << to );
98 if ( nodeMap[from] == 0 )
99 {
100 Ptr<Node> tmpNode = CreateObject<Node> ();
101 nodeMap[from] = tmpNode;
102 nodes.Add (tmpNode);
103 nodesNumber++;
104 }
105
106 if (nodeMap[to] == 0)
107 {
108 Ptr<Node> tmpNode = CreateObject<Node> ();
109 nodeMap[to] = tmpNode;
110 nodes.Add (tmpNode);
111 nodesNumber++;
112 }
113
114 Link link ( nodeMap[from], from, nodeMap[to], to );
115 AddLink (link);
116
117 linksNumber++;
118 }
119 }
120 NS_LOG_INFO ("Orbis topology created with " << nodesNumber << " nodes and " << linksNumber << " links");
121 topgen.close ();
122
123 return nodes;
124}
125
126} /* namespace ns3 */
127
keep track of a set of node pointers.
Topology file reader (Orbis-format type).
static TypeId GetTypeId(void)
Get the type ID.
virtual NodeContainer Read(void)
Main topology reading function.
Interface for input file readers management.
void AddLink(Link link)
Adds a link to the topology.
std::string GetFileName(void) const
Returns the input file name.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::OrbisTopologyReader declaration.