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 | |
Alexander Afanasyev | c3c7f04 | 2015-08-21 11:38:00 -0700 | [diff] [blame] | 22 | #include "ns3/ndnSIM/NFD/daemon/fw/strategy.hpp" |
| 23 | |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 24 | #include "../tests-common.hpp" |
| 25 | |
| 26 | namespace ns3 { |
| 27 | namespace ndn { |
| 28 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 29 | class StrategyChoiceHelperFixture : public ScenarioHelperWithCleanupFixture |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 30 | { |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 31 | public: |
| 32 | StrategyChoiceHelperFixture() |
| 33 | { |
| 34 | Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("10Mbps")); |
| 35 | Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("1ms")); |
Alexander Afanasyev | 77f84c6 | 2018-10-08 13:37:42 -0400 | [diff] [blame] | 36 | Config::SetDefault("ns3::QueueBase::MaxSize", StringValue("500p")); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 37 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 38 | // Creating two 3 node topologies: // |
| 39 | // // |
| 40 | // +----+ +----+ // |
| 41 | // +- | B1 | +- | B2 | // |
| 42 | // / +----+ / +----+ // |
| 43 | // +----+ / +----+ / // |
| 44 | // | | --+ | | --+ // |
| 45 | // | A1 | | A2 | // |
| 46 | // | | --+ | | --+ // |
| 47 | // +----+ \ +----+ \ // |
| 48 | // \ +----+ \ +----+ // |
| 49 | // +- | C1 | +- | C2 | // |
| 50 | // +----+ +----+ // |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 52 | createTopology({ |
| 53 | {"A1", "B1"}, |
| 54 | {"A1", "C1"}, |
| 55 | {"A2", "B2"}, |
| 56 | {"A2", "C2"} |
| 57 | }); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 59 | addRoutes({ |
| 60 | {"A1", "B1", "/prefix", 200}, |
| 61 | {"A1", "C1", "/prefix", 100}, |
| 62 | {"A2", "B2", "/prefix", 100}, |
| 63 | {"A2", "C2", "/prefix", 200} |
| 64 | }); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 66 | addApps({ |
| 67 | {"A1", "ns3::ndn::ConsumerCbr", |
| 68 | {{"Prefix", "/prefix"}, {"Frequency", "1"}}, |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 69 | "0.1s", "100s"}, |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 70 | {"A2", "ns3::ndn::ConsumerCbr", |
| 71 | {{"Prefix", "/prefix"}, {"Frequency", "1"}}, |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 72 | "0.1s", "100s"}, |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 73 | }); |
| 74 | } |
| 75 | }; |
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_FIXTURE_TEST_SUITE(TestStrategyChoiceHelper, StrategyChoiceHelperFixture) |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 79 | BOOST_AUTO_TEST_CASE(DefaultStrategies) |
| 80 | { |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 81 | Simulator::Stop(Seconds(5)); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 82 | Simulator::Run(); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 83 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0); |
| 85 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 5); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 5); |
| 88 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 0); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 89 | } |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 91 | // static void |
| 92 | // Install(Ptr<Node> node, const Name& namePrefix, const Name& strategy); |
| 93 | BOOST_AUTO_TEST_CASE(InstallBuiltInStrategyOnNode) |
| 94 | { |
Alexander Afanasyev | c3c7f04 | 2015-08-21 11:38:00 -0700 | [diff] [blame] | 95 | StrategyChoiceHelper::Install(getNode("A2"), "/prefix", "/localhost/nfd/strategy/multicast"); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 97 | Simulator::Stop(Seconds(5)); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 98 | Simulator::Run(); |
| 99 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0); |
| 101 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 5); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 5); |
| 104 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 5); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 105 | } |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 106 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 107 | // static void |
| 108 | // Install(Ptr<Node> node, const Name& namePrefix, const Name& strategy); |
| 109 | BOOST_AUTO_TEST_CASE(InstallBuiltInStrategyOnNodeContainer) |
| 110 | { |
| 111 | NodeContainer nodes; |
| 112 | nodes.Add(getNode("A1")); |
| 113 | nodes.Add(getNode("A2")); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 114 | |
Alexander Afanasyev | c3c7f04 | 2015-08-21 11:38:00 -0700 | [diff] [blame] | 115 | StrategyChoiceHelper::Install(nodes, "/prefix", "/localhost/nfd/strategy/multicast"); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 116 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 117 | Simulator::Stop(Seconds(5)); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 118 | Simulator::Run(); |
| 119 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 5); |
| 121 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 5); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 122 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 5); |
| 124 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 5); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // static void |
| 128 | // InstallAll(const Name& namePrefix, const Name& strategy); |
| 129 | BOOST_AUTO_TEST_CASE(InstallAllBuiltInStrategy) |
| 130 | { |
Alexander Afanasyev | c3c7f04 | 2015-08-21 11:38:00 -0700 | [diff] [blame] | 131 | StrategyChoiceHelper::InstallAll("/prefix", "/localhost/nfd/strategy/multicast"); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 132 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 133 | Simulator::Stop(Seconds(5)); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 134 | Simulator::Run(); |
| 135 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 5); |
| 137 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 5); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 138 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 139 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 5); |
| 140 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 5); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | class NullStrategy : public nfd::fw::Strategy { |
| 145 | public: |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 146 | NullStrategy(nfd::Forwarder& forwarder, const Name& name = getStrategyName()) |
| 147 | : Strategy(forwarder) |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 148 | { |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 149 | this->setInstanceName(name); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | virtual void |
| 153 | afterReceiveInterest(const Face& inFace, const Interest& interest, |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame] | 154 | const shared_ptr<nfd::pit::Entry>& pitEntry) |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 155 | { |
| 156 | // this strategy doesn't forward interests |
| 157 | } |
| 158 | |
| 159 | public: |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 160 | static const Name& getStrategyName() |
| 161 | { |
| 162 | static Name strategyName("ndn:/localhost/nfd/strategy/unit-tests/null-strategy/%FD%00"); |
| 163 | return strategyName; |
| 164 | } |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 167 | // template<class Strategy> |
| 168 | // static void |
| 169 | // Install(Ptr<Node> node, const Name& namePrefix); |
| 170 | BOOST_AUTO_TEST_CASE(InstallCustomStrategyOnNode) |
| 171 | { |
| 172 | StrategyChoiceHelper::Install<NullStrategy>(getNode("A2"), "/prefix"); |
| 173 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 174 | Simulator::Stop(Seconds(5)); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 175 | Simulator::Run(); |
| 176 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 177 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0); |
| 178 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 5); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 179 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 180 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 0); |
| 181 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 0); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // template<class Strategy> |
| 185 | // static void |
| 186 | // Install(const NodeContainer& c, const Name& namePrefix); |
| 187 | BOOST_AUTO_TEST_CASE(InstallCustomStrategyOnNodeContainer) |
| 188 | { |
| 189 | NodeContainer nodes; |
| 190 | nodes.Add(getNode("A1")); |
| 191 | nodes.Add(getNode("A2")); |
| 192 | |
| 193 | StrategyChoiceHelper::Install<NullStrategy>(nodes, "/prefix"); |
| 194 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 195 | Simulator::Stop(Seconds(5)); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 196 | Simulator::Run(); |
| 197 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 198 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0); |
| 199 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 0); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 200 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 201 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 0); |
| 202 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 0); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // template<class Strategy> |
| 206 | // static void |
| 207 | // InstallAll(const Name& namePrefix); |
| 208 | BOOST_AUTO_TEST_CASE(InstallAllCustomStrategy) |
| 209 | { |
| 210 | StrategyChoiceHelper::InstallAll<NullStrategy>("/prefix"); |
| 211 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 212 | Simulator::Stop(Seconds(5)); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 213 | Simulator::Run(); |
| 214 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 215 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0); |
| 216 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 0); |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 217 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 218 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 0); |
| 219 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 0); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | BOOST_AUTO_TEST_SUITE_END() |
| 223 | |
| 224 | } // namespace ndn |
| 225 | } // namespace ns3 |