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 | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 22 | #ifndef NDN_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_FACE_HPP |
| 23 | #define NDN_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_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" |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 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 | { |
| 57 | if (wire.type() == Tlv::Interest) { |
| 58 | m_sentInterests->push_back(Interest(wire)); |
| 59 | } |
| 60 | else if (wire.type() == Tlv::Data) { |
| 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 |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 115 | |
Alexander Afanasyev | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 116 | #endif // NDN_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_FACE_HPP |