OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2015 Bucknell University |
| 4 * |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 * |
| 18 * Author: Li Li (ll024@bucknell.edu) |
| 19 */ |
| 20 |
| 21 #include <iostream> |
| 22 #include <string> |
| 23 #include "ns3/time-series-collector.h" |
| 24 #include "ns3/double-probe.h" |
| 25 #include "ns3/test.h" |
| 26 #include "ns3/core-module.h" |
| 27 |
| 28 using namespace ns3; |
| 29 |
| 30 class TimeSeriesCollectorSampleEmitter : public Object |
| 31 { |
| 32 public: |
| 33 static TypeId GetTypeId (void); |
| 34 TimeSeriesCollectorSampleEmitter () |
| 35 { |
| 36 m_var = CreateObject<ExponentialRandomVariable> (); |
| 37 } |
| 38 virtual ~TimeSeriesCollectorSampleEmitter () |
| 39 { |
| 40 } |
| 41 void Start () |
| 42 { |
| 43 Reschedule (); |
| 44 } |
| 45 void Reschedule () |
| 46 { |
| 47 m_time = m_var->GetValue (); |
| 48 Simulator::Schedule (Seconds (m_time), &TimeSeriesCollectorSampleEmitter::Re
port, this); |
| 49 m_time += Simulator::Now ().GetSeconds (); |
| 50 } |
| 51 double GetTime () |
| 52 { |
| 53 return m_time; |
| 54 } |
| 55 double GetValue () |
| 56 { |
| 57 return aux; |
| 58 } |
| 59 private: |
| 60 void Report () |
| 61 { |
| 62 aux = m_var->GetValue (); |
| 63 m_trace = aux; |
| 64 Reschedule (); |
| 65 } |
| 66 Ptr<ExponentialRandomVariable> m_var; |
| 67 double m_time; |
| 68 TracedValue<double> m_trace; |
| 69 double aux; |
| 70 }; |
| 71 |
| 72 |
| 73 TypeId |
| 74 TimeSeriesCollectorSampleEmitter::GetTypeId (void) |
| 75 { |
| 76 static TypeId tid = TypeId ("TimeSeriesCollectorSampleEmitter") |
| 77 .SetParent<Object> () |
| 78 .AddTraceSource ("Emitter", "XX", MakeTraceSourceAccessor (&TimeSeriesCollec
torSampleEmitter::m_trace), "ns3::TracedValue::DoubleCallback") |
| 79 ; |
| 80 return tid; |
| 81 } |
| 82 |
| 83 |
| 84 // =========================================================================== |
| 85 // Test case |
| 86 // =========================================================================== |
| 87 |
| 88 class TimeSeriesCollectorTestCase1 : public TestCase |
| 89 { |
| 90 public: |
| 91 TimeSeriesCollectorTestCase1 (); |
| 92 virtual ~TimeSeriesCollectorTestCase1 (); |
| 93 |
| 94 private: |
| 95 double currentTime; |
| 96 virtual void DoRun (void); |
| 97 void TraceSinkCallback (std::string context, double oldValue, double newValue)
; |
| 98 Ptr<TimeSeriesCollector> collector; |
| 99 Ptr<TimeSeriesCollectorSampleEmitter> s; |
| 100 }; |
| 101 |
| 102 TimeSeriesCollectorTestCase1::TimeSeriesCollectorTestCase1 () |
| 103 : TestCase ("TimeSeriesCollector test case 1") |
| 104 { |
| 105 } |
| 106 |
| 107 TimeSeriesCollectorTestCase1::~TimeSeriesCollectorTestCase1 () |
| 108 { |
| 109 } |
| 110 |
| 111 void |
| 112 TimeSeriesCollectorTestCase1::DoRun (void) |
| 113 { |
| 114 // let the collector generate outputs every 1 second |
| 115 collector = CreateObject<TimeSeriesCollector> (); |
| 116 collector->SetPeriod (Seconds (1)); |
| 117 collector->Enable (); |
| 118 s = CreateObject<TimeSeriesCollectorSampleEmitter> (); |
| 119 currentTime = 1; |
| 120 |
| 121 Ptr<DoubleProbe> p = CreateObject<DoubleProbe> (); |
| 122 p->SetName ("TimeSeriesCollectorSampleProbe"); |
| 123 |
| 124 Simulator::Schedule (Seconds (0), &TimeSeriesCollectorSampleEmitter::Start, s)
; |
| 125 p->SetAttribute ("Start", TimeValue (Seconds (0.0))); |
| 126 p->SetAttribute ("Stop", TimeValue (Seconds (100.0))); |
| 127 Simulator::Stop (Seconds (200)); |
| 128 |
| 129 Names::Add ("/Names/TimeSeriesCollectorSampleEmitter", s); |
| 130 |
| 131 // Hook probe to the emitter. |
| 132 p->ConnectByObject ("Emitter", s); |
| 133 |
| 134 // Hook the collector to the probe |
| 135 bool connected = p->TraceConnectWithoutContext ("Output", MakeCallback (&Time
SeriesCollector::TraceSinkDouble, collector)); |
| 136 NS_ASSERT (connected == true); |
| 137 |
| 138 // connect the collector to the TraceSinkCallback. |
| 139 connected = collector->TraceConnect ("Output", p->GetName (), MakeCallback (&T
imeSeriesCollectorTestCase1::TraceSinkCallback, this)); |
| 140 NS_ASSERT (connected == true); |
| 141 NS_UNUSED (connected); |
| 142 |
| 143 Simulator::Run (); |
| 144 Simulator::Destroy (); |
| 145 } |
| 146 |
| 147 void |
| 148 TimeSeriesCollectorTestCase1::TraceSinkCallback (std::string context, double tim
e, double newValue) |
| 149 { |
| 150 // check whether the collector is generating outputs every 1 second. |
| 151 NS_TEST_ASSERT_MSG_EQ_TOL (currentTime, time, 0.0001, "Value generated by the
collector different than the actual value"); |
| 152 currentTime += 1; |
| 153 } |
| 154 |
| 155 class TimeSeriesCollectorTestSuite : public TestSuite |
| 156 { |
| 157 public: |
| 158 TimeSeriesCollectorTestSuite (); |
| 159 }; |
| 160 |
| 161 TimeSeriesCollectorTestSuite::TimeSeriesCollectorTestSuite () |
| 162 : TestSuite ("time-series-collector", UNIT) |
| 163 { |
| 164 AddTestCase (new TimeSeriesCollectorTestCase1, TestCase::QUICK); |
| 165 } |
| 166 |
| 167 static TimeSeriesCollectorTestSuite TimeSeriesCollectorTestSuite; |
| 168 |
OLD | NEW |