blob: b5ec7d70a239584f63ac66eae9e4981fd28aedea [file] [log] [blame]
Junxiao Shia60d9362014-11-12 09:38:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 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 /**
59 * @brief Create a dummy face with the provided IO service
60 */
61 DummyClientFace(boost::asio::io_service& ioService, const Options& options = DummyClientFace::DEFAULT_OPTIONS);
62
Junxiao Shia60d9362014-11-12 09:38:21 -070063 /** \brief cause the Face to receive a packet
64 * \tparam Packet either Interest or Data
65 */
66 template<typename Packet>
67 void
68 receive(const Packet& packet);
69
70private: // constructors
71 class Transport;
72
Junxiao Shia60d9362014-11-12 09:38:21 -070073 void
74 construct(const Options& options);
75
Junxiao Shia60d9362014-11-12 09:38:21 -070076private:
77 void
78 enablePacketLogging();
79
80 void
81 enableRegistrationReply();
82
83public:
84 /** \brief default options
85 *
86 * enablePacketLogging=true
87 * enableRegistrationReply=false
88 */
89 static const Options DEFAULT_OPTIONS;
90
91 /** \brief Interests sent out of this DummyClientFace
92 *
93 * Sent Interests are appended to this container if options.enablePacketLogger is true.
94 * User of this class is responsible for cleaning up the container, if necessary.
95 * After .expressInterest, .processEvents must be called before the Interest would show up here.
96 */
97 std::vector<Interest> sentInterests;
98
99 /** \brief Data sent out of this DummyClientFace
100 *
101 * Sent Data are appended to this container if options.enablePacketLogger is true.
102 * User of this class is responsible for cleaning up the container, if necessary.
103 * After .put, .processEvents must be called before the Data would show up here.
104 */
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800105 std::vector<Data> sentData;
106
107#ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
108 std::vector<Data>& sentDatas; ///< deprecated alias to sentData
109#endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
Junxiao Shia60d9362014-11-12 09:38:21 -0700110
Eric Newberry83872fd2015-08-06 17:01:24 -0700111 /** \brief NACKs sent out of this DummyClientFace
112 *
113 * Sent NACKs 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 NACK would show up here.
116 */
117 std::vector<lp::Nack> sentNacks;
118
Junxiao Shi27913b42014-12-23 14:49:38 -0700119 /** \brief emits whenever an Interest is sent
Junxiao Shia60d9362014-11-12 09:38:21 -0700120 *
Junxiao Shi27913b42014-12-23 14:49:38 -0700121 * After .expressInterest, .processEvents must be called before this signal would be emitted.
122 */
123 Signal<DummyClientFace, Interest> onSendInterest;
124
125 /** \brief emits whenever a Data packet is sent
126 *
127 * After .put, .processEvents must be called before this signal would be emitted.
128 */
129 Signal<DummyClientFace, Data> onSendData;
130
Eric Newberry83872fd2015-08-06 17:01:24 -0700131 /** \brief emits whenever a NACK is sent
132 *
133 * After .put, .processEvents must be called before this signal would be emitted.
134 */
135 Signal<DummyClientFace, lp::Nack> onSendNack;
Junxiao Shia60d9362014-11-12 09:38:21 -0700136};
137
Eric Newberry664dc032015-11-23 12:49:50 -0700138template<>
139void
140DummyClientFace::receive(const lp::Nack& nack);
141
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800142#ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
143/**
144 * @brief Create a dummy face with internal IO service
145 * @deprecated Use the DummyFace constructor directly
146 */
Junxiao Shia60d9362014-11-12 09:38:21 -0700147shared_ptr<DummyClientFace>
148makeDummyClientFace(const DummyClientFace::Options& options = DummyClientFace::DEFAULT_OPTIONS);
149
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800150/**
151 * @brief Create a dummy face with the provided IO service
152 * @deprecated Use the DummyFace constructor directly
153 */
Junxiao Shia60d9362014-11-12 09:38:21 -0700154shared_ptr<DummyClientFace>
155makeDummyClientFace(boost::asio::io_service& ioService,
156 const DummyClientFace::Options& options = DummyClientFace::DEFAULT_OPTIONS);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800157#endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
Junxiao Shia60d9362014-11-12 09:38:21 -0700158
159} // namespace util
160} // namespace ndn
161
162#endif // NDN_UTIL_DUMMY_CLIENT_FACE_HPP