Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2ed9e07 | 2017-08-13 16:45:48 +0000 | [diff] [blame] | 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 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 75 | class AlreadyLinkedError : public Error |
| 76 | { |
| 77 | public: |
| 78 | AlreadyLinkedError(); |
| 79 | }; |
| 80 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 81 | /** \brief Create a dummy face with internal IO service |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 82 | */ |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 83 | explicit |
| 84 | DummyClientFace(const Options& options = Options()); |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 85 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 86 | /** \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] | 87 | */ |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 88 | explicit |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 89 | DummyClientFace(KeyChain& keyChain, const Options& options = Options()); |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 90 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 91 | /** \brief Create a dummy face with the provided IO service |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 92 | */ |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 93 | explicit |
| 94 | DummyClientFace(boost::asio::io_service& ioService, const Options& options = Options()); |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 96 | /** \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] | 97 | */ |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 98 | DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain, |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 99 | const Options& options = Options()); |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 100 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 101 | ~DummyClientFace(); |
| 102 | |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 103 | /** \brief cause the Face to receive an interest |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 104 | */ |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 105 | void |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 106 | receive(const Interest& interest); |
| 107 | |
| 108 | /** \brief cause the Face to receive a data |
| 109 | */ |
| 110 | void |
| 111 | receive(const Data& data); |
| 112 | |
| 113 | /** \brief cause the Face to receive a nack |
| 114 | */ |
| 115 | void |
| 116 | receive(const lp::Nack& nack); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 117 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 118 | /** \brief link another DummyClientFace through a broadcast media |
| 119 | */ |
| 120 | void |
| 121 | linkTo(DummyClientFace& other); |
| 122 | |
| 123 | /** \brief unlink the broadcast media if previously linked |
| 124 | */ |
| 125 | void |
| 126 | unlink(); |
| 127 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 128 | private: |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 129 | class Transport; |
| 130 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 131 | void |
| 132 | construct(const Options& options); |
| 133 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 134 | void |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 135 | enableBroadcastLink(); |
| 136 | |
| 137 | void |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 138 | enablePacketLogging(); |
| 139 | |
| 140 | void |
| 141 | enableRegistrationReply(); |
| 142 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 143 | void |
Junxiao Shi | 2ed9e07 | 2017-08-13 16:45:48 +0000 | [diff] [blame] | 144 | doProcessEvents(time::milliseconds timeout, bool keepThread) override; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 145 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 146 | public: |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 147 | /** \brief Interests sent out of this DummyClientFace |
| 148 | * |
| 149 | * Sent Interests are appended to this container if options.enablePacketLogger is true. |
| 150 | * User of this class is responsible for cleaning up the container, if necessary. |
| 151 | * After .expressInterest, .processEvents must be called before the Interest would show up here. |
| 152 | */ |
| 153 | std::vector<Interest> sentInterests; |
| 154 | |
| 155 | /** \brief Data sent out of this DummyClientFace |
| 156 | * |
| 157 | * Sent Data are appended to this container if options.enablePacketLogger is true. |
| 158 | * User of this class is responsible for cleaning up the container, if necessary. |
| 159 | * After .put, .processEvents must be called before the Data would show up here. |
| 160 | */ |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 161 | std::vector<Data> sentData; |
| 162 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 163 | /** \brief Nacks sent out of this DummyClientFace |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 164 | * |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 165 | * Sent Nacks are appended to this container if options.enablePacketLogger is true. |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 166 | * User of this class is responsible for cleaning up the container, if necessary. |
| 167 | * After .put, .processEvents must be called before the NACK would show up here. |
| 168 | */ |
| 169 | std::vector<lp::Nack> sentNacks; |
| 170 | |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 171 | /** \brief emits whenever an Interest is sent |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 172 | * |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 173 | * After .expressInterest, .processEvents must be called before this signal would be emitted. |
| 174 | */ |
| 175 | Signal<DummyClientFace, Interest> onSendInterest; |
| 176 | |
| 177 | /** \brief emits whenever a Data packet is sent |
| 178 | * |
| 179 | * After .put, .processEvents must be called before this signal would be emitted. |
| 180 | */ |
| 181 | Signal<DummyClientFace, Data> onSendData; |
| 182 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 183 | /** \brief emits whenever a Nack is sent |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 184 | * |
| 185 | * After .put, .processEvents must be called before this signal would be emitted. |
| 186 | */ |
| 187 | Signal<DummyClientFace, lp::Nack> onSendNack; |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 188 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 189 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 190 | struct BroadcastLink; |
| 191 | shared_ptr<BroadcastLink> m_bcastLink; |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 192 | std::unique_ptr<KeyChain> m_internalKeyChain; |
| 193 | KeyChain& m_keyChain; |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 194 | std::function<void(time::milliseconds)> m_processEventsOverride; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 197 | } // namespace util |
| 198 | } // namespace ndn |
| 199 | |
| 200 | #endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP |