NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndn-app-delay-tracer.cc
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-data.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 
47 static std::list< boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer> > > > g_tracers;
48 
49 template<class T>
50 static inline void
51 NullDeleter (T *ptr)
52 {
53 }
54 
55 void
56 AppDelayTracer::Destroy ()
57 {
58  g_tracers.clear ();
59 }
60 
61 void
62 AppDelayTracer::InstallAll (const std::string &file)
63 {
64  using namespace boost;
65  using namespace std;
66 
67  std::list<Ptr<AppDelayTracer> > tracers;
68  boost::shared_ptr<std::ostream> outputStream;
69  if (file != "-")
70  {
71  boost::shared_ptr<std::ofstream> os (new std::ofstream ());
72  os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
73 
74  if (!os->is_open ())
75  {
76  NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
77  return;
78  }
79 
80  outputStream = os;
81  }
82  else
83  {
84  outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
85  }
86 
87  for (NodeList::Iterator node = NodeList::Begin ();
88  node != NodeList::End ();
89  node++)
90  {
91  Ptr<AppDelayTracer> trace = Install (*node, outputStream);
92  tracers.push_back (trace);
93  }
94 
95  if (tracers.size () > 0)
96  {
97  // *m_l3RateTrace << "# "; // not necessary for R's read.table
98  tracers.front ()->PrintHeader (*outputStream);
99  *outputStream << "\n";
100  }
101 
102  g_tracers.push_back (boost::make_tuple (outputStream, tracers));
103 }
104 
105 void
106 AppDelayTracer::Install (const NodeContainer &nodes, const std::string &file)
107 {
108  using namespace boost;
109  using namespace std;
110 
111  std::list<Ptr<AppDelayTracer> > tracers;
112  boost::shared_ptr<std::ostream> outputStream;
113  if (file != "-")
114  {
115  boost::shared_ptr<std::ofstream> os (new std::ofstream ());
116  os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
117 
118  if (!os->is_open ())
119  {
120  NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
121  return;
122  }
123 
124  outputStream = os;
125  }
126  else
127  {
128  outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
129  }
130 
131  for (NodeContainer::Iterator node = nodes.Begin ();
132  node != nodes.End ();
133  node++)
134  {
135  Ptr<AppDelayTracer> trace = Install (*node, outputStream);
136  tracers.push_back (trace);
137  }
138 
139  if (tracers.size () > 0)
140  {
141  // *m_l3RateTrace << "# "; // not necessary for R's read.table
142  tracers.front ()->PrintHeader (*outputStream);
143  *outputStream << "\n";
144  }
145 
146  g_tracers.push_back (boost::make_tuple (outputStream, tracers));
147 }
148 
149 void
150 AppDelayTracer::Install (Ptr<Node> node, const std::string &file)
151 {
152  using namespace boost;
153  using namespace std;
154 
155  std::list<Ptr<AppDelayTracer> > tracers;
156  boost::shared_ptr<std::ostream> outputStream;
157  if (file != "-")
158  {
159  boost::shared_ptr<std::ofstream> os (new std::ofstream ());
160  os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
161 
162  if (!os->is_open ())
163  {
164  NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
165  return;
166  }
167 
168  outputStream = os;
169  }
170  else
171  {
172  outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
173  }
174 
175  Ptr<AppDelayTracer> trace = Install (node, outputStream);
176  tracers.push_back (trace);
177 
178  if (tracers.size () > 0)
179  {
180  // *m_l3RateTrace << "# "; // not necessary for R's read.table
181  tracers.front ()->PrintHeader (*outputStream);
182  *outputStream << "\n";
183  }
184 
185  g_tracers.push_back (boost::make_tuple (outputStream, tracers));
186 }
187 
188 
189 Ptr<AppDelayTracer>
190 AppDelayTracer::Install (Ptr<Node> node,
191  boost::shared_ptr<std::ostream> outputStream)
192 {
193  NS_LOG_DEBUG ("Node: " << node->GetId ());
194 
195  Ptr<AppDelayTracer> trace = Create<AppDelayTracer> (outputStream, node);
196 
197  return trace;
198 }
199 
203 
204 AppDelayTracer::AppDelayTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
205 : m_nodePtr (node)
206 , m_os (os)
207 {
208  m_node = boost::lexical_cast<string> (m_nodePtr->GetId ());
209 
210  Connect ();
211 
212  string name = Names::FindName (node);
213  if (!name.empty ())
214  {
215  m_node = name;
216  }
217 }
218 
219 AppDelayTracer::AppDelayTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
220 : m_node (node)
221 , m_os (os)
222 {
223  Connect ();
224 }
225 
227 {
228 };
229 
230 
231 void
232 AppDelayTracer::Connect ()
233 {
234  Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/LastRetransmittedInterestDataDelay",
235  MakeCallback (&AppDelayTracer::LastRetransmittedInterestDataDelay, this));
236 
237  Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/FirstInterestDataDelay",
238  MakeCallback (&AppDelayTracer::FirstInterestDataDelay, this));
239 }
240 
241 void
242 AppDelayTracer::PrintHeader (std::ostream &os) const
243 {
244  os << "Time" << "\t"
245  << "Node" << "\t"
246  << "AppId" << "\t"
247  << "SeqNo" << "\t"
248 
249  << "Type" << "\t"
250  << "DelayS" << "\t"
251  << "DelayUS" << "\t"
252  << "RetxCount" << "\t"
253  << "HopCount" << "";
254 }
255 
256 void
257 AppDelayTracer::LastRetransmittedInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, int32_t hopCount)
258 {
259  *m_os << Simulator::Now ().ToDouble (Time::S) << "\t"
260  << m_node << "\t"
261  << app->GetId () << "\t"
262  << seqno << "\t"
263  << "LastDelay" << "\t"
264  << delay.ToDouble (Time::S) << "\t"
265  << delay.ToDouble (Time::US) << "\t"
266  << 1 << "\t"
267  << hopCount << "\n";
268 }
269 
270 void
271 AppDelayTracer::FirstInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, uint32_t retxCount, int32_t hopCount)
272 {
273  *m_os << Simulator::Now ().ToDouble (Time::S) << "\t"
274  << m_node << "\t"
275  << app->GetId () << "\t"
276  << seqno << "\t"
277  << "FullDelay" << "\t"
278  << delay.ToDouble (Time::S) << "\t"
279  << delay.ToDouble (Time::US) << "\t"
280  << retxCount << "\t"
281  << hopCount << "\n";
282 }
283 
284 
285 } // namespace ndn
286 } // namespace ns3
void PrintHeader(std::ostream &os) const
Print head of the trace (e.g., for post-processing)
AppDelayTracer(boost::shared_ptr< std::ostream > os, Ptr< Node > node)
Trace constructor that attaches to all applications on the node using node's pointer.