Index: src/stats/examples/time-averaging-collector-example.cc |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/stats/examples/time-averaging-collector-example.cc |
@@ -0,0 +1,172 @@ |
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
+/* |
+ * Copyright (c) 2015 Bucknell University |
+ * |
+ * This program is free software; you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 2 as |
+ * published by the Free Software Foundation; |
+ * |
+ * This program is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with this program; if not, write to the Free Software |
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
+ * |
+ * Author: Li Li (ll024@bucknell.edu) |
+ */ |
+ |
+/* |
+ * This example is designed to show the main features of an |
+ * ns3::TimeAveragingCollector. |
+ * The example is modified from double-probe-example.cc. |
+ * |
+ * The ns3::TimeAveragingCollector is able to generate outputs downstream |
+ * in every specified time interval. The output data are the averages of the |
+ * data received by the collector during that time interval. |
+ * The example below uses ns3::TimeAveragingCollector to collect |
+ * data from an Emitter object and |
+ * generates outputs to a txt file every 1 second. |
+ */ |
+ |
+#include <iostream> |
+#include <string> |
+ |
+#include "ns3/core-module.h" |
+#include "ns3/double-probe.h" |
+#include "ns3/time-averaging-collector.h" |
+#include "ns3/stats-module.h" |
+ |
+using namespace ns3; |
+ |
+NS_LOG_COMPONENT_DEFINE ("TimeAveragingCollectorExample"); |
+ |
+ |
+// Declare a TimeAveragingCollector as a global variable. |
+// The collector will spit out outputs every one second. |
+Ptr<TimeAveragingCollector> collector = CreateObject<TimeAveragingCollector> (); |
+ |
+ |
+/* |
+ * This is our test object, an object that increments counters at |
+ * various times and emits one of them as a trace source. |
+ */ |
+class Emitter : public Object |
+{ |
+public: |
+ static TypeId GetTypeId (void); |
+ Emitter (); |
+private: |
+ void DoInitialize (void); |
+ void Emit (void); |
+ void Count (void); |
+ |
+ TracedValue<double> m_counter; // normally this would be integer type |
+ Ptr<ExponentialRandomVariable> m_var; |
+ |
+}; |
+ |
+NS_OBJECT_ENSURE_REGISTERED (Emitter) |
+; |
+ |
+TypeId |
+Emitter::GetTypeId (void) |
+{ |
+ static TypeId tid = TypeId ("ns3::Emitter") |
+ .AddConstructor<Emitter> () |
+ .SetParent<Object> () |
+ .AddTraceSource ("Counter", |
+ "sample counter", |
+ MakeTraceSourceAccessor (&Emitter::m_counter), |
+ "ns3::TracedValue::DoubleCallback") |
+ ; |
+ return tid; |
+} |
+ |
+Emitter::Emitter (void) |
+{ |
+ NS_LOG_FUNCTION (this); |
+ m_counter = 0; |
+ m_var = CreateObject<ExponentialRandomVariable> (); |
+} |
+ |
+void |
+Emitter::DoInitialize (void) |
+{ |
+ NS_LOG_FUNCTION (this); |
+ Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this); |
+ Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this); |
+} |
+ |
+void |
+Emitter::Emit (void) |
+{ |
+ NS_LOG_FUNCTION (this); |
+ NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds ()); |
+ Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this); |
+} |
+ |
+void |
+Emitter::Count (void) |
+{ |
+ NS_LOG_FUNCTION (this); |
+ NS_LOG_DEBUG ("Counting at " << Simulator::Now ().GetSeconds ()); |
+ m_counter += 1.0; |
+ DoubleProbe::SetValueByPath ("/Names/StaticallyAccessedProbe", m_counter); |
+ Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this); |
+} |
+ |
+ |
+int main (int argc, char *argv[]) |
+{ |
+ CommandLine cmd; |
+ cmd.Parse (argc, argv); |
+ bool connected; |
+ |
+ Ptr<Emitter> emitter = CreateObject<Emitter> (); |
+ Names::Add ("/Names/Emitter", emitter); |
+ |
+ collector->SetPeriod (Seconds (3)); |
+ |
+ // |
+ // testProbe will be called by the emitter directly through the |
+ // static method SetValueByPath(). |
+ // |
+ Ptr<DoubleProbe> testProbe = CreateObject<DoubleProbe> (); |
+ testProbe->SetName ("StaticallyAccessedProbe"); |
+ // We must add it to the config database |
+ Names::Add ("/Names/Probes", testProbe->GetName (), testProbe); |
+ |
+ // hook the probe to the callback function |
+ connected = testProbe->TraceConnectWithoutContext ("Output", MakeCallback (&TimeAveragingCollector::TraceSinkDouble, collector)); |
+ NS_ASSERT (connected == true); |
+ NS_UNUSED (connected); // silence unused variable warnings |
+ |
+ // enable the collector |
+ collector->Enable (); |
+ |
+ // Set up a FileAggregator |
+ std::string outputFileName = "time-averaging-collector-output.txt"; |
+ enum FileAggregator::FileType m_fileType = FileAggregator::SPACE_SEPARATED; |
+ |
+ Ptr<FileAggregator> aggregator = CreateObject<FileAggregator> (outputFileName, m_fileType); |
+ |
+ aggregator->SetHeading ("time value"); |
+ aggregator->Enable (); |
+ |
+ // connect the collector with the aggregator |
+ connected = collector->TraceConnect ("Output", "/Names/Probes/StaticallyAccessedProbe/Output", MakeCallback (&FileAggregator::Write2d, aggregator)); |
+ NS_ASSERT (connected == true); |
+ |
+ // The Emitter object is not associated with an ns-3 node, so |
+ // it won't get started automatically, so we need to do this ourselves |
+ Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter); |
+ |
+ Simulator::Stop (Seconds (100.0)); |
+ Simulator::Run (); |
+ Simulator::Destroy (); |
+ |
+ return 0; |
+} |