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