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