blob: 649e71492130fd4fb00ae21a9e29be5f8506092f [file] [log] [blame]
Shock Jiang0b165f42014-10-24 09:08:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * Copyright (c) 2013-2014 Regents of the University of California.
22 *
23 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
24 *
25 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
26 * terms of the GNU Lesser General Public License as published by the Free Software
27 * Foundation, either version 3 of the License, or (at your option) any later version.
28 *
29 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
30 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
31 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
32 *
33 * You should have received copies of the GNU General Public License and GNU Lesser
34 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
35 * <http://www.gnu.org/licenses/>.
36 *
37 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
38 */
39
40#ifndef NDNS_TESTS_UNIT_DUMMY_CLIENT_FACE_HPP
41#define NDNS_TESTS_UNIT_DUMMY_CLIENT_FACE_HPP
42
43#include <ndn-cxx/face.hpp>
44#include <ndn-cxx/transport/transport.hpp>
45#include <ndn-cxx/management/nfd-controller.hpp>
46#include <ndn-cxx/management/nfd-control-response.hpp>
47#include <ndn-cxx/util/event-emitter.hpp>
48
49
50namespace ndn {
51namespace tests {
52
53class DummyClientTransport : public ndn::Transport
54{
55public:
56 void
57 receive(const Block& block)
58 {
59 if (static_cast<bool>(m_receiveCallback))
60 m_receiveCallback(block);
61 }
62
63 virtual void
64 close()
65 {
66 }
67
68 virtual void
69 pause()
70 {
71 }
72
73 virtual void
74 resume()
75 {
76 }
77
78 virtual void
79 send(const Block& wire)
80 {
81 if (wire.type() == tlv::Interest) {
82 shared_ptr<Interest> interest = make_shared<Interest>(wire);
83 (*m_onInterest)(*interest, this);
84 }
85 else if (wire.type() == tlv::Data) {
86 shared_ptr<Data> data = make_shared<Data>(wire);
87 (*m_onData)(*data, this);
88 }
89 }
90
91 virtual void
92 send(const Block& header, const Block& payload)
93 {
94 this->send(payload);
95 }
96
97 boost::asio::io_service&
98 getIoService()
99 {
100 return *m_ioService;
101 }
102
103private:
104 friend class DummyClientFace;
105 util::EventEmitter<Interest, DummyClientTransport*>* m_onInterest;
106 util::EventEmitter<Data, DummyClientTransport*>* m_onData;
107};
108
109
110/** \brief Callback to connect
111 */
112inline void
113replyNfdRibCommands(const Interest& interest, DummyClientTransport* transport)
114{
115 static const Name localhostRegistration("/localhost/nfd/rib");
116 if (localhostRegistration.isPrefixOf(interest.getName())) {
117 shared_ptr<Data> okResponse = make_shared<Data>(interest.getName());
118 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
119 params.setFaceId(1);
120 params.setOrigin(0);
121 if (interest.getName().get(3) == name::Component("register")) {
122 params.setCost(0);
123 }
124 nfd::ControlResponse resp;
125 resp.setCode(200);
126 resp.setBody(params.wireEncode());
127 okResponse->setContent(resp.wireEncode());
128 KeyChain keyChain;
129 keyChain.signWithSha256(*okResponse);
130
131 transport->getIoService().post(bind(&DummyClientTransport::receive, transport,
132 okResponse->wireEncode()));
133 }
134}
135
136/** \brief a client-side face for unit testing
137 */
138class DummyClientFace : public ndn::Face
139{
140public:
141 explicit
142 DummyClientFace(shared_ptr<DummyClientTransport> transport)
143 : Face(transport)
144 , m_transport(transport)
145 {
146 m_transport->m_onInterest = &onInterest;
147 m_transport->m_onData = &onData;
148
149 enablePacketLogging();
150 }
151
152 DummyClientFace(shared_ptr<DummyClientTransport> transport, boost::asio::io_service& ioService)
153 : Face(transport, ioService)
154 , m_transport(transport)
155 {
156 m_transport->m_onInterest = &onInterest;
157 m_transport->m_onData = &onData;
158
159 enablePacketLogging();
160 }
161
162 /** \brief cause the Face to receive a packet
163 */
164 template<typename Packet>
165 void
166 receive(const Packet& packet)
167 {
168 m_transport->receive(packet.wireEncode());
169 }
170
171 void
172 enablePacketLogging()
173 {
174 // @todo Replace with C++11 lambdas
175
176 onInterest += bind(static_cast<void(std::vector<Interest>::*)(const Interest&)>(
177 &std::vector<Interest>::push_back),
178 &m_sentInterests, _1);
179
180 onData += bind(static_cast<void(std::vector<Data>::*)(const Data&)>(
181 &std::vector<Data>::push_back),
182 &m_sentDatas, _1);
183 }
184
185 void
186 enableRegistrationReply()
187 {
188 onInterest += &replyNfdRibCommands;
189 }
190
191public:
192 /** \brief sent Interests
193 * \note After .expressInterest, .processEvents must be called before
194 * the Interest would show up here.
195 */
196 std::vector<Interest> m_sentInterests;
197 /** \brief sent Datas
198 * \note After .put, .processEvents must be called before
199 * the Interest would show up here.
200 */
201 std::vector<Data> m_sentDatas;
202
203public:
204 /** \brief Event to be called whenever an Interest is received
205 * \note After .expressInterest, .processEvents must be called before
206 * the Interest would show up here.
207 */
208 util::EventEmitter<Interest, DummyClientTransport*> onInterest;
209
210 /** \brief Event to be called whenever a Data packet is received
211 * \note After .put, .processEvents must be called before
212 * the Interest would show up here.
213 */
214 util::EventEmitter<Data, DummyClientTransport*> onData;
215
216private:
217 shared_ptr<DummyClientTransport> m_transport;
218};
219
220inline shared_ptr<DummyClientFace>
221makeDummyClientFace()
222{
223 return make_shared<DummyClientFace>(make_shared<DummyClientTransport>());
224}
225
226inline shared_ptr<DummyClientFace>
227makeDummyClientFace(boost::asio::io_service& ioService)
228{
229 return make_shared<DummyClientFace>(make_shared<DummyClientTransport>(), ref(ioService));
230}
231
232
233} // namespace tests
234} // namespace ndn
235
236#endif // NDNS_TESTS_UNIT_DUMMY_CLIENT_FACE_HPP