blob: 8cdb9dc782a6833131ab06c2861ebd9151fd82c4 [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
Junxiao Shia60d9362014-11-12 09:38:21 -070092{
93 this->construct(options);
94}
95
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080096DummyClientFace::DummyClientFace(boost::asio::io_service& ioService,
97 const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
98 : Face(make_shared<DummyClientFace::Transport>(), ioService)
99#ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
100 , sentDatas(sentData)
101#endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
Junxiao Shia60d9362014-11-12 09:38:21 -0700102{
103 this->construct(options);
104}
105
106void
107DummyClientFace::construct(const Options& options)
108{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800109 static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (const Block& blockFromDaemon) {
Eric Newberrye3e25052015-11-28 20:37:08 -0700110 Block packet(blockFromDaemon);
111 packet.encode();
112 lp::Packet lpPacket(packet);
Alexander Afanasyevea719672015-02-10 20:25:23 -0800113
Eric Newberrye3e25052015-11-28 20:37:08 -0700114 Buffer::const_iterator begin, end;
115 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
116 Block block(&*begin, std::distance(begin, end));
Alexander Afanasyevea719672015-02-10 20:25:23 -0800117
Eric Newberrye3e25052015-11-28 20:37:08 -0700118 if (block.type() == tlv::Interest) {
119 shared_ptr<Interest> interest = make_shared<Interest>(block);
120 if (lpPacket.has<lp::NackField>()) {
121 shared_ptr<lp::Nack> nack = make_shared<lp::Nack>(std::move(*interest));
122 nack->setHeader(lpPacket.get<lp::NackField>());
123 if (lpPacket.has<lp::NextHopFaceIdField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000124 nack->setTag(make_shared<lp::NextHopFaceIdTag>(lpPacket.get<lp::NextHopFaceIdField>()));
Eric Newberry83872fd2015-08-06 17:01:24 -0700125 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700126 onSendNack(*nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700127 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700128 else {
129 if (lpPacket.has<lp::NextHopFaceIdField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000130 interest->setTag(make_shared<lp::NextHopFaceIdTag>(lpPacket.get<lp::NextHopFaceIdField>()));
Eric Newberry83872fd2015-08-06 17:01:24 -0700131 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700132 onSendInterest(*interest);
Eric Newberry83872fd2015-08-06 17:01:24 -0700133 }
Junxiao Shi27913b42014-12-23 14:49:38 -0700134 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700135 else if (block.type() == tlv::Data) {
136 shared_ptr<Data> data = make_shared<Data>(block);
137
138 if (lpPacket.has<lp::CachePolicyField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000139 data->setTag(make_shared<lp::CachePolicyTag>(lpPacket.get<lp::CachePolicyField>()));
Eric Newberrye3e25052015-11-28 20:37:08 -0700140 }
141
142 onSendData(*data);
Junxiao Shi27913b42014-12-23 14:49:38 -0700143 }
144 });
145
Junxiao Shia60d9362014-11-12 09:38:21 -0700146 if (options.enablePacketLogging)
147 this->enablePacketLogging();
148
149 if (options.enableRegistrationReply)
150 this->enableRegistrationReply();
151}
152
153void
154DummyClientFace::enablePacketLogging()
155{
Junxiao Shi27913b42014-12-23 14:49:38 -0700156 onSendInterest.connect([this] (const Interest& interest) {
157 this->sentInterests.push_back(interest);
158 });
159 onSendData.connect([this] (const Data& data) {
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800160 this->sentData.push_back(data);
Junxiao Shi27913b42014-12-23 14:49:38 -0700161 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700162 onSendNack.connect([this] (const lp::Nack& nack) {
163 this->sentNacks.push_back(nack);
164 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700165}
166
167void
168DummyClientFace::enableRegistrationReply()
169{
Junxiao Shi27913b42014-12-23 14:49:38 -0700170 onSendInterest.connect([this] (const Interest& interest) {
Junxiao Shia60d9362014-11-12 09:38:21 -0700171 static const Name localhostRegistration("/localhost/nfd/rib");
172 if (!localhostRegistration.isPrefixOf(interest.getName()))
173 return;
174
175 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
176 params.setFaceId(1);
177 params.setOrigin(0);
178 if (interest.getName().get(3) == name::Component("register")) {
179 params.setCost(0);
180 }
181
182 nfd::ControlResponse resp;
183 resp.setCode(200);
184 resp.setBody(params.wireEncode());
185
186 shared_ptr<Data> data = make_shared<Data>(interest.getName());
187 data->setContent(resp.wireEncode());
188
189 KeyChain keyChain;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700190 keyChain.sign(*data, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256));
Junxiao Shia60d9362014-11-12 09:38:21 -0700191
192 this->getIoService().post([this, data] { this->receive(*data); });
Junxiao Shi27913b42014-12-23 14:49:38 -0700193 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700194}
195
196template<typename Packet>
197void
198DummyClientFace::receive(const Packet& packet)
199{
Eric Newberry83872fd2015-08-06 17:01:24 -0700200 lp::Packet lpPacket(packet.wireEncode());
Alexander Afanasyevea719672015-02-10 20:25:23 -0800201
Junxiao Shi4b469982015-12-03 18:20:19 +0000202 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag =
203 static_cast<const TagHost&>(packet).getTag<lp::IncomingFaceIdTag>();
204 if (incomingFaceIdTag != nullptr) {
205 lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag);
Alexander Afanasyevea719672015-02-10 20:25:23 -0800206 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700207
Junxiao Shi4b469982015-12-03 18:20:19 +0000208 shared_ptr<lp::NextHopFaceIdTag> nextHopFaceIdTag =
209 static_cast<const TagHost&>(packet).getTag<lp::NextHopFaceIdTag>();
210 if (nextHopFaceIdTag != nullptr) {
211 lpPacket.add<lp::NextHopFaceIdField>(*nextHopFaceIdTag);
Alexander Afanasyevea719672015-02-10 20:25:23 -0800212 }
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800213 static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
Junxiao Shia60d9362014-11-12 09:38:21 -0700214}
215
216template void
217DummyClientFace::receive<Interest>(const Interest& packet);
218
219template void
220DummyClientFace::receive<Data>(const Data& packet);
221
Eric Newberry83872fd2015-08-06 17:01:24 -0700222template<>
223void
224DummyClientFace::receive<lp::Nack>(const lp::Nack& nack)
225{
226 lp::Packet lpPacket;
227 lpPacket.add<lp::NackField>(nack.getHeader());
228 Block interest = nack.getInterest().wireEncode();
229 lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end()));
230
Junxiao Shi4b469982015-12-03 18:20:19 +0000231 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = nack.getTag<lp::IncomingFaceIdTag>();
232 if (incomingFaceIdTag != nullptr) {
233 lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag);
Eric Newberry83872fd2015-08-06 17:01:24 -0700234 }
235
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800236 static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
Eric Newberry83872fd2015-08-06 17:01:24 -0700237}
Junxiao Shia60d9362014-11-12 09:38:21 -0700238
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800239#ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
240
Junxiao Shia60d9362014-11-12 09:38:21 -0700241shared_ptr<DummyClientFace>
242makeDummyClientFace(const DummyClientFace::Options& options)
243{
Junxiao Shia1ea5062014-12-27 22:33:39 -0700244 // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800245 return shared_ptr<DummyClientFace>(new DummyClientFace(options));
Junxiao Shia60d9362014-11-12 09:38:21 -0700246}
247
248shared_ptr<DummyClientFace>
249makeDummyClientFace(boost::asio::io_service& ioService,
250 const DummyClientFace::Options& options)
251{
Junxiao Shia1ea5062014-12-27 22:33:39 -0700252 // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800253 return shared_ptr<DummyClientFace>(new DummyClientFace(ref(ioService), options));
Junxiao Shia60d9362014-11-12 09:38:21 -0700254}
255
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800256#endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
257
Junxiao Shia60d9362014-11-12 09:38:21 -0700258} // namespace util
259} // namespace ndn