Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 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-app-delay-tracer.h" |
| 22 | #include "ns3/node.h" |
| 23 | #include "ns3/packet.h" |
| 24 | #include "ns3/config.h" |
| 25 | #include "ns3/names.h" |
| 26 | #include "ns3/callback.h" |
| 27 | |
| 28 | #include "ns3/ndn-app.h" |
| 29 | #include "ns3/ndn-interest.h" |
| 30 | #include "ns3/ndn-content-object.h" |
| 31 | #include "ns3/simulator.h" |
| 32 | #include "ns3/node-list.h" |
| 33 | #include "ns3/log.h" |
| 34 | |
| 35 | #include <boost/lexical_cast.hpp> |
| 36 | #include <boost/make_shared.hpp> |
| 37 | |
| 38 | #include <fstream> |
| 39 | |
| 40 | NS_LOG_COMPONENT_DEFINE ("ndn.AppDelayTracer"); |
| 41 | |
| 42 | using namespace std; |
| 43 | |
| 44 | namespace ns3 { |
| 45 | namespace ndn { |
| 46 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 47 | template<class T> |
| 48 | static inline void |
| 49 | NullDeleter (T *ptr) |
| 50 | { |
| 51 | } |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 52 | |
| 53 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer> > > |
| 54 | AppDelayTracer::InstallAll (const std::string &file) |
| 55 | { |
| 56 | using namespace boost; |
| 57 | using namespace std; |
Alexander Afanasyev | fc8425c | 2013-01-31 13:33:49 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 59 | std::list<Ptr<AppDelayTracer> > tracers; |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 60 | boost::shared_ptr<std::ostream> outputStream; |
| 61 | if (file != "-") |
| 62 | { |
| 63 | boost::shared_ptr<std::ofstream> os (new std::ofstream ()); |
| 64 | os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc); |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 66 | if (!os->is_open ()) |
| 67 | return boost::make_tuple (outputStream, tracers); |
| 68 | |
| 69 | outputStream = os; |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>); |
| 74 | } |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 75 | |
| 76 | for (NodeList::Iterator node = NodeList::Begin (); |
| 77 | node != NodeList::End (); |
| 78 | node++) |
| 79 | { |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 80 | Ptr<AppDelayTracer> trace = Install (*node, outputStream); |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 81 | tracers.push_back (trace); |
| 82 | } |
| 83 | |
| 84 | if (tracers.size () > 0) |
| 85 | { |
| 86 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 87 | tracers.front ()->PrintHeader (*outputStream); |
| 88 | *outputStream << "\n"; |
| 89 | } |
| 90 | |
| 91 | return boost::make_tuple (outputStream, tracers); |
| 92 | } |
| 93 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 94 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer> > > |
| 95 | AppDelayTracer::Install (const NodeContainer &nodes, const std::string &file) |
| 96 | { |
| 97 | using namespace boost; |
| 98 | using namespace std; |
| 99 | |
| 100 | std::list<Ptr<AppDelayTracer> > tracers; |
| 101 | boost::shared_ptr<std::ostream> outputStream; |
| 102 | if (file != "-") |
| 103 | { |
| 104 | boost::shared_ptr<std::ofstream> os (new std::ofstream ()); |
| 105 | os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc); |
| 106 | |
| 107 | if (!os->is_open ()) |
| 108 | return boost::make_tuple (outputStream, tracers); |
| 109 | |
| 110 | outputStream = os; |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>); |
| 115 | } |
| 116 | |
| 117 | for (NodeContainer::Iterator node = nodes.Begin (); |
| 118 | node != nodes.End (); |
| 119 | node++) |
| 120 | { |
| 121 | Ptr<AppDelayTracer> trace = Install (*node, outputStream); |
| 122 | tracers.push_back (trace); |
| 123 | } |
| 124 | |
| 125 | if (tracers.size () > 0) |
| 126 | { |
| 127 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 128 | tracers.front ()->PrintHeader (*outputStream); |
| 129 | *outputStream << "\n"; |
| 130 | } |
| 131 | |
| 132 | return boost::make_tuple (outputStream, tracers); |
| 133 | } |
| 134 | |
| 135 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer> > > |
| 136 | AppDelayTracer::Install (Ptr<Node> node, const std::string &file) |
| 137 | { |
| 138 | using namespace boost; |
| 139 | using namespace std; |
| 140 | |
| 141 | std::list<Ptr<AppDelayTracer> > tracers; |
| 142 | boost::shared_ptr<std::ostream> outputStream; |
| 143 | if (file != "-") |
| 144 | { |
| 145 | boost::shared_ptr<std::ofstream> os (new std::ofstream ()); |
| 146 | os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc); |
| 147 | |
| 148 | if (!os->is_open ()) |
| 149 | return boost::make_tuple (outputStream, tracers); |
| 150 | |
| 151 | outputStream = os; |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>); |
| 156 | } |
| 157 | |
| 158 | Ptr<AppDelayTracer> trace = Install (node, outputStream); |
| 159 | tracers.push_back (trace); |
| 160 | |
| 161 | if (tracers.size () > 0) |
| 162 | { |
| 163 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 164 | tracers.front ()->PrintHeader (*outputStream); |
| 165 | *outputStream << "\n"; |
| 166 | } |
| 167 | |
| 168 | return boost::make_tuple (outputStream, tracers); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | Ptr<AppDelayTracer> |
| 173 | AppDelayTracer::Install (Ptr<Node> node, |
| 174 | boost::shared_ptr<std::ostream> outputStream) |
| 175 | { |
| 176 | NS_LOG_DEBUG ("Node: " << node->GetId ()); |
| 177 | |
| 178 | Ptr<AppDelayTracer> trace = Create<AppDelayTracer> (outputStream, node); |
| 179 | |
| 180 | return trace; |
| 181 | } |
| 182 | |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 183 | ////////////////////////////////////////////////////////////////////////////// |
| 184 | ////////////////////////////////////////////////////////////////////////////// |
| 185 | ////////////////////////////////////////////////////////////////////////////// |
| 186 | |
| 187 | AppDelayTracer::AppDelayTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node) |
| 188 | : m_nodePtr (node) |
| 189 | , m_os (os) |
| 190 | { |
| 191 | m_node = boost::lexical_cast<string> (m_nodePtr->GetId ()); |
| 192 | |
| 193 | Connect (); |
| 194 | |
| 195 | string name = Names::FindName (node); |
| 196 | if (!name.empty ()) |
| 197 | { |
| 198 | m_node = name; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | AppDelayTracer::AppDelayTracer (boost::shared_ptr<std::ostream> os, const std::string &node) |
| 203 | : m_node (node) |
| 204 | , m_os (os) |
| 205 | { |
| 206 | Connect (); |
| 207 | } |
| 208 | |
| 209 | AppDelayTracer::~AppDelayTracer () |
| 210 | { |
| 211 | }; |
| 212 | |
| 213 | |
| 214 | void |
| 215 | AppDelayTracer::Connect () |
| 216 | { |
| 217 | Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/LastRetransmittedInterestDataDelay", |
| 218 | MakeCallback (&AppDelayTracer::LastRetransmittedInterestDataDelay, this)); |
| 219 | |
Alexander Afanasyev | b9faa44 | 2013-01-18 19:53:43 -0800 | [diff] [blame] | 220 | Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/FirstInterestDataDelay", |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 221 | MakeCallback (&AppDelayTracer::FirstInterestDataDelay, this)); |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | AppDelayTracer::PrintHeader (std::ostream &os) const |
| 226 | { |
Alexander Afanasyev | fc8425c | 2013-01-31 13:33:49 -0800 | [diff] [blame] | 227 | os << "Time" << "\t" |
| 228 | << "Node" << "\t" |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 229 | << "AppId" << "\t" |
| 230 | << "SeqNo" << "\t" |
| 231 | |
| 232 | << "Type" << "\t" |
| 233 | << "DelayS" << "\t" |
Alexander Afanasyev | 400aae1 | 2013-01-19 13:27:52 -0800 | [diff] [blame] | 234 | << "DelayUS" << "\t" |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 235 | << "RetxCount" << "\t" |
| 236 | << "HopCount" << ""; |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Alexander Afanasyev | fc8425c | 2013-01-31 13:33:49 -0800 | [diff] [blame] | 239 | void |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 240 | AppDelayTracer::LastRetransmittedInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, int32_t hopCount) |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 241 | { |
Alexander Afanasyev | fc8425c | 2013-01-31 13:33:49 -0800 | [diff] [blame] | 242 | *m_os << Simulator::Now ().ToDouble (Time::S) << "\t" |
| 243 | << m_node << "\t" |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 244 | << app->GetId () << "\t" |
| 245 | << seqno << "\t" |
| 246 | << "LastDelay" << "\t" |
| 247 | << delay.ToDouble (Time::S) << "\t" |
Alexander Afanasyev | 400aae1 | 2013-01-19 13:27:52 -0800 | [diff] [blame] | 248 | << delay.ToDouble (Time::US) << "\t" |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 249 | << 1 << "\t" |
| 250 | << hopCount << "\n"; |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 251 | } |
Alexander Afanasyev | fc8425c | 2013-01-31 13:33:49 -0800 | [diff] [blame] | 252 | |
| 253 | void |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 254 | AppDelayTracer::FirstInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, uint32_t retxCount, int32_t hopCount) |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 255 | { |
Alexander Afanasyev | fc8425c | 2013-01-31 13:33:49 -0800 | [diff] [blame] | 256 | *m_os << Simulator::Now ().ToDouble (Time::S) << "\t" |
| 257 | << m_node << "\t" |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 258 | << app->GetId () << "\t" |
| 259 | << seqno << "\t" |
| 260 | << "FullDelay" << "\t" |
| 261 | << delay.ToDouble (Time::S) << "\t" |
Alexander Afanasyev | 400aae1 | 2013-01-19 13:27:52 -0800 | [diff] [blame] | 262 | << delay.ToDouble (Time::US) << "\t" |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 263 | << retxCount << "\t" |
| 264 | << hopCount << "\n"; |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
| 268 | } // namespace ndn |
| 269 | } // namespace ns3 |