blob: 3e006e4225457df84f242536f049a242c2ec7498 [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
23#include "ns3/ccnx-interest-header.h"
24#include "ns3/ccnx-content-object-header.h"
25#include "ns3/config.h"
26#include "ns3/callback.h"
27#include "ns3/ccnx-app.h"
28#include "ns3/ccnx-face.h"
29#include "ns3/packet.h"
30#include "ns3/log.h"
31#include "ns3/node-list.h"
32#include "ns3/object-vector.h"
33
34#include <fstream>
35#include <boost/lexical_cast.hpp>
36
37using namespace std;
38using namespace boost;
39
40NS_LOG_COMPONENT_DEFINE ("CcnxTraceHelper");
41
42namespace ns3 {
43
44CcnxAppTracer::CcnxAppTracer (const std::string &app, const std::string &node, const std::string &appId)
45 : m_app (app)
46 , m_appId (appId)
47 , m_node (node)
48{
49 Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+appId+"/$"+m_app+"/TransmittedInterests",
50 MakeCallback (&CcnxAppTracer::OutInterests, this));
51
52 Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+appId+"/$"+m_app+"/ReceivedNacks",
53 MakeCallback (&CcnxAppTracer::InNacks, this));
54
55 Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+appId+"/$"+m_app+"/ReceivedInterests",
56 MakeCallback (&CcnxAppTracer::InInterests, this));
57
58 Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+appId+"/$"+m_app+"/TransmittedContentObjects",
59 MakeCallback (&CcnxAppTracer::OutData, this));
60
61 Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+appId+"/$"+m_app+"/ReceivedContentObjects",
62 MakeCallback (&CcnxAppTracer::InData, this));
63}
64
65////////////////////////////////////////////////////////////////////
66////////////////////////////////////////////////////////////////////
67////////////////////////////////////////////////////////////////////
68
69CcnxL3Tracer::CcnxL3Tracer (const std::string &node)
70: m_node (node)
71{
72 Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/ForwardingStrategy/OutInterests",
73 MakeCallback (&CcnxL3Tracer::OutInterests, this));
74 Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/InInterests",
75 MakeCallback (&CcnxL3Tracer::InInterests, this));
76 Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/DropInterests",
77 MakeCallback (&CcnxL3Tracer::DropInterests, this));
78
79 Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/OutNacks",
80 MakeCallback (&CcnxL3Tracer::OutNacks, this));
81 Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/InNacks",
82 MakeCallback (&CcnxL3Tracer::InNacks, this));
83 Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/DropNacks",
84 MakeCallback (&CcnxL3Tracer::DropNacks, this));
85
86 Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/OutData",
87 MakeCallback (&CcnxL3Tracer::OutData, this));
88 Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/InData",
89 MakeCallback (&CcnxL3Tracer::InData, this));
90 Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/DropData",
91 MakeCallback (&CcnxL3Tracer::DropData, this));
92}
93
94////////////////////////////////////////////////////////////////////
95////////////////////////////////////////////////////////////////////
96////////////////////////////////////////////////////////////////////
97
98CcnxAggregateAppTracer::CcnxAggregateAppTracer (const std::string &app, const std::string &node, const std::string &appId)
99 : CcnxAppTracer (app, node, appId)
100 , m_inInterests (0)
101 , m_outInterests (0)
102 , m_inNacks (0)
103 , m_inData (0)
104 , m_outData (0)
105{
106}
107
108void
109CcnxAggregateAppTracer::PrintHeader (std::ostream &os) const
110{
111 os << "NodeId" << "\t"
112 << "App" << "\t"
113 << "AppId" << "\t"
114 << "InInterests" << "\t"
115 << "OutInterests" << "\t"
116
117 << "InNacks" << "\t"
118
119 << "InData" << "\t"
120 << "OutData";
121}
122
123void
124CcnxAggregateAppTracer::Print (std::ostream &os) const
125{
126 os << m_node << "\t"
127 << m_app << "\t"
128 << m_appId << "\t"
129 << m_inInterests << "\t"
130 << m_outInterests << "\t"
131
132 << m_inNacks << "\t"
133
134 << m_inData << "\t"
135 << m_outData;
136}
137
138void
139CcnxAggregateAppTracer::OutInterests (std::string context,
140 Ptr<const CcnxInterestHeader>, Ptr<CcnxApp>, Ptr<CcnxFace>)
141{
142 m_outInterests++;
143}
144
145void
146CcnxAggregateAppTracer::OutData (std::string context,
147 Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>,
148 Ptr<CcnxApp>, Ptr<CcnxFace>)
149{
150 m_outData++;
151}
152
153void
154CcnxAggregateAppTracer::InInterests (std::string context,
155 Ptr<const CcnxInterestHeader>,
156 Ptr<CcnxApp>, Ptr<CcnxFace>)
157{
158 m_inInterests++;
159}
160
161void
162CcnxAggregateAppTracer::InNacks (std::string context,
163 Ptr<const CcnxInterestHeader>,
164 Ptr<CcnxApp>, Ptr<CcnxFace>)
165{
166 m_inNacks++;
167}
168
169void
170CcnxAggregateAppTracer::InData (std::string context,
171 Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>,
172 Ptr<CcnxApp>, Ptr<CcnxFace>)
173{
174 m_inData++;
175}
176
177
178////////////////////////////////////////////////////////////////////
179////////////////////////////////////////////////////////////////////
180////////////////////////////////////////////////////////////////////
181
182
183CcnxAggregateL3Tracer::CcnxAggregateL3Tracer (const std::string &node)
184 : CcnxL3Tracer (node)
185 , m_inInterests (0)
186 , m_outInterests (0)
187 , m_dropInterests (0)
188 , m_inNacks (0)
189 , m_outNacks (0)
190 , m_dropNacks (0)
191 , m_inData (0)
192 , m_outData (0)
193 , m_dropData (0)
194{
195}
196
197void
198CcnxAggregateL3Tracer::PrintHeader (std::ostream &os) const
199{
200 os << "Node" << "\t"
201 << "InInterests" << "\t"
202 << "OutInterests" << "\t"
203 << "DropInterests" << "\t"
204
205 << "InNacks" << "\t"
206 << "OutNacks" << "\t"
207 << "DropNacks" << "\t"
208
209 << "InData" << "\t"
210 << "OutData" << "\t"
211 << "DropData";
212}
213
214void
215CcnxAggregateL3Tracer::Print (std::ostream &os) const
216{
217 os << m_node << "\t"
218 << m_inInterests << "\t"
219 << m_outInterests << "\t"
220 << m_dropInterests << "\t"
221
222 << m_inNacks << "\t"
223 << m_outNacks << "\t"
224 << m_dropNacks << "\t"
225
226 << m_inData << "\t"
227 << m_outData << "\t"
228 << m_dropData;
229}
230
231void
232CcnxAggregateL3Tracer::OutInterests (std::string context,
233 Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>)
234{
235 m_outInterests++;
236}
237
238void
239CcnxAggregateL3Tracer::InInterests (std::string context,
240 Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>)
241{
242 m_inInterests++;
243}
244
245void
246CcnxAggregateL3Tracer::DropInterests (std::string context,
247 Ptr<const CcnxInterestHeader>, Ccnx::DropReason, Ptr<const CcnxFace>)
248{
249 m_dropInterests++;
250}
251
252void
253CcnxAggregateL3Tracer::OutNacks (std::string context,
254 Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>)
255{
256 m_outNacks++;
257}
258
259void
260CcnxAggregateL3Tracer::InNacks (std::string context,
261 Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>)
262{
263 m_inNacks++;
264}
265
266void
267CcnxAggregateL3Tracer::DropNacks (std::string context,
268 Ptr<const CcnxInterestHeader>, Ccnx::DropReason, Ptr<const CcnxFace>)
269{
270 m_dropNacks++;
271}
272
273void
274CcnxAggregateL3Tracer::OutData (std::string context,
275 Ptr<const CcnxContentObjectHeader>, bool fromCache, Ptr<const CcnxFace>)
276{
277 m_inData++;
278}
279
280void
281CcnxAggregateL3Tracer::InData (std::string context,
282 Ptr<const CcnxContentObjectHeader>, Ptr<const CcnxFace>)
283{
284 m_outData++;
285}
286
287void
288CcnxAggregateL3Tracer::DropData (std::string context,
289 Ptr<const CcnxContentObjectHeader>, Ccnx::DropReason, Ptr<const CcnxFace>)
290{
291 m_dropData++;
292}
293
294
295////////////////////////////////////////////////////////////////////
296////////////////////////////////////////////////////////////////////
297////////////////////////////////////////////////////////////////////
298
299CcnxTraceHelper::~CcnxTraceHelper ()
300{
301 NS_LOG_FUNCTION (this);
302
303 if (m_apps.size () > 0)
304 {
305 ofstream of;
306 if (!m_appTrace.empty ())
307 {
308 of.open (m_appTrace.c_str (), ios_base::trunc | ios_base::out);
309 of << "# ";
310 m_apps.front ()->PrintHeader (of);
311 of << "\n";
312 }
313
314 for (std::list<Ptr<CcnxAggregateAppTracer> >::iterator app = m_apps.begin ();
315 app != m_apps.end ();
316 app++)
317 {
318 if (!m_appTrace.empty ())
319 {
320 (*app)->Print (of);
321 of << "\n";
322 }
323 else
324 {
325 NS_LOG_INFO (*(*app));
326 }
327 }
328 }
329
330 if (m_l3s.size () > 0)
331 {
332 ofstream of;
333 if (!m_l3Trace.empty ())
334 {
335 of.open (m_l3Trace.c_str (), ios_base::trunc | ios_base::out);
336 of << "# ";
337 m_l3s.front ()->PrintHeader (of);
338 of << "\n";
339 }
340
341 for (std::list<Ptr<CcnxAggregateL3Tracer> >::iterator l3 = m_l3s.begin ();
342 l3 != m_l3s.end ();
343 l3++)
344 {
345 if (!m_l3Trace.empty ())
346 {
347 (*l3)->Print (of);
348 of << "\n";
349 }
350 else
351 {
352 NS_LOG_INFO (*(*l3));
353 }
354 }
355 }
356}
357
358void
359CcnxTraceHelper::SetAppTraceFile (const std::string &appTrace)
360{
361 NS_LOG_FUNCTION (this << appTrace);
362 m_appTrace = appTrace;
363}
364
365void
366CcnxTraceHelper::SetL3TraceFile (const std::string &l3Trace)
367{
368 NS_LOG_FUNCTION (this << l3Trace);
369 m_l3Trace = l3Trace;
370}
371
372void
373CcnxTraceHelper::EnableAggregateAppAll (const std::string &appName)
374{
375 NS_LOG_FUNCTION (this << appName);
376 for (NodeList::Iterator node = NodeList::Begin ();
377 node != NodeList::End ();
378 node++)
379 {
380 ObjectVectorValue apps;
381 (*node)->GetAttribute ("ApplicationList", apps);
382
383 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
384
385 uint32_t appId = 0;
386 for (ObjectVectorValue::Iterator app = apps.Begin ();
387 app != apps.End ();
388 app++, appId++)
389 {
390 NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ());
391 if ((*app)->GetInstanceTypeId ().GetName () == appName)
392 {
393 m_apps.push_back (Create<CcnxAggregateAppTracer> (appName,
394 lexical_cast<string> ((*node)->GetId ()),
395 lexical_cast<string> (appId)));
396 }
397 }
398 }
399}
400
401void
402CcnxTraceHelper::EnableAggregateL3All ()
403{
404 NS_LOG_FUNCTION (this);
405
406 for (NodeList::Iterator node = NodeList::Begin ();
407 node != NodeList::End ();
408 node++)
409 {
410 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
411
412 m_l3s.push_back (Create<CcnxAggregateL3Tracer> (lexical_cast<string> ((*node)->GetId ())));
413 }
414}
415
416
417} // namespace ns3