OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2015 Bucknell University |
| 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: Li Li (ll024@bucknell.edu) |
| 19 */ |
| 20 |
| 21 /* |
| 22 * This example is designed to show the main features of an |
| 23 * ns3::TimeAveragingCollector. |
| 24 * The example is modified from double-probe-example.cc. |
| 25 * |
| 26 * The ns3::TimeAveragingCollector is able to generate outputs downstream |
| 27 * in every specified time interval. The output data are the averages of the |
| 28 * data received by the collector during that time interval. |
| 29 * The example below uses ns3::TimeAveragingCollector to collect |
| 30 * data from an Emitter object and |
| 31 * generates outputs to a txt file every 1 second. |
| 32 */ |
| 33 |
| 34 #include <iostream> |
| 35 #include <string> |
| 36 |
| 37 #include "ns3/core-module.h" |
| 38 #include "ns3/double-probe.h" |
| 39 #include "ns3/time-averaging-collector.h" |
| 40 #include "ns3/stats-module.h" |
| 41 |
| 42 using namespace ns3; |
| 43 |
| 44 NS_LOG_COMPONENT_DEFINE ("TimeAveragingCollectorExample"); |
| 45 |
| 46 |
| 47 // Declare a TimeAveragingCollector as a global variable. |
| 48 // The collector will spit out outputs every one second. |
| 49 Ptr<TimeAveragingCollector> collector = CreateObject<TimeAveragingCollector> (); |
| 50 |
| 51 |
| 52 /* |
| 53 * This is our test object, an object that increments counters at |
| 54 * various times and emits one of them as a trace source. |
| 55 */ |
| 56 class Emitter : public Object |
| 57 { |
| 58 public: |
| 59 static TypeId GetTypeId (void); |
| 60 Emitter (); |
| 61 private: |
| 62 void DoInitialize (void); |
| 63 void Emit (void); |
| 64 void Count (void); |
| 65 |
| 66 TracedValue<double> m_counter; // normally this would be integer type |
| 67 Ptr<ExponentialRandomVariable> m_var; |
| 68 |
| 69 }; |
| 70 |
| 71 NS_OBJECT_ENSURE_REGISTERED (Emitter) |
| 72 ; |
| 73 |
| 74 TypeId |
| 75 Emitter::GetTypeId (void) |
| 76 { |
| 77 static TypeId tid = TypeId ("ns3::Emitter") |
| 78 .AddConstructor<Emitter> () |
| 79 .SetParent<Object> () |
| 80 .AddTraceSource ("Counter", |
| 81 "sample counter", |
| 82 MakeTraceSourceAccessor (&Emitter::m_counter), |
| 83 "ns3::TracedValue::DoubleCallback") |
| 84 ; |
| 85 return tid; |
| 86 } |
| 87 |
| 88 Emitter::Emitter (void) |
| 89 { |
| 90 NS_LOG_FUNCTION (this); |
| 91 m_counter = 0; |
| 92 m_var = CreateObject<ExponentialRandomVariable> (); |
| 93 } |
| 94 |
| 95 void |
| 96 Emitter::DoInitialize (void) |
| 97 { |
| 98 NS_LOG_FUNCTION (this); |
| 99 Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this); |
| 100 Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this); |
| 101 } |
| 102 |
| 103 void |
| 104 Emitter::Emit (void) |
| 105 { |
| 106 NS_LOG_FUNCTION (this); |
| 107 NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds ()); |
| 108 Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this); |
| 109 } |
| 110 |
| 111 void |
| 112 Emitter::Count (void) |
| 113 { |
| 114 NS_LOG_FUNCTION (this); |
| 115 NS_LOG_DEBUG ("Counting at " << Simulator::Now ().GetSeconds ()); |
| 116 m_counter += 1.0; |
| 117 DoubleProbe::SetValueByPath ("/Names/StaticallyAccessedProbe", m_counter); |
| 118 Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this); |
| 119 } |
| 120 |
| 121 |
| 122 int main (int argc, char *argv[]) |
| 123 { |
| 124 CommandLine cmd; |
| 125 cmd.Parse (argc, argv); |
| 126 bool connected; |
| 127 |
| 128 Ptr<Emitter> emitter = CreateObject<Emitter> (); |
| 129 Names::Add ("/Names/Emitter", emitter); |
| 130 |
| 131 collector->SetPeriod (Seconds (3)); |
| 132 |
| 133 // |
| 134 // testProbe will be called by the emitter directly through the |
| 135 // static method SetValueByPath(). |
| 136 // |
| 137 Ptr<DoubleProbe> testProbe = CreateObject<DoubleProbe> (); |
| 138 testProbe->SetName ("StaticallyAccessedProbe"); |
| 139 // We must add it to the config database |
| 140 Names::Add ("/Names/Probes", testProbe->GetName (), testProbe); |
| 141 |
| 142 // hook the probe to the callback function |
| 143 connected = testProbe->TraceConnectWithoutContext ("Output", MakeCallback (&T
imeAveragingCollector::TraceSinkDouble, collector)); |
| 144 NS_ASSERT (connected == true); |
| 145 NS_UNUSED (connected); // silence unused variable warnings |
| 146 |
| 147 // enable the collector |
| 148 collector->Enable (); |
| 149 |
| 150 // Set up a FileAggregator |
| 151 std::string outputFileName = "time-averaging-collector-output.txt"; |
| 152 enum FileAggregator::FileType m_fileType = FileAggregator::SPACE_SEPARATED; |
| 153 |
| 154 Ptr<FileAggregator> aggregator = CreateObject<FileAggregator> (outputFileName,
m_fileType); |
| 155 |
| 156 aggregator->SetHeading ("time value"); |
| 157 aggregator->Enable (); |
| 158 |
| 159 // connect the collector with the aggregator |
| 160 connected = collector->TraceConnect ("Output", "/Names/Probes/StaticallyAccess
edProbe/Output", MakeCallback (&FileAggregator::Write2d, aggregator)); |
| 161 NS_ASSERT (connected == true); |
| 162 |
| 163 // The Emitter object is not associated with an ns-3 node, so |
| 164 // it won't get started automatically, so we need to do this ourselves |
| 165 Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter); |
| 166 |
| 167 Simulator::Stop (Seconds (100.0)); |
| 168 Simulator::Run (); |
| 169 Simulator::Destroy (); |
| 170 |
| 171 return 0; |
| 172 } |
OLD | NEW |