blob: 7d661a790c6dd69f1d699428534b590b597bf579 [file] [log] [blame]
Junxiao Shia60d9362014-11-12 09:38:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Junxiao Shia60d9362014-11-12 09:38:21 -07004 *
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 Shi27913b42014-12-23 14:49:38 -070026#include "signal.hpp"
Junxiao Shic828dfc2016-09-15 13:26:22 +000027#include "../security/key-chain.hpp"
Junxiao Shia60d9362014-11-12 09:38:21 -070028
29namespace ndn {
30namespace util {
31
32/** \brief a client-side face for unit testing
33 */
34class DummyClientFace : public ndn::Face
35{
36public:
37 /** \brief options for DummyClientFace
38 */
Junxiao Shic828dfc2016-09-15 13:26:22 +000039 class Options
Junxiao Shia60d9362014-11-12 09:38:21 -070040 {
Junxiao Shic828dfc2016-09-15 13:26:22 +000041 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 Shia60d9362014-11-12 09:38:21 -070061 /** \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 Shic828dfc2016-09-15 13:26:22 +000069
70 /** \brief if not empty, face.processEvents() will be overridden by this function
71 */
72 std::function<void(time::milliseconds)> processEventsOverride;
Junxiao Shia60d9362014-11-12 09:38:21 -070073 };
74
Junxiao Shic828dfc2016-09-15 13:26:22 +000075 /** \brief Create a dummy face with internal IO service
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080076 */
Junxiao Shic828dfc2016-09-15 13:26:22 +000077 explicit
78 DummyClientFace(const Options& options = Options());
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080079
Junxiao Shic828dfc2016-09-15 13:26:22 +000080 /** \brief Create a dummy face with internal IO service and the specified KeyChain
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070081 */
Junxiao Shic828dfc2016-09-15 13:26:22 +000082 explicit
83 DummyClientFace(KeyChain& keyChain, const Options& options = Options());
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070084
Junxiao Shic828dfc2016-09-15 13:26:22 +000085 /** \brief Create a dummy face with the provided IO service
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080086 */
Junxiao Shic828dfc2016-09-15 13:26:22 +000087 explicit
88 DummyClientFace(boost::asio::io_service& ioService, const Options& options = Options());
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070089
Junxiao Shic828dfc2016-09-15 13:26:22 +000090 /** \brief Create a dummy face with the provided IO service and the specified KeyChain
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070091 */
92 DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain,
Junxiao Shic828dfc2016-09-15 13:26:22 +000093 const Options& options = Options());
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080094
Junxiao Shia60d9362014-11-12 09:38:21 -070095 /** \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 Shic828dfc2016-09-15 13:26:22 +0000102private:
Junxiao Shia60d9362014-11-12 09:38:21 -0700103 class Transport;
104
Junxiao Shia60d9362014-11-12 09:38:21 -0700105 void
106 construct(const Options& options);
107
Junxiao Shia60d9362014-11-12 09:38:21 -0700108 void
109 enablePacketLogging();
110
111 void
112 enableRegistrationReply();
113
Junxiao Shic828dfc2016-09-15 13:26:22 +0000114 virtual void
115 doProcessEvents(const time::milliseconds& timeout, bool keepThread) override;
Junxiao Shia60d9362014-11-12 09:38:21 -0700116
Junxiao Shic828dfc2016-09-15 13:26:22 +0000117public:
Junxiao Shia60d9362014-11-12 09:38:21 -0700118 /** \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 Afanasyev3a6da362015-12-29 20:31:03 -0800132 std::vector<Data> sentData;
133
Junxiao Shic828dfc2016-09-15 13:26:22 +0000134 /** \brief Nacks sent out of this DummyClientFace
Eric Newberry83872fd2015-08-06 17:01:24 -0700135 *
Junxiao Shic828dfc2016-09-15 13:26:22 +0000136 * Sent Nacks are appended to this container if options.enablePacketLogger is true.
Eric Newberry83872fd2015-08-06 17:01:24 -0700137 * 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 Shi27913b42014-12-23 14:49:38 -0700142 /** \brief emits whenever an Interest is sent
Junxiao Shia60d9362014-11-12 09:38:21 -0700143 *
Junxiao Shi27913b42014-12-23 14:49:38 -0700144 * 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 Shic828dfc2016-09-15 13:26:22 +0000154 /** \brief emits whenever a Nack is sent
Eric Newberry83872fd2015-08-06 17:01:24 -0700155 *
156 * After .put, .processEvents must be called before this signal would be emitted.
157 */
158 Signal<DummyClientFace, lp::Nack> onSendNack;
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700159
160private:
161 std::unique_ptr<KeyChain> m_internalKeyChain;
162 KeyChain& m_keyChain;
Junxiao Shic828dfc2016-09-15 13:26:22 +0000163 std::function<void(time::milliseconds)> m_processEventsOverride;
Junxiao Shia60d9362014-11-12 09:38:21 -0700164};
165
Eric Newberry664dc032015-11-23 12:49:50 -0700166template<>
167void
168DummyClientFace::receive(const lp::Nack& nack);
169
Junxiao Shia60d9362014-11-12 09:38:21 -0700170} // namespace util
171} // namespace ndn
172
173#endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP