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 | |
| 34 | using face::LpFaceWrapper; |
| 35 | |
| 36 | TopologyForwarderTransport::TopologyForwarderTransport( |
| 37 | const FaceUri& localUri, const FaceUri& remoteUri, |
| 38 | ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType) |
| 39 | { |
| 40 | this->setLocalUri(localUri); |
| 41 | this->setRemoteUri(remoteUri); |
| 42 | this->setScope(scope); |
| 43 | this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 44 | this->setLinkType(linkType); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | TopologyForwarderTransport::receiveFromTopology(const Block& packet) |
| 49 | { |
| 50 | Packet p; |
| 51 | p.packet = packet; |
| 52 | this->receive(std::move(p)); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | TopologyForwarderTransport::doSend(Packet&& packet) |
| 57 | { |
| 58 | this->emitSignal(afterSend, packet.packet); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | TopologyClientTransport::receiveFromTopology(const Block& packet) |
| 63 | { |
| 64 | if (m_receiveCallback) { |
| 65 | m_receiveCallback(packet); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | TopologyClientTransport::send(const Block& wire) |
| 71 | { |
| 72 | this->emitSignal(afterSend, wire); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | TopologyClientTransport::send(const Block& header, const Block& payload) |
| 77 | { |
| 78 | ndn::EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size()); |
| 79 | encoder.appendByteArray(header.wire(), header.size()); |
| 80 | encoder.appendByteArray(payload.wire(), payload.size()); |
| 81 | |
| 82 | this->send(encoder.block()); |
| 83 | } |
| 84 | |
| 85 | TopologyLinkBase::TopologyLinkBase() |
| 86 | : m_isUp(true) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | TopologyLinkBase::attachTransport(TopologyNode i, TopologyTransportBase* transport) |
| 92 | { |
| 93 | BOOST_ASSERT(transport != nullptr); |
| 94 | BOOST_ASSERT(m_transports.count(i) == 0); |
| 95 | |
| 96 | m_transports[i] = transport; |
| 97 | transport->afterSend.connect([this, i] (const Block& packet) { this->transmit(i, packet); }); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | TopologyLinkBase::transmit(TopologyNode i, const Block& packet) |
| 102 | { |
| 103 | if (!m_isUp) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | for (auto&& p : m_transports) { |
| 108 | if (p.first == i) { |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | TopologyTransportBase* recipient = p.second; |
| 113 | this->scheduleReceive(recipient, packet); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | TopologyLink::TopologyLink(const time::nanoseconds& delay) |
| 118 | : m_delay(delay) |
| 119 | { |
| 120 | BOOST_ASSERT(delay > time::nanoseconds::zero()); |
| 121 | // zero delay does not work on OSX |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | TopologyLink::addFace(TopologyNode i, shared_ptr<LpFaceWrapper> face) |
| 126 | { |
| 127 | this->attachTransport(i, dynamic_cast<TopologyTransportBase*>(face->getLpFace()->getTransport())); |
| 128 | m_faces[i] = face; |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | TopologyLink::scheduleReceive(TopologyTransportBase* recipient, const Block& packet) |
| 133 | { |
| 134 | scheduler::schedule(m_delay, [packet, recipient] { |
| 135 | recipient->receiveFromTopology(packet); |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | TopologyAppLink::TopologyAppLink(shared_ptr<LpFaceWrapper> face) |
| 140 | : m_face(face) |
| 141 | { |
| 142 | this->attachTransport(0, dynamic_cast<TopologyTransportBase*>(face->getLpFace()->getTransport())); |
| 143 | |
| 144 | auto clientTransport = make_shared<TopologyClientTransport>(); |
| 145 | m_client = make_shared<ndn::Face>(clientTransport, getGlobalIoService()); |
| 146 | this->attachTransport(1, clientTransport.get()); |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | TopologyAppLink::scheduleReceive(TopologyTransportBase* recipient, const Block& packet) |
| 151 | { |
| 152 | getGlobalIoService().post([packet, recipient] { |
| 153 | recipient->receiveFromTopology(packet); |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | TopologyNode |
| 158 | TopologyTester::addForwarder(const std::string& label) |
| 159 | { |
| 160 | size_t i = m_forwarders.size(); |
| 161 | m_forwarders.push_back(std::move(make_unique<Forwarder>())); |
| 162 | m_forwarderLabels.push_back(label); |
| 163 | BOOST_ASSERT(m_forwarders.size() == m_forwarderLabels.size()); |
| 164 | return i; |
| 165 | } |
| 166 | |
| 167 | shared_ptr<TopologyLink> |
| 168 | TopologyTester::addLink(const std::string& label, const time::nanoseconds& delay, |
| 169 | std::initializer_list<TopologyNode> forwarders, |
| 170 | bool forceMultiAccessFace) |
| 171 | { |
| 172 | auto link = make_shared<TopologyLink>(delay); |
| 173 | FaceUri remoteUri("topology://link/" + label); |
| 174 | ndn::nfd::LinkType linkType = (forceMultiAccessFace || forwarders.size() > 2) ? |
| 175 | ndn::nfd::LINK_TYPE_MULTI_ACCESS : |
| 176 | ndn::nfd::LINK_TYPE_POINT_TO_POINT; |
| 177 | |
| 178 | for (TopologyNode i : forwarders) { |
| 179 | Forwarder& forwarder = this->getForwarder(i); |
| 180 | FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/" + label); |
| 181 | |
| 182 | auto service = make_unique<face::GenericLinkService>(); |
| 183 | auto transport = make_unique<TopologyForwarderTransport>(localUri, remoteUri, |
| 184 | ndn::nfd::FACE_SCOPE_NON_LOCAL, linkType); |
| 185 | auto face = make_unique<LpFace>(std::move(service), std::move(transport)); |
| 186 | auto faceW = make_shared<LpFaceWrapper>(std::move(face)); |
| 187 | |
| 188 | forwarder.addFace(faceW); |
| 189 | link->addFace(i, faceW); |
| 190 | } |
| 191 | |
| 192 | m_links.push_back(link); // keep a shared_ptr so callers don't have to |
| 193 | return link; |
| 194 | } |
| 195 | |
| 196 | shared_ptr<TopologyAppLink> |
| 197 | TopologyTester::addAppFace(const std::string& label, TopologyNode i) |
| 198 | { |
| 199 | Forwarder& forwarder = this->getForwarder(i); |
| 200 | FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/local/" + label); |
| 201 | FaceUri remoteUri("topology://" + m_forwarderLabels.at(i) + "/app/" + label); |
| 202 | |
| 203 | auto service = make_unique<face::GenericLinkService>(); |
| 204 | auto transport = make_unique<TopologyForwarderTransport>(localUri, remoteUri, |
| 205 | ndn::nfd::FACE_SCOPE_LOCAL, ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 206 | auto face = make_unique<LpFace>(std::move(service), std::move(transport)); |
| 207 | auto faceW = make_shared<LpFaceWrapper>(std::move(face)); |
| 208 | |
| 209 | forwarder.addFace(faceW); |
| 210 | |
| 211 | auto al = make_shared<TopologyAppLink>(faceW); |
| 212 | m_appLinks.push_back(al); // keep a shared_ptr so callers don't have to |
| 213 | return al; |
| 214 | } |
| 215 | |
| 216 | shared_ptr<TopologyAppLink> |
| 217 | TopologyTester::addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost) |
| 218 | { |
| 219 | shared_ptr<TopologyAppLink> al = this->addAppFace(label, i); |
| 220 | this->registerPrefix(i, al->getForwarderFace(), prefix, cost); |
| 221 | return al; |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | TopologyTester::registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost) |
| 226 | { |
| 227 | Forwarder& forwarder = this->getForwarder(i); |
| 228 | Fib& fib = forwarder.getFib(); |
| 229 | shared_ptr<fib::Entry> fibEntry = fib.insert(prefix).first; |
| 230 | fibEntry->addNextHop(const_cast<Face&>(face).shared_from_this(), cost); |
| 231 | } |
| 232 | |
| 233 | void |
| 234 | TopologyTester::addEchoProducer(ndn::Face& face, const Name& prefix) |
| 235 | { |
| 236 | face.setInterestFilter(prefix, |
| 237 | [&face] (const ndn::InterestFilter&, const Interest& interest) { |
| 238 | shared_ptr<Data> data = makeData(interest.getName()); |
| 239 | face.put(*data); |
| 240 | }); |
| 241 | } |
| 242 | |
| 243 | void |
| 244 | TopologyTester::addIntervalConsumer(ndn::Face& face, const Name& prefix, |
| 245 | const time::nanoseconds& interval, size_t n) |
| 246 | { |
| 247 | Name name(prefix); |
| 248 | name.appendTimestamp(); |
| 249 | shared_ptr<Interest> interest = makeInterest(name); |
| 250 | face.expressInterest(*interest, bind([]{})); |
| 251 | |
| 252 | if (n > 1) { |
| 253 | scheduler::schedule(interval, bind(&TopologyTester::addIntervalConsumer, this, |
| 254 | ref(face), prefix, interval, n - 1)); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | } // namespace tests |
| 259 | } // namespace fw |
| 260 | } // namespace nfd |