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/scaling-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 ScalingCollectorSampleEmitter : public Object |
| 31 { |
| 32 public: |
| 33 static TypeId GetTypeId (void); |
| 34 ScalingCollectorSampleEmitter () |
| 35 { |
| 36 m_var = CreateObject<ExponentialRandomVariable> (); |
| 37 } |
| 38 virtual ~ScalingCollectorSampleEmitter () |
| 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), &ScalingCollectorSampleEmitter::Repor
t, 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 ScalingCollectorSampleEmitter::GetTypeId (void) |
| 75 { |
| 76 static TypeId tid = TypeId ("ScalingCollectorSampleEmitter") |
| 77 .SetParent<Object> () |
| 78 .AddTraceSource ("Emitter", "XX", MakeTraceSourceAccessor (&ScalingCollector
SampleEmitter::m_trace), "ns3::TracedValue::DoubleCallback") |
| 79 ; |
| 80 return tid; |
| 81 } |
| 82 |
| 83 |
| 84 // =========================================================================== |
| 85 // Test case |
| 86 // =========================================================================== |
| 87 |
| 88 class ScalingCollectorTestCase1 : public TestCase |
| 89 { |
| 90 public: |
| 91 ScalingCollectorTestCase1 (); |
| 92 virtual ~ScalingCollectorTestCase1 (); |
| 93 |
| 94 private: |
| 95 virtual void DoRun (void); |
| 96 void TraceSinkCallback (std::string context, double oldValue, double newValue)
; |
| 97 void ReceiveDouble (std::string context, double oldValue, double newValue); |
| 98 double receivedDouble; |
| 99 Ptr<ScalingCollector> collector; |
| 100 Ptr<ScalingCollectorSampleEmitter> s; |
| 101 }; |
| 102 |
| 103 ScalingCollectorTestCase1::ScalingCollectorTestCase1 () |
| 104 : TestCase ("ScalingCollector test case 1") |
| 105 { |
| 106 } |
| 107 |
| 108 ScalingCollectorTestCase1::~ScalingCollectorTestCase1 () |
| 109 { |
| 110 } |
| 111 |
| 112 void |
| 113 ScalingCollectorTestCase1::DoRun (void) |
| 114 { |
| 115 collector = CreateObject<ScalingCollector> (); |
| 116 collector->SetAttribute ("ScalingFactor", DoubleValue (100)); |
| 117 s = CreateObject<ScalingCollectorSampleEmitter> (); |
| 118 |
| 119 Ptr<DoubleProbe> p = CreateObject<DoubleProbe> (); |
| 120 p->SetName ("ScalingCollectorSampleProbe"); |
| 121 |
| 122 Simulator::Schedule (Seconds (1), &ScalingCollectorSampleEmitter::Start, s); |
| 123 p->SetAttribute ("Start", TimeValue (Seconds (100.0))); |
| 124 p->SetAttribute ("Stop", TimeValue (Seconds (200.0))); |
| 125 Simulator::Stop (Seconds (300)); |
| 126 |
| 127 Names::Add ("/Names/ScalingCollectorSampleEmitter", s); |
| 128 |
| 129 // Hook probe to the emitter. |
| 130 p->ConnectByObject ("Emitter", s); |
| 131 |
| 132 // update the variable that keeps track of the data received |
| 133 bool connected = p->TraceConnect ("Output", p->GetName (), MakeCallback (&Scal
ingCollectorTestCase1::ReceiveDouble, this)); |
| 134 NS_UNUSED (connected); |
| 135 |
| 136 // Hook the collector to the probe |
| 137 connected = p->TraceConnectWithoutContext ("Output", MakeCallback (&ScalingCo
llector::TraceSinkDouble, collector)); |
| 138 |
| 139 // connect the collector to the TraceSinkCallback. |
| 140 connected = collector->TraceConnect ("Output", p->GetName (), MakeCallback (&S
calingCollectorTestCase1::TraceSinkCallback, this)); |
| 141 |
| 142 Simulator::Run (); |
| 143 Simulator::Destroy (); |
| 144 } |
| 145 |
| 146 void |
| 147 ScalingCollectorTestCase1::TraceSinkCallback (std::string context, double oldVal
ue, double newValue) |
| 148 { |
| 149 NS_TEST_ASSERT_MSG_EQ_TOL (receivedDouble * 100, newValue, 0.0001, "Value gene
rated by the collector different than the actual value"); |
| 150 } |
| 151 |
| 152 void |
| 153 ScalingCollectorTestCase1::ReceiveDouble (std::string context, double oldValue,
double newValue) |
| 154 { |
| 155 receivedDouble = newValue; |
| 156 } |
| 157 |
| 158 |
| 159 class ScalingCollectorTestSuite : public TestSuite |
| 160 { |
| 161 public: |
| 162 ScalingCollectorTestSuite (); |
| 163 }; |
| 164 |
| 165 ScalingCollectorTestSuite::ScalingCollectorTestSuite () |
| 166 : TestSuite ("scaling-collector", UNIT) |
| 167 { |
| 168 AddTestCase (new ScalingCollectorTestCase1, TestCase::QUICK); |
| 169 } |
| 170 |
| 171 static ScalingCollectorTestSuite ScalingCollectorTestSuite; |
| 172 |
OLD | NEW |