Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 University of Memphis, |
| 4 | * Regents of the University of California |
| 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | **/ |
| 21 | |
| 22 | #ifndef NLSR_TEST_DUMMY_FACE_HPP |
| 23 | #define NLSR_TEST_DUMMY_FACE_HPP |
| 24 | |
| 25 | #include <ndn-cxx/face.hpp> |
| 26 | #include <ndn-cxx/transport/transport.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | |
| 30 | class DummyTransport : public Transport |
| 31 | { |
| 32 | public: |
| 33 | void |
| 34 | receive(const Block& block) |
| 35 | { |
| 36 | m_receiveCallback(block); |
| 37 | } |
| 38 | |
| 39 | virtual void |
| 40 | close() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | virtual void |
| 45 | pause() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | virtual void |
| 50 | resume() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | virtual void |
| 55 | send(const Block& wire) |
| 56 | { |
Junxiao Shi | 92e5ffb | 2014-10-19 09:45:55 -0700 | [diff] [blame] | 57 | if (wire.type() == tlv::Interest) { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 58 | m_sentInterests->push_back(Interest(wire)); |
| 59 | } |
Junxiao Shi | 92e5ffb | 2014-10-19 09:45:55 -0700 | [diff] [blame] | 60 | else if (wire.type() == tlv::Data) { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 61 | m_sentDatas->push_back(Data(wire)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | virtual void |
| 66 | send(const Block& header, const Block& payload) |
| 67 | { |
| 68 | this->send(payload); |
| 69 | } |
| 70 | |
| 71 | public: |
| 72 | std::vector<Interest>* m_sentInterests; |
| 73 | std::vector<Data>* m_sentDatas; |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | /** \brief a Face for unit testing |
| 78 | */ |
| 79 | class DummyFace : public Face |
| 80 | { |
| 81 | public: |
| 82 | explicit |
| 83 | DummyFace(shared_ptr<DummyTransport> transport) |
| 84 | : Face(transport) |
| 85 | , m_transport(transport) |
| 86 | { |
| 87 | m_transport->m_sentInterests = &m_sentInterests; |
| 88 | m_transport->m_sentDatas = &m_sentDatas; |
| 89 | } |
| 90 | |
| 91 | /** \brief cause the Face to receive a packet |
| 92 | */ |
| 93 | template<typename Packet> |
| 94 | void |
| 95 | receive(const Packet& packet) |
| 96 | { |
| 97 | m_transport->receive(packet.wireEncode()); |
| 98 | } |
| 99 | |
| 100 | public: |
| 101 | std::vector<Interest> m_sentInterests; |
| 102 | std::vector<Data> m_sentDatas; |
| 103 | |
| 104 | private: |
| 105 | shared_ptr<DummyTransport> m_transport; |
| 106 | }; |
| 107 | |
| 108 | inline shared_ptr<DummyFace> |
| 109 | makeDummyFace() |
| 110 | { |
| 111 | return make_shared<DummyFace>(make_shared<DummyTransport>()); |
| 112 | } |
| 113 | |
| 114 | } // namespace ndn |
| 115 | |
Junxiao Shi | 92e5ffb | 2014-10-19 09:45:55 -0700 | [diff] [blame] | 116 | #endif // NLSR_TEST_DUMMY_FACE_HPP |