blob: 6eeeeb98ab62641caa99df9984a7decc469f3767 [file] [log] [blame]
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -08001/* -*- 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"
Alexander Afanasyev6eba36f2013-08-07 17:42:54 -070030#include "ns3/ndn-data.h"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080031#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
40NS_LOG_COMPONENT_DEFINE ("ndn.AppDelayTracer");
41
42using namespace std;
43
44namespace ns3 {
45namespace ndn {
46
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070047static std::list< boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer> > > > g_tracers;
48
Alexander Afanasyev5352af32013-07-15 09:51:28 -070049template<class T>
50static inline void
51NullDeleter (T *ptr)
52{
53}
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080054
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070055void
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070056AppDelayTracer::Destroy ()
57{
58 g_tracers.clear ();
59}
60
61void
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080062AppDelayTracer::InstallAll (const std::string &file)
63{
64 using namespace boost;
65 using namespace std;
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -080066
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080067 std::list<Ptr<AppDelayTracer> > tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070068 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);
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080073
Alexander Afanasyev5352af32013-07-15 09:51:28 -070074 if (!os->is_open ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070075 {
76 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
77 return;
78 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -070079
80 outputStream = os;
81 }
82 else
83 {
84 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
85 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080086
87 for (NodeList::Iterator node = NodeList::Begin ();
88 node != NodeList::End ();
89 node++)
90 {
Alexander Afanasyev5352af32013-07-15 09:51:28 -070091 Ptr<AppDelayTracer> trace = Install (*node, outputStream);
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080092 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
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700102 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800103}
104
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700105void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700106AppDelayTracer::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 ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700119 {
120 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
121 return;
122 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700123
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
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700146 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700147}
148
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700149void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700150AppDelayTracer::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 ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700163 {
164 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
165 return;
166 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700167
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
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700185 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700186}
187
188
189Ptr<AppDelayTracer>
190AppDelayTracer::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
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800200//////////////////////////////////////////////////////////////////////////////
201//////////////////////////////////////////////////////////////////////////////
202//////////////////////////////////////////////////////////////////////////////
203
204AppDelayTracer::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
219AppDelayTracer::AppDelayTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
220: m_node (node)
221, m_os (os)
222{
223 Connect ();
224}
225
226AppDelayTracer::~AppDelayTracer ()
227{
228};
229
230
231void
232AppDelayTracer::Connect ()
233{
234 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/LastRetransmittedInterestDataDelay",
235 MakeCallback (&AppDelayTracer::LastRetransmittedInterestDataDelay, this));
236
Alexander Afanasyevb9faa442013-01-18 19:53:43 -0800237 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/FirstInterestDataDelay",
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800238 MakeCallback (&AppDelayTracer::FirstInterestDataDelay, this));
239}
240
241void
242AppDelayTracer::PrintHeader (std::ostream &os) const
243{
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800244 os << "Time" << "\t"
245 << "Node" << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800246 << "AppId" << "\t"
247 << "SeqNo" << "\t"
248
249 << "Type" << "\t"
250 << "DelayS" << "\t"
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800251 << "DelayUS" << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800252 << "RetxCount" << "\t"
253 << "HopCount" << "";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800254}
255
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800256void
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800257AppDelayTracer::LastRetransmittedInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800258{
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800259 *m_os << Simulator::Now ().ToDouble (Time::S) << "\t"
260 << m_node << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800261 << app->GetId () << "\t"
262 << seqno << "\t"
263 << "LastDelay" << "\t"
264 << delay.ToDouble (Time::S) << "\t"
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800265 << delay.ToDouble (Time::US) << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800266 << 1 << "\t"
267 << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800268}
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800269
270void
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800271AppDelayTracer::FirstInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, uint32_t retxCount, int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800272{
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800273 *m_os << Simulator::Now ().ToDouble (Time::S) << "\t"
274 << m_node << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800275 << app->GetId () << "\t"
276 << seqno << "\t"
277 << "FullDelay" << "\t"
278 << delay.ToDouble (Time::S) << "\t"
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800279 << delay.ToDouble (Time::US) << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800280 << retxCount << "\t"
281 << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800282}
283
284
285} // namespace ndn
286} // namespace ns3