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