blob: 82d9c7cfd78b27edee54a9229f53112901624dde [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070020 */
21
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070022#ifndef NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP
23#define NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070024
25#include "face.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070026#include "transport/transport.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040027#include "management/nfd-controller.hpp"
28#include "management/nfd-control-response.hpp"
29#include "util/event-emitter.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070030
31namespace ndn {
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070032namespace tests {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070033
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070034class DummyClientTransport : public ndn::Transport
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070035{
36public:
37 void
38 receive(const Block& block)
39 {
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070040 if (static_cast<bool>(m_receiveCallback))
41 m_receiveCallback(block);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -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 {
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060062 if (wire.type() == tlv::Interest) {
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040063 shared_ptr<Interest> interest = make_shared<Interest>(wire);
64 (*m_onInterest)(*interest, this);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070065 }
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060066 else if (wire.type() == tlv::Data) {
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040067 shared_ptr<Data> data = make_shared<Data>(wire);
68 (*m_onData)(*data, this);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070069 }
70 }
71
72 virtual void
73 send(const Block& header, const Block& payload)
74 {
75 this->send(payload);
76 }
77
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040078 boost::asio::io_service&
79 getIoService()
80 {
81 return *m_ioService;
82 }
83
84private:
85 friend class DummyClientFace;
86 util::EventEmitter<Interest, DummyClientTransport*>* m_onInterest;
87 util::EventEmitter<Data, DummyClientTransport*>* m_onData;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070088};
89
90
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040091/** \brief Callback to connect
92 */
93inline void
94replyNfdRibCommands(const Interest& interest, DummyClientTransport* transport)
95{
96 static const Name localhostRegistration("/localhost/nfd/rib");
97 if (localhostRegistration.isPrefixOf(interest.getName())) {
98 shared_ptr<Data> okResponse = make_shared<Data>(interest.getName());
99 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
100 params.setFaceId(1);
101 params.setOrigin(0);
102 if (interest.getName().get(3) == name::Component("register")) {
103 params.setCost(0);
104 }
105 nfd::ControlResponse resp;
106 resp.setCode(200);
107 resp.setBody(params.wireEncode());
108 okResponse->setContent(resp.wireEncode());
109 KeyChain keyChain;
110 keyChain.signWithSha256(*okResponse);
111
112 transport->getIoService().post(bind(&DummyClientTransport::receive, transport,
113 okResponse->wireEncode()));
114 }
115}
116
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700117/** \brief a client-side face for unit testing
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700118 */
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700119class DummyClientFace : public ndn::Face
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700120{
121public:
122 explicit
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700123 DummyClientFace(shared_ptr<DummyClientTransport> transport)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700124 : Face(transport)
125 , m_transport(transport)
126 {
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400127 m_transport->m_onInterest = &onInterest;
128 m_transport->m_onData = &onData;
129
130 enablePacketLogging();
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700131 }
132
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500133 DummyClientFace(shared_ptr<DummyClientTransport> transport, boost::asio::io_service& ioService)
134 : Face(transport, ioService)
135 , m_transport(transport)
136 {
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400137 m_transport->m_onInterest = &onInterest;
138 m_transport->m_onData = &onData;
139
140 enablePacketLogging();
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500141 }
142
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700143 /** \brief cause the Face to receive a packet
144 */
145 template<typename Packet>
146 void
147 receive(const Packet& packet)
148 {
149 m_transport->receive(packet.wireEncode());
150 }
151
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400152 void
153 enablePacketLogging()
154 {
155 // @todo Replace with C++11 lambdas
156
157 onInterest += bind(static_cast<void(std::vector<Interest>::*)(const Interest&)>(
158 &std::vector<Interest>::push_back),
159 &m_sentInterests, _1);
160
161 onData += bind(static_cast<void(std::vector<Data>::*)(const Data&)>(
162 &std::vector<Data>::push_back),
163 &m_sentDatas, _1);
164 }
165
166 void
167 enableRegistrationReply()
168 {
169 onInterest += &replyNfdRibCommands;
170 }
171
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700172public:
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700173 /** \brief sent Interests
174 * \note After .expressInterest, .processEvents must be called before
175 * the Interest would show up here.
176 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700177 std::vector<Interest> m_sentInterests;
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700178 /** \brief sent Datas
179 * \note After .put, .processEvents must be called before
180 * the Interest would show up here.
181 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700182 std::vector<Data> m_sentDatas;
183
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400184public:
185 /** \brief Event to be called whenever an Interest is received
186 * \note After .expressInterest, .processEvents must be called before
187 * the Interest would show up here.
188 */
189 util::EventEmitter<Interest, DummyClientTransport*> onInterest;
190
191 /** \brief Event to be called whenever a Data packet is received
192 * \note After .put, .processEvents must be called before
193 * the Interest would show up here.
194 */
195 util::EventEmitter<Data, DummyClientTransport*> onData;
196
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700197private:
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700198 shared_ptr<DummyClientTransport> m_transport;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700199};
200
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700201inline shared_ptr<DummyClientFace>
202makeDummyClientFace()
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700203{
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700204 return make_shared<DummyClientFace>(make_shared<DummyClientTransport>());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700205}
206
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500207inline shared_ptr<DummyClientFace>
208makeDummyClientFace(boost::asio::io_service& ioService)
209{
210 return make_shared<DummyClientFace>(make_shared<DummyClientTransport>(), ref(ioService));
211}
212
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400213
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700214} // namespace tests
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700215} // namespace ndn
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700216
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700217#endif // NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP