blob: a7f80149f14d94652d19445b470cb02ae7825898 [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 Afanasyevc86c2832011-12-23 02:56:22 -080035
36#include <boost/ref.hpp>
37#include <boost/lexical_cast.hpp>
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080038
39#include "tracers/ccnx-aggregate-app-tracer.h"
40#include "tracers/ccnx-aggregate-l3-tracer.h"
41#include "tracers/ccnx-rate-l3-tracer.h"
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080042#include "tracers/ccnx-seqs-app-tracer.h"
Alexander Afanasyevc4f88282012-01-03 11:27:20 -080043#include "tracers/ipv4-seqs-app-tracer.h"
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080044#include "tracers/ccnx-consumer-window-tracer.h"
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080045
46#include "ns3/ccnx-interest-header.h"
47#include "ns3/ccnx-content-object-header.h"
Alexander Afanasyev141d1312011-12-18 14:58:35 -080048
49#include <fstream>
Alexander Afanasyev141d1312011-12-18 14:58:35 -080050
51using namespace std;
52using namespace boost;
53
54NS_LOG_COMPONENT_DEFINE ("CcnxTraceHelper");
55
56namespace ns3 {
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080057
58CcnxTraceHelper::CcnxTraceHelper ()
59 : m_l3RateTrace (0)
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080060 , m_appSeqsTrace (0)
Alexander Afanasyevc4f88282012-01-03 11:27:20 -080061 , m_ipv4AppSeqsTrace (0)
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080062 , m_windowsTrace (0)
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080063 , m_windowsTcpTrace (0)
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080064{
65}
66
Alexander Afanasyev141d1312011-12-18 14:58:35 -080067CcnxTraceHelper::~CcnxTraceHelper ()
68{
69 NS_LOG_FUNCTION (this);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080070 if (m_l3RateTrace != 0) delete m_l3RateTrace;
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080071 if (m_appSeqsTrace != 0) delete m_appSeqsTrace;
Alexander Afanasyevc4f88282012-01-03 11:27:20 -080072 if (m_ipv4AppSeqsTrace != 0) delete m_ipv4AppSeqsTrace;
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080073 if (m_windowsTrace != 0) delete m_windowsTrace;
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080074 if (m_windowsTcpTrace != 0) delete m_windowsTcpTrace;
Alexander Afanasyev141d1312011-12-18 14:58:35 -080075
76 if (m_apps.size () > 0)
77 {
78 ofstream of;
79 if (!m_appTrace.empty ())
80 {
81 of.open (m_appTrace.c_str (), ios_base::trunc | ios_base::out);
82 of << "# ";
83 m_apps.front ()->PrintHeader (of);
84 of << "\n";
85 }
86
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080087 for (std::list<Ptr<CcnxAppTracer> >::iterator app = m_apps.begin ();
Alexander Afanasyev141d1312011-12-18 14:58:35 -080088 app != m_apps.end ();
89 app++)
90 {
91 if (!m_appTrace.empty ())
92 {
93 (*app)->Print (of);
94 of << "\n";
95 }
96 else
97 {
98 NS_LOG_INFO (*(*app));
99 }
100 }
101 }
102
103 if (m_l3s.size () > 0)
104 {
105 ofstream of;
106 if (!m_l3Trace.empty ())
107 {
108 of.open (m_l3Trace.c_str (), ios_base::trunc | ios_base::out);
109 of << "# ";
110 m_l3s.front ()->PrintHeader (of);
111 of << "\n";
112 }
113
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800114 for (std::list<Ptr<CcnxL3Tracer> >::iterator l3 = m_l3s.begin ();
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800115 l3 != m_l3s.end ();
116 l3++)
117 {
118 if (!m_l3Trace.empty ())
119 {
120 (*l3)->Print (of);
121 of << "\n";
122 }
123 else
124 {
125 NS_LOG_INFO (*(*l3));
126 }
127 }
128 }
129}
130
131void
132CcnxTraceHelper::SetAppTraceFile (const std::string &appTrace)
133{
134 NS_LOG_FUNCTION (this << appTrace);
135 m_appTrace = appTrace;
136}
137
138void
139CcnxTraceHelper::SetL3TraceFile (const std::string &l3Trace)
140{
141 NS_LOG_FUNCTION (this << l3Trace);
142 m_l3Trace = l3Trace;
143}
144
145void
146CcnxTraceHelper::EnableAggregateAppAll (const std::string &appName)
147{
148 NS_LOG_FUNCTION (this << appName);
149 for (NodeList::Iterator node = NodeList::Begin ();
150 node != NodeList::End ();
151 node++)
152 {
153 ObjectVectorValue apps;
154 (*node)->GetAttribute ("ApplicationList", apps);
155
156 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
157
158 uint32_t appId = 0;
159 for (ObjectVectorValue::Iterator app = apps.Begin ();
160 app != apps.End ();
161 app++, appId++)
162 {
163 NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ());
164 if ((*app)->GetInstanceTypeId ().GetName () == appName)
165 {
166 m_apps.push_back (Create<CcnxAggregateAppTracer> (appName,
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800167 *node,
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800168 lexical_cast<string> (appId)));
169 }
170 }
171 }
172}
173
174void
175CcnxTraceHelper::EnableAggregateL3All ()
176{
177 NS_LOG_FUNCTION (this);
178
179 for (NodeList::Iterator node = NodeList::Begin ();
180 node != NodeList::End ();
181 node++)
182 {
183 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
184
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800185 m_l3s.push_back (Create<CcnxAggregateL3Tracer> (*node));
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800186 }
187}
188
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800189void
190CcnxTraceHelper::EnableRateL3All (const std::string &l3RateTrace)
191{
192 NS_LOG_FUNCTION (this);
193 m_l3RateTrace = new ofstream (l3RateTrace.c_str (), ios::trunc);
194
195 for (NodeList::Iterator node = NodeList::Begin ();
196 node != NodeList::End ();
197 node++)
198 {
199 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
200
201 Ptr<CcnxRateL3Tracer> trace = Create<CcnxRateL3Tracer> (boost::ref(*m_l3RateTrace), *node);
202 trace->SetAveragingPeriod (Seconds (0.5));
203 m_l3Rates.push_back (trace);
204 }
205
206 if (m_l3Rates.size () > 0)
207 {
208 // *m_l3RateTrace << "# "; // not necessary for R's read.table
209 m_l3Rates.front ()->PrintHeader (*m_l3RateTrace);
210 *m_l3RateTrace << "\n";
211 }
212}
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800213
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800214void
215CcnxTraceHelper::EnableSeqsAppAll (const std::string &appName, const std::string &trace)
216{
217 NS_LOG_FUNCTION (this);
218 m_appSeqsTrace = new ofstream (trace.c_str (), ios::trunc);
219
220 for (NodeList::Iterator node = NodeList::Begin ();
221 node != NodeList::End ();
222 node++)
223 {
224 ObjectVectorValue apps;
225 (*node)->GetAttribute ("ApplicationList", apps);
226
227 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
228
229 uint32_t appId = 0;
230 for (ObjectVectorValue::Iterator app = apps.Begin ();
231 app != apps.End ();
232 app++, appId++)
233 {
234 NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ());
235 if ((*app)->GetInstanceTypeId ().GetName () == appName)
236 {
237 Ptr<CcnxSeqsAppTracer> trace = Create<CcnxSeqsAppTracer> (boost::ref(*m_appSeqsTrace),
238 appName,
239 *node,
240 lexical_cast<string> (appId));
241 m_appSeqs.push_back (trace);
242 }
243 }
244
245 }
246
247 if (m_appSeqs.size () > 0)
248 {
249 // *m_l3RateTrace << "# "; // not necessary for R's read.table
250 m_appSeqs.front ()->PrintHeader (*m_appSeqsTrace);
251 *m_appSeqsTrace << "\n";
252 }
253}
254
Alexander Afanasyevc4f88282012-01-03 11:27:20 -0800255void
256CcnxTraceHelper::EnableIpv4SeqsAppAll (const std::string &trace)
257{
258 NS_LOG_FUNCTION (this);
259 m_ipv4AppSeqsTrace = new ofstream (trace.c_str (), ios::trunc);
260
261 for (NodeList::Iterator node = NodeList::Begin ();
262 node != NodeList::End ();
263 node++)
264 {
265 ObjectVectorValue apps;
266 (*node)->GetAttribute ("ApplicationList", apps);
267
268 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
269
270 uint32_t appId = 0;
271 for (ObjectVectorValue::Iterator app = apps.Begin ();
272 app != apps.End ();
273 app++, appId++)
274 {
275 NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ());
276 if ((*app)->GetInstanceTypeId ().GetName () == "ns3::PacketSink" ||
277 (*app)->GetInstanceTypeId ().GetName () == "ns3::BulkSendApplication")
278 {
279 Ptr<Ipv4SeqsAppTracer> trace = Create<Ipv4SeqsAppTracer> (boost::ref(*m_ipv4AppSeqsTrace),
280 *node,
281 lexical_cast<string> (appId));
282 m_ipv4AppSeqs.push_back (trace);
283 }
284 }
285
286 }
287
288 if (m_ipv4AppSeqs.size () > 0)
289 {
290 m_ipv4AppSeqs.front ()->PrintHeader (*m_ipv4AppSeqsTrace);
291 *m_ipv4AppSeqsTrace << "\n";
292 }
293}
294
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -0800295void
296CcnxTraceHelper::EnableWindowsAll (const std::string &windowTrace)
297{
298 NS_LOG_FUNCTION (this);
299 m_windowsTrace = new ofstream (windowTrace.c_str (), ios::trunc);
300
301 for (NodeList::Iterator node = NodeList::Begin ();
302 node != NodeList::End ();
303 node++)
304 {
305 ObjectVectorValue apps;
306 (*node)->GetAttribute ("ApplicationList", apps);
307
308 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
309
310 uint32_t appId = 0;
311 for (ObjectVectorValue::Iterator app = apps.Begin ();
312 app != apps.End ();
313 app++, appId++)
314 {
315 if ((*app)->GetInstanceTypeId ().GetName () == "ns3::CcnxConsumerWindow")
316 {
317 Ptr<CcnxConsumerWindowTracer> trace = Create<CcnxConsumerWindowTracer> (boost::ref(*m_windowsTrace),
318 *node,
319 lexical_cast<string> (appId));
320 m_windows.push_back (trace);
321 }
322 }
323
324 }
325
326 if (m_windows.size () > 0)
327 {
328 m_windows.front ()->PrintHeader (*m_windowsTrace);
329 *m_windowsTrace << "\n";
330 }
331}
332
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800333void
334CcnxTraceHelper::TcpConnect (Ptr<Node> node)
335{
336 ObjectVectorValue sockets;
337 node->GetObject<TcpL4Protocol> ()->GetAttribute ("SocketList", sockets);
338
339 uint32_t sockId = 0;
340 for (ObjectVectorValue::Iterator socket = sockets.Begin ();
341 socket != sockets.End ();
342 socket++, sockId++)
343 {
344 // std::cout << "Node: " << node->GetId () << ", Socket " << sockId << "\n";
345
346 Ptr<TcpCongestionWindowTracer> trace = Create<TcpCongestionWindowTracer> (boost::ref(*m_windowsTcpTrace),
347 node,
348 lexical_cast<string> (sockId));
349 m_windowsTcp.push_back (trace);
350 }
351}
352
353void
354CcnxTraceHelper::EnableWindowsTcpAll (const std::string &windowTrace)
355{
356 NS_LOG_FUNCTION (this);
357 m_windowsTcpTrace = new ofstream (windowTrace.c_str (), ios::trunc);
358
359 WindowTracer::PrintHeader (*m_windowsTcpTrace);
360 *m_windowsTcpTrace << "\n";
361}
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800362
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800363} // namespace ns3