blob: c9ea6fc5fd402430d5420d0d391e89a204495582 [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"
Eric Newberry83872fd2015-08-06 17:01:24 -070027#include "../lp/packet.hpp"
Junxiao Shia60d9362014-11-12 09:38:21 -070028
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080029#define NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
30
Junxiao Shia60d9362014-11-12 09:38:21 -070031namespace ndn {
32namespace util {
33
34/** \brief a client-side face for unit testing
35 */
36class DummyClientFace : public ndn::Face
37{
38public:
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 Afanasyev3a6da362015-12-29 20:31:03 -080053 /**
54 * @brief Create a dummy face with internal IO service
55 */
56 DummyClientFace(const Options& options = DummyClientFace::DEFAULT_OPTIONS);
57
58 /**
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070059 * @brief Create a dummy face with internal IO service and the specified KeyChain
60 */
61 DummyClientFace(KeyChain& keyChain, const Options& options = DummyClientFace::DEFAULT_OPTIONS);
62
63 /**
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080064 * @brief Create a dummy face with the provided IO service
65 */
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070066 DummyClientFace(boost::asio::io_service& ioService,
67 const Options& options = DummyClientFace::DEFAULT_OPTIONS);
68
69 /**
70 * @brief Create a dummy face with the provided IO service and the specified KeyChain
71 */
72 DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain,
73 const Options& options = DummyClientFace::DEFAULT_OPTIONS);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080074
Junxiao Shia60d9362014-11-12 09:38:21 -070075 /** \brief cause the Face to receive a packet
76 * \tparam Packet either Interest or Data
77 */
78 template<typename Packet>
79 void
80 receive(const Packet& packet);
81
82private: // constructors
83 class Transport;
84
Junxiao Shia60d9362014-11-12 09:38:21 -070085 void
86 construct(const Options& options);
87
Junxiao Shia60d9362014-11-12 09:38:21 -070088private:
89 void
90 enablePacketLogging();
91
92 void
93 enableRegistrationReply();
94
95public:
96 /** \brief default options
97 *
98 * enablePacketLogging=true
99 * enableRegistrationReply=false
100 */
101 static const Options DEFAULT_OPTIONS;
102
103 /** \brief Interests sent out of this DummyClientFace
104 *
105 * Sent Interests are appended to this container if options.enablePacketLogger is true.
106 * User of this class is responsible for cleaning up the container, if necessary.
107 * After .expressInterest, .processEvents must be called before the Interest would show up here.
108 */
109 std::vector<Interest> sentInterests;
110
111 /** \brief Data sent out of this DummyClientFace
112 *
113 * Sent Data 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 Data would show up here.
116 */
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800117 std::vector<Data> sentData;
118
119#ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
120 std::vector<Data>& sentDatas; ///< deprecated alias to sentData
121#endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
Junxiao Shia60d9362014-11-12 09:38:21 -0700122
Eric Newberry83872fd2015-08-06 17:01:24 -0700123 /** \brief NACKs sent out of this DummyClientFace
124 *
125 * Sent NACKs are appended to this container if options.enablePacketLogger is true.
126 * User of this class is responsible for cleaning up the container, if necessary.
127 * After .put, .processEvents must be called before the NACK would show up here.
128 */
129 std::vector<lp::Nack> sentNacks;
130
Junxiao Shi27913b42014-12-23 14:49:38 -0700131 /** \brief emits whenever an Interest is sent
Junxiao Shia60d9362014-11-12 09:38:21 -0700132 *
Junxiao Shi27913b42014-12-23 14:49:38 -0700133 * After .expressInterest, .processEvents must be called before this signal would be emitted.
134 */
135 Signal<DummyClientFace, Interest> onSendInterest;
136
137 /** \brief emits whenever a Data packet is sent
138 *
139 * After .put, .processEvents must be called before this signal would be emitted.
140 */
141 Signal<DummyClientFace, Data> onSendData;
142
Eric Newberry83872fd2015-08-06 17:01:24 -0700143 /** \brief emits whenever a NACK is sent
144 *
145 * After .put, .processEvents must be called before this signal would be emitted.
146 */
147 Signal<DummyClientFace, lp::Nack> onSendNack;
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700148
149private:
150 std::unique_ptr<KeyChain> m_internalKeyChain;
151 KeyChain& m_keyChain;
Junxiao Shia60d9362014-11-12 09:38:21 -0700152};
153
Eric Newberry664dc032015-11-23 12:49:50 -0700154template<>
155void
156DummyClientFace::receive(const lp::Nack& nack);
157
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800158#ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
159/**
160 * @brief Create a dummy face with internal IO service
161 * @deprecated Use the DummyFace constructor directly
162 */
Junxiao Shia60d9362014-11-12 09:38:21 -0700163shared_ptr<DummyClientFace>
164makeDummyClientFace(const DummyClientFace::Options& options = DummyClientFace::DEFAULT_OPTIONS);
165
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800166/**
167 * @brief Create a dummy face with the provided IO service
168 * @deprecated Use the DummyFace constructor directly
169 */
Junxiao Shia60d9362014-11-12 09:38:21 -0700170shared_ptr<DummyClientFace>
171makeDummyClientFace(boost::asio::io_service& ioService,
172 const DummyClientFace::Options& options = DummyClientFace::DEFAULT_OPTIONS);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800173#endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
Junxiao Shia60d9362014-11-12 09:38:21 -0700174
175} // namespace util
176} // namespace ndn
177
178#endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP