blob: 8b94abc7c49790e395b3eabc404f02c30428a985 [file] [log] [blame]
Junxiao Shi5e5e4452015-09-24 16:56:52 -07001/* -*- 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
30namespace nfd {
31namespace fw {
32namespace tests {
33
Junxiao Shi6535f1e2015-10-08 13:02:18 -070034using face::InternalTransportBase;
35using face::InternalForwarderTransport;
36using face::InternalClientTransport;
Junxiao Shi5e5e4452015-09-24 16:56:52 -070037using face::LpFaceWrapper;
Junxiao Shi6535f1e2015-10-08 13:02:18 -070038using face::GenericLinkService;
Junxiao Shi5e5e4452015-09-24 16:56:52 -070039
Junxiao Shi6535f1e2015-10-08 13:02:18 -070040TopologyLink::TopologyLink(const time::nanoseconds& delay)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070041 : m_isUp(true)
Junxiao Shi6535f1e2015-10-08 13:02:18 -070042 , m_delay(delay)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070043{
Junxiao Shi6535f1e2015-10-08 13:02:18 -070044 BOOST_ASSERT(delay > time::nanoseconds::zero());
45 // zero delay does not work on OSX
Junxiao Shi5e5e4452015-09-24 16:56:52 -070046}
47
48void
Junxiao Shi6535f1e2015-10-08 13:02:18 -070049TopologyLink::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
55void
56TopologyLink::attachTransport(TopologyNode i, InternalTransportBase* transport)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070057{
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
65void
Junxiao Shi6535f1e2015-10-08 13:02:18 -070066TopologyLink::transmit(TopologyNode i, const Block& packet)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070067{
68 if (!m_isUp) {
69 return;
70 }
71
72 for (auto&& p : m_transports) {
73 if (p.first == i) {
74 continue;
75 }
76
Junxiao Shi6535f1e2015-10-08 13:02:18 -070077 InternalTransportBase* recipient = p.second;
Junxiao Shi5e5e4452015-09-24 16:56:52 -070078 this->scheduleReceive(recipient, packet);
79 }
80}
81
Junxiao Shi5e5e4452015-09-24 16:56:52 -070082void
Junxiao Shi6535f1e2015-10-08 13:02:18 -070083TopologyLink::scheduleReceive(InternalTransportBase* recipient, const Block& packet)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070084{
85 scheduler::schedule(m_delay, [packet, recipient] {
Junxiao Shi6535f1e2015-10-08 13:02:18 -070086 recipient->receiveFromLink(packet);
Junxiao Shi5e5e4452015-09-24 16:56:52 -070087 });
88}
89
Junxiao Shi6535f1e2015-10-08 13:02:18 -070090TopologyAppLink::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 Shi5e5e4452015-09-24 16:56:52 -070095{
Junxiao Shi6535f1e2015-10-08 13:02:18 -070096 this->recover();
Junxiao Shi5e5e4452015-09-24 16:56:52 -070097}
98
99void
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700100TopologyAppLink::fail()
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700101{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700102 m_clientTransport->connectToForwarder(nullptr);
103}
104
105void
106TopologyAppLink::recover()
107{
108 m_clientTransport->connectToForwarder(m_forwarderTransport);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700109}
110
111TopologyNode
112TopologyTester::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
121shared_ptr<TopologyLink>
122TopologyTester::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 Shi6535f1e2015-10-08 13:02:18 -0700136 auto service = make_unique<GenericLinkService>();
137 auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700138 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
150shared_ptr<TopologyAppLink>
151TopologyTester::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 Shi6535f1e2015-10-08 13:02:18 -0700157 auto service = make_unique<GenericLinkService>();
158 auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700159 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
170shared_ptr<TopologyAppLink>
171TopologyTester::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
178void
179TopologyTester::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
187void
188TopologyTester::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
197void
198TopologyTester::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