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