Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(997)

Unified Diff: src/stats/examples/event-driven-collector-example.cc

Issue 245260043: DCF collectors
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/stats/examples/event-driven-collector-example.cc
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/stats/examples/event-driven-collector-example.cc
@@ -0,0 +1,166 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014 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::EventDrivenCollector. The example is modified from double-probe-example.cc.
+ * The ns3::EventDrivenCollector is the simplest collector. It collects
+ * a single volumn of double valued data and outputs the data downstream.
+ * The example below uses a ns3::EventDrivenCollector to collect data
+ * generated from an Emitter object and dumps the data to a txt file with
+ * a FileAggregator object.
+ */
+
+#include <iostream>
+#include <string>
+
+#include "ns3/core-module.h"
+#include "ns3/double-probe.h"
+#include "ns3/event-driven-collector.h"
+#include "ns3/stats-module.h"
+
+using namespace ns3;
+
+NS_LOG_COMPONENT_DEFINE ("EventDrivenCollectorExample");
+
+
+// Declare a EventDrivenCollector as a global variable
+Ptr<EventDrivenCollector> collector = CreateObject<EventDrivenCollector> ();
+
+
+/*
+ * 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);
+
+ //
+ // 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 (&EventDrivenCollector::TraceSinkDouble, collector));
+ NS_ASSERT (connected == true);
+
+ // enable the collector
+ collector->Enable ();
+
+ // Set up a FileAggregator
+ std::string outputFileName = "event-driven-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);
+ NS_UNUSED (connected);
+
+ // 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;
+}

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b