Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 13 | #ifndef NDN_TESTS_TRANSPORT_DUMMY_FACE_HPP |
| 14 | #define NDN_TESTS_TRANSPORT_DUMMY_FACE_HPP |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 15 | |
| 16 | #include "face.hpp" |
| 17 | |
| 18 | namespace ndn { |
| 19 | |
| 20 | class DummyTransport : public Transport |
| 21 | { |
| 22 | public: |
| 23 | void |
| 24 | receive(const Block& block) |
| 25 | { |
| 26 | m_receiveCallback(block); |
| 27 | } |
| 28 | |
| 29 | virtual void |
| 30 | close() |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | virtual void |
| 35 | pause() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | virtual void |
| 40 | resume() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | virtual void |
| 45 | send(const Block& wire) |
| 46 | { |
| 47 | if (wire.type() == Tlv::Interest) { |
| 48 | m_sentInterests->push_back(Interest(wire)); |
| 49 | } |
| 50 | else if (wire.type() == Tlv::Data) { |
| 51 | m_sentDatas->push_back(Data(wire)); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | virtual void |
| 56 | send(const Block& header, const Block& payload) |
| 57 | { |
| 58 | this->send(payload); |
| 59 | } |
| 60 | |
| 61 | public: |
| 62 | std::vector<Interest>* m_sentInterests; |
| 63 | std::vector<Data>* m_sentDatas; |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | /** \brief a Face for unit testing |
| 68 | */ |
| 69 | class DummyFace : public Face |
| 70 | { |
| 71 | public: |
| 72 | explicit |
| 73 | DummyFace(shared_ptr<DummyTransport> transport) |
| 74 | : Face(transport) |
| 75 | , m_transport(transport) |
| 76 | { |
| 77 | m_transport->m_sentInterests = &m_sentInterests; |
| 78 | m_transport->m_sentDatas = &m_sentDatas; |
| 79 | } |
| 80 | |
| 81 | /** \brief cause the Face to receive a packet |
| 82 | */ |
| 83 | template<typename Packet> |
| 84 | void |
| 85 | receive(const Packet& packet) |
| 86 | { |
| 87 | m_transport->receive(packet.wireEncode()); |
| 88 | } |
| 89 | |
| 90 | public: |
| 91 | std::vector<Interest> m_sentInterests; |
| 92 | std::vector<Data> m_sentDatas; |
| 93 | |
| 94 | private: |
| 95 | shared_ptr<DummyTransport> m_transport; |
| 96 | }; |
| 97 | |
| 98 | inline shared_ptr<DummyFace> |
| 99 | makeDummyFace() |
| 100 | { |
| 101 | return make_shared<DummyFace>(make_shared<DummyTransport>()); |
| 102 | } |
| 103 | |
| 104 | } // namespace ndn |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 105 | |
| 106 | #endif // NDN_TESTS_TRANSPORT_DUMMY_FACE_HPP |