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