blob: ae32e1469014b340e2d5df4537a1f5490e4ae912 [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
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 */
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
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080051 /**
52 * @brief Create a dummy face with internal IO service
53 */
54 DummyClientFace(const Options& options = DummyClientFace::DEFAULT_OPTIONS);
55
56 /**
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070057 * @brief Create a dummy face with internal IO service and the specified KeyChain
58 */
59 DummyClientFace(KeyChain& keyChain, const Options& options = DummyClientFace::DEFAULT_OPTIONS);
60
61 /**
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080062 * @brief Create a dummy face with the provided IO service
63 */
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070064 DummyClientFace(boost::asio::io_service& ioService,
65 const Options& options = DummyClientFace::DEFAULT_OPTIONS);
66
67 /**
68 * @brief Create a dummy face with the provided IO service and the specified KeyChain
69 */
70 DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain,
71 const Options& options = DummyClientFace::DEFAULT_OPTIONS);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080072
Junxiao Shia60d9362014-11-12 09:38:21 -070073 /** \brief cause the Face to receive a packet
74 * \tparam Packet either Interest or Data
75 */
76 template<typename Packet>
77 void
78 receive(const Packet& packet);
79
80private: // constructors
81 class Transport;
82
Junxiao Shia60d9362014-11-12 09:38:21 -070083 void
84 construct(const Options& options);
85
Junxiao Shia60d9362014-11-12 09:38:21 -070086private:
87 void
88 enablePacketLogging();
89
90 void
91 enableRegistrationReply();
92
93public:
94 /** \brief default options
95 *
96 * enablePacketLogging=true
97 * enableRegistrationReply=false
98 */
99 static const Options DEFAULT_OPTIONS;
100
101 /** \brief Interests sent out of this DummyClientFace
102 *
103 * Sent Interests are appended to this container if options.enablePacketLogger is true.
104 * User of this class is responsible for cleaning up the container, if necessary.
105 * After .expressInterest, .processEvents must be called before the Interest would show up here.
106 */
107 std::vector<Interest> sentInterests;
108
109 /** \brief Data sent out of this DummyClientFace
110 *
111 * Sent Data are appended to this container if options.enablePacketLogger is true.
112 * User of this class is responsible for cleaning up the container, if necessary.
113 * After .put, .processEvents must be called before the Data would show up here.
114 */
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800115 std::vector<Data> sentData;
116
Eric Newberry83872fd2015-08-06 17:01:24 -0700117 /** \brief NACKs sent out of this DummyClientFace
118 *
119 * Sent NACKs are appended to this container if options.enablePacketLogger is true.
120 * User of this class is responsible for cleaning up the container, if necessary.
121 * After .put, .processEvents must be called before the NACK would show up here.
122 */
123 std::vector<lp::Nack> sentNacks;
124
Junxiao Shi27913b42014-12-23 14:49:38 -0700125 /** \brief emits whenever an Interest is sent
Junxiao Shia60d9362014-11-12 09:38:21 -0700126 *
Junxiao Shi27913b42014-12-23 14:49:38 -0700127 * After .expressInterest, .processEvents must be called before this signal would be emitted.
128 */
129 Signal<DummyClientFace, Interest> onSendInterest;
130
131 /** \brief emits whenever a Data packet is sent
132 *
133 * After .put, .processEvents must be called before this signal would be emitted.
134 */
135 Signal<DummyClientFace, Data> onSendData;
136
Eric Newberry83872fd2015-08-06 17:01:24 -0700137 /** \brief emits whenever a NACK is sent
138 *
139 * After .put, .processEvents must be called before this signal would be emitted.
140 */
141 Signal<DummyClientFace, lp::Nack> onSendNack;
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700142
143private:
144 std::unique_ptr<KeyChain> m_internalKeyChain;
145 KeyChain& m_keyChain;
Junxiao Shia60d9362014-11-12 09:38:21 -0700146};
147
Eric Newberry664dc032015-11-23 12:49:50 -0700148template<>
149void
150DummyClientFace::receive(const lp::Nack& nack);
151
Junxiao Shia60d9362014-11-12 09:38:21 -0700152} // namespace util
153} // namespace ndn
154
155#endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP