blob: 3016bf4f2f7f3ca78e236d737980070872b77e3d [file] [log] [blame]
Alexander Afanasyev86287992012-12-10 16:11:50 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 UCLA
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "ndn-cs-imp-tracer.h"
22
23#include "ns3/node.h"
24#include "ns3/packet.h"
25#include "ns3/config.h"
26#include "ns3/callback.h"
27#include "ns3/ndn-app.h"
28#include "ns3/ndn-interest.h"
29#include "ns3/ndn-content-object.h"
30#include "ns3/simulator.h"
31#include "ns3/node-list.h"
32#include "ns3/log.h"
33
34#include <fstream>
35
36NS_LOG_COMPONENT_DEFINE ("ndn.CsImpTracer");
37
38namespace ns3 {
39namespace ndn {
40
41boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsImpTracer> > >
42CsImpTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
43{
44 using namespace boost;
45 using namespace std;
46
47 std::list<Ptr<CsImpTracer> > tracers;
48 boost::shared_ptr<std::ofstream> outputStream (new std::ofstream ());
49 outputStream->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
50
51 if (!outputStream->is_open ())
52 return boost::make_tuple (outputStream, tracers);
53
54 for (NodeList::Iterator node = NodeList::Begin ();
55 node != NodeList::End ();
56 node++)
57 {
58 NS_LOG_DEBUG ("Node: " << (*node)->GetId ());
59
60 Ptr<CsImpTracer> trace = Create<CsImpTracer> (outputStream, *node);
61 trace->SetAveragingPeriod (averagingPeriod);
62 tracers.push_back (trace);
63 }
64
65 if (tracers.size () > 0)
66 {
67 // *m_l3RateTrace << "# "; // not necessary for R's read.table
68 tracers.front ()->PrintHeader (*outputStream);
69 *outputStream << "\n";
70 }
71
72 return boost::make_tuple (outputStream, tracers);
73}
74
75
76
77CsImpTracer::CsImpTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
78 : CsTracer (node)
79 , m_os (os)
80{
81 Reset ();
82}
83
84CsImpTracer::CsImpTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
85 : CsTracer (node)
86 , m_os (os)
87{
88 Reset ();
89}
90
91CsImpTracer::~CsImpTracer ()
92{
93};
94
95void
96CsImpTracer::SetAveragingPeriod (const Time &period)
97{
98 m_period = period;
99 m_printEvent.Cancel ();
100 m_printEvent = Simulator::Schedule (m_period, &CsImpTracer::PeriodicPrinter, this);
101}
102
103void
104CsImpTracer::PeriodicPrinter ()
105{
106 Print (*m_os);
107 Reset ();
108
109 m_printEvent = Simulator::Schedule (m_period, &CsImpTracer::PeriodicPrinter, this);
110}
111
112void
113CsImpTracer::PrintHeader (std::ostream &os) const
114{
115 os << "Time" << "\t"
116
117 << "Node" << "\t"
118// << "FaceId" << "\t"
119 // << "FaceDescr" << "\t"
120
121 << "Type" << "\t"
122 << "Packets" << "\t";
123// << "Kilobytes";
124}
125
126void
127CsImpTracer::Reset ()
128{
129 m_stats.Reset();
130}
131
132#define PRINTER(printName, fieldName) \
133 os << time.ToDouble (Time::S) << "\t" \
134 << m_node << "\t" \
135 << printName << "\t" \
136 << m_stats.fieldName << "\n";
137
138
139void
140CsImpTracer::Print (std::ostream &os) const
141{
142 Time time = Simulator::Now ();
143
144 PRINTER ("CacheHits", m_cacheHits);
145 PRINTER ("CacheMisses", m_cacheMisses);
146}
147
148void
149CsImpTracer::CacheHits (std::string context,
150 Ptr<const InterestHeader>, Ptr<const ContentObjectHeader>)
151{
152 m_stats.m_cacheHits ++;
153}
154
155void
156CsImpTracer::CacheMisses (std::string context,
157 Ptr<const InterestHeader>)
158{
159 m_stats.m_cacheMisses ++;
160}
161
162
163} // namespace ndn
164} // namespace ns3