NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
ndn-cs-tracer.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #include "ndn-cs-tracer.hpp"
21 #include "ns3/node.h"
22 #include "ns3/packet.h"
23 #include "ns3/config.h"
24 #include "ns3/names.h"
25 #include "ns3/callback.h"
26 
27 #include "apps/ndn-app.hpp"
28 #include "ns3/simulator.h"
29 #include "ns3/node-list.h"
30 #include "ns3/log.h"
31 
32 #include <boost/lexical_cast.hpp>
33 
34 #include <fstream>
35 
36 NS_LOG_COMPONENT_DEFINE("ndn.CsTracer");
37 
38 namespace ns3 {
39 namespace ndn {
40 
41 static std::list<std::tuple<shared_ptr<std::ostream>, std::list<Ptr<CsTracer>>>> g_tracers;
42 
43 void
45 {
46  g_tracers.clear();
47 }
48 
49 void
50 CsTracer::InstallAll(const std::string& file, Time averagingPeriod /* = Seconds (0.5)*/)
51 {
52  using namespace boost;
53  using namespace std;
54 
55  std::list<Ptr<CsTracer>> tracers;
56  shared_ptr<std::ostream> outputStream;
57  if (file != "-") {
58  shared_ptr<std::ofstream> os(new std::ofstream());
59  os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
60 
61  if (!os->is_open()) {
62  NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
63  return;
64  }
65 
66  outputStream = os;
67  }
68  else {
69  outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
70  }
71 
72  for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
73  Ptr<CsTracer> trace = Install(*node, outputStream, averagingPeriod);
74  tracers.push_back(trace);
75  }
76 
77  if (tracers.size() > 0) {
78  // *m_l3RateTrace << "# "; // not necessary for R's read.table
79  tracers.front()->PrintHeader(*outputStream);
80  *outputStream << "\n";
81  }
82 
83  g_tracers.push_back(std::make_tuple(outputStream, tracers));
84 }
85 
86 void
87 CsTracer::Install(const NodeContainer& nodes, const std::string& file,
88  Time averagingPeriod /* = Seconds (0.5)*/)
89 {
90  using namespace boost;
91  using namespace std;
92 
93  std::list<Ptr<CsTracer>> tracers;
94  shared_ptr<std::ostream> outputStream;
95  if (file != "-") {
96  shared_ptr<std::ofstream> os(new std::ofstream());
97  os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
98 
99  if (!os->is_open()) {
100  NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
101  return;
102  }
103 
104  outputStream = os;
105  }
106  else {
107  outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
108  }
109 
110  for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) {
111  Ptr<CsTracer> trace = Install(*node, outputStream, averagingPeriod);
112  tracers.push_back(trace);
113  }
114 
115  if (tracers.size() > 0) {
116  // *m_l3RateTrace << "# "; // not necessary for R's read.table
117  tracers.front()->PrintHeader(*outputStream);
118  *outputStream << "\n";
119  }
120 
121  g_tracers.push_back(std::make_tuple(outputStream, tracers));
122 }
123 
124 void
125 CsTracer::Install(Ptr<Node> node, const std::string& file,
126  Time averagingPeriod /* = Seconds (0.5)*/)
127 {
128  using namespace boost;
129  using namespace std;
130 
131  std::list<Ptr<CsTracer>> tracers;
132  shared_ptr<std::ostream> outputStream;
133  if (file != "-") {
134  shared_ptr<std::ofstream> os(new std::ofstream());
135  os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
136 
137  if (!os->is_open()) {
138  NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
139  return;
140  }
141 
142  outputStream = os;
143  }
144  else {
145  outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
146  }
147 
148  Ptr<CsTracer> trace = Install(node, outputStream, averagingPeriod);
149  tracers.push_back(trace);
150 
151  if (tracers.size() > 0) {
152  // *m_l3RateTrace << "# "; // not necessary for R's read.table
153  tracers.front()->PrintHeader(*outputStream);
154  *outputStream << "\n";
155  }
156 
157  g_tracers.push_back(std::make_tuple(outputStream, tracers));
158 }
159 
160 Ptr<CsTracer>
161 CsTracer::Install(Ptr<Node> node, shared_ptr<std::ostream> outputStream,
162  Time averagingPeriod /* = Seconds (0.5)*/)
163 {
164  NS_LOG_DEBUG("Node: " << node->GetId());
165 
166  Ptr<CsTracer> trace = Create<CsTracer>(outputStream, node);
167  trace->SetAveragingPeriod(averagingPeriod);
168 
169  return trace;
170 }
171 
175 
176 CsTracer::CsTracer(shared_ptr<std::ostream> os, Ptr<Node> node)
177  : m_nodePtr(node)
178  , m_os(os)
179 {
180  m_node = boost::lexical_cast<std::string>(m_nodePtr->GetId());
181 
182  Connect();
183 
184  std::string name = Names::FindName(node);
185  if (!name.empty()) {
186  m_node = name;
187  }
188 }
189 
190 CsTracer::CsTracer(shared_ptr<std::ostream> os, const std::string& node)
191  : m_node(node)
192  , m_os(os)
193 {
194  Connect();
195 }
196 
198 
199 void
200 CsTracer::Connect()
201 {
202  // // @TODO Do the same with NFD content store...
203  // Ptr<ContentStore> cs = m_nodePtr->GetObject<ContentStore>();
204  // cs->TraceConnectWithoutContext("CacheHits", MakeCallback(&CsTracer::CacheHits, this));
205  // cs->TraceConnectWithoutContext("CacheMisses", MakeCallback(&CsTracer::CacheMisses, this));
206 
207  Reset();
208 }
209 
210 void
211 CsTracer::SetAveragingPeriod(const Time& period)
212 {
213  m_period = period;
214  m_printEvent.Cancel();
215  m_printEvent = Simulator::Schedule(m_period, &CsTracer::PeriodicPrinter, this);
216 }
217 
218 void
219 CsTracer::PeriodicPrinter()
220 {
221  Print(*m_os);
222  Reset();
223 
224  m_printEvent = Simulator::Schedule(m_period, &CsTracer::PeriodicPrinter, this);
225 }
226 
227 void
228 CsTracer::PrintHeader(std::ostream& os) const
229 {
230  os << "Time"
231  << "\t"
232 
233  << "Node"
234  << "\t"
235 
236  << "Type"
237  << "\t"
238  << "Packets"
239  << "\t";
240 }
241 
242 void
243 CsTracer::Reset()
244 {
245  m_stats.Reset();
246 }
247 
248 #define PRINTER(printName, fieldName) \
249  os << time.ToDouble(Time::S) << "\t" << m_node << "\t" << printName << "\t" << m_stats.fieldName \
250  << "\n";
251 
252 void
253 CsTracer::Print(std::ostream& os) const
254 {
255  Time time = Simulator::Now();
256 
257  PRINTER("CacheHits", m_cacheHits);
258  PRINTER("CacheMisses", m_cacheMisses);
259 }
260 
261 void
262 CsTracer::CacheHits(shared_ptr<const Interest>, shared_ptr<const Data>)
263 {
264  m_stats.m_cacheHits++;
265 }
266 
267 void
268 CsTracer::CacheMisses(shared_ptr<const Interest>)
269 {
270  m_stats.m_cacheMisses++;
271 }
272 
273 } // namespace ndn
274 } // namespace ns3
ndn-app.hpp
ns3
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-app-link-service.cpp:32
ns3::ndn::CsTracer::PrintHeader
void PrintHeader(std::ostream &os) const
Print head of the trace (e.g., for post-processing)
Definition: ndn-cs-tracer.cpp:228
ndn-cs-tracer.hpp
boost
Definition: block.hpp:31
ns3::ndn::CsTracer::CsTracer
CsTracer(shared_ptr< std::ostream > os, Ptr< Node > node)
Trace constructor that attaches to the node using node pointer.
Definition: ndn-cs-tracer.cpp:176
ns3::ndn::g_tracers
static std::list< std::tuple< shared_ptr< std::ostream >, std::list< Ptr< AppDelayTracer > > > > g_tracers
Definition: ndn-app-delay-tracer.cpp:43
ns3::ndn::CsTracer::~CsTracer
~CsTracer()
Destructor.
Definition: ndn-cs-tracer.cpp:197
ndn::time
Definition: time-custom-clock.hpp:28
PRINTER
#define PRINTER(printName, fieldName)
Definition: ndn-cs-tracer.cpp:248
ns3::ndn::CsTracer::InstallAll
static void InstallAll(const std::string &file, Time averagingPeriod=Seconds(0.5))
Helper method to install tracers on all simulation nodes.
Definition: ndn-cs-tracer.cpp:50
ns3::ndn::CsTracer::Print
void Print(std::ostream &os) const
Print current trace data.
Definition: ndn-cs-tracer.cpp:253
ndn::name
Definition: name-component-types.hpp:33
ns3::ndn::CsTracer::Install
static void Install(const NodeContainer &nodes, const std::string &file, Time averagingPeriod=Seconds(0.5))
Helper method to install tracers on the selected simulation nodes.
Definition: ndn-cs-tracer.cpp:87
ns3::ndn::CsTracer::Destroy
static void Destroy()
Explicit request to remove all statically created tracers.
Definition: ndn-cs-tracer.cpp:44
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34