blob: 7d40f65ae6df41921538aa7ac46758a5cf224e2a [file] [log] [blame]
Junxiao Shia60d9362014-11-12 09:38:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2ed9e072017-08-13 16:45:48 +00002/*
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -08003 * Copyright (c) 2013-2017 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
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080075 class AlreadyLinkedError : public Error
76 {
77 public:
78 AlreadyLinkedError();
79 };
80
Junxiao Shic828dfc2016-09-15 13:26:22 +000081 /** \brief Create a dummy face with internal IO service
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080082 */
Junxiao Shic828dfc2016-09-15 13:26:22 +000083 explicit
84 DummyClientFace(const Options& options = Options());
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080085
Junxiao Shic828dfc2016-09-15 13:26:22 +000086 /** \brief Create a dummy face with internal IO service and the specified KeyChain
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070087 */
Junxiao Shic828dfc2016-09-15 13:26:22 +000088 explicit
Alexander Afanasyev80782e02017-01-04 13:16:54 -080089 DummyClientFace(KeyChain& keyChain, const Options& options = Options());
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070090
Junxiao Shic828dfc2016-09-15 13:26:22 +000091 /** \brief Create a dummy face with the provided IO service
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080092 */
Junxiao Shic828dfc2016-09-15 13:26:22 +000093 explicit
94 DummyClientFace(boost::asio::io_service& ioService, const Options& options = Options());
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070095
Junxiao Shic828dfc2016-09-15 13:26:22 +000096 /** \brief Create a dummy face with the provided IO service and the specified KeyChain
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070097 */
Alexander Afanasyev80782e02017-01-04 13:16:54 -080098 DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain,
Junxiao Shic828dfc2016-09-15 13:26:22 +000099 const Options& options = Options());
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800100
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800101 ~DummyClientFace();
102
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800103 /** \brief cause the Face to receive an interest
Junxiao Shia60d9362014-11-12 09:38:21 -0700104 */
Junxiao Shia60d9362014-11-12 09:38:21 -0700105 void
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -0800106 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 Shia60d9362014-11-12 09:38:21 -0700117
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800118 /** \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 Shic828dfc2016-09-15 13:26:22 +0000128private:
Junxiao Shia60d9362014-11-12 09:38:21 -0700129 class Transport;
130
Junxiao Shia60d9362014-11-12 09:38:21 -0700131 void
132 construct(const Options& options);
133
Junxiao Shia60d9362014-11-12 09:38:21 -0700134 void
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800135 enableBroadcastLink();
136
137 void
Junxiao Shia60d9362014-11-12 09:38:21 -0700138 enablePacketLogging();
139
140 void
141 enableRegistrationReply();
142
Davide Pesavento57c07df2016-12-11 18:41:45 -0500143 void
Junxiao Shi2ed9e072017-08-13 16:45:48 +0000144 doProcessEvents(time::milliseconds timeout, bool keepThread) override;
Junxiao Shia60d9362014-11-12 09:38:21 -0700145
Junxiao Shic828dfc2016-09-15 13:26:22 +0000146public:
Junxiao Shia60d9362014-11-12 09:38:21 -0700147 /** \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 Afanasyev3a6da362015-12-29 20:31:03 -0800161 std::vector<Data> sentData;
162
Junxiao Shic828dfc2016-09-15 13:26:22 +0000163 /** \brief Nacks sent out of this DummyClientFace
Eric Newberry83872fd2015-08-06 17:01:24 -0700164 *
Junxiao Shic828dfc2016-09-15 13:26:22 +0000165 * Sent Nacks are appended to this container if options.enablePacketLogger is true.
Eric Newberry83872fd2015-08-06 17:01:24 -0700166 * 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 Shi27913b42014-12-23 14:49:38 -0700171 /** \brief emits whenever an Interest is sent
Junxiao Shia60d9362014-11-12 09:38:21 -0700172 *
Junxiao Shi27913b42014-12-23 14:49:38 -0700173 * 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 Shic828dfc2016-09-15 13:26:22 +0000183 /** \brief emits whenever a Nack is sent
Eric Newberry83872fd2015-08-06 17:01:24 -0700184 *
185 * After .put, .processEvents must be called before this signal would be emitted.
186 */
187 Signal<DummyClientFace, lp::Nack> onSendNack;
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700188
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800189NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
190 struct BroadcastLink;
191 shared_ptr<BroadcastLink> m_bcastLink;
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800192 std::unique_ptr<KeyChain> m_internalKeyChain;
193 KeyChain& m_keyChain;
Junxiao Shic828dfc2016-09-15 13:26:22 +0000194 std::function<void(time::milliseconds)> m_processEventsOverride;
Junxiao Shia60d9362014-11-12 09:38:21 -0700195};
196
Junxiao Shia60d9362014-11-12 09:38:21 -0700197} // namespace util
198} // namespace ndn
199
200#endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP