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