blob: a27ba1ac0ae9613dc23ef26db839d2d5ec199da2 [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"
34
35#include <boost/ref.hpp>
36#include <boost/lexical_cast.hpp>
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080037
38#include "tracers/ccnx-aggregate-app-tracer.h"
39#include "tracers/ccnx-aggregate-l3-tracer.h"
40#include "tracers/ccnx-rate-l3-tracer.h"
41
42#include "ns3/ccnx-interest-header.h"
43#include "ns3/ccnx-content-object-header.h"
Alexander Afanasyev141d1312011-12-18 14:58:35 -080044
45#include <fstream>
Alexander Afanasyev141d1312011-12-18 14:58:35 -080046
47using namespace std;
48using namespace boost;
49
50NS_LOG_COMPONENT_DEFINE ("CcnxTraceHelper");
51
52namespace ns3 {
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080053
54CcnxTraceHelper::CcnxTraceHelper ()
55 : m_l3RateTrace (0)
56{
57}
58
Alexander Afanasyev141d1312011-12-18 14:58:35 -080059CcnxTraceHelper::~CcnxTraceHelper ()
60{
61 NS_LOG_FUNCTION (this);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080062 if (m_l3RateTrace != 0) delete m_l3RateTrace;
Alexander Afanasyev141d1312011-12-18 14:58:35 -080063
64 if (m_apps.size () > 0)
65 {
66 ofstream of;
67 if (!m_appTrace.empty ())
68 {
69 of.open (m_appTrace.c_str (), ios_base::trunc | ios_base::out);
70 of << "# ";
71 m_apps.front ()->PrintHeader (of);
72 of << "\n";
73 }
74
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080075 for (std::list<Ptr<CcnxAppTracer> >::iterator app = m_apps.begin ();
Alexander Afanasyev141d1312011-12-18 14:58:35 -080076 app != m_apps.end ();
77 app++)
78 {
79 if (!m_appTrace.empty ())
80 {
81 (*app)->Print (of);
82 of << "\n";
83 }
84 else
85 {
86 NS_LOG_INFO (*(*app));
87 }
88 }
89 }
90
91 if (m_l3s.size () > 0)
92 {
93 ofstream of;
94 if (!m_l3Trace.empty ())
95 {
96 of.open (m_l3Trace.c_str (), ios_base::trunc | ios_base::out);
97 of << "# ";
98 m_l3s.front ()->PrintHeader (of);
99 of << "\n";
100 }
101
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800102 for (std::list<Ptr<CcnxL3Tracer> >::iterator l3 = m_l3s.begin ();
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800103 l3 != m_l3s.end ();
104 l3++)
105 {
106 if (!m_l3Trace.empty ())
107 {
108 (*l3)->Print (of);
109 of << "\n";
110 }
111 else
112 {
113 NS_LOG_INFO (*(*l3));
114 }
115 }
116 }
117}
118
119void
120CcnxTraceHelper::SetAppTraceFile (const std::string &appTrace)
121{
122 NS_LOG_FUNCTION (this << appTrace);
123 m_appTrace = appTrace;
124}
125
126void
127CcnxTraceHelper::SetL3TraceFile (const std::string &l3Trace)
128{
129 NS_LOG_FUNCTION (this << l3Trace);
130 m_l3Trace = l3Trace;
131}
132
133void
134CcnxTraceHelper::EnableAggregateAppAll (const std::string &appName)
135{
136 NS_LOG_FUNCTION (this << appName);
137 for (NodeList::Iterator node = NodeList::Begin ();
138 node != NodeList::End ();
139 node++)
140 {
141 ObjectVectorValue apps;
142 (*node)->GetAttribute ("ApplicationList", apps);
143
144 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
145
146 uint32_t appId = 0;
147 for (ObjectVectorValue::Iterator app = apps.Begin ();
148 app != apps.End ();
149 app++, appId++)
150 {
151 NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ());
152 if ((*app)->GetInstanceTypeId ().GetName () == appName)
153 {
154 m_apps.push_back (Create<CcnxAggregateAppTracer> (appName,
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800155 *node,
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800156 lexical_cast<string> (appId)));
157 }
158 }
159 }
160}
161
162void
163CcnxTraceHelper::EnableAggregateL3All ()
164{
165 NS_LOG_FUNCTION (this);
166
167 for (NodeList::Iterator node = NodeList::Begin ();
168 node != NodeList::End ();
169 node++)
170 {
171 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
172
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800173 m_l3s.push_back (Create<CcnxAggregateL3Tracer> (*node));
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800174 }
175}
176
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800177void
178CcnxTraceHelper::EnableRateL3All (const std::string &l3RateTrace)
179{
180 NS_LOG_FUNCTION (this);
181 m_l3RateTrace = new ofstream (l3RateTrace.c_str (), ios::trunc);
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
189 Ptr<CcnxRateL3Tracer> trace = Create<CcnxRateL3Tracer> (boost::ref(*m_l3RateTrace), *node);
190 trace->SetAveragingPeriod (Seconds (0.5));
191 m_l3Rates.push_back (trace);
192 }
193
194 if (m_l3Rates.size () > 0)
195 {
196 // *m_l3RateTrace << "# "; // not necessary for R's read.table
197 m_l3Rates.front ()->PrintHeader (*m_l3RateTrace);
198 *m_l3RateTrace << "\n";
199 }
200}
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800201
202} // namespace ns3