blob: c0f881db37d8c0fc0d846dbe40ebbf229ca01e12 [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 Afanasyevdb64ff12013-01-18 16:37:31 -080056AppDelayTracer::InstallAll (const std::string &file)
57{
58 using namespace boost;
59 using namespace std;
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -080060
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080061 std::list<Ptr<AppDelayTracer> > tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070062 boost::shared_ptr<std::ostream> outputStream;
63 if (file != "-")
64 {
65 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
66 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080067
Alexander Afanasyev5352af32013-07-15 09:51:28 -070068 if (!os->is_open ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070069 {
70 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
71 return;
72 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -070073
74 outputStream = os;
75 }
76 else
77 {
78 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
79 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080080
81 for (NodeList::Iterator node = NodeList::Begin ();
82 node != NodeList::End ();
83 node++)
84 {
Alexander Afanasyev5352af32013-07-15 09:51:28 -070085 Ptr<AppDelayTracer> trace = Install (*node, outputStream);
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080086 tracers.push_back (trace);
87 }
88
89 if (tracers.size () > 0)
90 {
91 // *m_l3RateTrace << "# "; // not necessary for R's read.table
92 tracers.front ()->PrintHeader (*outputStream);
93 *outputStream << "\n";
94 }
95
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070096 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080097}
98
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070099void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700100AppDelayTracer::Install (const NodeContainer &nodes, const std::string &file)
101{
102 using namespace boost;
103 using namespace std;
104
105 std::list<Ptr<AppDelayTracer> > tracers;
106 boost::shared_ptr<std::ostream> outputStream;
107 if (file != "-")
108 {
109 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
110 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
111
112 if (!os->is_open ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700113 {
114 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
115 return;
116 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700117
118 outputStream = os;
119 }
120 else
121 {
122 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
123 }
124
125 for (NodeContainer::Iterator node = nodes.Begin ();
126 node != nodes.End ();
127 node++)
128 {
129 Ptr<AppDelayTracer> trace = Install (*node, outputStream);
130 tracers.push_back (trace);
131 }
132
133 if (tracers.size () > 0)
134 {
135 // *m_l3RateTrace << "# "; // not necessary for R's read.table
136 tracers.front ()->PrintHeader (*outputStream);
137 *outputStream << "\n";
138 }
139
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700140 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700141}
142
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700143void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700144AppDelayTracer::Install (Ptr<Node> node, const std::string &file)
145{
146 using namespace boost;
147 using namespace std;
148
149 std::list<Ptr<AppDelayTracer> > tracers;
150 boost::shared_ptr<std::ostream> outputStream;
151 if (file != "-")
152 {
153 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
154 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
155
156 if (!os->is_open ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700157 {
158 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
159 return;
160 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700161
162 outputStream = os;
163 }
164 else
165 {
166 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
167 }
168
169 Ptr<AppDelayTracer> trace = Install (node, outputStream);
170 tracers.push_back (trace);
171
172 if (tracers.size () > 0)
173 {
174 // *m_l3RateTrace << "# "; // not necessary for R's read.table
175 tracers.front ()->PrintHeader (*outputStream);
176 *outputStream << "\n";
177 }
178
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700179 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700180}
181
182
183Ptr<AppDelayTracer>
184AppDelayTracer::Install (Ptr<Node> node,
185 boost::shared_ptr<std::ostream> outputStream)
186{
187 NS_LOG_DEBUG ("Node: " << node->GetId ());
188
189 Ptr<AppDelayTracer> trace = Create<AppDelayTracer> (outputStream, node);
190
191 return trace;
192}
193
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800194//////////////////////////////////////////////////////////////////////////////
195//////////////////////////////////////////////////////////////////////////////
196//////////////////////////////////////////////////////////////////////////////
197
198AppDelayTracer::AppDelayTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
199: m_nodePtr (node)
200, m_os (os)
201{
202 m_node = boost::lexical_cast<string> (m_nodePtr->GetId ());
203
204 Connect ();
205
206 string name = Names::FindName (node);
207 if (!name.empty ())
208 {
209 m_node = name;
210 }
211}
212
213AppDelayTracer::AppDelayTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
214: m_node (node)
215, m_os (os)
216{
217 Connect ();
218}
219
220AppDelayTracer::~AppDelayTracer ()
221{
222};
223
224
225void
226AppDelayTracer::Connect ()
227{
228 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/LastRetransmittedInterestDataDelay",
229 MakeCallback (&AppDelayTracer::LastRetransmittedInterestDataDelay, this));
230
Alexander Afanasyevb9faa442013-01-18 19:53:43 -0800231 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/FirstInterestDataDelay",
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800232 MakeCallback (&AppDelayTracer::FirstInterestDataDelay, this));
233}
234
235void
236AppDelayTracer::PrintHeader (std::ostream &os) const
237{
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800238 os << "Time" << "\t"
239 << "Node" << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800240 << "AppId" << "\t"
241 << "SeqNo" << "\t"
242
243 << "Type" << "\t"
244 << "DelayS" << "\t"
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800245 << "DelayUS" << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800246 << "RetxCount" << "\t"
247 << "HopCount" << "";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800248}
249
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800250void
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800251AppDelayTracer::LastRetransmittedInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800252{
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800253 *m_os << Simulator::Now ().ToDouble (Time::S) << "\t"
254 << m_node << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800255 << app->GetId () << "\t"
256 << seqno << "\t"
257 << "LastDelay" << "\t"
258 << delay.ToDouble (Time::S) << "\t"
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800259 << delay.ToDouble (Time::US) << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800260 << 1 << "\t"
261 << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800262}
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800263
264void
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800265AppDelayTracer::FirstInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, uint32_t retxCount, int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800266{
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800267 *m_os << Simulator::Now ().ToDouble (Time::S) << "\t"
268 << m_node << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800269 << app->GetId () << "\t"
270 << seqno << "\t"
271 << "FullDelay" << "\t"
272 << delay.ToDouble (Time::S) << "\t"
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800273 << delay.ToDouble (Time::US) << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800274 << retxCount << "\t"
275 << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800276}
277
278
279} // namespace ndn
280} // namespace ns3