Klaus Schneider | 3878430 | 2019-08-31 18:26:36 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2019 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 "ns3/command-line.h" |
| 21 | #include "ns3/double.h" |
| 22 | #include "ns3/names.h" |
| 23 | #include "ns3/point-to-point-channel.h" |
| 24 | #include "ns3/uinteger.h" |
| 25 | |
| 26 | #include "ns3/ndnSIM/helper/ndn-app-helper.hpp" |
| 27 | #include "ns3/ndnSIM/helper/ndn-global-routing-helper.hpp" |
| 28 | #include "ns3/ndnSIM/helper/ndn-stack-helper.hpp" |
| 29 | #include "ns3/ndnSIM/helper/ndn-strategy-choice-helper.hpp" |
| 30 | #include "ns3/ndnSIM/model/ndn-l3-protocol.hpp" |
| 31 | #include "ns3/ndnSIM/model/ndn-net-device-transport.hpp" |
| 32 | //#include "ns3/ndnSIM/NFD/daemon/fw/random-strategy.hpp" |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame^] | 33 | #include "ns3/ndnSIM/NFD/daemon/fw/best-route-strategy.hpp" |
Klaus Schneider | 3878430 | 2019-08-31 18:26:36 -0700 | [diff] [blame] | 34 | #include "ns3/ndnSIM/utils/topology/annotated-topology-reader.hpp" |
| 35 | |
| 36 | namespace ns3 { |
| 37 | |
| 38 | void |
| 39 | displayRoutes(const NodeContainer& allNodes, const std::string& prefix) |
| 40 | { |
| 41 | for (const auto& n : allNodes) { |
| 42 | const auto& fib = n->GetObject<ndn::L3Protocol>()->getForwarder()->getFib(); |
| 43 | const auto& e = fib.findLongestPrefixMatch(prefix); |
| 44 | |
| 45 | std::cout << "Node " << n->GetId() << ", prefix: " << prefix << "\n"; |
| 46 | |
| 47 | for (const auto& nh : e.getNextHops()) { |
| 48 | // Get remote nodeId from face: |
| 49 | const auto transport = dynamic_cast<ndn::NetDeviceTransport*>(nh.getFace().getTransport()); |
| 50 | if (transport == nullptr) |
| 51 | continue; |
| 52 | |
| 53 | const auto nd1 = transport->GetNetDevice()->GetObject<PointToPointNetDevice>(); |
| 54 | if (nd1 == nullptr) |
| 55 | continue; |
| 56 | |
| 57 | const auto ppChannel = DynamicCast<PointToPointChannel>(nd1->GetChannel()); |
| 58 | if (ppChannel == nullptr) |
| 59 | continue; |
| 60 | |
| 61 | auto nd2 = ppChannel->GetDevice(0); |
| 62 | if (nd2->GetNode() == n) |
| 63 | nd2 = ppChannel->GetDevice(1); |
| 64 | |
| 65 | std::cout << " NextHop: " << nd2->GetNode()->GetId() << ", cost: " << nh.getCost() << "\n"; |
| 66 | } |
| 67 | std::cout << "\n"; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | int |
| 72 | main(int argc, char* argv[]) |
| 73 | { |
| 74 | bool grid = false; // Use grid topology? |
| 75 | std::string routing = "lfid"; |
| 76 | CommandLine cmd; |
| 77 | cmd.AddValue("grid", "use grid topology (instead of abilene)", grid); |
| 78 | cmd.AddValue("routing", "which route computation to use (lfid, sp, allroutes)", routing); |
| 79 | cmd.Parse(argc, argv); |
| 80 | |
| 81 | std::string topoName = "abilene"; |
| 82 | if (grid) { |
| 83 | topoName = "grid"; |
| 84 | } |
| 85 | |
| 86 | std::cout << "Using " << topoName << " topology\n\n"; |
| 87 | |
| 88 | AnnotatedTopologyReader topologyReader{}; |
| 89 | topologyReader.SetFileName("src/ndnSIM/examples/topologies/topo-" + topoName + ".txt"); |
| 90 | topologyReader.Read(); |
| 91 | |
| 92 | ndn::StackHelper stackHelper{}; |
| 93 | stackHelper.InstallAll(); |
| 94 | |
| 95 | // IMPORTANT: Has to be run after StackHelper! |
| 96 | topologyReader.ApplyOspfMetric(); |
| 97 | |
| 98 | const std::string prefix{"/prefix"}; |
| 99 | |
| 100 | Ptr<Node> consumerN = Names::Find<Node>("router0"); |
| 101 | Ptr<Node> producerN = Names::Find<Node>("producer"); |
| 102 | NS_ABORT_MSG_UNLESS(consumerN && producerN, "consumer or producer name does not exist in topo file!"); |
| 103 | |
| 104 | ndn::GlobalRoutingHelper routingHelper; |
| 105 | routingHelper.InstallAll(); // Fills GlobalRouter with incidencies. |
| 106 | routingHelper.AddOrigin(prefix, producerN); |
| 107 | |
| 108 | if (routing == "lfid") { |
| 109 | routingHelper.CalculateLfidRoutes(); |
| 110 | } |
| 111 | else if (routing == "sp") { |
| 112 | routingHelper.CalculateRoutes(); |
| 113 | } |
| 114 | else if (routing == "allroutes") { |
| 115 | routingHelper.CalculateAllPossibleRoutes(); |
| 116 | } |
| 117 | else { |
| 118 | NS_FATAL_ERROR("Unknown route calculation! Use --routing {lfid|sp|allroutes}"); |
| 119 | } |
| 120 | |
| 121 | // IMPORTANT: Some strategy needs to be installed for displayRoutes() to work. |
| 122 | ndn::StrategyChoiceHelper strategyHelper; |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame^] | 123 | strategyHelper.InstallAll<nfd::fw::BestRouteStrategy>("/"); |
Klaus Schneider | 3878430 | 2019-08-31 18:26:36 -0700 | [diff] [blame] | 124 | |
| 125 | // TODO: Needs RandomStrategy for test to work! |
| 126 | // Uncomment after NFD version has been updated. |
| 127 | // strategyHelper.InstallAll<nfd::fw::RandomStrategy>("/"); |
| 128 | |
| 129 | displayRoutes(topologyReader.GetNodes(), prefix); |
| 130 | |
| 131 | // Installing applications |
| 132 | ndn::AppHelper consumerHelperX{"ns3::ndn::ConsumerCbr"}; |
| 133 | consumerHelperX.SetPrefix(prefix); |
| 134 | consumerHelperX.SetAttribute("Frequency", DoubleValue(100.0)); |
| 135 | consumerHelperX.Install(consumerN); |
| 136 | |
| 137 | ndn::AppHelper producerHelper0{"ns3::ndn::Producer"}; |
| 138 | producerHelper0.SetPrefix(prefix); |
| 139 | producerHelper0.Install(producerN); |
| 140 | |
| 141 | Simulator::Stop(Seconds(30)); |
| 142 | Simulator::Run(); |
| 143 | Simulator::Destroy(); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | } // namespace ns3 |
| 149 | |
| 150 | int |
| 151 | main(int argc, char* argv[]) |
| 152 | { |
| 153 | return ns3::main(argc, argv); |
| 154 | } |