Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "topology-tester.hpp" |
| 27 | #include <ndn-cxx/encoding/encoding-buffer-fwd.hpp> |
| 28 | #include "face/generic-link-service.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace fw { |
| 32 | namespace tests { |
| 33 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 34 | using face::InternalTransportBase; |
| 35 | using face::InternalForwarderTransport; |
| 36 | using face::InternalClientTransport; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 37 | using face::LpFaceWrapper; |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 38 | using face::GenericLinkService; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 40 | TopologyLink::TopologyLink(const time::nanoseconds& delay) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 41 | : m_isUp(true) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 42 | , m_delay(delay) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 43 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 44 | BOOST_ASSERT(delay > time::nanoseconds::zero()); |
| 45 | // zero delay does not work on OSX |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 49 | TopologyLink::addFace(TopologyNode i, shared_ptr<LpFaceWrapper> face) |
| 50 | { |
| 51 | this->attachTransport(i, dynamic_cast<InternalTransportBase*>(face->getLpFace()->getTransport())); |
| 52 | m_faces[i] = face; |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | TopologyLink::attachTransport(TopologyNode i, InternalTransportBase* transport) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 57 | { |
| 58 | BOOST_ASSERT(transport != nullptr); |
| 59 | BOOST_ASSERT(m_transports.count(i) == 0); |
| 60 | |
| 61 | m_transports[i] = transport; |
| 62 | transport->afterSend.connect([this, i] (const Block& packet) { this->transmit(i, packet); }); |
| 63 | } |
| 64 | |
| 65 | void |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 66 | TopologyLink::transmit(TopologyNode i, const Block& packet) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 67 | { |
| 68 | if (!m_isUp) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | for (auto&& p : m_transports) { |
| 73 | if (p.first == i) { |
| 74 | continue; |
| 75 | } |
| 76 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 77 | InternalTransportBase* recipient = p.second; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 78 | this->scheduleReceive(recipient, packet); |
| 79 | } |
| 80 | } |
| 81 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 82 | void |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 83 | TopologyLink::scheduleReceive(InternalTransportBase* recipient, const Block& packet) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 84 | { |
| 85 | scheduler::schedule(m_delay, [packet, recipient] { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 86 | recipient->receiveFromLink(packet); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 87 | }); |
| 88 | } |
| 89 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 90 | TopologyAppLink::TopologyAppLink(shared_ptr<LpFaceWrapper> forwarderFace) |
| 91 | : m_face(forwarderFace) |
| 92 | , m_forwarderTransport(static_cast<InternalForwarderTransport*>(forwarderFace->getLpFace()->getTransport())) |
| 93 | , m_clientTransport(make_shared<InternalClientTransport>()) |
| 94 | , m_client(make_shared<ndn::Face>(m_clientTransport, getGlobalIoService())) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 95 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 96 | this->recover(); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 100 | TopologyAppLink::fail() |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 101 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 102 | m_clientTransport->connectToForwarder(nullptr); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | TopologyAppLink::recover() |
| 107 | { |
| 108 | m_clientTransport->connectToForwarder(m_forwarderTransport); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TopologyNode |
| 112 | TopologyTester::addForwarder(const std::string& label) |
| 113 | { |
| 114 | size_t i = m_forwarders.size(); |
| 115 | m_forwarders.push_back(std::move(make_unique<Forwarder>())); |
| 116 | m_forwarderLabels.push_back(label); |
| 117 | BOOST_ASSERT(m_forwarders.size() == m_forwarderLabels.size()); |
| 118 | return i; |
| 119 | } |
| 120 | |
| 121 | shared_ptr<TopologyLink> |
| 122 | TopologyTester::addLink(const std::string& label, const time::nanoseconds& delay, |
| 123 | std::initializer_list<TopologyNode> forwarders, |
| 124 | bool forceMultiAccessFace) |
| 125 | { |
| 126 | auto link = make_shared<TopologyLink>(delay); |
| 127 | FaceUri remoteUri("topology://link/" + label); |
| 128 | ndn::nfd::LinkType linkType = (forceMultiAccessFace || forwarders.size() > 2) ? |
| 129 | ndn::nfd::LINK_TYPE_MULTI_ACCESS : |
| 130 | ndn::nfd::LINK_TYPE_POINT_TO_POINT; |
| 131 | |
| 132 | for (TopologyNode i : forwarders) { |
| 133 | Forwarder& forwarder = this->getForwarder(i); |
| 134 | FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/" + label); |
| 135 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 136 | auto service = make_unique<GenericLinkService>(); |
| 137 | auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri, |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 138 | ndn::nfd::FACE_SCOPE_NON_LOCAL, linkType); |
| 139 | auto face = make_unique<LpFace>(std::move(service), std::move(transport)); |
| 140 | auto faceW = make_shared<LpFaceWrapper>(std::move(face)); |
| 141 | |
| 142 | forwarder.addFace(faceW); |
| 143 | link->addFace(i, faceW); |
| 144 | } |
| 145 | |
| 146 | m_links.push_back(link); // keep a shared_ptr so callers don't have to |
| 147 | return link; |
| 148 | } |
| 149 | |
| 150 | shared_ptr<TopologyAppLink> |
| 151 | TopologyTester::addAppFace(const std::string& label, TopologyNode i) |
| 152 | { |
| 153 | Forwarder& forwarder = this->getForwarder(i); |
| 154 | FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/local/" + label); |
| 155 | FaceUri remoteUri("topology://" + m_forwarderLabels.at(i) + "/app/" + label); |
| 156 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 157 | auto service = make_unique<GenericLinkService>(); |
| 158 | auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri, |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 159 | ndn::nfd::FACE_SCOPE_LOCAL, ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 160 | auto face = make_unique<LpFace>(std::move(service), std::move(transport)); |
| 161 | auto faceW = make_shared<LpFaceWrapper>(std::move(face)); |
| 162 | |
| 163 | forwarder.addFace(faceW); |
| 164 | |
| 165 | auto al = make_shared<TopologyAppLink>(faceW); |
| 166 | m_appLinks.push_back(al); // keep a shared_ptr so callers don't have to |
| 167 | return al; |
| 168 | } |
| 169 | |
| 170 | shared_ptr<TopologyAppLink> |
| 171 | TopologyTester::addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost) |
| 172 | { |
| 173 | shared_ptr<TopologyAppLink> al = this->addAppFace(label, i); |
| 174 | this->registerPrefix(i, al->getForwarderFace(), prefix, cost); |
| 175 | return al; |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | TopologyTester::registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost) |
| 180 | { |
| 181 | Forwarder& forwarder = this->getForwarder(i); |
| 182 | Fib& fib = forwarder.getFib(); |
| 183 | shared_ptr<fib::Entry> fibEntry = fib.insert(prefix).first; |
| 184 | fibEntry->addNextHop(const_cast<Face&>(face).shared_from_this(), cost); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | TopologyTester::addEchoProducer(ndn::Face& face, const Name& prefix) |
| 189 | { |
| 190 | face.setInterestFilter(prefix, |
| 191 | [&face] (const ndn::InterestFilter&, const Interest& interest) { |
| 192 | shared_ptr<Data> data = makeData(interest.getName()); |
| 193 | face.put(*data); |
| 194 | }); |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | TopologyTester::addIntervalConsumer(ndn::Face& face, const Name& prefix, |
| 199 | const time::nanoseconds& interval, size_t n) |
| 200 | { |
| 201 | Name name(prefix); |
| 202 | name.appendTimestamp(); |
| 203 | shared_ptr<Interest> interest = makeInterest(name); |
| 204 | face.expressInterest(*interest, bind([]{})); |
| 205 | |
| 206 | if (n > 1) { |
| 207 | scheduler::schedule(interval, bind(&TopologyTester::addIntervalConsumer, this, |
| 208 | ref(face), prefix, interval, n - 1)); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | } // namespace tests |
| 213 | } // namespace fw |
| 214 | } // namespace nfd |