Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 23 | #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 Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 29 | #include "ns3/assert.h" |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 30 | #include "ns3/node-list.h" |
| 31 | #include "ns3/object-vector.h" |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 32 | #include "ns3/simulator.h" |
| 33 | #include "ns3/names.h" |
| 34 | |
| 35 | #include <boost/ref.hpp> |
| 36 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | b3e4b85 | 2011-12-23 15:58:20 -0800 | [diff] [blame] | 37 | |
| 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 Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 41 | #include "tracers/ccnx-seqs-app-tracer.h" |
Alexander Afanasyev | b3e4b85 | 2011-12-23 15:58:20 -0800 | [diff] [blame] | 42 | |
| 43 | #include "ns3/ccnx-interest-header.h" |
| 44 | #include "ns3/ccnx-content-object-header.h" |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 45 | |
| 46 | #include <fstream> |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 47 | |
| 48 | using namespace std; |
| 49 | using namespace boost; |
| 50 | |
| 51 | NS_LOG_COMPONENT_DEFINE ("CcnxTraceHelper"); |
| 52 | |
| 53 | namespace ns3 { |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 54 | |
| 55 | CcnxTraceHelper::CcnxTraceHelper () |
| 56 | : m_l3RateTrace (0) |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 57 | , m_appSeqsTrace (0) |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 58 | { |
| 59 | } |
| 60 | |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 61 | CcnxTraceHelper::~CcnxTraceHelper () |
| 62 | { |
| 63 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 64 | if (m_l3RateTrace != 0) delete m_l3RateTrace; |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 65 | if (m_appSeqsTrace != 0) delete m_appSeqsTrace; |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 66 | |
| 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 Afanasyev | b3e4b85 | 2011-12-23 15:58:20 -0800 | [diff] [blame] | 78 | for (std::list<Ptr<CcnxAppTracer> >::iterator app = m_apps.begin (); |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 79 | 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 Afanasyev | b3e4b85 | 2011-12-23 15:58:20 -0800 | [diff] [blame] | 105 | for (std::list<Ptr<CcnxL3Tracer> >::iterator l3 = m_l3s.begin (); |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 106 | 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 | |
| 122 | void |
| 123 | CcnxTraceHelper::SetAppTraceFile (const std::string &appTrace) |
| 124 | { |
| 125 | NS_LOG_FUNCTION (this << appTrace); |
| 126 | m_appTrace = appTrace; |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | CcnxTraceHelper::SetL3TraceFile (const std::string &l3Trace) |
| 131 | { |
| 132 | NS_LOG_FUNCTION (this << l3Trace); |
| 133 | m_l3Trace = l3Trace; |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | CcnxTraceHelper::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 Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 158 | *node, |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 159 | lexical_cast<string> (appId))); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | CcnxTraceHelper::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 Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 176 | m_l3s.push_back (Create<CcnxAggregateL3Tracer> (*node)); |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 180 | void |
| 181 | CcnxTraceHelper::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 Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 204 | |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 205 | void |
| 206 | CcnxTraceHelper::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 Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 247 | } // namespace ns3 |