Index: src/stats/model/time-averaging-collector.cc |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/stats/model/time-averaging-collector.cc |
@@ -0,0 +1,158 @@ |
+/* -*- 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) |
+ */ |
+ |
+#include "ns3/object.h" |
+#include "ns3/log.h" |
+#include "ns3/simulator.h" |
+#include "ns3/time-averaging-collector.h" |
+ |
+NS_LOG_COMPONENT_DEFINE ("TimeAveragingCollector"); |
+ |
+namespace ns3 { |
+ |
+NS_OBJECT_ENSURE_REGISTERED (TimeAveragingCollector); |
+ |
+TypeId |
+TimeAveragingCollector::GetTypeId (void) |
+{ |
+ static TypeId tid = TypeId ("ns3::TimeAveragingCollector") |
+ .SetParent<Collector> () |
+ .AddConstructor<TimeAveragingCollector> () |
+ .AddAttribute ("Period", |
+ "The period in seconds between reports", |
+ TimeValue (Seconds (1.0)), |
+ MakeTimeAccessor (&TimeAveragingCollector::SetPeriod, |
+ &TimeAveragingCollector::GetPeriod), |
+ MakeTimeChecker ()) |
+ .AddTraceSource ("Output", |
+ "The double value received by the trace sink", |
+ MakeTraceSourceAccessor (&TimeAveragingCollector::m_output), |
+ "ns3::DataCollectionObject::CollectorTraceCallback") |
+ ; |
+ return tid; |
+} |
+ |
+TimeAveragingCollector::TimeAveragingCollector () |
+ : Collector (), |
+ m_doubleValue (0), |
+ m_hasReportBeenScheduled (false), |
+ m_batchCount (0) |
+{ |
+ NS_LOG_FUNCTION (this); |
+} |
+ |
+TimeAveragingCollector::~TimeAveragingCollector () |
+{ |
+ NS_LOG_FUNCTION (this); |
+} |
+ |
+void |
+TimeAveragingCollector::ReportData (void) |
+{ |
+ NS_LOG_FUNCTION (this); |
+ if (!IsEnabled ()) |
+ { |
+ return; |
+ } |
+ double time = Simulator::Now ().GetSeconds (); |
+ // output the data downstream |
+ m_output (time, m_doubleValue); |
+ |
+ // clear the batch count |
+ m_batchCount = 0; |
+ |
+ m_reportEventId = Simulator::Schedule (m_period, &TimeAveragingCollector::ReportData, this); |
buherman
2015/06/29 08:53:54
If m_period is 0, the simulation will run indefini
|
+ m_hasReportBeenScheduled = true; |
+} |
+ |
+void |
+TimeAveragingCollector::TraceSinkDouble (double oldValue, double newValue) |
+{ |
+ NS_LOG_FUNCTION (this << oldValue << newValue); |
+ if (!IsEnabled ()) |
+ { |
+ NS_LOG_DEBUG ("Collector not enabled"); |
+ return; |
+ } |
+ |
+ |
+ // keep track of the average value |
+ m_doubleValue = (m_doubleValue * m_batchCount + newValue) / (m_batchCount + 1); |
+ |
+ // increment the batch count |
+ m_batchCount++; |
+} |
+ |
+void |
+TimeAveragingCollector::SetPeriod (Time period) |
+{ |
+ m_period = period; |
+ PeriodChanged (); |
+ NS_LOG_DEBUG ("TimeAveragingCollector " << this << " period changed"); |
+} |
+ |
+Time |
+TimeAveragingCollector::GetPeriod () const |
+{ |
+ NS_LOG_FUNCTION (this); |
+ return m_period; |
+} |
+ |
+void |
+TimeAveragingCollector::PeriodChanged (void) |
+{ |
+ NS_LOG_FUNCTION (this); |
+ if (m_hasReportBeenScheduled) |
+ { |
+ Simulator::Cancel (m_reportEventId); |
+ m_hasReportBeenScheduled = false; |
+ NS_LOG_DEBUG ("TimeAveragingCollector " << this << " data report has been cancelled"); |
+ } |
+ |
+ if (!IsEnabled ()) |
+ { |
+ return; |
+ } |
+ |
+ m_batchCount = 0; |
+ m_doubleValue = 0; |
+ m_reportEventId = Simulator::Schedule (m_period, &TimeAveragingCollector::ReportData, this); |
+ m_hasReportBeenScheduled = true; |
+ NS_LOG_DEBUG ("TimeAveragingCollector " << this << " data report has been scheduled"); |
+} |
+ |
+// Override DataCollectionObject::Enable() to schedule the very first event |
+// whenever the collector is enabled |
+void |
+TimeAveragingCollector::Enable (void) |
+{ |
+ DataCollectionObject::Enable (); |
+ if (m_hasReportBeenScheduled) |
+ { |
+ Simulator::Cancel (m_reportEventId); |
+ m_hasReportBeenScheduled = false; |
+ NS_LOG_DEBUG ("TimeAveragingCollector " << this << " data report has been cancelled"); |
+ } |
+ |
+ m_reportEventId = Simulator::Schedule (m_period, &TimeAveragingCollector::ReportData, this); |
+ m_hasReportBeenScheduled = true; |
+} |
+ |
+} // namespace ns3 |