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 | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 37 | using face::GenericLinkService; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 38 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 39 | TopologyLink::TopologyLink(const time::nanoseconds& delay) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 40 | : m_isUp(true) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 41 | , m_delay(delay) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 42 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 43 | BOOST_ASSERT(delay > time::nanoseconds::zero()); |
| 44 | // zero delay does not work on OSX |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 48 | TopologyLink::addFace(TopologyNode i, shared_ptr<Face> face) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 49 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 50 | this->attachTransport(i, dynamic_cast<InternalTransportBase*>(face->getTransport())); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 51 | m_faces[i] = face; |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | TopologyLink::attachTransport(TopologyNode i, InternalTransportBase* transport) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 56 | { |
| 57 | BOOST_ASSERT(transport != nullptr); |
| 58 | BOOST_ASSERT(m_transports.count(i) == 0); |
| 59 | |
| 60 | m_transports[i] = transport; |
| 61 | transport->afterSend.connect([this, i] (const Block& packet) { this->transmit(i, packet); }); |
| 62 | } |
| 63 | |
| 64 | void |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 65 | TopologyLink::transmit(TopologyNode i, const Block& packet) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 66 | { |
| 67 | if (!m_isUp) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | for (auto&& p : m_transports) { |
| 72 | if (p.first == i) { |
| 73 | continue; |
| 74 | } |
| 75 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 76 | InternalTransportBase* recipient = p.second; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 77 | this->scheduleReceive(recipient, packet); |
| 78 | } |
| 79 | } |
| 80 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 81 | void |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 82 | TopologyLink::scheduleReceive(InternalTransportBase* recipient, const Block& packet) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 83 | { |
| 84 | scheduler::schedule(m_delay, [packet, recipient] { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 85 | recipient->receiveFromLink(packet); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 86 | }); |
| 87 | } |
| 88 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 89 | TopologyAppLink::TopologyAppLink(shared_ptr<Face> forwarderFace) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 90 | : m_face(forwarderFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 91 | , m_forwarderTransport(static_cast<InternalForwarderTransport*>(forwarderFace->getTransport())) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 92 | , m_clientTransport(make_shared<InternalClientTransport>()) |
| 93 | , m_client(make_shared<ndn::Face>(m_clientTransport, getGlobalIoService())) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 94 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 95 | this->recover(); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 99 | TopologyAppLink::fail() |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 100 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 101 | m_clientTransport->connectToForwarder(nullptr); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | TopologyAppLink::recover() |
| 106 | { |
| 107 | m_clientTransport->connectToForwarder(m_forwarderTransport); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TopologyNode |
| 111 | TopologyTester::addForwarder(const std::string& label) |
| 112 | { |
| 113 | size_t i = m_forwarders.size(); |
| 114 | m_forwarders.push_back(std::move(make_unique<Forwarder>())); |
| 115 | m_forwarderLabels.push_back(label); |
| 116 | BOOST_ASSERT(m_forwarders.size() == m_forwarderLabels.size()); |
| 117 | return i; |
| 118 | } |
| 119 | |
| 120 | shared_ptr<TopologyLink> |
| 121 | TopologyTester::addLink(const std::string& label, const time::nanoseconds& delay, |
| 122 | std::initializer_list<TopologyNode> forwarders, |
| 123 | bool forceMultiAccessFace) |
| 124 | { |
| 125 | auto link = make_shared<TopologyLink>(delay); |
| 126 | FaceUri remoteUri("topology://link/" + label); |
| 127 | ndn::nfd::LinkType linkType = (forceMultiAccessFace || forwarders.size() > 2) ? |
| 128 | ndn::nfd::LINK_TYPE_MULTI_ACCESS : |
| 129 | ndn::nfd::LINK_TYPE_POINT_TO_POINT; |
| 130 | |
| 131 | for (TopologyNode i : forwarders) { |
| 132 | Forwarder& forwarder = this->getForwarder(i); |
| 133 | FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/" + label); |
| 134 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 135 | auto service = make_unique<GenericLinkService>(); |
| 136 | auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri, |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 137 | ndn::nfd::FACE_SCOPE_NON_LOCAL, linkType); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 138 | auto face = make_shared<Face>(std::move(service), std::move(transport)); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 139 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 140 | forwarder.addFace(face); |
| 141 | link->addFace(i, face); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | m_links.push_back(link); // keep a shared_ptr so callers don't have to |
| 145 | return link; |
| 146 | } |
| 147 | |
| 148 | shared_ptr<TopologyAppLink> |
| 149 | TopologyTester::addAppFace(const std::string& label, TopologyNode i) |
| 150 | { |
| 151 | Forwarder& forwarder = this->getForwarder(i); |
| 152 | FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/local/" + label); |
| 153 | FaceUri remoteUri("topology://" + m_forwarderLabels.at(i) + "/app/" + label); |
| 154 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 155 | auto service = make_unique<GenericLinkService>(); |
| 156 | auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri, |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 157 | ndn::nfd::FACE_SCOPE_LOCAL, ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 158 | auto face = make_shared<Face>(std::move(service), std::move(transport)); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 159 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 160 | forwarder.addFace(face); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 161 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 162 | auto al = make_shared<TopologyAppLink>(face); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 163 | m_appLinks.push_back(al); // keep a shared_ptr so callers don't have to |
| 164 | return al; |
| 165 | } |
| 166 | |
| 167 | shared_ptr<TopologyAppLink> |
| 168 | TopologyTester::addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost) |
| 169 | { |
| 170 | shared_ptr<TopologyAppLink> al = this->addAppFace(label, i); |
| 171 | this->registerPrefix(i, al->getForwarderFace(), prefix, cost); |
| 172 | return al; |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | TopologyTester::registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost) |
| 177 | { |
| 178 | Forwarder& forwarder = this->getForwarder(i); |
| 179 | Fib& fib = forwarder.getFib(); |
| 180 | shared_ptr<fib::Entry> fibEntry = fib.insert(prefix).first; |
| 181 | fibEntry->addNextHop(const_cast<Face&>(face).shared_from_this(), cost); |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | TopologyTester::addEchoProducer(ndn::Face& face, const Name& prefix) |
| 186 | { |
| 187 | face.setInterestFilter(prefix, |
| 188 | [&face] (const ndn::InterestFilter&, const Interest& interest) { |
| 189 | shared_ptr<Data> data = makeData(interest.getName()); |
| 190 | face.put(*data); |
| 191 | }); |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | TopologyTester::addIntervalConsumer(ndn::Face& face, const Name& prefix, |
| 196 | const time::nanoseconds& interval, size_t n) |
| 197 | { |
| 198 | Name name(prefix); |
| 199 | name.appendTimestamp(); |
| 200 | shared_ptr<Interest> interest = makeInterest(name); |
| 201 | face.expressInterest(*interest, bind([]{})); |
| 202 | |
| 203 | if (n > 1) { |
| 204 | scheduler::schedule(interval, bind(&TopologyTester::addIntervalConsumer, this, |
| 205 | ref(face), prefix, interval, n - 1)); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | } // namespace tests |
| 210 | } // namespace fw |
| 211 | } // namespace nfd |