blob: cc593ef9e327a0b97cf0f337d627a435b68c7dde [file] [log] [blame]
Junxiao Shia60d9362014-11-12 09:38:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento537dc3a2016-02-18 19:35:26 +01003 * Copyright (c) 2013-2016 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"
23#include "../transport/transport.hpp"
24#include "../management/nfd-controller.hpp"
25#include "../management/nfd-control-response.hpp"
26
Davide Pesavento537dc3a2016-02-18 19:35:26 +010027#include <boost/asio/io_service.hpp>
28
Junxiao Shia60d9362014-11-12 09:38:21 -070029namespace ndn {
30namespace util {
31
32const DummyClientFace::Options DummyClientFace::DEFAULT_OPTIONS { true, false };
33
34class DummyClientFace::Transport : public ndn::Transport
35{
36public:
37 void
Eric Newberry83872fd2015-08-06 17:01:24 -070038 receive(Block block)
Junxiao Shia60d9362014-11-12 09:38:21 -070039 {
Eric Newberry83872fd2015-08-06 17:01:24 -070040 block.encode();
41 if (static_cast<bool>(m_receiveCallback)) {
Junxiao Shia60d9362014-11-12 09:38:21 -070042 m_receiveCallback(block);
Eric Newberry83872fd2015-08-06 17:01:24 -070043 }
Junxiao Shia60d9362014-11-12 09:38:21 -070044 }
45
46 virtual void
47 close()
48 {
49 }
50
51 virtual void
52 pause()
53 {
54 }
55
56 virtual void
57 resume()
58 {
59 }
60
61 virtual void
62 send(const Block& wire)
63 {
Junxiao Shi27913b42014-12-23 14:49:38 -070064 onSendBlock(wire);
Junxiao Shia60d9362014-11-12 09:38:21 -070065 }
66
67 virtual void
68 send(const Block& header, const Block& payload)
69 {
Alexander Afanasyevea719672015-02-10 20:25:23 -080070 EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size());
71 encoder.appendByteArray(header.wire(), header.size());
72 encoder.appendByteArray(payload.wire(), payload.size());
73
74 this->send(encoder.block());
Junxiao Shia60d9362014-11-12 09:38:21 -070075 }
76
77 boost::asio::io_service&
78 getIoService()
79 {
80 return *m_ioService;
81 }
82
Junxiao Shi27913b42014-12-23 14:49:38 -070083public:
84 Signal<Transport, Block> onSendBlock;
Junxiao Shia60d9362014-11-12 09:38:21 -070085};
86
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080087DummyClientFace::DummyClientFace(const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
88 : Face(make_shared<DummyClientFace::Transport>())
89#ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
90 , sentDatas(sentData)
91#endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070092 , m_internalKeyChain(new KeyChain)
93 , m_keyChain(*m_internalKeyChain)
94{
95 this->construct(options);
96}
97
98DummyClientFace::DummyClientFace(KeyChain& keyChain,
99 const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
100 : Face(make_shared<DummyClientFace::Transport>(), keyChain)
101#ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
102 , sentDatas(sentData)
103#endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
104 , m_keyChain(keyChain)
Junxiao Shia60d9362014-11-12 09:38:21 -0700105{
106 this->construct(options);
107}
108
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800109DummyClientFace::DummyClientFace(boost::asio::io_service& ioService,
110 const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
111 : Face(make_shared<DummyClientFace::Transport>(), ioService)
112#ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
113 , sentDatas(sentData)
114#endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700115 , m_internalKeyChain(new KeyChain)
116 , m_keyChain(*m_internalKeyChain)
117{
118 this->construct(options);
119}
120
121DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain,
122 const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
123 : Face(make_shared<DummyClientFace::Transport>(), ioService, keyChain)
124#ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
125 , sentDatas(sentData)
126#endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
127 , m_keyChain(keyChain)
Junxiao Shia60d9362014-11-12 09:38:21 -0700128{
129 this->construct(options);
130}
131
132void
133DummyClientFace::construct(const Options& options)
134{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800135 static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (const Block& blockFromDaemon) {
Eric Newberrye3e25052015-11-28 20:37:08 -0700136 Block packet(blockFromDaemon);
137 packet.encode();
138 lp::Packet lpPacket(packet);
Alexander Afanasyevea719672015-02-10 20:25:23 -0800139
Eric Newberrye3e25052015-11-28 20:37:08 -0700140 Buffer::const_iterator begin, end;
141 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
142 Block block(&*begin, std::distance(begin, end));
Alexander Afanasyevea719672015-02-10 20:25:23 -0800143
Eric Newberrye3e25052015-11-28 20:37:08 -0700144 if (block.type() == tlv::Interest) {
145 shared_ptr<Interest> interest = make_shared<Interest>(block);
146 if (lpPacket.has<lp::NackField>()) {
147 shared_ptr<lp::Nack> nack = make_shared<lp::Nack>(std::move(*interest));
148 nack->setHeader(lpPacket.get<lp::NackField>());
149 if (lpPacket.has<lp::NextHopFaceIdField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000150 nack->setTag(make_shared<lp::NextHopFaceIdTag>(lpPacket.get<lp::NextHopFaceIdField>()));
Eric Newberry83872fd2015-08-06 17:01:24 -0700151 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700152 onSendNack(*nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700153 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700154 else {
155 if (lpPacket.has<lp::NextHopFaceIdField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000156 interest->setTag(make_shared<lp::NextHopFaceIdTag>(lpPacket.get<lp::NextHopFaceIdField>()));
Eric Newberry83872fd2015-08-06 17:01:24 -0700157 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700158 onSendInterest(*interest);
Eric Newberry83872fd2015-08-06 17:01:24 -0700159 }
Junxiao Shi27913b42014-12-23 14:49:38 -0700160 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700161 else if (block.type() == tlv::Data) {
162 shared_ptr<Data> data = make_shared<Data>(block);
163
164 if (lpPacket.has<lp::CachePolicyField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000165 data->setTag(make_shared<lp::CachePolicyTag>(lpPacket.get<lp::CachePolicyField>()));
Eric Newberrye3e25052015-11-28 20:37:08 -0700166 }
167
168 onSendData(*data);
Junxiao Shi27913b42014-12-23 14:49:38 -0700169 }
170 });
171
Junxiao Shia60d9362014-11-12 09:38:21 -0700172 if (options.enablePacketLogging)
173 this->enablePacketLogging();
174
175 if (options.enableRegistrationReply)
176 this->enableRegistrationReply();
177}
178
179void
180DummyClientFace::enablePacketLogging()
181{
Junxiao Shi27913b42014-12-23 14:49:38 -0700182 onSendInterest.connect([this] (const Interest& interest) {
183 this->sentInterests.push_back(interest);
184 });
185 onSendData.connect([this] (const Data& data) {
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800186 this->sentData.push_back(data);
Junxiao Shi27913b42014-12-23 14:49:38 -0700187 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700188 onSendNack.connect([this] (const lp::Nack& nack) {
189 this->sentNacks.push_back(nack);
190 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700191}
192
193void
194DummyClientFace::enableRegistrationReply()
195{
Junxiao Shi27913b42014-12-23 14:49:38 -0700196 onSendInterest.connect([this] (const Interest& interest) {
Junxiao Shia60d9362014-11-12 09:38:21 -0700197 static const Name localhostRegistration("/localhost/nfd/rib");
198 if (!localhostRegistration.isPrefixOf(interest.getName()))
199 return;
200
201 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
202 params.setFaceId(1);
203 params.setOrigin(0);
204 if (interest.getName().get(3) == name::Component("register")) {
205 params.setCost(0);
206 }
207
208 nfd::ControlResponse resp;
209 resp.setCode(200);
210 resp.setBody(params.wireEncode());
211
212 shared_ptr<Data> data = make_shared<Data>(interest.getName());
213 data->setContent(resp.wireEncode());
214
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700215 m_keyChain.sign(*data, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256));
Junxiao Shia60d9362014-11-12 09:38:21 -0700216
217 this->getIoService().post([this, data] { this->receive(*data); });
Junxiao Shi27913b42014-12-23 14:49:38 -0700218 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700219}
220
221template<typename Packet>
222void
223DummyClientFace::receive(const Packet& packet)
224{
Eric Newberry83872fd2015-08-06 17:01:24 -0700225 lp::Packet lpPacket(packet.wireEncode());
Alexander Afanasyevea719672015-02-10 20:25:23 -0800226
Junxiao Shi4b469982015-12-03 18:20:19 +0000227 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag =
228 static_cast<const TagHost&>(packet).getTag<lp::IncomingFaceIdTag>();
229 if (incomingFaceIdTag != nullptr) {
230 lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag);
Alexander Afanasyevea719672015-02-10 20:25:23 -0800231 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700232
Junxiao Shi4b469982015-12-03 18:20:19 +0000233 shared_ptr<lp::NextHopFaceIdTag> nextHopFaceIdTag =
234 static_cast<const TagHost&>(packet).getTag<lp::NextHopFaceIdTag>();
235 if (nextHopFaceIdTag != nullptr) {
236 lpPacket.add<lp::NextHopFaceIdField>(*nextHopFaceIdTag);
Alexander Afanasyevea719672015-02-10 20:25:23 -0800237 }
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800238 static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
Junxiao Shia60d9362014-11-12 09:38:21 -0700239}
240
241template void
242DummyClientFace::receive<Interest>(const Interest& packet);
243
244template void
245DummyClientFace::receive<Data>(const Data& packet);
246
Eric Newberry83872fd2015-08-06 17:01:24 -0700247template<>
248void
249DummyClientFace::receive<lp::Nack>(const lp::Nack& nack)
250{
251 lp::Packet lpPacket;
252 lpPacket.add<lp::NackField>(nack.getHeader());
253 Block interest = nack.getInterest().wireEncode();
254 lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end()));
255
Junxiao Shi4b469982015-12-03 18:20:19 +0000256 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = nack.getTag<lp::IncomingFaceIdTag>();
257 if (incomingFaceIdTag != nullptr) {
258 lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag);
Eric Newberry83872fd2015-08-06 17:01:24 -0700259 }
260
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800261 static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
Eric Newberry83872fd2015-08-06 17:01:24 -0700262}
Junxiao Shia60d9362014-11-12 09:38:21 -0700263
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800264#ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
265
Junxiao Shia60d9362014-11-12 09:38:21 -0700266shared_ptr<DummyClientFace>
267makeDummyClientFace(const DummyClientFace::Options& options)
268{
Junxiao Shia1ea5062014-12-27 22:33:39 -0700269 // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800270 return shared_ptr<DummyClientFace>(new DummyClientFace(options));
Junxiao Shia60d9362014-11-12 09:38:21 -0700271}
272
273shared_ptr<DummyClientFace>
274makeDummyClientFace(boost::asio::io_service& ioService,
275 const DummyClientFace::Options& options)
276{
Junxiao Shia1ea5062014-12-27 22:33:39 -0700277 // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800278 return shared_ptr<DummyClientFace>(new DummyClientFace(ref(ioService), options));
Junxiao Shia60d9362014-11-12 09:38:21 -0700279}
280
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800281#endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
282
Junxiao Shia60d9362014-11-12 09:38:21 -0700283} // namespace util
284} // namespace ndn