blob: 2031a01cbe8573283268c2a9d7bed3adf9675867 [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
108 onSendInterest.connect([this] (const Interest& interest) { onInterest(interest); });
109 onSendData.connect([this] (const Data& data) { onData(data); });
Junxiao Shia60d9362014-11-12 09:38:21 -0700110
111 if (options.enablePacketLogging)
112 this->enablePacketLogging();
113
114 if (options.enableRegistrationReply)
115 this->enableRegistrationReply();
116}
117
118void
119DummyClientFace::enablePacketLogging()
120{
Junxiao Shi27913b42014-12-23 14:49:38 -0700121 onSendInterest.connect([this] (const Interest& interest) {
122 this->sentInterests.push_back(interest);
123 });
124 onSendData.connect([this] (const Data& data) {
125 this->sentDatas.push_back(data);
126 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700127}
128
129void
130DummyClientFace::enableRegistrationReply()
131{
Junxiao Shi27913b42014-12-23 14:49:38 -0700132 onSendInterest.connect([this] (const Interest& interest) {
Junxiao Shia60d9362014-11-12 09:38:21 -0700133 static const Name localhostRegistration("/localhost/nfd/rib");
134 if (!localhostRegistration.isPrefixOf(interest.getName()))
135 return;
136
137 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
138 params.setFaceId(1);
139 params.setOrigin(0);
140 if (interest.getName().get(3) == name::Component("register")) {
141 params.setCost(0);
142 }
143
144 nfd::ControlResponse resp;
145 resp.setCode(200);
146 resp.setBody(params.wireEncode());
147
148 shared_ptr<Data> data = make_shared<Data>(interest.getName());
149 data->setContent(resp.wireEncode());
150
151 KeyChain keyChain;
152 keyChain.signWithSha256(*data);
153
154 this->getIoService().post([this, data] { this->receive(*data); });
Junxiao Shi27913b42014-12-23 14:49:38 -0700155 });
Junxiao Shia60d9362014-11-12 09:38:21 -0700156}
157
158template<typename Packet>
159void
160DummyClientFace::receive(const Packet& packet)
161{
162 m_transport->receive(packet.wireEncode());
163}
164
165template void
166DummyClientFace::receive<Interest>(const Interest& packet);
167
168template void
169DummyClientFace::receive<Data>(const Data& packet);
170
171
172shared_ptr<DummyClientFace>
173makeDummyClientFace(const DummyClientFace::Options& options)
174{
Junxiao Shia1ea5062014-12-27 22:33:39 -0700175 // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
Junxiao Shia60d9362014-11-12 09:38:21 -0700176 return shared_ptr<DummyClientFace>(
177 new DummyClientFace(options, make_shared<DummyClientFace::Transport>()));
178}
179
180shared_ptr<DummyClientFace>
181makeDummyClientFace(boost::asio::io_service& ioService,
182 const DummyClientFace::Options& options)
183{
Junxiao Shia1ea5062014-12-27 22:33:39 -0700184 // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
Junxiao Shia60d9362014-11-12 09:38:21 -0700185 return shared_ptr<DummyClientFace>(
186 new DummyClientFace(options, make_shared<DummyClientFace::Transport>(),
187 ref(ioService)));
188}
189
190} // namespace util
191} // namespace ndn