Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011,2012 University of California, Los Angeles |
| 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: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include "fw-per-fib-limits.h" |
| 22 | #include "ns3/core-module.h" |
| 23 | #include "ns3/ndnSIM-module.h" |
| 24 | #include "ns3/point-to-point-module.h" |
| 25 | |
| 26 | #include <boost/lexical_cast.hpp> |
| 27 | |
| 28 | NS_LOG_COMPONENT_DEFINE ("ndn.test.fw.PerFibLimits"); |
| 29 | |
| 30 | namespace ns3 { |
| 31 | namespace ndn { |
| 32 | |
| 33 | void Decay (Ptr<fib::Entry> entry) |
| 34 | { |
| 35 | entry->GetLimits ().DecayCurrentLimit (); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | template<class T> |
| 40 | void |
| 41 | PrintTracedValue (std::string context, T oldValue, T newValue) |
| 42 | { |
| 43 | NS_LOG_DEBUG (context << ": " << |
| 44 | oldValue << " => " << newValue); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | Test1 (Ptr<fib::Entry> entry) |
| 49 | { |
| 50 | entry->GetLimits ().IsBelowLimit (); |
| 51 | entry->GetLimits ().DecreaseLimit (); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | Test2 (Ptr<fib::Entry> entry) |
| 56 | { |
| 57 | entry->GetLimits ().RemoveOutstanding (); |
| 58 | for (uint32_t i = 0; i < 40; i++) |
| 59 | entry->GetLimits ().IncreaseLimit (); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | FwPerFibLimits::CheckCurMaxLimit (Ptr<fib::Entry> entry, double amount) |
| 64 | { |
| 65 | NS_TEST_ASSERT_MSG_EQ_TOL ((double)entry->GetLimits ().m_curMaxLimit, amount, 0.1, ""); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | FwPerFibLimits::CheckOutstanding (Ptr<fib::Entry> entry, uint32_t amount) |
| 70 | { |
| 71 | NS_TEST_ASSERT_MSG_EQ ((double)entry->GetLimits ().m_outstanding, amount, ""); |
| 72 | } |
| 73 | |
| 74 | // void |
| 75 | // FwPerFibLimits::Check2 (Ptr<fib::Entry> entry) |
| 76 | // { |
| 77 | // NS_TEST_ASSERT_MSG_EQ ((double)entry->GetLimits ().m_outstanding, 0, ""); |
| 78 | // } |
| 79 | |
| 80 | void |
| 81 | FwPerFibLimits::DoRun () |
| 82 | { |
| 83 | Simulator::Destroy (); |
| 84 | |
| 85 | NodeContainer nodes; |
| 86 | nodes.Create (2); |
| 87 | |
| 88 | PointToPointHelper p2pHelper; |
| 89 | p2pHelper.Install (nodes); |
| 90 | |
| 91 | StackHelper ndn; |
| 92 | ndn.SetForwardingStrategy ("ns3::ndn::fw::PerFibLimits"); |
| 93 | ndn.Install (nodes); |
| 94 | |
| 95 | Ptr<Fib> fib = nodes.Get (0)->GetObject<Fib> (); |
| 96 | ndn.AddRoute (nodes.Get (0), "/bla", 0, 10); |
| 97 | |
| 98 | Ptr<fib::Entry> entry = fib->Begin (); |
| 99 | |
| 100 | bool ok = entry->GetLimits ().TraceConnect ("CurMaxLimit", "fibEntry.curMax", MakeCallback (PrintTracedValue<double>)); |
| 101 | NS_TEST_ASSERT_MSG_EQ (ok, true, ""); |
| 102 | |
| 103 | ok = entry->GetLimits ().TraceConnect ("Outstanding", "fibEntry.out", MakeCallback (PrintTracedValue<uint32_t>)); |
| 104 | NS_TEST_ASSERT_MSG_EQ (ok, true, ""); |
| 105 | |
| 106 | ok = nodes.Get (0)->GetObject<L3Protocol> ()->GetFace (0)->GetLimits ()->TraceConnect ("CurMaxLimit", "face.curMax", MakeCallback (PrintTracedValue<double>)); |
| 107 | NS_TEST_ASSERT_MSG_EQ (ok, true, ""); |
| 108 | ok = nodes.Get (0)->GetObject<L3Protocol> ()->GetFace (0)->GetLimits ()->TraceConnect ("Outstanding", "face.out", MakeCallback (PrintTracedValue<uint32_t>)); |
| 109 | NS_TEST_ASSERT_MSG_EQ (ok, true, ""); |
| 110 | |
| 111 | nodes.Get (0)->GetObject<L3Protocol> ()->GetFace (0)->GetLimits ()->SetMaxLimit (100); |
| 112 | |
| 113 | entry->GetLimits ().SetMaxLimit (100); |
| 114 | NS_TEST_ASSERT_MSG_EQ_TOL ((double)entry->GetLimits ().m_curMaxLimit, 100, 0.1, ""); |
| 115 | |
| 116 | entry->GetLimits ().DecreaseLimit (); |
| 117 | NS_TEST_ASSERT_MSG_EQ_TOL ((double)entry->GetLimits ().m_curMaxLimit, 50, 0.1, ""); |
| 118 | |
| 119 | entry = fib->Begin (); |
| 120 | |
| 121 | NS_LOG_DEBUG (entry); |
| 122 | Simulator::Schedule (Seconds (0.1), Decay, entry); |
| 123 | Simulator::Schedule (Seconds (25.0), Decay, entry); |
| 124 | Simulator::Schedule (Seconds (28.0), Decay, entry); |
| 125 | Simulator::Schedule (Seconds (40.0), Decay, entry); |
| 126 | Simulator::Schedule (Seconds (60.0), Decay, entry); |
| 127 | Simulator::Schedule (Seconds (100.0), Decay, entry); |
| 128 | |
| 129 | Simulator::Schedule (Seconds (100.1), &FwPerFibLimits::CheckCurMaxLimit, this, entry, 81.5); |
| 130 | |
| 131 | Simulator::Schedule (Seconds (100.5), &FwPerFibLimits::CheckOutstanding, this, entry, 0); |
| 132 | Simulator::Schedule (Seconds (101.0), Test1, entry); |
| 133 | Simulator::Schedule (Seconds (101.5), &FwPerFibLimits::CheckOutstanding, this, entry, 1); |
| 134 | Simulator::Schedule (Seconds (101.5), &FwPerFibLimits::CheckCurMaxLimit, this, entry, 40.75); |
| 135 | |
| 136 | Simulator::Schedule (Seconds (102.0), Test2, entry); |
| 137 | Simulator::Schedule (Seconds (102.5), &FwPerFibLimits::CheckOutstanding, this, entry, 0); |
| 138 | Simulator::Schedule (Seconds (102.5), &FwPerFibLimits::CheckCurMaxLimit, this, entry, 41.75); |
| 139 | |
| 140 | // Config::Connect ("/NodeList/*/$ns3::ndn::L3Protocol/FaceList/*/Limits/CurMaxLimit", MakeCallback (PrintTracedValue<double>)); |
| 141 | // Config::Connect ("/NodeList/*/$ns3::ndn::L3Protocol/FaceList/*/Limits/Outstanding", MakeCallback (PrintTracedValue<uint32_t>)); |
| 142 | |
| 143 | AppHelper consumer ("ns3::ndn::ConsumerBatches"); |
| 144 | consumer.SetPrefix ("/bla"); |
| 145 | consumer.SetAttribute ("Batches", StringValue ("105 1")); |
| 146 | consumer.SetAttribute ("LifeTime", StringValue ("1s")); |
| 147 | consumer.Install (nodes.Get (0)); |
| 148 | |
| 149 | Simulator::Stop (Seconds (110.0)); |
| 150 | Simulator::Run (); |
| 151 | |
| 152 | Simulator::Destroy (); |
| 153 | } |
| 154 | |
| 155 | } // namespace ndn |
| 156 | } // namespace ns3 |