blob: 303d1aa355ba8f9271b06193c10d5442e3119c0f [file] [log] [blame]
Alexander Afanasyev141d1312011-12-18 14:58:35 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 UCLA
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 "ccnx-trace-helper.h"
22
Alexander Afanasyev141d1312011-12-18 14:58:35 -080023#include "ns3/config.h"
24#include "ns3/callback.h"
25#include "ns3/ccnx-app.h"
26#include "ns3/ccnx-face.h"
27#include "ns3/packet.h"
28#include "ns3/log.h"
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080029#include "ns3/assert.h"
Alexander Afanasyev141d1312011-12-18 14:58:35 -080030#include "ns3/node-list.h"
31#include "ns3/object-vector.h"
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080032#include "ns3/simulator.h"
33#include "ns3/names.h"
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080034#include "ns3/tcp-l4-protocol.h"
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080035#include "ns3/node.h"
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080036
37#include <boost/ref.hpp>
38#include <boost/lexical_cast.hpp>
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080039
40#include "tracers/ccnx-aggregate-app-tracer.h"
41#include "tracers/ccnx-aggregate-l3-tracer.h"
42#include "tracers/ccnx-rate-l3-tracer.h"
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080043#include "tracers/ccnx-seqs-app-tracer.h"
Alexander Afanasyevc4f88282012-01-03 11:27:20 -080044#include "tracers/ipv4-seqs-app-tracer.h"
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080045#include "tracers/ccnx-consumer-window-tracer.h"
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080046#include "tracers/ccnx-path-weight-tracer.h"
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080047
48#include "ns3/ccnx-interest-header.h"
49#include "ns3/ccnx-content-object-header.h"
Alexander Afanasyev141d1312011-12-18 14:58:35 -080050
51#include <fstream>
Alexander Afanasyev141d1312011-12-18 14:58:35 -080052
53using namespace std;
54using namespace boost;
55
56NS_LOG_COMPONENT_DEFINE ("CcnxTraceHelper");
57
58namespace ns3 {
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080059
60CcnxTraceHelper::CcnxTraceHelper ()
61 : m_l3RateTrace (0)
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080062 , m_appSeqsTrace (0)
Alexander Afanasyevc4f88282012-01-03 11:27:20 -080063 , m_ipv4AppSeqsTrace (0)
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080064 , m_windowsTrace (0)
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080065 , m_windowsTcpTrace (0)
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080066 , m_pathWeightsTrace (0)
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080067{
68}
69
Alexander Afanasyev141d1312011-12-18 14:58:35 -080070CcnxTraceHelper::~CcnxTraceHelper ()
71{
72 NS_LOG_FUNCTION (this);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080073 if (m_l3RateTrace != 0) delete m_l3RateTrace;
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080074 if (m_appSeqsTrace != 0) delete m_appSeqsTrace;
Alexander Afanasyevc4f88282012-01-03 11:27:20 -080075 if (m_ipv4AppSeqsTrace != 0) delete m_ipv4AppSeqsTrace;
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080076 if (m_windowsTrace != 0) delete m_windowsTrace;
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080077 if (m_windowsTcpTrace != 0) delete m_windowsTcpTrace;
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080078 if (m_pathWeightsTrace != 0) delete m_pathWeightsTrace;
Alexander Afanasyev141d1312011-12-18 14:58:35 -080079
80 if (m_apps.size () > 0)
81 {
82 ofstream of;
83 if (!m_appTrace.empty ())
84 {
85 of.open (m_appTrace.c_str (), ios_base::trunc | ios_base::out);
86 of << "# ";
87 m_apps.front ()->PrintHeader (of);
88 of << "\n";
89 }
90
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080091 for (std::list<Ptr<CcnxAppTracer> >::iterator app = m_apps.begin ();
Alexander Afanasyev141d1312011-12-18 14:58:35 -080092 app != m_apps.end ();
93 app++)
94 {
95 if (!m_appTrace.empty ())
96 {
97 (*app)->Print (of);
98 of << "\n";
99 }
100 else
101 {
102 NS_LOG_INFO (*(*app));
103 }
104 }
105 }
106
107 if (m_l3s.size () > 0)
108 {
109 ofstream of;
110 if (!m_l3Trace.empty ())
111 {
112 of.open (m_l3Trace.c_str (), ios_base::trunc | ios_base::out);
113 of << "# ";
114 m_l3s.front ()->PrintHeader (of);
115 of << "\n";
116 }
117
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800118 for (std::list<Ptr<CcnxL3Tracer> >::iterator l3 = m_l3s.begin ();
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800119 l3 != m_l3s.end ();
120 l3++)
121 {
122 if (!m_l3Trace.empty ())
123 {
124 (*l3)->Print (of);
125 of << "\n";
126 }
127 else
128 {
129 NS_LOG_INFO (*(*l3));
130 }
131 }
132 }
133}
134
135void
136CcnxTraceHelper::SetAppTraceFile (const std::string &appTrace)
137{
138 NS_LOG_FUNCTION (this << appTrace);
139 m_appTrace = appTrace;
140}
141
142void
143CcnxTraceHelper::SetL3TraceFile (const std::string &l3Trace)
144{
145 NS_LOG_FUNCTION (this << l3Trace);
146 m_l3Trace = l3Trace;
147}
148
149void
150CcnxTraceHelper::EnableAggregateAppAll (const std::string &appName)
151{
152 NS_LOG_FUNCTION (this << appName);
153 for (NodeList::Iterator node = NodeList::Begin ();
154 node != NodeList::End ();
155 node++)
156 {
157 ObjectVectorValue apps;
158 (*node)->GetAttribute ("ApplicationList", apps);
159
160 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
161
162 uint32_t appId = 0;
163 for (ObjectVectorValue::Iterator app = apps.Begin ();
164 app != apps.End ();
165 app++, appId++)
166 {
167 NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ());
168 if ((*app)->GetInstanceTypeId ().GetName () == appName)
169 {
170 m_apps.push_back (Create<CcnxAggregateAppTracer> (appName,
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800171 *node,
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800172 lexical_cast<string> (appId)));
173 }
174 }
175 }
176}
177
178void
179CcnxTraceHelper::EnableAggregateL3All ()
180{
181 NS_LOG_FUNCTION (this);
182
183 for (NodeList::Iterator node = NodeList::Begin ();
184 node != NodeList::End ();
185 node++)
186 {
187 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
188
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800189 m_l3s.push_back (Create<CcnxAggregateL3Tracer> (*node));
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800190 }
191}
192
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800193void
194CcnxTraceHelper::EnableRateL3All (const std::string &l3RateTrace)
195{
196 NS_LOG_FUNCTION (this);
197 m_l3RateTrace = new ofstream (l3RateTrace.c_str (), ios::trunc);
198
199 for (NodeList::Iterator node = NodeList::Begin ();
200 node != NodeList::End ();
201 node++)
202 {
203 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
204
205 Ptr<CcnxRateL3Tracer> trace = Create<CcnxRateL3Tracer> (boost::ref(*m_l3RateTrace), *node);
206 trace->SetAveragingPeriod (Seconds (0.5));
207 m_l3Rates.push_back (trace);
208 }
209
210 if (m_l3Rates.size () > 0)
211 {
212 // *m_l3RateTrace << "# "; // not necessary for R's read.table
213 m_l3Rates.front ()->PrintHeader (*m_l3RateTrace);
214 *m_l3RateTrace << "\n";
215 }
216}
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800217
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800218void
219CcnxTraceHelper::EnableSeqsAppAll (const std::string &appName, const std::string &trace)
220{
221 NS_LOG_FUNCTION (this);
222 m_appSeqsTrace = new ofstream (trace.c_str (), ios::trunc);
223
224 for (NodeList::Iterator node = NodeList::Begin ();
225 node != NodeList::End ();
226 node++)
227 {
228 ObjectVectorValue apps;
229 (*node)->GetAttribute ("ApplicationList", apps);
230
231 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
232
233 uint32_t appId = 0;
234 for (ObjectVectorValue::Iterator app = apps.Begin ();
235 app != apps.End ();
236 app++, appId++)
237 {
238 NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ());
239 if ((*app)->GetInstanceTypeId ().GetName () == appName)
240 {
241 Ptr<CcnxSeqsAppTracer> trace = Create<CcnxSeqsAppTracer> (boost::ref(*m_appSeqsTrace),
242 appName,
243 *node,
244 lexical_cast<string> (appId));
245 m_appSeqs.push_back (trace);
246 }
247 }
248
249 }
250
251 if (m_appSeqs.size () > 0)
252 {
253 // *m_l3RateTrace << "# "; // not necessary for R's read.table
254 m_appSeqs.front ()->PrintHeader (*m_appSeqsTrace);
255 *m_appSeqsTrace << "\n";
256 }
257}
258
Alexander Afanasyevc4f88282012-01-03 11:27:20 -0800259void
260CcnxTraceHelper::EnableIpv4SeqsAppAll (const std::string &trace)
261{
262 NS_LOG_FUNCTION (this);
263 m_ipv4AppSeqsTrace = new ofstream (trace.c_str (), ios::trunc);
264
265 for (NodeList::Iterator node = NodeList::Begin ();
266 node != NodeList::End ();
267 node++)
268 {
269 ObjectVectorValue apps;
270 (*node)->GetAttribute ("ApplicationList", apps);
271
272 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
273
274 uint32_t appId = 0;
275 for (ObjectVectorValue::Iterator app = apps.Begin ();
276 app != apps.End ();
277 app++, appId++)
278 {
279 NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ());
280 if ((*app)->GetInstanceTypeId ().GetName () == "ns3::PacketSink" ||
281 (*app)->GetInstanceTypeId ().GetName () == "ns3::BulkSendApplication")
282 {
283 Ptr<Ipv4SeqsAppTracer> trace = Create<Ipv4SeqsAppTracer> (boost::ref(*m_ipv4AppSeqsTrace),
284 *node,
285 lexical_cast<string> (appId));
286 m_ipv4AppSeqs.push_back (trace);
287 }
288 }
289
290 }
291
292 if (m_ipv4AppSeqs.size () > 0)
293 {
294 m_ipv4AppSeqs.front ()->PrintHeader (*m_ipv4AppSeqsTrace);
295 *m_ipv4AppSeqsTrace << "\n";
296 }
297}
298
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -0800299void
300CcnxTraceHelper::EnableWindowsAll (const std::string &windowTrace)
301{
302 NS_LOG_FUNCTION (this);
303 m_windowsTrace = new ofstream (windowTrace.c_str (), ios::trunc);
304
305 for (NodeList::Iterator node = NodeList::Begin ();
306 node != NodeList::End ();
307 node++)
308 {
309 ObjectVectorValue apps;
310 (*node)->GetAttribute ("ApplicationList", apps);
311
312 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
313
314 uint32_t appId = 0;
315 for (ObjectVectorValue::Iterator app = apps.Begin ();
316 app != apps.End ();
317 app++, appId++)
318 {
319 if ((*app)->GetInstanceTypeId ().GetName () == "ns3::CcnxConsumerWindow")
320 {
321 Ptr<CcnxConsumerWindowTracer> trace = Create<CcnxConsumerWindowTracer> (boost::ref(*m_windowsTrace),
322 *node,
323 lexical_cast<string> (appId));
324 m_windows.push_back (trace);
325 }
326 }
327
328 }
329
330 if (m_windows.size () > 0)
331 {
332 m_windows.front ()->PrintHeader (*m_windowsTrace);
333 *m_windowsTrace << "\n";
334 }
335}
336
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800337void
338CcnxTraceHelper::TcpConnect (Ptr<Node> node)
339{
340 ObjectVectorValue sockets;
341 node->GetObject<TcpL4Protocol> ()->GetAttribute ("SocketList", sockets);
342
343 uint32_t sockId = 0;
344 for (ObjectVectorValue::Iterator socket = sockets.Begin ();
345 socket != sockets.End ();
346 socket++, sockId++)
347 {
348 // std::cout << "Node: " << node->GetId () << ", Socket " << sockId << "\n";
349
350 Ptr<TcpCongestionWindowTracer> trace = Create<TcpCongestionWindowTracer> (boost::ref(*m_windowsTcpTrace),
351 node,
352 lexical_cast<string> (sockId));
353 m_windowsTcp.push_back (trace);
354 }
355}
356
357void
358CcnxTraceHelper::EnableWindowsTcpAll (const std::string &windowTrace)
359{
360 NS_LOG_FUNCTION (this);
361 m_windowsTcpTrace = new ofstream (windowTrace.c_str (), ios::trunc);
362
363 WindowTracer::PrintHeader (*m_windowsTcpTrace);
364 *m_windowsTcpTrace << "\n";
365}
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800366
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800367void
368CcnxTraceHelper::EnablePathWeights (const std::string &pathWeights)
369{
370 NS_LOG_FUNCTION (this);
371 m_pathWeightsTrace = new ofstream (pathWeights.c_str (), ios::trunc);
372
373 WindowTracer::PrintHeader (*m_pathWeightsTrace);
374 *m_pathWeightsTrace << "\n";
375}
376
377void
378CcnxTraceHelper::WeightsConnect (Ptr<Node> node, Ptr<Application> app)
379{
380 // small hack to find out app index
381 uint32_t appId = 0;
382 for (uint32_t appId = 0; appId < node->GetNApplications (); appId++)
383 {
384 if (app == node->GetApplication (appId)) break;
385 }
386 NS_ASSERT (appId != node->GetNApplications ());
387
388 Ptr<CcnxPathWeightTracer> trace = Create<CcnxPathWeightTracer> (boost::ref(*m_pathWeightsTrace),
389 node,
390 lexical_cast<string> (appId));
391 m_pathWeights.push_back (trace);
392}
393
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800394} // namespace ns3