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")); |
| 36 | Config::SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("500")); |
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"}}, |
| 69 | "0s", "100s"}, |
| 70 | {"A2", "ns3::ndn::ConsumerCbr", |
| 71 | {{"Prefix", "/prefix"}, {"Frequency", "1"}}, |
| 72 | "0s", "100s"}, |
| 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 | { |
| 81 | Simulator::Stop(Seconds(5.0)); |
| 82 | Simulator::Run(); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 83 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 0); |
| 85 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 5); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 5); |
| 88 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 0); |
| 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 | |
| 97 | Simulator::Stop(Seconds(5.0)); |
| 98 | Simulator::Run(); |
| 99 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 0); |
| 101 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 5); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | c512dba | 2015-08-13 16:51:26 -0700 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 5); |
| 104 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 5); |
| 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 | |
| 117 | Simulator::Stop(Seconds(5.0)); |
| 118 | Simulator::Run(); |
| 119 | |
| 120 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 5); |
| 121 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 5); |
| 122 | |
| 123 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 5); |
| 124 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 5); |
| 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 | |
| 133 | Simulator::Stop(Seconds(5.0)); |
| 134 | Simulator::Run(); |
| 135 | |
| 136 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 5); |
| 137 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 5); |
| 138 | |
| 139 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 5); |
| 140 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 5); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | class NullStrategy : public nfd::fw::Strategy { |
| 145 | public: |
| 146 | NullStrategy(nfd::Forwarder& forwarder) |
| 147 | : Strategy(forwarder, STRATEGY_NAME) |
| 148 | { |
| 149 | } |
| 150 | |
| 151 | virtual void |
| 152 | afterReceiveInterest(const Face& inFace, const Interest& interest, |
| 153 | shared_ptr<nfd::fib::Entry> fibEntry, shared_ptr<nfd::pit::Entry> pitEntry) |
| 154 | { |
| 155 | // this strategy doesn't forward interests |
| 156 | } |
| 157 | |
| 158 | public: |
| 159 | static const Name STRATEGY_NAME; |
| 160 | }; |
| 161 | |
| 162 | const Name NullStrategy::STRATEGY_NAME = "ndn:/localhost/nfd/strategy/unit-tests/null-strategy"; |
| 163 | |
| 164 | // template<class Strategy> |
| 165 | // static void |
| 166 | // Install(Ptr<Node> node, const Name& namePrefix); |
| 167 | BOOST_AUTO_TEST_CASE(InstallCustomStrategyOnNode) |
| 168 | { |
| 169 | StrategyChoiceHelper::Install<NullStrategy>(getNode("A2"), "/prefix"); |
| 170 | |
| 171 | Simulator::Stop(Seconds(5.0)); |
| 172 | Simulator::Run(); |
| 173 | |
| 174 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 0); |
| 175 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 5); |
| 176 | |
| 177 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 0); |
| 178 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 0); |
| 179 | } |
| 180 | |
| 181 | // template<class Strategy> |
| 182 | // static void |
| 183 | // Install(const NodeContainer& c, const Name& namePrefix); |
| 184 | BOOST_AUTO_TEST_CASE(InstallCustomStrategyOnNodeContainer) |
| 185 | { |
| 186 | NodeContainer nodes; |
| 187 | nodes.Add(getNode("A1")); |
| 188 | nodes.Add(getNode("A2")); |
| 189 | |
| 190 | StrategyChoiceHelper::Install<NullStrategy>(nodes, "/prefix"); |
| 191 | |
| 192 | Simulator::Stop(Seconds(5.0)); |
| 193 | Simulator::Run(); |
| 194 | |
| 195 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 0); |
| 196 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 0); |
| 197 | |
| 198 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 0); |
| 199 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 0); |
| 200 | } |
| 201 | |
| 202 | // template<class Strategy> |
| 203 | // static void |
| 204 | // InstallAll(const Name& namePrefix); |
| 205 | BOOST_AUTO_TEST_CASE(InstallAllCustomStrategy) |
| 206 | { |
| 207 | StrategyChoiceHelper::InstallAll<NullStrategy>("/prefix"); |
| 208 | |
| 209 | Simulator::Stop(Seconds(5.0)); |
| 210 | Simulator::Run(); |
| 211 | |
| 212 | BOOST_CHECK_EQUAL(getFace("A1", "B1")->getFaceStatus().getNOutInterests(), 0); |
| 213 | BOOST_CHECK_EQUAL(getFace("A1", "C1")->getFaceStatus().getNOutInterests(), 0); |
| 214 | |
| 215 | BOOST_CHECK_EQUAL(getFace("A2", "B2")->getFaceStatus().getNOutInterests(), 0); |
| 216 | BOOST_CHECK_EQUAL(getFace("A2", "C2")->getFaceStatus().getNOutInterests(), 0); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | BOOST_AUTO_TEST_SUITE_END() |
| 220 | |
| 221 | } // namespace ndn |
| 222 | } // namespace ns3 |