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" |
| 41 | |
| 42 | #include "ns3/ccnx-interest-header.h" |
| 43 | #include "ns3/ccnx-content-object-header.h" |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 44 | |
| 45 | #include <fstream> |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 46 | |
| 47 | using namespace std; |
| 48 | using namespace boost; |
| 49 | |
| 50 | NS_LOG_COMPONENT_DEFINE ("CcnxTraceHelper"); |
| 51 | |
| 52 | namespace ns3 { |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 53 | |
| 54 | CcnxTraceHelper::CcnxTraceHelper () |
| 55 | : m_l3RateTrace (0) |
| 56 | { |
| 57 | } |
| 58 | |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 59 | CcnxTraceHelper::~CcnxTraceHelper () |
| 60 | { |
| 61 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 62 | if (m_l3RateTrace != 0) delete m_l3RateTrace; |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 63 | |
| 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 Afanasyev | b3e4b85 | 2011-12-23 15:58:20 -0800 | [diff] [blame^] | 75 | for (std::list<Ptr<CcnxAppTracer> >::iterator app = m_apps.begin (); |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 76 | 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 Afanasyev | b3e4b85 | 2011-12-23 15:58:20 -0800 | [diff] [blame^] | 102 | for (std::list<Ptr<CcnxL3Tracer> >::iterator l3 = m_l3s.begin (); |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 103 | 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 | |
| 119 | void |
| 120 | CcnxTraceHelper::SetAppTraceFile (const std::string &appTrace) |
| 121 | { |
| 122 | NS_LOG_FUNCTION (this << appTrace); |
| 123 | m_appTrace = appTrace; |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | CcnxTraceHelper::SetL3TraceFile (const std::string &l3Trace) |
| 128 | { |
| 129 | NS_LOG_FUNCTION (this << l3Trace); |
| 130 | m_l3Trace = l3Trace; |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | CcnxTraceHelper::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 Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 155 | *node, |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 156 | lexical_cast<string> (appId))); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | CcnxTraceHelper::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 Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 173 | m_l3s.push_back (Create<CcnxAggregateL3Tracer> (*node)); |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 177 | void |
| 178 | CcnxTraceHelper::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 Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 201 | |
| 202 | } // namespace ns3 |