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