Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 1 | /* -*- 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 | #ifndef NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP |
| 23 | #define NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP |
| 24 | |
| 25 | #include <ndn-cxx/face.hpp> |
| 26 | #include <ndn-cxx/transport/transport.hpp> |
| 27 | |
| 28 | namespace chronos { |
| 29 | namespace test { |
| 30 | |
| 31 | class DummyClientTransport : public ndn::Transport |
| 32 | { |
| 33 | public: |
| 34 | void |
| 35 | receive(const Block& block) |
| 36 | { |
| 37 | if (static_cast<bool>(m_receiveCallback)) |
| 38 | m_receiveCallback(block); |
| 39 | } |
| 40 | |
| 41 | virtual void |
| 42 | close() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | virtual void |
| 47 | pause() |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | virtual void |
| 52 | resume() |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | virtual void |
| 57 | send(const Block& wire) |
| 58 | { |
| 59 | if (wire.type() == tlv::Interest) { |
| 60 | m_sentInterests->push_back(Interest(wire)); |
| 61 | } |
| 62 | else if (wire.type() == tlv::Data) { |
| 63 | m_sentDatas->push_back(Data(wire)); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | virtual void |
| 68 | send(const Block& header, const Block& payload) |
| 69 | { |
| 70 | this->send(payload); |
| 71 | } |
| 72 | |
| 73 | public: |
| 74 | std::vector<Interest>* m_sentInterests; |
| 75 | std::vector<Data>* m_sentDatas; |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | /** \brief a client-side face for unit testing |
| 80 | */ |
| 81 | class DummyClientFace : public ndn::Face |
| 82 | { |
| 83 | public: |
| 84 | explicit |
| 85 | DummyClientFace(shared_ptr<DummyClientTransport> transport) |
| 86 | : Face(transport) |
| 87 | , m_transport(transport) |
| 88 | { |
| 89 | m_transport->m_sentInterests = &m_sentInterests; |
| 90 | m_transport->m_sentDatas = &m_sentDatas; |
| 91 | } |
| 92 | |
| 93 | DummyClientFace(shared_ptr<DummyClientTransport> transport, boost::asio::io_service& ioService) |
| 94 | : Face(transport, ioService) |
| 95 | , m_transport(transport) |
| 96 | { |
| 97 | m_transport->m_sentInterests = &m_sentInterests; |
| 98 | m_transport->m_sentDatas = &m_sentDatas; |
| 99 | } |
| 100 | |
| 101 | /** \brief cause the Face to receive a packet |
| 102 | */ |
| 103 | template<typename Packet> |
| 104 | void |
| 105 | receive(const Packet& packet) |
| 106 | { |
| 107 | m_transport->receive(packet.wireEncode()); |
| 108 | } |
| 109 | |
| 110 | public: |
| 111 | /** \brief sent Interests |
| 112 | * \note After .expressInterest, .processEvents must be called before |
| 113 | * the Interest would show up here. |
| 114 | */ |
| 115 | std::vector<Interest> m_sentInterests; |
| 116 | /** \brief sent Datas |
| 117 | * \note After .put, .processEvents must be called before |
| 118 | * the Interest would show up here. |
| 119 | */ |
| 120 | std::vector<Data> m_sentDatas; |
| 121 | |
| 122 | private: |
| 123 | shared_ptr<DummyClientTransport> m_transport; |
| 124 | }; |
| 125 | |
| 126 | inline shared_ptr<DummyClientFace> |
| 127 | makeDummyClientFace() |
| 128 | { |
| 129 | return make_shared<DummyClientFace>(make_shared<DummyClientTransport>()); |
| 130 | } |
| 131 | |
| 132 | inline shared_ptr<DummyClientFace> |
| 133 | makeDummyClientFace(boost::asio::io_service& ioService) |
| 134 | { |
| 135 | return make_shared<DummyClientFace>(make_shared<DummyClientTransport>(), ref(ioService)); |
| 136 | } |
| 137 | |
| 138 | } // namespace tests |
| 139 | } // namespace ndn |
| 140 | |
| 141 | #endif // NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP |