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