Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 22 | #ifndef NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP |
| 23 | #define NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 24 | |
| 25 | #include "face.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 26 | #include "transport/transport.hpp" |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 27 | #include "management/nfd-controller.hpp" |
| 28 | #include "management/nfd-control-response.hpp" |
| 29 | #include "util/event-emitter.hpp" |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 32 | namespace tests { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 34 | class DummyClientTransport : public ndn::Transport |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 35 | { |
| 36 | public: |
| 37 | void |
| 38 | receive(const Block& block) |
| 39 | { |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 40 | if (static_cast<bool>(m_receiveCallback)) |
| 41 | m_receiveCallback(block); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 42 | } |
| 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 DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 62 | if (wire.type() == tlv::Interest) { |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 63 | shared_ptr<Interest> interest = make_shared<Interest>(wire); |
| 64 | (*m_onInterest)(*interest, this); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 65 | } |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 66 | else if (wire.type() == tlv::Data) { |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 67 | shared_ptr<Data> data = make_shared<Data>(wire); |
| 68 | (*m_onData)(*data, this); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | virtual void |
| 73 | send(const Block& header, const Block& payload) |
| 74 | { |
| 75 | this->send(payload); |
| 76 | } |
| 77 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 78 | boost::asio::io_service& |
| 79 | getIoService() |
| 80 | { |
| 81 | return *m_ioService; |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | friend class DummyClientFace; |
| 86 | util::EventEmitter<Interest, DummyClientTransport*>* m_onInterest; |
| 87 | util::EventEmitter<Data, DummyClientTransport*>* m_onData; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 91 | /** \brief Callback to connect |
| 92 | */ |
| 93 | inline void |
| 94 | replyNfdRibCommands(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 Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 117 | /** \brief a client-side face for unit testing |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 118 | */ |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 119 | class DummyClientFace : public ndn::Face |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 120 | { |
| 121 | public: |
| 122 | explicit |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 123 | DummyClientFace(shared_ptr<DummyClientTransport> transport) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 124 | : Face(transport) |
| 125 | , m_transport(transport) |
| 126 | { |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 127 | m_transport->m_onInterest = &onInterest; |
| 128 | m_transport->m_onData = &onData; |
| 129 | |
| 130 | enablePacketLogging(); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 133 | DummyClientFace(shared_ptr<DummyClientTransport> transport, boost::asio::io_service& ioService) |
| 134 | : Face(transport, ioService) |
| 135 | , m_transport(transport) |
| 136 | { |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 137 | m_transport->m_onInterest = &onInterest; |
| 138 | m_transport->m_onData = &onData; |
| 139 | |
| 140 | enablePacketLogging(); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 143 | /** \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 Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 152 | 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 Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 172 | public: |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 173 | /** \brief sent Interests |
| 174 | * \note After .expressInterest, .processEvents must be called before |
| 175 | * the Interest would show up here. |
| 176 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 177 | std::vector<Interest> m_sentInterests; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 178 | /** \brief sent Datas |
| 179 | * \note After .put, .processEvents must be called before |
| 180 | * the Interest would show up here. |
| 181 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 182 | std::vector<Data> m_sentDatas; |
| 183 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 184 | public: |
| 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 Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 197 | private: |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 198 | shared_ptr<DummyClientTransport> m_transport; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 201 | inline shared_ptr<DummyClientFace> |
| 202 | makeDummyClientFace() |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 203 | { |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 204 | return make_shared<DummyClientFace>(make_shared<DummyClientTransport>()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 207 | inline shared_ptr<DummyClientFace> |
| 208 | makeDummyClientFace(boost::asio::io_service& ioService) |
| 209 | { |
| 210 | return make_shared<DummyClientFace>(make_shared<DummyClientTransport>(), ref(ioService)); |
| 211 | } |
| 212 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 213 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 214 | } // namespace tests |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 215 | } // namespace ndn |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 216 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 217 | #endif // NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP |