A Discrete-Event Network Simulator
API
outputmanager.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 
3 
4 /*
5 * Copyright (c) 2020 DLTLT
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation;
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Corresponding author: Niki Hrovatin <niki.hrovatin@famnit.upr.si>
21 */
22 
23 
24 #include "outputmanager.h"
25 
26 namespace ns3 {
27 
28 //Zagotovi, da se registrira TypeId
29 NS_OBJECT_ENSURE_REGISTERED (OutputManager);
30 
31 NS_LOG_COMPONENT_DEFINE ("outputmanager");
32 
33 TypeId
35 {
36  static TypeId tid =
37  TypeId ("ns3::OutputManager")
38  .SetParent<Object> ()
39  .AddConstructor<OutputManager> ()
40  .AddAttribute ("OutputPath", "Path to the directory where output files are stored",
41  StringValue ("./src/onion_routing_wsn/sim_results/"),
42  MakeStringAccessor (&OutputManager::m_outputFilePath),
43  MakeStringChecker ());
44  return tid;
45 }
46 
48 {
49 }
50 
51 OutputManager::OutputManager (std::string name, uint16_t simNum, uint16_t numNodes,
52  enum Topology topology, enum Routing routing, bool printDescription)
53 {
54 
55  m_printDescription = printDescription;
56 
57  m_simName = name;
58 
59  m_simDetails = std::to_string (simNum) + "," + std::to_string (numNodes) + ",";
60 
61  switch (topology)
62  {
63  case Topology::GRID:
64  m_simDetails = m_simDetails + "grid" + ",";
65  break;
66  case Topology::DISC:
67  m_simDetails = m_simDetails + "disc" + ",";
68  break;
69  }
70 
71  switch (routing)
72  {
73  case Routing::AODV:
74  m_simDetails = m_simDetails + "aodv";
75  break;
76  case Routing::DSR:
77  m_simDetails = m_simDetails + "dsr";
78  break;
79  case Routing::OLSR:
80  m_simDetails = m_simDetails + "olsr";
81  break;
82  case Routing::DSDV:
83  m_simDetails = m_simDetails + "dsdv";
84  break;
85  }
86  //set routing
87  m_routing = routing;
88 }
89 
90 void
92 {
93  //if filename exists -> remove
94  std::remove (std::string (m_outputFilePath + m_simName + ".csv").c_str ());
95 
96  Ptr<OutputStreamWrapper> StreamWrapper =
97  Create<OutputStreamWrapper> (m_outputFilePath + m_simName + ".csv", std::ios::app);
98  m_simStreamWrapper = StreamWrapper;
99 }
100 
102 {
103 }
104 
105 void
106 OutputManager::PrintIntro (std::string intro)
107 {
108 
109  NS_LOG_INFO (
110  "---------------------------------Simulation description-----------------------------------\n"
111  << intro
112  << "-----------------------------------Simulation "
113  "output--------------------------------------");
114  if (m_printDescription)
115  {
116  PrintLine ("---------------------------------Simulation "
117  "description-----------------------------------\n" +
118  intro + "--csv headers--\n" + h_onionHeader + "\n" + h_routingHeader + "\n" +
120  "\n-----------------------------------Simulation "
121  "output--------------------------------------");
122  }
123 }
124 
125 void
126 OutputManager::SimulationEnd (std::string end_at)
127 {
128  if (m_printDescription)
129  {
130  PrintLine ("-------------------- Simulation end: " + end_at);
131  }
132 }
133 
134 void
135 OutputManager::SendOnion (int packet_size, int head_size, int body_size, int onion_path_len,
136  Time sent_at)
137 {
138  m_onionId++;
139  m_onionPathLength = onion_path_len;
140  t_onionDelta = sent_at.GetSeconds ();
141  m_onionData = "onion_details," + m_simName + "," + m_simDetails + "," +
142  std::to_string (m_onionId) + "," + std::to_string (packet_size) + "," +
143  std::to_string (head_size) + "," + std::to_string (body_size) + "," +
144  std::to_string (onion_path_len) + "," + std::to_string (sent_at.GetSeconds ());
145 
146  NS_LOG_INFO ("--------------- Onion message sent at time: "
147  << std::to_string (sent_at.GetSeconds ()) << ", with onion id: " << m_onionId
148  << ", onion path length: " << onion_path_len
149  << ", packet size: " + std::to_string (packet_size) << " B, head size: " << head_size
150  << " B, body size: " << body_size << " B");
151 }
152 
153 void
155 {
156  m_onionData = m_onionData + "," + std::to_string (recv_at.GetSeconds ()) + "," +
157  std::to_string (recv_at.GetSeconds () - t_onionDelta);
159 
160  NS_LOG_INFO ("--------------- Onion message received back at time :"
161  << std::to_string (recv_at.GetSeconds ()) << " with onion id: " << m_onionId
162  << ", onion traveling time: "
163  << std::to_string (recv_at.GetSeconds () - t_onionDelta));
164 }
165 
166 void
167 OutputManager::OnionRoutingSend (Ipv4Address send_ip, Ipv4Address recv_ip, int packet_size,
168  int head_size, int body_size, Time sent_at)
169 {
170  t_hopDelta = sent_at.GetSeconds ();
171  m_routingData = "onion_routing," + m_simName + "," + m_simDetails + "," +
172  std::to_string (m_onionId) + "," + Ipv4ToString (send_ip) + "," +
173  Ipv4ToString (recv_ip) + "," + std::to_string (packet_size) + "," +
174  std::to_string (head_size) + "," + std::to_string (body_size) + "," +
175  std::to_string (sent_at.GetSeconds ());
176 
177  m_routingLog = "Onion routing-- onion sent from node ip: " + Ipv4ToString (send_ip) +
178  ", of packet size: " + std::to_string (packet_size) +
179  " B, header size: " + std::to_string (head_size) +
180  " B, trailer size: " + std::to_string (body_size) +
181  " B, received at node ip: " + Ipv4ToString (recv_ip);
182 }
183 
184 void
186 {
187  m_routingData = m_routingData + "," + std::to_string (recv_at.GetSeconds ()) + "," +
188  std::to_string (recv_at.GetSeconds () - t_hopDelta);
190 
191  NS_LOG_INFO (m_routingLog << ", received at time: " + std::to_string (recv_at.GetSeconds ())
192  << ", hop traveling time: "
193  << std::to_string (recv_at.GetSeconds () - t_hopDelta));
194 }
195 
196 void
198 {
199  std::string abort_data = "onion_aborted," + m_simName + "," + m_simDetails + "," +
200  std::to_string (m_onionId) + "," + std::to_string (m_onionPathLength) +
201  "," + std::to_string (abort_at.GetSeconds ());
202  PrintLine (abort_data);
203 
204  NS_LOG_INFO ("Onion Was aborted at time: " << std::to_string (abort_at.GetSeconds ())
205  << " , with onion id: " << m_onionId);
206 }
207 
208 //prints the output when a new node registers to the sink
209 void
210 OutputManager::NewHandshake (int node_num, Ipv4Address node_ip, Time recv_at)
211 {
212  NS_LOG_INFO ("New node, number: " << node_num << " ip: " << Ipv4ToString (node_ip)
213  << " at time: " << std::to_string (recv_at.GetSeconds ()));
214 }
215 
216 void
217 OutputManager::AddNodeDetails (Ipv4Address node_ip, int coord_x, int coord_y)
218 {
219  std::string tmp = "node_details," + m_simName + "," + m_simDetails + "," +
220  Ipv4ToString (node_ip) + "," + std::to_string (coord_x) + "," +
221  std::to_string (coord_y) + ",null";
222  this->m_nodeDetails[node_ip.Get ()] = tmp;
223 }
224 
225 void
226 OutputManager::AddNodeDetails (Ipv4Address node_ip, int coord_x, int coord_y, int n_degree)
227 {
228  std::string tmp = "node_details," + m_simName + "," + m_simDetails + "," +
229  Ipv4ToString (node_ip) + "," + std::to_string (coord_x) + "," +
230  std::to_string (coord_y) + "," + std::to_string (n_degree);
231  this->m_nodeDetails[node_ip.Get ()] = tmp;
232 }
233 
234 void
235 OutputManager::PrintNodeDetails (std::map<uint32_t, std::string> reachable)
236 {
237  for (auto const &x : m_nodeDetails)
238  {
239  if (reachable.find (x.first) != reachable.end ())
240  {
241  PrintLine (x.second);
242  }
243  }
244 }
245 
246 void
247 OutputManager::PrintLine (std::string line)
248 {
249  std::ostream *stream = m_simStreamWrapper->GetStream ();
250  *stream << line << std::endl;
251 }
252 
253 std::string
255 {
256  std::stringstream ss;
257  ip.Print (ss);
258  return ss.str ();
259 }
260 
261 std::string
263 {
264  time_t givemetime = time (NULL);
265  std::string timme = std::string (ctime (&givemetime));
266  return timme.substr (0, timme.length () - 5);
267 }
268 
269 void
271 {
272  this->m_routing = routing;
273 }
274 
275 enum Routing
277 {
278  return m_routing;
279 }
280 
281 } // namespace ns3
ns3::OutputManager::h_onionHeader
std::string h_onionHeader
header of CSV format
Definition: outputmanager.h:215
ns3::OutputManager::OutputManager
OutputManager()
Default constructor.
Definition: outputmanager.cc:47
ns3::OutputManager::SimulationEnd
void SimulationEnd(std::string end_at)
Print at the end of the simulation.
Definition: outputmanager.cc:126
ns3::OLSR
@ OLSR
Optimized Link State Routing Protocol ns3::Olsr.
Definition: enums.h:44
ns3::OutputManager::OnionRoutingSend
void OnionRoutingSend(Ipv4Address send_ip, Ipv4Address recv_ip, int packet_size, int head_size, int body_size, Time sent_at)
Called by each node that sends an onion message.
Definition: outputmanager.cc:167
ns3::OutputManager::m_onionId
int m_onionId
identifies the onion message
Definition: outputmanager.h:231
ns3
Definition: sensornode-helper.cc:26
ns3::OutputManager::OnionRoutingRecv
void OnionRoutingRecv(Time recv_at)
Called by each node that receives an onion message.
Definition: outputmanager.cc:185
ns3::OutputManager::AddNodeDetails
void AddNodeDetails(Ipv4Address node_ip, int coord_x, int coord_y)
register node details: IP, location on the network
Definition: outputmanager.cc:217
ns3::DSR
@ DSR
Dynamic Source Routing ns3::Dsr.
Definition: enums.h:43
ns3::DISC
@ DISC
Random disc topology.
Definition: enums.h:57
ns3::OutputManager::SendOnion
void SendOnion(int packet_size, int head_size, int body_size, int onion_path_len, Time sent_at)
Called by the sink node when it sends a new onion message.
Definition: outputmanager.cc:135
ns3::OutputManager::CreateOutputFile
void CreateOutputFile()
Create stream wrapper and the output file, if the output file already exists, then delete the content...
Definition: outputmanager.cc:91
ns3::OutputManager::Ipv4ToString
std::string Ipv4ToString(Ipv4Address ip)
Convert an IpV4 address to a string.
Definition: outputmanager.cc:254
ns3::OutputManager::m_simDetails
std::string m_simDetails
holds details of the simulation
Definition: outputmanager.h:212
ns3::GRID
@ GRID
Grid topology.
Definition: enums.h:56
ns3::OutputManager::m_routingLog
std::string m_routingLog
holds data of the onion message traveling from hop to hop
Definition: outputmanager.h:230
ns3::OutputManager::PrintLine
void PrintLine(std::string line)
print the given argument on the csv file
Definition: outputmanager.cc:247
ns3::OutputManager::h_routingHeader
std::string h_routingHeader
header of CSV format
Definition: outputmanager.h:219
ns3::DSDV
@ DSDV
Destination-Sequenced Distance Vector routing ns3::Dsdv.
Definition: enums.h:45
ns3::OutputManager::CurrentTime
std::string CurrentTime(void)
return the current time as a string
Definition: outputmanager.cc:262
ns3::OutputManager::m_onionData
std::string m_onionData
holds data of the onion message currently executing in the network
Definition: outputmanager.h:228
ns3::OutputManager::m_onionPathLength
int m_onionPathLength
the onion path length
Definition: outputmanager.h:232
ns3::OutputManager::m_printDescription
bool m_printDescription
boolean choice to print the description of the simulation parameters
Definition: outputmanager.h:208
ns3::OutputManager::PrintIntro
void PrintIntro(std::string intro)
Print the simulation description containing simulation settings on the csv file or on the console log...
Definition: outputmanager.cc:106
ns3::OutputManager::AbortOnion
void AbortOnion(Time abort_at)
Called when an onion is deleted.
Definition: outputmanager.cc:197
ns3::OutputManager::m_routingData
std::string m_routingData
holds data of the onion message traveling from hop to hop
Definition: outputmanager.h:229
ns3::OutputManager::GetTypeId
static TypeId GetTypeId(void)
Register this type.
Definition: outputmanager.cc:34
ns3::OutputManager::PrintNodeDetails
void PrintNodeDetails(std::map< uint32_t, std::string > reachable)
print node details on the csv file, print only nodes reachable by the sink node
Definition: outputmanager.cc:235
ns3::Topology
Topology
Enumeration defining different network topologies that can be used in the simulation.
Definition: enums.h:55
ns3::OutputManager::NewHandshake
void NewHandshake(int node_num, Ipv4Address node_ip, Time recv_at)
Called when the sink receives a new handhake message.
Definition: outputmanager.cc:210
ns3::OutputManager::t_onionDelta
double t_onionDelta
Hold time information of the onion message traveling in the network.
Definition: outputmanager.h:234
ns3::OutputManager::RecvOnion
void RecvOnion(Time recv_at)
Called by the sink node when it receives back the onion message.
Definition: outputmanager.cc:154
ns3::OutputManager::m_simName
std::string m_simName
holds the name of the simulation
Definition: outputmanager.h:210
ns3::OutputManager::m_routing
enum Routing m_routing
information on the routing protocol
Definition: outputmanager.h:237
ns3::OutputManager::m_nodeDetails
std::map< uint32_t, std::string > m_nodeDetails
holds details of nodes in the network for printing at the end of the csv file.
Definition: outputmanager.h:240
ns3::OutputManager::SetRouting
void SetRouting(enum Routing routing)
set the enum of the current routing algorithm used in the network
Definition: outputmanager.cc:270
ns3::AODV
@ AODV
Ad Hoc On-Demand Distance Vector ns3::Aodv.
Definition: enums.h:42
ns3::OutputManager::GetRouting
enum Routing GetRouting(void)
return the enum of the current routing algorithm used in the network
Definition: outputmanager.cc:276
ns3::OutputManager::h_timeoutHeader
std::string h_timeoutHeader
header of CSV format
Definition: outputmanager.h:222
outputmanager.h
ns3::Routing
Routing
Enumeration defining different routing algorithms that can be used in the simulator.
Definition: enums.h:41
ns3::OutputManager::m_simStreamWrapper
Ptr< OutputStreamWrapper > m_simStreamWrapper
stream wrapper to write on file
Definition: outputmanager.h:206
ns3::OutputManager::~OutputManager
~OutputManager()
Default destructor.
Definition: outputmanager.cc:101
ns3::OutputManager::m_outputFilePath
std::string m_outputFilePath
path to the directory where output files are stored
Definition: outputmanager.h:242
ns3::OutputManager::t_hopDelta
double t_hopDelta
Hold time information of the onion message traveling from hop to hop.
Definition: outputmanager.h:235
ns3::OutputManager::h_nodeDetailsHeader
std::string h_nodeDetailsHeader
header of CSV format
Definition: outputmanager.h:224