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 | |
| 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 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 51 | /** |
| 52 | * @brief Create a dummy face with internal IO service |
| 53 | */ |
| 54 | DummyClientFace(const Options& options = DummyClientFace::DEFAULT_OPTIONS); |
| 55 | |
| 56 | /** |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 57 | * @brief Create a dummy face with internal IO service and the specified KeyChain |
| 58 | */ |
| 59 | DummyClientFace(KeyChain& keyChain, const Options& options = DummyClientFace::DEFAULT_OPTIONS); |
| 60 | |
| 61 | /** |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 62 | * @brief Create a dummy face with the provided IO service |
| 63 | */ |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 64 | DummyClientFace(boost::asio::io_service& ioService, |
| 65 | const Options& options = DummyClientFace::DEFAULT_OPTIONS); |
| 66 | |
| 67 | /** |
| 68 | * @brief Create a dummy face with the provided IO service and the specified KeyChain |
| 69 | */ |
| 70 | DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain, |
| 71 | const Options& options = DummyClientFace::DEFAULT_OPTIONS); |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 72 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 73 | /** \brief cause the Face to receive a packet |
| 74 | * \tparam Packet either Interest or Data |
| 75 | */ |
| 76 | template<typename Packet> |
| 77 | void |
| 78 | receive(const Packet& packet); |
| 79 | |
| 80 | private: // constructors |
| 81 | class Transport; |
| 82 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 83 | void |
| 84 | construct(const Options& options); |
| 85 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 86 | private: |
| 87 | void |
| 88 | enablePacketLogging(); |
| 89 | |
| 90 | void |
| 91 | enableRegistrationReply(); |
| 92 | |
| 93 | public: |
| 94 | /** \brief default options |
| 95 | * |
| 96 | * enablePacketLogging=true |
| 97 | * enableRegistrationReply=false |
| 98 | */ |
| 99 | static const Options DEFAULT_OPTIONS; |
| 100 | |
| 101 | /** \brief Interests sent out of this DummyClientFace |
| 102 | * |
| 103 | * Sent Interests are appended to this container if options.enablePacketLogger is true. |
| 104 | * User of this class is responsible for cleaning up the container, if necessary. |
| 105 | * After .expressInterest, .processEvents must be called before the Interest would show up here. |
| 106 | */ |
| 107 | std::vector<Interest> sentInterests; |
| 108 | |
| 109 | /** \brief Data sent out of this DummyClientFace |
| 110 | * |
| 111 | * Sent Data are appended to this container if options.enablePacketLogger is true. |
| 112 | * User of this class is responsible for cleaning up the container, if necessary. |
| 113 | * After .put, .processEvents must be called before the Data would show up here. |
| 114 | */ |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 115 | std::vector<Data> sentData; |
| 116 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 117 | /** \brief NACKs sent out of this DummyClientFace |
| 118 | * |
| 119 | * Sent NACKs are appended to this container if options.enablePacketLogger is true. |
| 120 | * User of this class is responsible for cleaning up the container, if necessary. |
| 121 | * After .put, .processEvents must be called before the NACK would show up here. |
| 122 | */ |
| 123 | std::vector<lp::Nack> sentNacks; |
| 124 | |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 125 | /** \brief emits whenever an Interest is sent |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 126 | * |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 127 | * After .expressInterest, .processEvents must be called before this signal would be emitted. |
| 128 | */ |
| 129 | Signal<DummyClientFace, Interest> onSendInterest; |
| 130 | |
| 131 | /** \brief emits whenever a Data packet is sent |
| 132 | * |
| 133 | * After .put, .processEvents must be called before this signal would be emitted. |
| 134 | */ |
| 135 | Signal<DummyClientFace, Data> onSendData; |
| 136 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 137 | /** \brief emits whenever a NACK is sent |
| 138 | * |
| 139 | * After .put, .processEvents must be called before this signal would be emitted. |
| 140 | */ |
| 141 | Signal<DummyClientFace, lp::Nack> onSendNack; |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 142 | |
| 143 | private: |
| 144 | std::unique_ptr<KeyChain> m_internalKeyChain; |
| 145 | KeyChain& m_keyChain; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Eric Newberry | 664dc03 | 2015-11-23 12:49:50 -0700 | [diff] [blame] | 148 | template<> |
| 149 | void |
| 150 | DummyClientFace::receive(const lp::Nack& nack); |
| 151 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 152 | } // namespace util |
| 153 | } // namespace ndn |
| 154 | |
| 155 | #endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP |