Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
| 7 | * |
| 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
| 20 | #include "helper/ndn-strategy-choice-helper.hpp" |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 21 | |
| 22 | #include "../tests-common.hpp" |
| 23 | |
| 24 | namespace ns3 { |
| 25 | namespace ndn { |
| 26 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 27 | class StrategyChoiceHelperFixture : public ScenarioHelperWithCleanupFixture |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 28 | { |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 29 | public: |
| 30 | StrategyChoiceHelperFixture() |
| 31 | { |
| 32 | Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("10Mbps")); |
| 33 | Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("1ms")); |
| 34 | Config::SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("500")); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 36 | // Creating two 3 node topologies: // |
| 37 | // // |
| 38 | // +----+ +----+ // |
| 39 | // +- | B1 | +- | B2 | // |
| 40 | // / +----+ / +----+ // |
| 41 | // +----+ / +----+ / // |
| 42 | // | | --+ | | --+ // |
| 43 | // | A1 | | A2 | // |
| 44 | // | | --+ | | --+ // |
| 45 | // +----+ \ +----+ \ // |
| 46 | // \ +----+ \ +----+ // |
| 47 | // +- | C1 | +- | C2 | // |
| 48 | // +----+ +----+ // |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 50 | createTopology({ |
| 51 | {"A1", "B1"}, |
| 52 | {"A1", "C1"}, |
| 53 | {"A2", "B2"}, |
| 54 | {"A2", "C2"} |
| 55 | }); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 57 | addRoutes({ |
| 58 | {"A1", "B1", "/prefix", 200}, |
| 59 | {"A1", "C1", "/prefix", 100}, |
| 60 | {"A2", "B2", "/prefix", 100}, |
| 61 | {"A2", "C2", "/prefix", 200} |
| 62 | }); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 63 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 64 | addApps({ |
| 65 | {"A1", "ns3::ndn::ConsumerCbr", |
| 66 | {{"Prefix", "/prefix"}, {"Frequency", "1"}}, |
| 67 | "0s", "100s"}, |
| 68 | {"A2", "ns3::ndn::ConsumerCbr", |
| 69 | {{"Prefix", "/prefix"}, {"Frequency", "1"}}, |
| 70 | "0s", "100s"}, |
| 71 | }); |
| 72 | } |
| 73 | }; |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 74 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 75 | BOOST_FIXTURE_TEST_SUITE(TestStrategyChoiceHelper, StrategyChoiceHelperFixture) |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 76 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 77 | BOOST_AUTO_TEST_CASE(DefaultStrategies) |
| 78 | { |
| 79 | Simulator::Stop(Seconds(5.0)); |
| 80 | Simulator::Run(); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 0); |
| 83 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 5); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 84 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 5); |
| 86 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 0); |
| 87 | } |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 88 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 89 | // static void |
| 90 | // Install(Ptr<Node> node, const Name& namePrefix, const Name& strategy); |
| 91 | BOOST_AUTO_TEST_CASE(InstallBuiltInStrategyOnNode) |
| 92 | { |
| 93 | StrategyChoiceHelper::Install(getNode("A2"), "/prefix", "/localhost/nfd/strategy/broadcast"); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 94 | |
| 95 | Simulator::Stop(Seconds(5.0)); |
| 96 | Simulator::Run(); |
| 97 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 0); |
| 99 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 5); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 100 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 5); |
| 102 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 5); |
| 103 | } |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 105 | // static void |
| 106 | // Install(Ptr<Node> node, const Name& namePrefix, const Name& strategy); |
| 107 | BOOST_AUTO_TEST_CASE(InstallBuiltInStrategyOnNodeContainer) |
| 108 | { |
| 109 | NodeContainer nodes; |
| 110 | nodes.Add(getNode("A1")); |
| 111 | nodes.Add(getNode("A2")); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 112 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 113 | StrategyChoiceHelper::Install(nodes, "/prefix", "/localhost/nfd/strategy/broadcast"); |
| 114 | |
| 115 | Simulator::Stop(Seconds(5.0)); |
| 116 | Simulator::Run(); |
| 117 | |
| 118 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 5); |
| 119 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 5); |
| 120 | |
| 121 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 5); |
| 122 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 5); |
| 123 | } |
| 124 | |
| 125 | // static void |
| 126 | // InstallAll(const Name& namePrefix, const Name& strategy); |
| 127 | BOOST_AUTO_TEST_CASE(InstallAllBuiltInStrategy) |
| 128 | { |
| 129 | StrategyChoiceHelper::InstallAll("/prefix", "/localhost/nfd/strategy/broadcast"); |
| 130 | |
| 131 | Simulator::Stop(Seconds(5.0)); |
| 132 | Simulator::Run(); |
| 133 | |
| 134 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 5); |
| 135 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 5); |
| 136 | |
| 137 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 5); |
| 138 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 5); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | class NullStrategy : public nfd::fw::Strategy { |
| 143 | public: |
| 144 | NullStrategy(nfd::Forwarder& forwarder) |
| 145 | : Strategy(forwarder, STRATEGY_NAME) |
| 146 | { |
| 147 | } |
| 148 | |
| 149 | virtual void |
| 150 | afterReceiveInterest(const Face& inFace, const Interest& interest, |
| 151 | shared_ptr<nfd::fib::Entry> fibEntry, shared_ptr<nfd::pit::Entry> pitEntry) |
| 152 | { |
| 153 | // this strategy doesn't forward interests |
| 154 | } |
| 155 | |
| 156 | public: |
| 157 | static const Name STRATEGY_NAME; |
| 158 | }; |
| 159 | |
| 160 | const Name NullStrategy::STRATEGY_NAME = "ndn:/localhost/nfd/strategy/unit-tests/null-strategy"; |
| 161 | |
| 162 | // template<class Strategy> |
| 163 | // static void |
| 164 | // Install(Ptr<Node> node, const Name& namePrefix); |
| 165 | BOOST_AUTO_TEST_CASE(InstallCustomStrategyOnNode) |
| 166 | { |
| 167 | StrategyChoiceHelper::Install<NullStrategy>(getNode("A2"), "/prefix"); |
| 168 | |
| 169 | Simulator::Stop(Seconds(5.0)); |
| 170 | Simulator::Run(); |
| 171 | |
| 172 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 0); |
| 173 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 5); |
| 174 | |
| 175 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 0); |
| 176 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 0); |
| 177 | } |
| 178 | |
| 179 | // template<class Strategy> |
| 180 | // static void |
| 181 | // Install(const NodeContainer& c, const Name& namePrefix); |
| 182 | BOOST_AUTO_TEST_CASE(InstallCustomStrategyOnNodeContainer) |
| 183 | { |
| 184 | NodeContainer nodes; |
| 185 | nodes.Add(getNode("A1")); |
| 186 | nodes.Add(getNode("A2")); |
| 187 | |
| 188 | StrategyChoiceHelper::Install<NullStrategy>(nodes, "/prefix"); |
| 189 | |
| 190 | Simulator::Stop(Seconds(5.0)); |
| 191 | Simulator::Run(); |
| 192 | |
| 193 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 0); |
| 194 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 0); |
| 195 | |
| 196 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 0); |
| 197 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 0); |
| 198 | } |
| 199 | |
| 200 | // template<class Strategy> |
| 201 | // static void |
| 202 | // InstallAll(const Name& namePrefix); |
| 203 | BOOST_AUTO_TEST_CASE(InstallAllCustomStrategy) |
| 204 | { |
| 205 | StrategyChoiceHelper::InstallAll<NullStrategy>("/prefix"); |
| 206 | |
| 207 | Simulator::Stop(Seconds(5.0)); |
| 208 | Simulator::Run(); |
| 209 | |
| 210 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 0); |
| 211 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 0); |
| 212 | |
| 213 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 0); |
| 214 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 0); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | BOOST_AUTO_TEST_SUITE_END() |
| 218 | |
| 219 | } // namespace ndn |
| 220 | } // namespace ns3 |