blob: b9ba1e5ec7598375d8ef819ed226dc08af80b890 [file] [log] [blame]
Junxiao Shia60d9362014-11-12 09:38:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
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
36 receive(const Block& block)
37 {
38 if (static_cast<bool>(m_receiveCallback))
39 m_receiveCallback(block);
40 }
41
42 virtual void
43 close()
44 {
45 }
46
47 virtual void
48 pause()
49 {
50 }
51
52 virtual void
53 resume()
54 {
55 }
56
57 virtual void
58 send(const Block& wire)
59 {
Junxiao Shi27913b42014-12-23 14:49:38 -070060 onSendBlock(wire);
Junxiao Shia60d9362014-11-12 09:38:21 -070061 }
62
63 virtual void
64 send(const Block& header, const Block& payload)
65 {
66 this->send(payload);
67 }
68
69 boost::asio::io_service&
70 getIoService()
71 {
72 return *m_ioService;
73 }
74
Junxiao Shi27913b42014-12-23 14:49:38 -070075public:
76 Signal<Transport, Block> onSendBlock;
Junxiao Shia60d9362014-11-12 09:38:21 -070077};
78
79DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport)
80 : Face(transport)
81 , m_transport(transport)
82{
83 this->construct(options);
84}
85
86DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport,
87 boost::asio::io_service& ioService)
88 : Face(transport, ioService)
89 , m_transport(transport)
90{
91 this->construct(options);
92}
93
94void
95DummyClientFace::construct(const Options& options)
96{
Junxiao Shi27913b42014-12-23 14:49:38 -070097 m_transport->onSendBlock.connect([this] (const Block& wire) {
98 if (wire.type() == tlv::Interest) {
99 shared_ptr<Interest> interest = make_shared<Interest>(wire);
100 onSendInterest(*interest);
101 }
102 else if (wire.type() == tlv::Data) {
103 shared_ptr<Data> data = make_shared<Data>(wire);
104 onSendData(*data);
105 }
106 });
107
Junxiao Shia60d9362014-11-12 09:38:21 -0700108 if (options.enablePacketLogging)
109 this->enablePacketLogging();
110
111 if (options.enableRegistrationReply)
112 this->enableRegistrationReply();
113}
114
115void
116DummyClientFace::enablePacketLogging()
117{
Junxiao Shi27913b42014-12-23 14:49:38 -0700118 onSendInterest.connect([this] (const Interest& interest) {
119 this->sentInterests.push_back(interest);
120 });
121 onSendData.connect([this] (const Data& data) {
122 this->sentDatas.push_back(data);
123 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700124}
125
126void
127DummyClientFace::enableRegistrationReply()
128{
Junxiao Shi27913b42014-12-23 14:49:38 -0700129 onSendInterest.connect([this] (const Interest& interest) {
Junxiao Shia60d9362014-11-12 09:38:21 -0700130 static const Name localhostRegistration("/localhost/nfd/rib");
131 if (!localhostRegistration.isPrefixOf(interest.getName()))
132 return;
133
134 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
135 params.setFaceId(1);
136 params.setOrigin(0);
137 if (interest.getName().get(3) == name::Component("register")) {
138 params.setCost(0);
139 }
140
141 nfd::ControlResponse resp;
142 resp.setCode(200);
143 resp.setBody(params.wireEncode());
144
145 shared_ptr<Data> data = make_shared<Data>(interest.getName());
146 data->setContent(resp.wireEncode());
147
148 KeyChain keyChain;
149 keyChain.signWithSha256(*data);
150
151 this->getIoService().post([this, data] { this->receive(*data); });
Junxiao Shi27913b42014-12-23 14:49:38 -0700152 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700153}
154
155template<typename Packet>
156void
157DummyClientFace::receive(const Packet& packet)
158{
159 m_transport->receive(packet.wireEncode());
160}
161
162template void
163DummyClientFace::receive<Interest>(const Interest& packet);
164
165template void
166DummyClientFace::receive<Data>(const Data& packet);
167
168
169shared_ptr<DummyClientFace>
170makeDummyClientFace(const DummyClientFace::Options& options)
171{
Junxiao Shia1ea5062014-12-27 22:33:39 -0700172 // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
Junxiao Shia60d9362014-11-12 09:38:21 -0700173 return shared_ptr<DummyClientFace>(
174 new DummyClientFace(options, make_shared<DummyClientFace::Transport>()));
175}
176
177shared_ptr<DummyClientFace>
178makeDummyClientFace(boost::asio::io_service& ioService,
179 const DummyClientFace::Options& options)
180{
Junxiao Shia1ea5062014-12-27 22:33:39 -0700181 // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
Junxiao Shia60d9362014-11-12 09:38:21 -0700182 return shared_ptr<DummyClientFace>(
183 new DummyClientFace(options, make_shared<DummyClientFace::Transport>(),
184 ref(ioService)));
185}
186
187} // namespace util
188} // namespace ndn