blob: 989b4551e548250562e7a69461555615a34a844d [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
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080088DummyClientFace::DummyClientFace(const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
89 : Face(make_shared<DummyClientFace::Transport>())
Alexander Afanasyev80782e02017-01-04 13:16:54 -080090 , m_internalKeyChain(new KeyChain)
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070091 , m_keyChain(*m_internalKeyChain)
92{
93 this->construct(options);
94}
95
Alexander Afanasyev80782e02017-01-04 13:16:54 -080096DummyClientFace::DummyClientFace(KeyChain& keyChain,
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070097 const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
98 : Face(make_shared<DummyClientFace::Transport>(), keyChain)
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070099 , m_keyChain(keyChain)
Junxiao Shia60d9362014-11-12 09:38:21 -0700100{
101 this->construct(options);
102}
103
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800104DummyClientFace::DummyClientFace(boost::asio::io_service& ioService,
105 const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
106 : Face(make_shared<DummyClientFace::Transport>(), ioService)
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800107 , m_internalKeyChain(new KeyChain)
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700108 , m_keyChain(*m_internalKeyChain)
109{
110 this->construct(options);
111}
112
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800113DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain,
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700114 const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
115 : Face(make_shared<DummyClientFace::Transport>(), ioService, keyChain)
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700116 , m_keyChain(keyChain)
Junxiao Shia60d9362014-11-12 09:38:21 -0700117{
118 this->construct(options);
119}
120
121void
122DummyClientFace::construct(const Options& options)
123{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800124 static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (const Block& blockFromDaemon) {
Eric Newberrye3e25052015-11-28 20:37:08 -0700125 Block packet(blockFromDaemon);
126 packet.encode();
127 lp::Packet lpPacket(packet);
Alexander Afanasyevea719672015-02-10 20:25:23 -0800128
Eric Newberrye3e25052015-11-28 20:37:08 -0700129 Buffer::const_iterator begin, end;
130 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
131 Block block(&*begin, std::distance(begin, end));
Alexander Afanasyevea719672015-02-10 20:25:23 -0800132
Eric Newberrye3e25052015-11-28 20:37:08 -0700133 if (block.type() == tlv::Interest) {
134 shared_ptr<Interest> interest = make_shared<Interest>(block);
135 if (lpPacket.has<lp::NackField>()) {
136 shared_ptr<lp::Nack> nack = make_shared<lp::Nack>(std::move(*interest));
137 nack->setHeader(lpPacket.get<lp::NackField>());
Junxiao Shib38e6d42017-08-16 16:15:28 +0000138 addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*nack, lpPacket);
Eric Newberrye3e25052015-11-28 20:37:08 -0700139 onSendNack(*nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700140 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700141 else {
Junxiao Shib38e6d42017-08-16 16:15:28 +0000142 addTagFromField<lp::NextHopFaceIdTag, lp::NextHopFaceIdField>(*interest, lpPacket);
143 addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*interest, lpPacket);
Eric Newberrye3e25052015-11-28 20:37:08 -0700144 onSendInterest(*interest);
Eric Newberry83872fd2015-08-06 17:01:24 -0700145 }
Junxiao Shi27913b42014-12-23 14:49:38 -0700146 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700147 else if (block.type() == tlv::Data) {
148 shared_ptr<Data> data = make_shared<Data>(block);
Junxiao Shib38e6d42017-08-16 16:15:28 +0000149 addTagFromField<lp::CachePolicyTag, lp::CachePolicyField>(*data, lpPacket);
150 addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*data, lpPacket);
Eric Newberrye3e25052015-11-28 20:37:08 -0700151 onSendData(*data);
Junxiao Shi27913b42014-12-23 14:49:38 -0700152 }
153 });
154
Junxiao Shia60d9362014-11-12 09:38:21 -0700155 if (options.enablePacketLogging)
156 this->enablePacketLogging();
157
158 if (options.enableRegistrationReply)
159 this->enableRegistrationReply();
Junxiao Shic828dfc2016-09-15 13:26:22 +0000160
161 m_processEventsOverride = options.processEventsOverride;
Junxiao Shia60d9362014-11-12 09:38:21 -0700162}
163
164void
165DummyClientFace::enablePacketLogging()
166{
Junxiao Shi27913b42014-12-23 14:49:38 -0700167 onSendInterest.connect([this] (const Interest& interest) {
168 this->sentInterests.push_back(interest);
169 });
170 onSendData.connect([this] (const Data& data) {
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800171 this->sentData.push_back(data);
Junxiao Shi27913b42014-12-23 14:49:38 -0700172 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700173 onSendNack.connect([this] (const lp::Nack& nack) {
174 this->sentNacks.push_back(nack);
175 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700176}
177
178void
179DummyClientFace::enableRegistrationReply()
180{
Junxiao Shi27913b42014-12-23 14:49:38 -0700181 onSendInterest.connect([this] (const Interest& interest) {
Junxiao Shia60d9362014-11-12 09:38:21 -0700182 static const Name localhostRegistration("/localhost/nfd/rib");
183 if (!localhostRegistration.isPrefixOf(interest.getName()))
184 return;
185
186 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
187 params.setFaceId(1);
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400188 params.setOrigin(nfd::ROUTE_ORIGIN_APP);
Junxiao Shia60d9362014-11-12 09:38:21 -0700189 if (interest.getName().get(3) == name::Component("register")) {
190 params.setCost(0);
191 }
192
193 nfd::ControlResponse resp;
194 resp.setCode(200);
195 resp.setBody(params.wireEncode());
196
197 shared_ptr<Data> data = make_shared<Data>(interest.getName());
198 data->setContent(resp.wireEncode());
199
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700200 m_keyChain.sign(*data, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256));
Junxiao Shia60d9362014-11-12 09:38:21 -0700201
202 this->getIoService().post([this, data] { this->receive(*data); });
Junxiao Shi27913b42014-12-23 14:49:38 -0700203 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700204}
205
Junxiao Shia60d9362014-11-12 09:38:21 -0700206void
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800207DummyClientFace::receive(const Interest& interest)
Junxiao Shia60d9362014-11-12 09:38:21 -0700208{
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800209 lp::Packet lpPacket(interest.wireEncode());
Alexander Afanasyevea719672015-02-10 20:25:23 -0800210
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800211 addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, interest);
212 addFieldFromTag<lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, interest);
213 addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, interest);
Eric Newberry83872fd2015-08-06 17:01:24 -0700214
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800215 static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
Junxiao Shia60d9362014-11-12 09:38:21 -0700216}
217
Eric Newberry83872fd2015-08-06 17:01:24 -0700218void
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800219DummyClientFace::receive(const Data& data)
220{
221 lp::Packet lpPacket(data.wireEncode());
222
223 addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, data);
224 addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, data);
225
226 static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
227}
228
229void
230DummyClientFace::receive(const lp::Nack& nack)
Eric Newberry83872fd2015-08-06 17:01:24 -0700231{
232 lp::Packet lpPacket;
233 lpPacket.add<lp::NackField>(nack.getHeader());
234 Block interest = nack.getInterest().wireEncode();
235 lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end()));
236
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800237 addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, nack);
238 addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700239
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800240 static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
Eric Newberry83872fd2015-08-06 17:01:24 -0700241}
Junxiao Shia60d9362014-11-12 09:38:21 -0700242
Junxiao Shic828dfc2016-09-15 13:26:22 +0000243void
Junxiao Shi2ed9e072017-08-13 16:45:48 +0000244DummyClientFace::doProcessEvents(time::milliseconds timeout, bool keepThread)
Junxiao Shic828dfc2016-09-15 13:26:22 +0000245{
246 if (m_processEventsOverride != nullptr) {
247 m_processEventsOverride(timeout);
248 }
249 else {
250 this->Face::doProcessEvents(timeout, keepThread);
251 }
252}
253
Junxiao Shia60d9362014-11-12 09:38:21 -0700254} // namespace util
255} // namespace ndn