blob: 8a539285d1ee00a1f3e96ca48cabfeadd5c3cc7d [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"
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080041#include "tracers/ccnx-seqs-app-tracer.h"
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080042
43#include "ns3/ccnx-interest-header.h"
44#include "ns3/ccnx-content-object-header.h"
Alexander Afanasyev141d1312011-12-18 14:58:35 -080045
46#include <fstream>
Alexander Afanasyev141d1312011-12-18 14:58:35 -080047
48using namespace std;
49using namespace boost;
50
51NS_LOG_COMPONENT_DEFINE ("CcnxTraceHelper");
52
53namespace ns3 {
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080054
55CcnxTraceHelper::CcnxTraceHelper ()
56 : m_l3RateTrace (0)
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080057 , m_appSeqsTrace (0)
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080058{
59}
60
Alexander Afanasyev141d1312011-12-18 14:58:35 -080061CcnxTraceHelper::~CcnxTraceHelper ()
62{
63 NS_LOG_FUNCTION (this);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -080064 if (m_l3RateTrace != 0) delete m_l3RateTrace;
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080065 if (m_appSeqsTrace != 0) delete m_appSeqsTrace;
Alexander Afanasyev141d1312011-12-18 14:58:35 -080066
67 if (m_apps.size () > 0)
68 {
69 ofstream of;
70 if (!m_appTrace.empty ())
71 {
72 of.open (m_appTrace.c_str (), ios_base::trunc | ios_base::out);
73 of << "# ";
74 m_apps.front ()->PrintHeader (of);
75 of << "\n";
76 }
77
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080078 for (std::list<Ptr<CcnxAppTracer> >::iterator app = m_apps.begin ();
Alexander Afanasyev141d1312011-12-18 14:58:35 -080079 app != m_apps.end ();
80 app++)
81 {
82 if (!m_appTrace.empty ())
83 {
84 (*app)->Print (of);
85 of << "\n";
86 }
87 else
88 {
89 NS_LOG_INFO (*(*app));
90 }
91 }
92 }
93
94 if (m_l3s.size () > 0)
95 {
96 ofstream of;
97 if (!m_l3Trace.empty ())
98 {
99 of.open (m_l3Trace.c_str (), ios_base::trunc | ios_base::out);
100 of << "# ";
101 m_l3s.front ()->PrintHeader (of);
102 of << "\n";
103 }
104
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800105 for (std::list<Ptr<CcnxL3Tracer> >::iterator l3 = m_l3s.begin ();
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800106 l3 != m_l3s.end ();
107 l3++)
108 {
109 if (!m_l3Trace.empty ())
110 {
111 (*l3)->Print (of);
112 of << "\n";
113 }
114 else
115 {
116 NS_LOG_INFO (*(*l3));
117 }
118 }
119 }
120}
121
122void
123CcnxTraceHelper::SetAppTraceFile (const std::string &appTrace)
124{
125 NS_LOG_FUNCTION (this << appTrace);
126 m_appTrace = appTrace;
127}
128
129void
130CcnxTraceHelper::SetL3TraceFile (const std::string &l3Trace)
131{
132 NS_LOG_FUNCTION (this << l3Trace);
133 m_l3Trace = l3Trace;
134}
135
136void
137CcnxTraceHelper::EnableAggregateAppAll (const std::string &appName)
138{
139 NS_LOG_FUNCTION (this << appName);
140 for (NodeList::Iterator node = NodeList::Begin ();
141 node != NodeList::End ();
142 node++)
143 {
144 ObjectVectorValue apps;
145 (*node)->GetAttribute ("ApplicationList", apps);
146
147 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
148
149 uint32_t appId = 0;
150 for (ObjectVectorValue::Iterator app = apps.Begin ();
151 app != apps.End ();
152 app++, appId++)
153 {
154 NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ());
155 if ((*app)->GetInstanceTypeId ().GetName () == appName)
156 {
157 m_apps.push_back (Create<CcnxAggregateAppTracer> (appName,
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800158 *node,
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800159 lexical_cast<string> (appId)));
160 }
161 }
162 }
163}
164
165void
166CcnxTraceHelper::EnableAggregateL3All ()
167{
168 NS_LOG_FUNCTION (this);
169
170 for (NodeList::Iterator node = NodeList::Begin ();
171 node != NodeList::End ();
172 node++)
173 {
174 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
175
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800176 m_l3s.push_back (Create<CcnxAggregateL3Tracer> (*node));
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800177 }
178}
179
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800180void
181CcnxTraceHelper::EnableRateL3All (const std::string &l3RateTrace)
182{
183 NS_LOG_FUNCTION (this);
184 m_l3RateTrace = new ofstream (l3RateTrace.c_str (), ios::trunc);
185
186 for (NodeList::Iterator node = NodeList::Begin ();
187 node != NodeList::End ();
188 node++)
189 {
190 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
191
192 Ptr<CcnxRateL3Tracer> trace = Create<CcnxRateL3Tracer> (boost::ref(*m_l3RateTrace), *node);
193 trace->SetAveragingPeriod (Seconds (0.5));
194 m_l3Rates.push_back (trace);
195 }
196
197 if (m_l3Rates.size () > 0)
198 {
199 // *m_l3RateTrace << "# "; // not necessary for R's read.table
200 m_l3Rates.front ()->PrintHeader (*m_l3RateTrace);
201 *m_l3RateTrace << "\n";
202 }
203}
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800204
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800205void
206CcnxTraceHelper::EnableSeqsAppAll (const std::string &appName, const std::string &trace)
207{
208 NS_LOG_FUNCTION (this);
209 m_appSeqsTrace = new ofstream (trace.c_str (), ios::trunc);
210
211 for (NodeList::Iterator node = NodeList::Begin ();
212 node != NodeList::End ();
213 node++)
214 {
215 ObjectVectorValue apps;
216 (*node)->GetAttribute ("ApplicationList", apps);
217
218 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
219
220 uint32_t appId = 0;
221 for (ObjectVectorValue::Iterator app = apps.Begin ();
222 app != apps.End ();
223 app++, appId++)
224 {
225 NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ());
226 if ((*app)->GetInstanceTypeId ().GetName () == appName)
227 {
228 Ptr<CcnxSeqsAppTracer> trace = Create<CcnxSeqsAppTracer> (boost::ref(*m_appSeqsTrace),
229 appName,
230 *node,
231 lexical_cast<string> (appId));
232 m_appSeqs.push_back (trace);
233 }
234 }
235
236 }
237
238 if (m_appSeqs.size () > 0)
239 {
240 // *m_l3RateTrace << "# "; // not necessary for R's read.table
241 m_appSeqs.front ()->PrintHeader (*m_appSeqsTrace);
242 *m_appSeqsTrace << "\n";
243 }
244}
245
246
Alexander Afanasyev141d1312011-12-18 14:58:35 -0800247} // namespace ns3