Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_UTIL_DUMMY_CLIENT_FACE_HPP |
| 23 | #define NDN_UTIL_DUMMY_CLIENT_FACE_HPP |
| 24 | |
| 25 | #include "../face.hpp" |
| 26 | #include "event-emitter.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace util { |
| 30 | |
| 31 | /** \brief a client-side face for unit testing |
| 32 | */ |
| 33 | class DummyClientFace : public ndn::Face |
| 34 | { |
| 35 | public: |
| 36 | /** \brief options for DummyClientFace |
| 37 | */ |
| 38 | struct Options |
| 39 | { |
| 40 | /** \brief if true, packets sent out of DummyClientFace will be appended to a container |
| 41 | */ |
| 42 | bool enablePacketLogging; |
| 43 | |
| 44 | /** \brief if true, prefix registration command will be automatically |
| 45 | * replied with a successful response |
| 46 | */ |
| 47 | bool enableRegistrationReply; |
| 48 | }; |
| 49 | |
| 50 | /** \brief cause the Face to receive a packet |
| 51 | * \tparam Packet either Interest or Data |
| 52 | */ |
| 53 | template<typename Packet> |
| 54 | void |
| 55 | receive(const Packet& packet); |
| 56 | |
| 57 | private: // constructors |
| 58 | class Transport; |
| 59 | |
| 60 | // constructors are private; use makeDummyClientFace to create DummyClientFace |
| 61 | |
| 62 | DummyClientFace(const Options& options, shared_ptr<Transport> transport); |
| 63 | |
| 64 | DummyClientFace(const Options& options, shared_ptr<Transport> transport, |
| 65 | boost::asio::io_service& ioService); |
| 66 | |
| 67 | void |
| 68 | construct(const Options& options); |
| 69 | |
| 70 | friend shared_ptr<DummyClientFace> |
| 71 | makeDummyClientFace(const DummyClientFace::Options& options); |
| 72 | |
| 73 | friend shared_ptr<DummyClientFace> |
| 74 | makeDummyClientFace(boost::asio::io_service& ioService, const DummyClientFace::Options& options); |
| 75 | |
| 76 | private: |
| 77 | void |
| 78 | enablePacketLogging(); |
| 79 | |
| 80 | void |
| 81 | enableRegistrationReply(); |
| 82 | |
| 83 | public: |
| 84 | /** \brief default options |
| 85 | * |
| 86 | * enablePacketLogging=true |
| 87 | * enableRegistrationReply=false |
| 88 | */ |
| 89 | static const Options DEFAULT_OPTIONS; |
| 90 | |
| 91 | /** \brief Interests sent out of this DummyClientFace |
| 92 | * |
| 93 | * Sent Interests are appended to this container if options.enablePacketLogger is true. |
| 94 | * User of this class is responsible for cleaning up the container, if necessary. |
| 95 | * After .expressInterest, .processEvents must be called before the Interest would show up here. |
| 96 | */ |
| 97 | std::vector<Interest> sentInterests; |
| 98 | |
| 99 | /** \brief Data sent out of this DummyClientFace |
| 100 | * |
| 101 | * Sent Data are appended to this container if options.enablePacketLogger is true. |
| 102 | * User of this class is responsible for cleaning up the container, if necessary. |
| 103 | * After .put, .processEvents must be called before the Data would show up here. |
| 104 | */ |
| 105 | std::vector<Data> sentDatas; |
| 106 | |
| 107 | /** \brief Event to be called whenever an Interest is received |
| 108 | * |
| 109 | * After .expressInterest, .processEvents must be called before this event would be triggered. |
| 110 | */ |
| 111 | util::EventEmitter<Interest> onInterest; |
| 112 | |
| 113 | /** \brief Event to be called whenever a Data packet is received |
| 114 | * |
| 115 | * After .put, .processEvents must be called before this event would be triggered. |
| 116 | */ |
| 117 | util::EventEmitter<Data> onData; |
| 118 | |
| 119 | private: |
| 120 | shared_ptr<Transport> m_transport; |
| 121 | }; |
| 122 | |
| 123 | shared_ptr<DummyClientFace> |
| 124 | makeDummyClientFace(const DummyClientFace::Options& options = DummyClientFace::DEFAULT_OPTIONS); |
| 125 | |
| 126 | shared_ptr<DummyClientFace> |
| 127 | makeDummyClientFace(boost::asio::io_service& ioService, |
| 128 | const DummyClientFace::Options& options = DummyClientFace::DEFAULT_OPTIONS); |
| 129 | |
| 130 | } // namespace util |
| 131 | } // namespace ndn |
| 132 | |
| 133 | #endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP |