Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 36 | NS_LOG_COMPONENT_DEFINE ("ndn.CsImpTracer"); |
| 37 | |
| 38 | namespace ns3 { |
| 39 | namespace ndn { |
| 40 | |
| 41 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsImpTracer> > > |
| 42 | CsImpTracer::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 | |
| 77 | CsImpTracer::CsImpTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node) |
| 78 | : CsTracer (node) |
| 79 | , m_os (os) |
| 80 | { |
| 81 | Reset (); |
| 82 | } |
| 83 | |
| 84 | CsImpTracer::CsImpTracer (boost::shared_ptr<std::ostream> os, const std::string &node) |
| 85 | : CsTracer (node) |
| 86 | , m_os (os) |
| 87 | { |
| 88 | Reset (); |
| 89 | } |
| 90 | |
| 91 | CsImpTracer::~CsImpTracer () |
| 92 | { |
| 93 | }; |
| 94 | |
| 95 | void |
| 96 | CsImpTracer::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 | |
| 103 | void |
| 104 | CsImpTracer::PeriodicPrinter () |
| 105 | { |
| 106 | Print (*m_os); |
| 107 | Reset (); |
| 108 | |
| 109 | m_printEvent = Simulator::Schedule (m_period, &CsImpTracer::PeriodicPrinter, this); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | CsImpTracer::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 | |
| 126 | void |
| 127 | CsImpTracer::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 | |
| 139 | void |
| 140 | CsImpTracer::Print (std::ostream &os) const |
| 141 | { |
| 142 | Time time = Simulator::Now (); |
| 143 | |
| 144 | PRINTER ("CacheHits", m_cacheHits); |
| 145 | PRINTER ("CacheMisses", m_cacheMisses); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | CsImpTracer::CacheHits (std::string context, |
| 150 | Ptr<const InterestHeader>, Ptr<const ContentObjectHeader>) |
| 151 | { |
| 152 | m_stats.m_cacheHits ++; |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | CsImpTracer::CacheMisses (std::string context, |
| 157 | Ptr<const InterestHeader>) |
| 158 | { |
| 159 | m_stats.m_cacheMisses ++; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | } // namespace ndn |
| 164 | } // namespace ns3 |