blob: 676a7a04f512158c37de6148c401e0dab9bcebf1 [file] [log] [blame]
Junxiao Shia60d9362014-11-12 09:38:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 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
27namespace ndn {
28namespace util {
29
30const DummyClientFace::Options DummyClientFace::DEFAULT_OPTIONS { true, false };
31
32class DummyClientFace::Transport : public ndn::Transport
33{
34public:
35 void
Eric Newberry83872fd2015-08-06 17:01:24 -070036 receive(Block block)
Junxiao Shia60d9362014-11-12 09:38:21 -070037 {
Eric Newberry83872fd2015-08-06 17:01:24 -070038 block.encode();
39 if (static_cast<bool>(m_receiveCallback)) {
Junxiao Shia60d9362014-11-12 09:38:21 -070040 m_receiveCallback(block);
Eric Newberry83872fd2015-08-06 17:01:24 -070041 }
Junxiao Shia60d9362014-11-12 09:38:21 -070042 }
43
44 virtual void
45 close()
46 {
47 }
48
49 virtual void
50 pause()
51 {
52 }
53
54 virtual void
55 resume()
56 {
57 }
58
59 virtual void
60 send(const Block& wire)
61 {
Junxiao Shi27913b42014-12-23 14:49:38 -070062 onSendBlock(wire);
Junxiao Shia60d9362014-11-12 09:38:21 -070063 }
64
65 virtual void
66 send(const Block& header, const Block& payload)
67 {
Alexander Afanasyevea719672015-02-10 20:25:23 -080068 EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size());
69 encoder.appendByteArray(header.wire(), header.size());
70 encoder.appendByteArray(payload.wire(), payload.size());
71
72 this->send(encoder.block());
Junxiao Shia60d9362014-11-12 09:38:21 -070073 }
74
75 boost::asio::io_service&
76 getIoService()
77 {
78 return *m_ioService;
79 }
80
Junxiao Shi27913b42014-12-23 14:49:38 -070081public:
82 Signal<Transport, Block> onSendBlock;
Junxiao Shia60d9362014-11-12 09:38:21 -070083};
84
85DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport)
86 : Face(transport)
87 , m_transport(transport)
88{
89 this->construct(options);
90}
91
92DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport,
93 boost::asio::io_service& ioService)
94 : Face(transport, ioService)
95 , m_transport(transport)
96{
97 this->construct(options);
98}
99
100void
101DummyClientFace::construct(const Options& options)
102{
Alexander Afanasyevea719672015-02-10 20:25:23 -0800103 m_transport->onSendBlock.connect([this] (const Block& blockFromDaemon) {
Eric Newberrye3e25052015-11-28 20:37:08 -0700104 Block packet(blockFromDaemon);
105 packet.encode();
106 lp::Packet lpPacket(packet);
Alexander Afanasyevea719672015-02-10 20:25:23 -0800107
Eric Newberrye3e25052015-11-28 20:37:08 -0700108 Buffer::const_iterator begin, end;
109 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
110 Block block(&*begin, std::distance(begin, end));
Alexander Afanasyevea719672015-02-10 20:25:23 -0800111
Eric Newberrye3e25052015-11-28 20:37:08 -0700112 if (block.type() == tlv::Interest) {
113 shared_ptr<Interest> interest = make_shared<Interest>(block);
114 if (lpPacket.has<lp::NackField>()) {
115 shared_ptr<lp::Nack> nack = make_shared<lp::Nack>(std::move(*interest));
116 nack->setHeader(lpPacket.get<lp::NackField>());
117 if (lpPacket.has<lp::NextHopFaceIdField>()) {
118 nack->getLocalControlHeader().setNextHopFaceId(lpPacket.get<lp::NextHopFaceIdField>());
Eric Newberry83872fd2015-08-06 17:01:24 -0700119 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700120 onSendNack(*nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700121 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700122 else {
123 if (lpPacket.has<lp::NextHopFaceIdField>()) {
124 interest->getLocalControlHeader().
125 setNextHopFaceId(lpPacket.get<lp::NextHopFaceIdField>());
Eric Newberry83872fd2015-08-06 17:01:24 -0700126 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700127 onSendInterest(*interest);
Eric Newberry83872fd2015-08-06 17:01:24 -0700128 }
Junxiao Shi27913b42014-12-23 14:49:38 -0700129 }
Eric Newberrye3e25052015-11-28 20:37:08 -0700130 else if (block.type() == tlv::Data) {
131 shared_ptr<Data> data = make_shared<Data>(block);
132
133 if (lpPacket.has<lp::CachePolicyField>()) {
134 if (lpPacket.get<lp::CachePolicyField>().getPolicy() == lp::CachePolicyType::NO_CACHE) {
135 data->getLocalControlHeader().setCachingPolicy(nfd::LocalControlHeader::CachingPolicy::NO_CACHE);
136 }
137 }
138
139 onSendData(*data);
Junxiao Shi27913b42014-12-23 14:49:38 -0700140 }
141 });
142
Junxiao Shia60d9362014-11-12 09:38:21 -0700143 if (options.enablePacketLogging)
144 this->enablePacketLogging();
145
146 if (options.enableRegistrationReply)
147 this->enableRegistrationReply();
148}
149
150void
151DummyClientFace::enablePacketLogging()
152{
Junxiao Shi27913b42014-12-23 14:49:38 -0700153 onSendInterest.connect([this] (const Interest& interest) {
154 this->sentInterests.push_back(interest);
155 });
156 onSendData.connect([this] (const Data& data) {
157 this->sentDatas.push_back(data);
158 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700159 onSendNack.connect([this] (const lp::Nack& nack) {
160 this->sentNacks.push_back(nack);
161 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700162}
163
164void
165DummyClientFace::enableRegistrationReply()
166{
Junxiao Shi27913b42014-12-23 14:49:38 -0700167 onSendInterest.connect([this] (const Interest& interest) {
Junxiao Shia60d9362014-11-12 09:38:21 -0700168 static const Name localhostRegistration("/localhost/nfd/rib");
169 if (!localhostRegistration.isPrefixOf(interest.getName()))
170 return;
171
172 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
173 params.setFaceId(1);
174 params.setOrigin(0);
175 if (interest.getName().get(3) == name::Component("register")) {
176 params.setCost(0);
177 }
178
179 nfd::ControlResponse resp;
180 resp.setCode(200);
181 resp.setBody(params.wireEncode());
182
183 shared_ptr<Data> data = make_shared<Data>(interest.getName());
184 data->setContent(resp.wireEncode());
185
186 KeyChain keyChain;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700187 keyChain.sign(*data, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256));
Junxiao Shia60d9362014-11-12 09:38:21 -0700188
189 this->getIoService().post([this, data] { this->receive(*data); });
Junxiao Shi27913b42014-12-23 14:49:38 -0700190 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700191}
192
193template<typename Packet>
194void
195DummyClientFace::receive(const Packet& packet)
196{
Eric Newberry83872fd2015-08-06 17:01:24 -0700197 lp::Packet lpPacket(packet.wireEncode());
Alexander Afanasyevea719672015-02-10 20:25:23 -0800198
Eric Newberry83872fd2015-08-06 17:01:24 -0700199 nfd::LocalControlHeader localControlHeader = packet.getLocalControlHeader();
Alexander Afanasyevea719672015-02-10 20:25:23 -0800200
Eric Newberry83872fd2015-08-06 17:01:24 -0700201 if (localControlHeader.hasIncomingFaceId()) {
202 lpPacket.add<lp::IncomingFaceIdField>(localControlHeader.getIncomingFaceId());
Alexander Afanasyevea719672015-02-10 20:25:23 -0800203 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700204
205 if (localControlHeader.hasNextHopFaceId()) {
206 lpPacket.add<lp::NextHopFaceIdField>(localControlHeader.getNextHopFaceId());
Alexander Afanasyevea719672015-02-10 20:25:23 -0800207 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700208
209 m_transport->receive(lpPacket.wireEncode());
Junxiao Shia60d9362014-11-12 09:38:21 -0700210}
211
212template void
213DummyClientFace::receive<Interest>(const Interest& packet);
214
215template void
216DummyClientFace::receive<Data>(const Data& packet);
217
Eric Newberry83872fd2015-08-06 17:01:24 -0700218template<>
219void
220DummyClientFace::receive<lp::Nack>(const lp::Nack& nack)
221{
222 lp::Packet lpPacket;
223 lpPacket.add<lp::NackField>(nack.getHeader());
224 Block interest = nack.getInterest().wireEncode();
225 lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end()));
226
227 nfd::LocalControlHeader localControlHeader = nack.getLocalControlHeader();
228
229 if (localControlHeader.hasIncomingFaceId()) {
230 lpPacket.add<lp::IncomingFaceIdField>(localControlHeader.getIncomingFaceId());
231 }
232
233 m_transport->receive(lpPacket.wireEncode());
234}
Junxiao Shia60d9362014-11-12 09:38:21 -0700235
236shared_ptr<DummyClientFace>
237makeDummyClientFace(const DummyClientFace::Options& options)
238{
Junxiao Shia1ea5062014-12-27 22:33:39 -0700239 // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
Junxiao Shia60d9362014-11-12 09:38:21 -0700240 return shared_ptr<DummyClientFace>(
241 new DummyClientFace(options, make_shared<DummyClientFace::Transport>()));
242}
243
244shared_ptr<DummyClientFace>
245makeDummyClientFace(boost::asio::io_service& ioService,
246 const DummyClientFace::Options& options)
247{
Junxiao Shia1ea5062014-12-27 22:33:39 -0700248 // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
Junxiao Shia60d9362014-11-12 09:38:21 -0700249 return shared_ptr<DummyClientFace>(
250 new DummyClientFace(options, make_shared<DummyClientFace::Transport>(),
251 ref(ioService)));
252}
253
254} // namespace util
255} // namespace ndn