--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/scratch/double-time-driven-collector-example.cc Thu Jul 24 18:05:49 2014 -0400
@@ -0,0 +1,163 @@
+/* -*- 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::DoubleTimeDrivenCollector. The example is modified from double-probe-example.cc.
+ *
+ * The ns3::DoubleTimeDrivenCollector is able to generate outputs downstream
+ * in every specified time interval. The example below uses
+ * ns3::DoubleTimeDrivenCollector 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/double-time-driven-collector.h"
+#include "ns3/stats-module.h"
+
+using namespace ns3;
+
+NS_LOG_COMPONENT_DEFINE ("DoubleTimeDrivenCollectorExample");
+
+
+// Declare a BatchingCollector as a global variable.
+// The collector will spit out outputs every one second.
+Ptr<DoubleTimeDrivenCollector> collector = CreateObject<DoubleTimeDrivenCollector> (Seconds(1) );
+
+
+/*
+ * 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))
+ ;
+ 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 (&DoubleTimeDrivenCollector::TraceSinkDouble, collector));
+
+ // enable the collector
+ collector->Enable();
+
+ // Set up a FileAggregator
+ std::string outputFileName = "double-time-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));
+
+ // 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;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/stats/test/double-time-driven-collector-test-suite.cc Thu Jul 24 18:05:49 2014 -0400
@@ -0,0 +1,161 @@
+/* -*- 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)
+ */
+
+#include <iostream>
+#include <string>
+#include "ns3/double-time-driven-collector.h"
+#include "ns3/double-probe.h"
+#include "ns3/test.h"
+#include "ns3/core-module.h"
+
+using namespace ns3;
+
+class DoubleTimeDrivenCollectorSampleEmitter : public Object
+{
+public:
+ static TypeId GetTypeId (void);
+ DoubleTimeDrivenCollectorSampleEmitter ()
+ {
+ }
+ virtual ~DoubleTimeDrivenCollectorSampleEmitter ()
+ {
+ }
+ void Start ()
+ {
+ Reschedule ();
+ }
+ void Reschedule ()
+ {
+ m_time = m_var.GetValue ();
+ Simulator::Schedule (Seconds (m_time), &DoubleTimeDrivenCollectorSampleEmitter::Report, this);
+ m_time += Simulator::Now ().GetSeconds ();
+ }
+ double GetTime ()
+ {
+ return m_time;
+ }
+ double GetValue ()
+ {
+ return aux;
+ }
+private:
+ void Report ()
+ {
+ aux = m_var.GetValue ();
+ m_trace = aux;
+ Reschedule ();
+ }
+ ExponentialVariable m_var;
+ double m_time;
+ TracedValue<double> m_trace;
+ double aux;
+};
+
+
+TypeId
+DoubleTimeDrivenCollectorSampleEmitter::GetTypeId (void)
+{
+ static TypeId tid = TypeId ("DoubleTimeDrivenCollectorSampleEmitter")
+ .SetParent<Object> ()
+ .AddTraceSource ("Emitter", "XX", MakeTraceSourceAccessor (&DoubleTimeDrivenCollectorSampleEmitter::m_trace))
+ ;
+ return tid;
+}
+
+
+// ===========================================================================
+// Test case
+// ===========================================================================
+
+class DoubleTimeDrivenCollectorTestCase1 : public TestCase
+{
+public:
+ DoubleTimeDrivenCollectorTestCase1 ();
+ virtual ~DoubleTimeDrivenCollectorTestCase1 ();
+
+private:
+ double currentTime;
+ virtual void DoRun (void);
+ void TraceSinkCallback(std::string context, double oldValue, double newValue);
+ Ptr<DoubleTimeDrivenCollector> collector ;
+ Ptr<DoubleTimeDrivenCollectorSampleEmitter> s;
+};
+
+DoubleTimeDrivenCollectorTestCase1::DoubleTimeDrivenCollectorTestCase1 ()
+ : TestCase ("DoubleTimeDrivenCollector test case 1")
+{
+ // let the collector generate outputs every 1 second
+ collector = CreateObject<DoubleTimeDrivenCollector> (Seconds(1) );
+ s = CreateObject<DoubleTimeDrivenCollectorSampleEmitter> ();
+ currentTime = 1;
+}
+
+DoubleTimeDrivenCollectorTestCase1::~DoubleTimeDrivenCollectorTestCase1 ()
+{
+}
+
+void
+DoubleTimeDrivenCollectorTestCase1::DoRun (void)
+{
+ Ptr<DoubleProbe> p = CreateObject<DoubleProbe> ();
+ p->SetName ("DoubleTimeDrivenCollectorSampleProbe");
+
+ Simulator::Schedule (Seconds (0), &DoubleTimeDrivenCollectorSampleEmitter::Start, s);
+ p->SetAttribute ("Start", TimeValue (Seconds (0.0)));
+ p->SetAttribute ("Stop", TimeValue (Seconds (100.0)));
+ Simulator::Stop (Seconds (200));
+
+ Names::Add ("/Names/DoubleTimeDrivenCollectorSampleEmitter", s);
+
+ // Hook probe to the emitter.
+ p->ConnectByObject ("Emitter", s);
+
+ // Hook the collector to the probe
+ bool connected = p ->TraceConnectWithoutContext ("Output", MakeCallback (&DoubleTimeDrivenCollector::TraceSinkDouble, collector));
+
+ // connect the collector to the TraceSinkCallback.
+ connected = collector-> TraceConnect("Output", p->GetName(), MakeCallback(&DoubleTimeDrivenCollectorTestCase1::TraceSinkCallback, this));
+
+ Simulator::Run ();
+ Simulator::Destroy ();
+}
+
+void
+DoubleTimeDrivenCollectorTestCase1::TraceSinkCallback(std::string context, double time, double newValue)
+{
+ // check whether the collector is generating outputs every 1 second.
+ NS_TEST_ASSERT_MSG_EQ_TOL (currentTime, time, 0.0001, "Value generated by the collector different than the actual value");
+ currentTime += 1;
+}
+
+class DoubleTimeDrivenCollectorTestSuite : public TestSuite
+{
+public:
+ DoubleTimeDrivenCollectorTestSuite();
+};
+
+DoubleTimeDrivenCollectorTestSuite::DoubleTimeDrivenCollectorTestSuite()
+ : TestSuite("double-time-driven-collector", UNIT)
+{
+ AddTestCase(new DoubleTimeDrivenCollectorTestCase1, TestCase::QUICK);
+}
+
+static DoubleTimeDrivenCollectorTestSuite DoubleTimeDrivenCollectorTestSuite;
+
--- a/src/stats/wscript Thu Jul 24 17:18:19 2014 -0400
+++ b/src/stats/wscript Thu Jul 24 18:05:49 2014 -0400
@@ -50,6 +50,7 @@
'test/sum-collector-test-suite.cc',
'test/batching-collector-test-suite.cc',
'test/double-event-driven-collector-test-suite.cc',
+ 'test/double-time-driven-collector-test-suite.cc',
]
headers = bld(features='ns3header')