blob: cbf22f1352d5b18b66d87d0927e3028ed6bcc384 [file] [log] [blame]
Junxiao Shia60d9362014-11-12 09:38:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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
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
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080083 DummyClientFace(security::v1::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 */
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080092 DummyClientFace(boost::asio::io_service& ioService, security::v1::KeyChain& keyChain,
Junxiao Shic828dfc2016-09-15 13:26:22 +000093 const Options& options = Options());
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080094
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -080095 /** \brief cause the Face to receive an interest
Junxiao Shia60d9362014-11-12 09:38:21 -070096 */
Junxiao Shia60d9362014-11-12 09:38:21 -070097 void
Zhiyi Zhang9cbd0102017-01-03 11:03:01 -080098 receive(const Interest& interest);
99
100 /** \brief cause the Face to receive a data
101 */
102 void
103 receive(const Data& data);
104
105 /** \brief cause the Face to receive a nack
106 */
107 void
108 receive(const lp::Nack& nack);
Junxiao Shia60d9362014-11-12 09:38:21 -0700109
Junxiao Shic828dfc2016-09-15 13:26:22 +0000110private:
Junxiao Shia60d9362014-11-12 09:38:21 -0700111 class Transport;
112
Junxiao Shia60d9362014-11-12 09:38:21 -0700113 void
114 construct(const Options& options);
115
Junxiao Shia60d9362014-11-12 09:38:21 -0700116 void
117 enablePacketLogging();
118
119 void
120 enableRegistrationReply();
121
Davide Pesavento57c07df2016-12-11 18:41:45 -0500122 void
Junxiao Shic828dfc2016-09-15 13:26:22 +0000123 doProcessEvents(const time::milliseconds& timeout, bool keepThread) override;
Junxiao Shia60d9362014-11-12 09:38:21 -0700124
Junxiao Shic828dfc2016-09-15 13:26:22 +0000125public:
Junxiao Shia60d9362014-11-12 09:38:21 -0700126 /** \brief Interests sent out of this DummyClientFace
127 *
128 * Sent Interests 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 .expressInterest, .processEvents must be called before the Interest would show up here.
131 */
132 std::vector<Interest> sentInterests;
133
134 /** \brief Data sent out of this DummyClientFace
135 *
136 * Sent Data are appended to this container if options.enablePacketLogger is true.
137 * User of this class is responsible for cleaning up the container, if necessary.
138 * After .put, .processEvents must be called before the Data would show up here.
139 */
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800140 std::vector<Data> sentData;
141
Junxiao Shic828dfc2016-09-15 13:26:22 +0000142 /** \brief Nacks sent out of this DummyClientFace
Eric Newberry83872fd2015-08-06 17:01:24 -0700143 *
Junxiao Shic828dfc2016-09-15 13:26:22 +0000144 * Sent Nacks are appended to this container if options.enablePacketLogger is true.
Eric Newberry83872fd2015-08-06 17:01:24 -0700145 * User of this class is responsible for cleaning up the container, if necessary.
146 * After .put, .processEvents must be called before the NACK would show up here.
147 */
148 std::vector<lp::Nack> sentNacks;
149
Junxiao Shi27913b42014-12-23 14:49:38 -0700150 /** \brief emits whenever an Interest is sent
Junxiao Shia60d9362014-11-12 09:38:21 -0700151 *
Junxiao Shi27913b42014-12-23 14:49:38 -0700152 * After .expressInterest, .processEvents must be called before this signal would be emitted.
153 */
154 Signal<DummyClientFace, Interest> onSendInterest;
155
156 /** \brief emits whenever a Data packet is sent
157 *
158 * After .put, .processEvents must be called before this signal would be emitted.
159 */
160 Signal<DummyClientFace, Data> onSendData;
161
Junxiao Shic828dfc2016-09-15 13:26:22 +0000162 /** \brief emits whenever a Nack is sent
Eric Newberry83872fd2015-08-06 17:01:24 -0700163 *
164 * After .put, .processEvents must be called before this signal would be emitted.
165 */
166 Signal<DummyClientFace, lp::Nack> onSendNack;
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700167
168private:
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800169 std::unique_ptr<security::v1::KeyChain> m_internalKeyChain;
170 security::v1::KeyChain& m_keyChain;
Junxiao Shic828dfc2016-09-15 13:26:22 +0000171 std::function<void(time::milliseconds)> m_processEventsOverride;
Junxiao Shia60d9362014-11-12 09:38:21 -0700172};
173
Junxiao Shia60d9362014-11-12 09:38:21 -0700174} // namespace util
175} // namespace ndn
176
177#endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP