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" |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 27 | #include "../security/key-chain.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 | */ |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 39 | class Options |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 40 | { |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 41 | public: |
| 42 | Options(bool enablePacketLogging, bool enableRegistrationReply, |
| 43 | const std::function<void(time::milliseconds)>& processEventsOverride) |
| 44 | : enablePacketLogging(enablePacketLogging) |
| 45 | , enableRegistrationReply(enableRegistrationReply) |
| 46 | , processEventsOverride(processEventsOverride) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | Options(bool enablePacketLogging, bool enableRegistrationReply) |
| 51 | : Options(enablePacketLogging, enableRegistrationReply, nullptr) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | Options() |
| 56 | : Options(true, false) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | public: |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 61 | /** \brief if true, packets sent out of DummyClientFace will be appended to a container |
| 62 | */ |
| 63 | bool enablePacketLogging; |
| 64 | |
| 65 | /** \brief if true, prefix registration command will be automatically |
| 66 | * replied with a successful response |
| 67 | */ |
| 68 | bool enableRegistrationReply; |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 69 | |
| 70 | /** \brief if not empty, face.processEvents() will be overridden by this function |
| 71 | */ |
| 72 | std::function<void(time::milliseconds)> processEventsOverride; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 75 | /** \brief Create a dummy face with internal IO service |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 76 | */ |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 77 | explicit |
| 78 | DummyClientFace(const Options& options = Options()); |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 79 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 80 | /** \brief Create a dummy face with internal IO service and the specified KeyChain |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 81 | */ |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 82 | explicit |
| 83 | DummyClientFace(KeyChain& keyChain, const Options& options = Options()); |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 84 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 85 | /** \brief Create a dummy face with the provided IO service |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 86 | */ |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 87 | explicit |
| 88 | DummyClientFace(boost::asio::io_service& ioService, const Options& options = Options()); |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 90 | /** \brief Create a dummy face with the provided IO service and the specified KeyChain |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 91 | */ |
| 92 | DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain, |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 93 | const Options& options = Options()); |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 94 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 95 | /** \brief cause the Face to receive a packet |
| 96 | * \tparam Packet either Interest or Data |
| 97 | */ |
| 98 | template<typename Packet> |
| 99 | void |
| 100 | receive(const Packet& packet); |
| 101 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 102 | private: |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 103 | class Transport; |
| 104 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 105 | void |
| 106 | construct(const Options& options); |
| 107 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 108 | void |
| 109 | enablePacketLogging(); |
| 110 | |
| 111 | void |
| 112 | enableRegistrationReply(); |
| 113 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 114 | virtual void |
| 115 | doProcessEvents(const time::milliseconds& timeout, bool keepThread) override; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 117 | public: |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 118 | /** \brief Interests sent out of this DummyClientFace |
| 119 | * |
| 120 | * Sent Interests are appended to this container if options.enablePacketLogger is true. |
| 121 | * User of this class is responsible for cleaning up the container, if necessary. |
| 122 | * After .expressInterest, .processEvents must be called before the Interest would show up here. |
| 123 | */ |
| 124 | std::vector<Interest> sentInterests; |
| 125 | |
| 126 | /** \brief Data sent out of this DummyClientFace |
| 127 | * |
| 128 | * Sent Data are appended to this container if options.enablePacketLogger is true. |
| 129 | * User of this class is responsible for cleaning up the container, if necessary. |
| 130 | * After .put, .processEvents must be called before the Data would show up here. |
| 131 | */ |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 132 | std::vector<Data> sentData; |
| 133 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 134 | /** \brief Nacks sent out of this DummyClientFace |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 135 | * |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 136 | * Sent Nacks are appended to this container if options.enablePacketLogger is true. |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 137 | * User of this class is responsible for cleaning up the container, if necessary. |
| 138 | * After .put, .processEvents must be called before the NACK would show up here. |
| 139 | */ |
| 140 | std::vector<lp::Nack> sentNacks; |
| 141 | |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 142 | /** \brief emits whenever an Interest is sent |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 143 | * |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 144 | * After .expressInterest, .processEvents must be called before this signal would be emitted. |
| 145 | */ |
| 146 | Signal<DummyClientFace, Interest> onSendInterest; |
| 147 | |
| 148 | /** \brief emits whenever a Data packet is sent |
| 149 | * |
| 150 | * After .put, .processEvents must be called before this signal would be emitted. |
| 151 | */ |
| 152 | Signal<DummyClientFace, Data> onSendData; |
| 153 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 154 | /** \brief emits whenever a Nack is sent |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 155 | * |
| 156 | * After .put, .processEvents must be called before this signal would be emitted. |
| 157 | */ |
| 158 | Signal<DummyClientFace, lp::Nack> onSendNack; |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 159 | |
| 160 | private: |
| 161 | std::unique_ptr<KeyChain> m_internalKeyChain; |
| 162 | KeyChain& m_keyChain; |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame^] | 163 | std::function<void(time::milliseconds)> m_processEventsOverride; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
Eric Newberry | 664dc03 | 2015-11-23 12:49:50 -0700 | [diff] [blame] | 166 | template<> |
| 167 | void |
| 168 | DummyClientFace::receive(const lp::Nack& nack); |
| 169 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 170 | } // namespace util |
| 171 | } // namespace ndn |
| 172 | |
| 173 | #endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP |