Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 95 | /** \brief cause the Face to receive an interest |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 96 | */ |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 97 | void |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 98 | receive(const Interest& interest); |
| 99 | |
| 100 | /** \brief cause the Face to receive a data |
| 101 | */ |
| 102 | void |
| 103 | receive(const Data& data); |
| 104 | |
| 105 | /** \brief cause the Face to receive a nack |
| 106 | */ |
| 107 | void |
| 108 | receive(const lp::Nack& nack); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 109 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 110 | private: |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 111 | class Transport; |
| 112 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 113 | void |
| 114 | construct(const Options& options); |
| 115 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 116 | void |
| 117 | enablePacketLogging(); |
| 118 | |
| 119 | void |
| 120 | enableRegistrationReply(); |
| 121 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 122 | void |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 123 | doProcessEvents(const time::milliseconds& timeout, bool keepThread) override; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 124 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 125 | public: |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 126 | /** \brief Interests sent out of this DummyClientFace |
| 127 | * |
| 128 | * Sent Interests 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 .expressInterest, .processEvents must be called before the Interest would show up here. |
| 131 | */ |
| 132 | std::vector<Interest> sentInterests; |
| 133 | |
| 134 | /** \brief Data sent out of this DummyClientFace |
| 135 | * |
| 136 | * Sent Data are appended to this container if options.enablePacketLogger is true. |
| 137 | * User of this class is responsible for cleaning up the container, if necessary. |
| 138 | * After .put, .processEvents must be called before the Data would show up here. |
| 139 | */ |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 140 | std::vector<Data> sentData; |
| 141 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 142 | /** \brief Nacks sent out of this DummyClientFace |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 143 | * |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 144 | * Sent Nacks are appended to this container if options.enablePacketLogger is true. |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 145 | * User of this class is responsible for cleaning up the container, if necessary. |
| 146 | * After .put, .processEvents must be called before the NACK would show up here. |
| 147 | */ |
| 148 | std::vector<lp::Nack> sentNacks; |
| 149 | |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 150 | /** \brief emits whenever an Interest is sent |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 151 | * |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 152 | * After .expressInterest, .processEvents must be called before this signal would be emitted. |
| 153 | */ |
| 154 | Signal<DummyClientFace, Interest> onSendInterest; |
| 155 | |
| 156 | /** \brief emits whenever a Data packet is sent |
| 157 | * |
| 158 | * After .put, .processEvents must be called before this signal would be emitted. |
| 159 | */ |
| 160 | Signal<DummyClientFace, Data> onSendData; |
| 161 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 162 | /** \brief emits whenever a Nack is sent |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 163 | * |
| 164 | * After .put, .processEvents must be called before this signal would be emitted. |
| 165 | */ |
| 166 | Signal<DummyClientFace, lp::Nack> onSendNack; |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 167 | |
| 168 | private: |
| 169 | std::unique_ptr<KeyChain> m_internalKeyChain; |
| 170 | KeyChain& m_keyChain; |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 171 | std::function<void(time::milliseconds)> m_processEventsOverride; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 174 | } // namespace util |
| 175 | } // namespace ndn |
| 176 | |
| 177 | #endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP |