blob: 9f9383ad8eac2b3b3755a6309762d9b4f9fee5ab [file] [log] [blame]
Junxiao Shia60d9362014-11-12 09:38:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2ed9e072017-08-13 16:45:48 +00002/*
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Junxiao Shia60d9362014-11-12 09:38:21 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "dummy-client-face.hpp"
Junxiao Shib6e276f2017-08-14 20:10:04 +000023#include "../detail/lp-field-tag.hpp"
Junxiao Shic828dfc2016-09-15 13:26:22 +000024#include "../lp/packet.hpp"
Junxiao Shi7357ef22016-09-07 02:39:37 +000025#include "../lp/tags.hpp"
26#include "../mgmt/nfd/controller.hpp"
27#include "../mgmt/nfd/control-response.hpp"
Junxiao Shia60d9362014-11-12 09:38:21 -070028#include "../transport/transport.hpp"
Junxiao Shia60d9362014-11-12 09:38:21 -070029
Davide Pesavento537dc3a2016-02-18 19:35:26 +010030#include <boost/asio/io_service.hpp>
31
Junxiao Shia60d9362014-11-12 09:38:21 -070032namespace ndn {
33namespace util {
34
Junxiao Shia60d9362014-11-12 09:38:21 -070035class DummyClientFace::Transport : public ndn::Transport
36{
37public:
38 void
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020039 receive(Block block) const
Junxiao Shia60d9362014-11-12 09:38:21 -070040 {
Eric Newberry83872fd2015-08-06 17:01:24 -070041 block.encode();
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020042 if (m_receiveCallback) {
Junxiao Shia60d9362014-11-12 09:38:21 -070043 m_receiveCallback(block);
Eric Newberry83872fd2015-08-06 17:01:24 -070044 }
Junxiao Shia60d9362014-11-12 09:38:21 -070045 }
46
Davide Pesavento57c07df2016-12-11 18:41:45 -050047 void
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020048 close() override
Junxiao Shia60d9362014-11-12 09:38:21 -070049 {
50 }
51
Davide Pesavento57c07df2016-12-11 18:41:45 -050052 void
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020053 pause() override
Junxiao Shia60d9362014-11-12 09:38:21 -070054 {
55 }
56
Davide Pesavento57c07df2016-12-11 18:41:45 -050057 void
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020058 resume() override
Junxiao Shia60d9362014-11-12 09:38:21 -070059 {
60 }
61
Davide Pesavento57c07df2016-12-11 18:41:45 -050062 void
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020063 send(const Block& wire) override
Junxiao Shia60d9362014-11-12 09:38:21 -070064 {
Junxiao Shi27913b42014-12-23 14:49:38 -070065 onSendBlock(wire);
Junxiao Shia60d9362014-11-12 09:38:21 -070066 }
67
Davide Pesavento57c07df2016-12-11 18:41:45 -050068 void
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020069 send(const Block& header, const Block& payload) override
Junxiao Shia60d9362014-11-12 09:38:21 -070070 {
Alexander Afanasyevea719672015-02-10 20:25:23 -080071 EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size());
72 encoder.appendByteArray(header.wire(), header.size());
73 encoder.appendByteArray(payload.wire(), payload.size());
74
75 this->send(encoder.block());
Junxiao Shia60d9362014-11-12 09:38:21 -070076 }
77
78 boost::asio::io_service&
79 getIoService()
80 {
81 return *m_ioService;
82 }
83
Junxiao Shi27913b42014-12-23 14:49:38 -070084public:
85 Signal<Transport, Block> onSendBlock;
Junxiao Shia60d9362014-11-12 09:38:21 -070086};
87
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080088struct DummyClientFace::BroadcastLink
89{
90 std::vector<DummyClientFace*> faces;
91};
92
93DummyClientFace::AlreadyLinkedError::AlreadyLinkedError()
94 : Error("Face has already been linked to another face")
95{
96}
97
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080098DummyClientFace::DummyClientFace(const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
99 : Face(make_shared<DummyClientFace::Transport>())
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800100 , m_internalKeyChain(new KeyChain)
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700101 , m_keyChain(*m_internalKeyChain)
102{
103 this->construct(options);
104}
105
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800106DummyClientFace::DummyClientFace(KeyChain& keyChain,
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700107 const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
108 : Face(make_shared<DummyClientFace::Transport>(), keyChain)
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700109 , m_keyChain(keyChain)
Junxiao Shia60d9362014-11-12 09:38:21 -0700110{
111 this->construct(options);
112}
113
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800114DummyClientFace::DummyClientFace(boost::asio::io_service& ioService,
115 const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
116 : Face(make_shared<DummyClientFace::Transport>(), ioService)
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800117 , m_internalKeyChain(new KeyChain)
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700118 , m_keyChain(*m_internalKeyChain)
119{
120 this->construct(options);
121}
122
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800123DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain,
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700124 const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
125 : Face(make_shared<DummyClientFace::Transport>(), ioService, keyChain)
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700126 , m_keyChain(keyChain)
Junxiao Shia60d9362014-11-12 09:38:21 -0700127{
128 this->construct(options);
129}
130
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800131DummyClientFace::~DummyClientFace()
132{
133 unlink();
134}
135
Junxiao Shia60d9362014-11-12 09:38:21 -0700136void
137DummyClientFace::construct(const Options& options)
138{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800139 static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (const Block& blockFromDaemon) {
Eric Newberrye3e25052015-11-28 20:37:08 -0700140 Block packet(blockFromDaemon);
141 packet.encode();
142 lp::Packet lpPacket(packet);
Alexander Afanasyevea719672015-02-10 20:25:23 -0800143
Eric Newberrye3e25052015-11-28 20:37:08 -0700144 Buffer::const_iterator begin, end;
145 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
146 Block block(&*begin, std::distance(begin, end));
Alexander Afanasyevea719672015-02-10 20:25:23 -0800147
Eric Newberrye3e25052015-11-28 20:37:08 -0700148 if (block.type() == tlv::Interest) {
149 shared_ptr<Interest> interest = make_shared<Interest>(block);
150 if (lpPacket.has<lp::NackField>()) {
151 shared_ptr<lp::Nack> nack = make_shared<lp::Nack>(std::move(*interest));
152 nack->setHeader(lpPacket.get<lp::NackField>());
Junxiao Shib38e6d42017-08-16 16:15:28 +0000153 addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*nack, lpPacket);
Eric Newberrye3e25052015-11-28 20:37:08 -0700154 onSendNack(*nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700155 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700156 else {
Junxiao Shib38e6d42017-08-16 16:15:28 +0000157 addTagFromField<lp::NextHopFaceIdTag, lp::NextHopFaceIdField>(*interest, lpPacket);
158 addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*interest, lpPacket);
Eric Newberrye3e25052015-11-28 20:37:08 -0700159 onSendInterest(*interest);
Eric Newberry83872fd2015-08-06 17:01:24 -0700160 }
Junxiao Shi27913b42014-12-23 14:49:38 -0700161 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700162 else if (block.type() == tlv::Data) {
163 shared_ptr<Data> data = make_shared<Data>(block);
Junxiao Shib38e6d42017-08-16 16:15:28 +0000164 addTagFromField<lp::CachePolicyTag, lp::CachePolicyField>(*data, lpPacket);
165 addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*data, lpPacket);
Eric Newberrye3e25052015-11-28 20:37:08 -0700166 onSendData(*data);
Junxiao Shi27913b42014-12-23 14:49:38 -0700167 }
168 });
169
Junxiao Shia60d9362014-11-12 09:38:21 -0700170 if (options.enablePacketLogging)
171 this->enablePacketLogging();
172
173 if (options.enableRegistrationReply)
174 this->enableRegistrationReply();
Junxiao Shic828dfc2016-09-15 13:26:22 +0000175
176 m_processEventsOverride = options.processEventsOverride;
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800177
178 enableBroadcastLink();
179}
180
181void
182DummyClientFace::enableBroadcastLink()
183{
184 this->onSendInterest.connect([this] (const Interest& interest) {
185 if (m_bcastLink != nullptr) {
186 for (auto otherFace : m_bcastLink->faces) {
187 if (otherFace != this) {
188 otherFace->receive(interest);
189 }
190 }
191 }
192 });
193 this->onSendData.connect([this] (const Data& data) {
194 if (m_bcastLink != nullptr) {
195 for (auto otherFace : m_bcastLink->faces) {
196 if (otherFace != this) {
197 otherFace->receive(data);
198 }
199 }
200 }
201 });
202 this->onSendNack.connect([this] (const lp::Nack& nack) {
203 if (m_bcastLink != nullptr) {
204 for (auto otherFace : m_bcastLink->faces) {
205 if (otherFace != this) {
206 otherFace->receive(nack);
207 }
208 }
209 }
210 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700211}
212
213void
214DummyClientFace::enablePacketLogging()
215{
Junxiao Shi27913b42014-12-23 14:49:38 -0700216 onSendInterest.connect([this] (const Interest& interest) {
217 this->sentInterests.push_back(interest);
218 });
219 onSendData.connect([this] (const Data& data) {
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800220 this->sentData.push_back(data);
Junxiao Shi27913b42014-12-23 14:49:38 -0700221 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700222 onSendNack.connect([this] (const lp::Nack& nack) {
223 this->sentNacks.push_back(nack);
224 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700225}
226
227void
228DummyClientFace::enableRegistrationReply()
229{
Junxiao Shi27913b42014-12-23 14:49:38 -0700230 onSendInterest.connect([this] (const Interest& interest) {
Junxiao Shia60d9362014-11-12 09:38:21 -0700231 static const Name localhostRegistration("/localhost/nfd/rib");
232 if (!localhostRegistration.isPrefixOf(interest.getName()))
233 return;
234
235 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
236 params.setFaceId(1);
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400237 params.setOrigin(nfd::ROUTE_ORIGIN_APP);
Junxiao Shia60d9362014-11-12 09:38:21 -0700238 if (interest.getName().get(3) == name::Component("register")) {
239 params.setCost(0);
240 }
241
242 nfd::ControlResponse resp;
243 resp.setCode(200);
244 resp.setBody(params.wireEncode());
245
246 shared_ptr<Data> data = make_shared<Data>(interest.getName());
247 data->setContent(resp.wireEncode());
248
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700249 m_keyChain.sign(*data, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256));
Junxiao Shia60d9362014-11-12 09:38:21 -0700250
251 this->getIoService().post([this, data] { this->receive(*data); });
Junxiao Shi27913b42014-12-23 14:49:38 -0700252 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700253}
254
Junxiao Shia60d9362014-11-12 09:38:21 -0700255void
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800256DummyClientFace::receive(const Interest& interest)
Junxiao Shia60d9362014-11-12 09:38:21 -0700257{
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800258 lp::Packet lpPacket(interest.wireEncode());
Alexander Afanasyevea719672015-02-10 20:25:23 -0800259
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800260 addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, interest);
261 addFieldFromTag<lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, interest);
262 addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, interest);
Eric Newberry83872fd2015-08-06 17:01:24 -0700263
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800264 static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
Junxiao Shia60d9362014-11-12 09:38:21 -0700265}
266
Eric Newberry83872fd2015-08-06 17:01:24 -0700267void
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800268DummyClientFace::receive(const Data& data)
269{
270 lp::Packet lpPacket(data.wireEncode());
271
272 addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, data);
273 addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, data);
274
275 static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
276}
277
278void
279DummyClientFace::receive(const lp::Nack& nack)
Eric Newberry83872fd2015-08-06 17:01:24 -0700280{
281 lp::Packet lpPacket;
282 lpPacket.add<lp::NackField>(nack.getHeader());
283 Block interest = nack.getInterest().wireEncode();
284 lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end()));
285
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800286 addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, nack);
287 addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700288
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800289 static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
Eric Newberry83872fd2015-08-06 17:01:24 -0700290}
Junxiao Shia60d9362014-11-12 09:38:21 -0700291
Junxiao Shic828dfc2016-09-15 13:26:22 +0000292void
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800293DummyClientFace::linkTo(DummyClientFace& other)
294{
295 if (m_bcastLink != nullptr && other.m_bcastLink != nullptr) {
296 if (m_bcastLink != other.m_bcastLink) {
297 // already on different links
298 BOOST_THROW_EXCEPTION(AlreadyLinkedError());
299 }
300 }
301 else if (m_bcastLink == nullptr && other.m_bcastLink != nullptr) {
302 m_bcastLink = other.m_bcastLink;
303 m_bcastLink->faces.push_back(this);
304 }
305 else if (m_bcastLink != nullptr && other.m_bcastLink == nullptr) {
306 other.m_bcastLink = m_bcastLink;
307 m_bcastLink->faces.push_back(&other);
308 }
309 else {
310 m_bcastLink = other.m_bcastLink = make_shared<BroadcastLink>();
311 m_bcastLink->faces.push_back(this);
312 m_bcastLink->faces.push_back(&other);
313 }
314}
315
316void
317DummyClientFace::unlink()
318{
319 if (m_bcastLink == nullptr) {
320 return;
321 }
322
323 auto it = std::find(m_bcastLink->faces.begin(), m_bcastLink->faces.end(), this);
324 BOOST_ASSERT(it != m_bcastLink->faces.end());
325 m_bcastLink->faces.erase(it);
326
327 if (m_bcastLink->faces.size() == 1) {
328 m_bcastLink->faces[0]->m_bcastLink = nullptr;
329 m_bcastLink->faces.clear();
330 }
331 m_bcastLink = nullptr;
332}
333
334void
Junxiao Shi2ed9e072017-08-13 16:45:48 +0000335DummyClientFace::doProcessEvents(time::milliseconds timeout, bool keepThread)
Junxiao Shic828dfc2016-09-15 13:26:22 +0000336{
337 if (m_processEventsOverride != nullptr) {
338 m_processEventsOverride(timeout);
339 }
340 else {
341 this->Face::doProcessEvents(timeout, keepThread);
342 }
343}
344
Junxiao Shia60d9362014-11-12 09:38:21 -0700345} // namespace util
346} // namespace ndn