22 #include "ndn-cs-tracer.h"
24 #include "ns3/packet.h"
25 #include "ns3/config.h"
26 #include "ns3/names.h"
27 #include "ns3/callback.h"
29 #include "ns3/ndn-app.h"
30 #include "ns3/ndn-interest.h"
31 #include "ns3/ndn-data.h"
32 #include "ns3/ndn-content-store.h"
33 #include "ns3/simulator.h"
34 #include "ns3/node-list.h"
37 #include <boost/lexical_cast.hpp>
41 NS_LOG_COMPONENT_DEFINE (
"ndn.CsTracer");
48 static std::list< boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > > > g_tracers;
63 CsTracer::InstallAll (
const std::string &file, Time averagingPeriod)
65 using namespace boost;
68 std::list<Ptr<CsTracer> > tracers;
69 boost::shared_ptr<std::ostream> outputStream;
72 boost::shared_ptr<std::ofstream> os (
new std::ofstream ());
73 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
77 NS_LOG_ERROR (
"File " << file <<
" cannot be opened for writing. Tracing disabled");
85 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
88 for (NodeList::Iterator node = NodeList::Begin ();
89 node != NodeList::End ();
92 Ptr<CsTracer> trace = Install (*node, outputStream, averagingPeriod);
93 tracers.push_back (trace);
96 if (tracers.size () > 0)
99 tracers.front ()->PrintHeader (*outputStream);
100 *outputStream <<
"\n";
103 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
107 CsTracer::Install (
const NodeContainer &nodes,
const std::string &file, Time averagingPeriod)
109 using namespace boost;
112 std::list<Ptr<CsTracer> > tracers;
113 boost::shared_ptr<std::ostream> outputStream;
116 boost::shared_ptr<std::ofstream> os (
new std::ofstream ());
117 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
121 NS_LOG_ERROR (
"File " << file <<
" cannot be opened for writing. Tracing disabled");
129 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
132 for (NodeContainer::Iterator node = nodes.Begin ();
133 node != nodes.End ();
136 Ptr<CsTracer> trace = Install (*node, outputStream, averagingPeriod);
137 tracers.push_back (trace);
140 if (tracers.size () > 0)
143 tracers.front ()->PrintHeader (*outputStream);
144 *outputStream <<
"\n";
147 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
151 CsTracer::Install (Ptr<Node> node,
const std::string &file, Time averagingPeriod)
153 using namespace boost;
156 std::list<Ptr<CsTracer> > tracers;
157 boost::shared_ptr<std::ostream> outputStream;
160 boost::shared_ptr<std::ofstream> os (
new std::ofstream ());
161 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
165 NS_LOG_ERROR (
"File " << file <<
" cannot be opened for writing. Tracing disabled");
173 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
176 Ptr<CsTracer> trace = Install (node, outputStream, averagingPeriod);
177 tracers.push_back (trace);
179 if (tracers.size () > 0)
182 tracers.front ()->PrintHeader (*outputStream);
183 *outputStream <<
"\n";
186 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
191 CsTracer::Install (Ptr<Node> node,
192 boost::shared_ptr<std::ostream> outputStream,
193 Time averagingPeriod)
195 NS_LOG_DEBUG (
"Node: " << node->GetId ());
197 Ptr<CsTracer> trace = Create<CsTracer> (outputStream, node);
198 trace->SetAveragingPeriod (averagingPeriod);
207 CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
211 m_node = boost::lexical_cast<
string> (m_nodePtr->GetId ());
215 string name = Names::FindName (node);
237 Ptr<ContentStore> cs = m_nodePtr->GetObject<
ContentStore> ();
238 cs->TraceConnectWithoutContext (
"CacheHits", MakeCallback (&CsTracer::CacheHits,
this));
239 cs->TraceConnectWithoutContext (
"CacheMisses", MakeCallback (&CsTracer::CacheMisses,
this));
246 CsTracer::SetAveragingPeriod (
const Time &period)
249 m_printEvent.Cancel ();
250 m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter,
this);
254 CsTracer::PeriodicPrinter ()
259 m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter,
this);
270 <<
"Packets" <<
"\t";
279 #define PRINTER(printName, fieldName) \
280 os << time.ToDouble (Time::S) << "\t" \
282 << printName << "\t" \
283 << m_stats.fieldName << "\n";
289 Time time = Simulator::Now ();
291 PRINTER (
"CacheHits", m_cacheHits);
292 PRINTER (
"CacheMisses", m_cacheMisses);
296 CsTracer::CacheHits (Ptr<const Interest>, Ptr<const Data>)
298 m_stats.m_cacheHits ++;
302 CsTracer::CacheMisses (Ptr<const Interest>)
304 m_stats.m_cacheMisses ++;
Base class for NDN content store.
void Print(std::ostream &os) const
Print current trace data.
void PrintHeader(std::ostream &os) const
Print head of the trace (e.g., for post-processing)
CsTracer(boost::shared_ptr< std::ostream > os, Ptr< Node > node)
Trace constructor that attaches to the node using node pointer.