blob: 230be848f0188e4e7f2074c3a02be45e3b0005c2 [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 Afanasyev5352af32013-07-15 09:51:28 -070047template<class T>
48static inline void
49NullDeleter (T *ptr)
50{
51}
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080052
53boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer> > >
54AppDelayTracer::InstallAll (const std::string &file)
55{
56 using namespace boost;
57 using namespace std;
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -080058
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080059 std::list<Ptr<AppDelayTracer> > tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070060 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 Afanasyevdb64ff12013-01-18 16:37:31 -080065
Alexander Afanasyev5352af32013-07-15 09:51:28 -070066 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 Afanasyevdb64ff12013-01-18 16:37:31 -080075
76 for (NodeList::Iterator node = NodeList::Begin ();
77 node != NodeList::End ();
78 node++)
79 {
Alexander Afanasyev5352af32013-07-15 09:51:28 -070080 Ptr<AppDelayTracer> trace = Install (*node, outputStream);
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080081 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 Afanasyev5352af32013-07-15 09:51:28 -070094boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer> > >
95AppDelayTracer::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
135boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer> > >
136AppDelayTracer::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
172Ptr<AppDelayTracer>
173AppDelayTracer::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 Afanasyevdb64ff12013-01-18 16:37:31 -0800183//////////////////////////////////////////////////////////////////////////////
184//////////////////////////////////////////////////////////////////////////////
185//////////////////////////////////////////////////////////////////////////////
186
187AppDelayTracer::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
202AppDelayTracer::AppDelayTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
203: m_node (node)
204, m_os (os)
205{
206 Connect ();
207}
208
209AppDelayTracer::~AppDelayTracer ()
210{
211};
212
213
214void
215AppDelayTracer::Connect ()
216{
217 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/LastRetransmittedInterestDataDelay",
218 MakeCallback (&AppDelayTracer::LastRetransmittedInterestDataDelay, this));
219
Alexander Afanasyevb9faa442013-01-18 19:53:43 -0800220 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/FirstInterestDataDelay",
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800221 MakeCallback (&AppDelayTracer::FirstInterestDataDelay, this));
222}
223
224void
225AppDelayTracer::PrintHeader (std::ostream &os) const
226{
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800227 os << "Time" << "\t"
228 << "Node" << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800229 << "AppId" << "\t"
230 << "SeqNo" << "\t"
231
232 << "Type" << "\t"
233 << "DelayS" << "\t"
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800234 << "DelayUS" << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800235 << "RetxCount" << "\t"
236 << "HopCount" << "";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800237}
238
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800239void
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800240AppDelayTracer::LastRetransmittedInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800241{
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800242 *m_os << Simulator::Now ().ToDouble (Time::S) << "\t"
243 << m_node << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800244 << app->GetId () << "\t"
245 << seqno << "\t"
246 << "LastDelay" << "\t"
247 << delay.ToDouble (Time::S) << "\t"
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800248 << delay.ToDouble (Time::US) << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800249 << 1 << "\t"
250 << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800251}
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800252
253void
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800254AppDelayTracer::FirstInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, uint32_t retxCount, int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800255{
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800256 *m_os << Simulator::Now ().ToDouble (Time::S) << "\t"
257 << m_node << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800258 << app->GetId () << "\t"
259 << seqno << "\t"
260 << "FullDelay" << "\t"
261 << delay.ToDouble (Time::S) << "\t"
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800262 << delay.ToDouble (Time::US) << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800263 << retxCount << "\t"
264 << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800265}
266
267
268} // namespace ndn
269} // namespace ns3