Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 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" |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 27 | #include "../lp/packet.hpp" |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 29 | #define NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED |
| 30 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 31 | namespace ndn { |
| 32 | namespace util { |
| 33 | |
| 34 | /** \brief a client-side face for unit testing |
| 35 | */ |
| 36 | class DummyClientFace : public ndn::Face |
| 37 | { |
| 38 | public: |
| 39 | /** \brief options for DummyClientFace |
| 40 | */ |
| 41 | struct Options |
| 42 | { |
| 43 | /** \brief if true, packets sent out of DummyClientFace will be appended to a container |
| 44 | */ |
| 45 | bool enablePacketLogging; |
| 46 | |
| 47 | /** \brief if true, prefix registration command will be automatically |
| 48 | * replied with a successful response |
| 49 | */ |
| 50 | bool enableRegistrationReply; |
| 51 | }; |
| 52 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 53 | /** |
| 54 | * @brief Create a dummy face with internal IO service |
| 55 | */ |
| 56 | DummyClientFace(const Options& options = DummyClientFace::DEFAULT_OPTIONS); |
| 57 | |
| 58 | /** |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 59 | * @brief Create a dummy face with internal IO service and the specified KeyChain |
| 60 | */ |
| 61 | DummyClientFace(KeyChain& keyChain, const Options& options = DummyClientFace::DEFAULT_OPTIONS); |
| 62 | |
| 63 | /** |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 64 | * @brief Create a dummy face with the provided IO service |
| 65 | */ |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 66 | DummyClientFace(boost::asio::io_service& ioService, |
| 67 | const Options& options = DummyClientFace::DEFAULT_OPTIONS); |
| 68 | |
| 69 | /** |
| 70 | * @brief Create a dummy face with the provided IO service and the specified KeyChain |
| 71 | */ |
| 72 | DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain, |
| 73 | const Options& options = DummyClientFace::DEFAULT_OPTIONS); |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 74 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 75 | /** \brief cause the Face to receive a packet |
| 76 | * \tparam Packet either Interest or Data |
| 77 | */ |
| 78 | template<typename Packet> |
| 79 | void |
| 80 | receive(const Packet& packet); |
| 81 | |
| 82 | private: // constructors |
| 83 | class Transport; |
| 84 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 85 | void |
| 86 | construct(const Options& options); |
| 87 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 88 | private: |
| 89 | void |
| 90 | enablePacketLogging(); |
| 91 | |
| 92 | void |
| 93 | enableRegistrationReply(); |
| 94 | |
| 95 | public: |
| 96 | /** \brief default options |
| 97 | * |
| 98 | * enablePacketLogging=true |
| 99 | * enableRegistrationReply=false |
| 100 | */ |
| 101 | static const Options DEFAULT_OPTIONS; |
| 102 | |
| 103 | /** \brief Interests sent out of this DummyClientFace |
| 104 | * |
| 105 | * Sent Interests are appended to this container if options.enablePacketLogger is true. |
| 106 | * User of this class is responsible for cleaning up the container, if necessary. |
| 107 | * After .expressInterest, .processEvents must be called before the Interest would show up here. |
| 108 | */ |
| 109 | std::vector<Interest> sentInterests; |
| 110 | |
| 111 | /** \brief Data sent out of this DummyClientFace |
| 112 | * |
| 113 | * Sent Data are appended to this container if options.enablePacketLogger is true. |
| 114 | * User of this class is responsible for cleaning up the container, if necessary. |
| 115 | * After .put, .processEvents must be called before the Data would show up here. |
| 116 | */ |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 117 | std::vector<Data> sentData; |
| 118 | |
| 119 | #ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED |
| 120 | std::vector<Data>& sentDatas; ///< deprecated alias to sentData |
| 121 | #endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 122 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 123 | /** \brief NACKs sent out of this DummyClientFace |
| 124 | * |
| 125 | * Sent NACKs are appended to this container if options.enablePacketLogger is true. |
| 126 | * User of this class is responsible for cleaning up the container, if necessary. |
| 127 | * After .put, .processEvents must be called before the NACK would show up here. |
| 128 | */ |
| 129 | std::vector<lp::Nack> sentNacks; |
| 130 | |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 131 | /** \brief emits whenever an Interest is sent |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 132 | * |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 133 | * After .expressInterest, .processEvents must be called before this signal would be emitted. |
| 134 | */ |
| 135 | Signal<DummyClientFace, Interest> onSendInterest; |
| 136 | |
| 137 | /** \brief emits whenever a Data packet is sent |
| 138 | * |
| 139 | * After .put, .processEvents must be called before this signal would be emitted. |
| 140 | */ |
| 141 | Signal<DummyClientFace, Data> onSendData; |
| 142 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 143 | /** \brief emits whenever a NACK is sent |
| 144 | * |
| 145 | * After .put, .processEvents must be called before this signal would be emitted. |
| 146 | */ |
| 147 | Signal<DummyClientFace, lp::Nack> onSendNack; |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 148 | |
| 149 | private: |
| 150 | std::unique_ptr<KeyChain> m_internalKeyChain; |
| 151 | KeyChain& m_keyChain; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Eric Newberry | 664dc03 | 2015-11-23 12:49:50 -0700 | [diff] [blame] | 154 | template<> |
| 155 | void |
| 156 | DummyClientFace::receive(const lp::Nack& nack); |
| 157 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 158 | #ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED |
| 159 | /** |
| 160 | * @brief Create a dummy face with internal IO service |
| 161 | * @deprecated Use the DummyFace constructor directly |
| 162 | */ |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 163 | shared_ptr<DummyClientFace> |
| 164 | makeDummyClientFace(const DummyClientFace::Options& options = DummyClientFace::DEFAULT_OPTIONS); |
| 165 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 166 | /** |
| 167 | * @brief Create a dummy face with the provided IO service |
| 168 | * @deprecated Use the DummyFace constructor directly |
| 169 | */ |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 170 | shared_ptr<DummyClientFace> |
| 171 | makeDummyClientFace(boost::asio::io_service& ioService, |
| 172 | const DummyClientFace::Options& options = DummyClientFace::DEFAULT_OPTIONS); |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 173 | #endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 174 | |
| 175 | } // namespace util |
| 176 | } // namespace ndn |
| 177 | |
| 178 | #endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP |