blob: 19094601b07e9ed5e28d76b2b57eaa3e0601aeef [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2ed9e072017-08-13 16:45:48 +00002/*
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070020 */
21
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -070022#ifndef NDN_FACE_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -070023#define NDN_FACE_HPP
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070024
Junxiao Shib6e276f2017-08-14 20:10:04 +000025#include "data.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070026#include "name.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080027#include "interest.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070028#include "interest-filter.hpp"
Junxiao Shi4b469982015-12-03 18:20:19 +000029#include "encoding/nfd-constants.hpp"
Eric Newberry83872fd2015-08-06 17:01:24 -070030#include "lp/nack.hpp"
Davide Pesavento4d0d0962017-12-19 22:23:14 -050031#include "net/asio-fwd.hpp"
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080032#include "security/key-chain.hpp"
Junxiao Shib6e276f2017-08-14 20:10:04 +000033#include "security/signing-info.hpp"
Joao Pereira0b3cac52015-07-02 14:49:49 -040034
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070035namespace ndn {
36
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070037class Transport;
38
39class PendingInterestId;
40class RegisteredPrefixId;
41class InterestFilterId;
42
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070043namespace nfd {
44class Controller;
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070045} // namespace nfd
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070046
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070047/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000048 * @brief Callback invoked when expressed Interest gets satisfied with a Data packet
Eric Newberry83872fd2015-08-06 17:01:24 -070049 */
50typedef function<void(const Interest&, const Data&)> DataCallback;
51
52/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000053 * @brief Callback invoked when Nack is sent in response to expressed Interest
Eric Newberry83872fd2015-08-06 17:01:24 -070054 */
55typedef function<void(const Interest&, const lp::Nack&)> NackCallback;
56
57/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000058 * @brief Callback invoked when expressed Interest times out
Eric Newberry83872fd2015-08-06 17:01:24 -070059 */
60typedef function<void(const Interest&)> TimeoutCallback;
61
62/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000063 * @brief Callback invoked when incoming Interest matches the specified InterestFilter
64 */
65typedef function<void(const InterestFilter&, const Interest&)> InterestCallback;
66
67/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000068 * @brief Callback invoked when registerPrefix or setInterestFilter command succeeds
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080069 */
Alexander Afanasyev984ad192014-05-02 19:11:15 -070070typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080071
Alexander Afanasyev984ad192014-05-02 19:11:15 -070072/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000073 * @brief Callback invoked when registerPrefix or setInterestFilter command fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -070074 */
75typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
76
77/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000078 * @brief Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds
Alexander Afanasyev984ad192014-05-02 19:11:15 -070079 */
80typedef function<void()> UnregisterPrefixSuccessCallback;
81
82/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000083 * @brief Callback invoked when unregisterPrefix or unsetInterestFilter command fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -070084 */
85typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080086
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080087/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000088 * @brief Provide a communication channel with local or remote NDN forwarder
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070089 */
Alexander Afanasyev8460afb2014-02-15 20:31:42 -080090class Face : noncopyable
91{
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070092public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070093 class Error : public std::runtime_error
94 {
95 public:
96 explicit
97 Error(const std::string& what)
98 : std::runtime_error(what)
99 {
100 }
101 };
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800102
Junxiao Shib6e276f2017-08-14 20:10:04 +0000103 /**
104 * @brief Exception thrown when attempting to send a packet over size limit
105 */
106 class OversizedPacketError : public Error
107 {
108 public:
109 /**
110 * @brief Constructor
111 * @param pktType packet type, 'I' for Interest, 'D' for Data, 'N' for Nack
112 * @param name packet name
113 * @param wireSize wire encoding size
114 */
115 OversizedPacketError(char pktType, const Name& name, size_t wireSize);
116
117 public:
118 const char pktType;
119 const Name name;
120 const size_t wireSize;
121 };
122
Junxiao Shiedd834e2014-10-28 20:28:58 -0700123public: // constructors
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800124 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000125 * @brief Create Face using default transport and given io_service
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700126 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000127 * Usage examples:
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700128 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000129 * @code
130 * Face face1;
131 * Face face2(face1.getIoService());
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700132 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000133 * // Now the following ensures that events on both faces are processed
134 * face1.processEvents();
135 * // or face1.getIoService().run();
136 * @endcode
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700137 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000138 * or
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700139 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000140 * @code
141 * boost::asio::io_service ioService;
142 * Face face1(ioService);
143 * Face face2(ioService);
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700144 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000145 * ioService.run();
146 * @endcode
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700147 *
148 * @param ioService A reference to boost::io_service object that should control all
149 * IO operations.
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000150 * @throw ConfigFile::Error the configuration file cannot be parsed or specifies an unsupported protocol
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700151 */
152 explicit
Alexander Afanasyevc37bf162018-05-08 19:54:16 -0400153 Face(DummyIoService& ioService);
154
155 /**
156 * @brief Create Face using given transport (or default transport if omitted)
157 * @param transport the transport for lower layer communication. If nullptr,
158 * a default transport will be used. The default transport is
159 * determined from a FaceUri in NDN_CLIENT_TRANSPORT environ,
160 * a FaceUri in configuration file 'transport' key, or UnixTransport.
161 *
162 * @throw ConfigFile::Error @p transport is nullptr, and the configuration file cannot be
163 * parsed or specifies an unsupported protocol
164 * @note shared_ptr is passed by value because ownership is shared with this class
165 */
166 explicit
167 Face(shared_ptr<Transport> transport = nullptr);
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700168
169 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000170 * @brief Create Face using given transport and KeyChain
171 * @param transport the transport for lower layer communication. If nullptr,
172 * a default transport will be used.
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700173 * @param keyChain the KeyChain to sign commands
174 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000175 * @sa Face(shared_ptr<Transport>)
176 *
Junxiao Shib6e276f2017-08-14 20:10:04 +0000177 * @throw ConfigFile::Error @p transport is nullptr, and the configuration file cannot be
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000178 * parsed or specifies an unsupported protocol
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700179 * @note shared_ptr is passed by value because ownership is shared with this class
180 */
181 Face(shared_ptr<Transport> transport, KeyChain& keyChain);
182
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500183 virtual
Junxiao Shiedd834e2014-10-28 20:28:58 -0700184 ~Face();
185
186public: // consumer
187 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800188 * @brief Express Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700189 * @param interest the Interest; a copy will be made, so that the caller is not
190 * required to maintain the argument unchanged
191 * @param afterSatisfied function to be invoked if Data is returned
192 * @param afterNacked function to be invoked if Network NACK is returned
193 * @param afterTimeout function to be invoked if neither Data nor Network NACK
194 * is returned within InterestLifetime
Junxiao Shib6e276f2017-08-14 20:10:04 +0000195 * @throw OversizedPacketError encoded Interest size exceeds MAX_NDN_PACKET_SIZE
Eric Newberry83872fd2015-08-06 17:01:24 -0700196 */
197 const PendingInterestId*
198 expressInterest(const Interest& interest,
199 const DataCallback& afterSatisfied,
200 const NackCallback& afterNacked,
201 const TimeoutCallback& afterTimeout);
202
203 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700204 * @brief Cancel previously expressed Interest
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700205 *
Jeff Thompson11095142013-10-01 16:20:28 -0700206 * @param pendingInterestId The ID returned from expressInterest.
207 */
208 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800209 removePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700210
Jeff Thompson432c8be2013-08-09 16:16:08 -0700211 /**
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500212 * @brief Cancel all previously expressed Interests
213 */
214 void
215 removeAllPendingInterests();
216
217 /**
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700218 * @brief Get number of pending Interests
219 */
220 size_t
221 getNPendingInterests() const;
222
Junxiao Shiedd834e2014-10-28 20:28:58 -0700223public: // producer
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700224 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700225 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
226 * callback and register the filtered prefix with the connected NDN forwarder
227 *
228 * This version of setInterestFilter combines setInterestFilter and registerPrefix
229 * operations and is intended to be used when only one filter for the same prefix needed
230 * to be set. When multiple names sharing the same prefix should be dispatched to
231 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
232 * a series of setInterestFilter calls.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700233 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000234 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700235 * @param onInterest A callback to be called when a matching interest is received
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700236 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400237 * @param flags (optional) RIB flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400238 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
239 * used in the signature will be used.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700240 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700241 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
242 * removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700243 */
244 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000245 setInterestFilter(const InterestFilter& interestFilter,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000246 const InterestCallback& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700247 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400248 const security::SigningInfo& signingInfo = security::SigningInfo(),
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700249 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700250
251 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700252 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
253 * callback and register the filtered prefix with the connected NDN forwarder
254 *
255 * This version of setInterestFilter combines setInterestFilter and registerPrefix
256 * operations and is intended to be used when only one filter for the same prefix needed
257 * to be set. When multiple names sharing the same prefix should be dispatched to
258 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
259 * a series of setInterestFilter calls.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700260 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000261 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700262 * @param onInterest A callback to be called when a matching interest is received
263 * @param onSuccess A callback to be called when prefixRegister command succeeds
264 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400265 * @param flags (optional) RIB flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400266 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
267 * used in the signature will be used.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700268 *
Joao Pereira0b3cac52015-07-02 14:49:49 -0400269 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
270 * removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700271 */
272 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000273 setInterestFilter(const InterestFilter& interestFilter,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000274 const InterestCallback& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700275 const RegisterPrefixSuccessCallback& onSuccess,
276 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400277 const security::SigningInfo& signingInfo = security::SigningInfo(),
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700278 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700279
280 /**
281 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest callback
282 *
283 * @param interestFilter Interest
284 * @param onInterest A callback to be called when a matching interest is received
285 *
286 * This method modifies library's FIB only, and does not register the prefix with the
287 * forwarder. It will always succeed. To register prefix with the forwarder, use
288 * registerPrefix, or use the setInterestFilter overload taking two callbacks.
289 *
290 * @return Opaque interest filter ID which can be used with unsetInterestFilter
291 */
292 const InterestFilterId*
293 setInterestFilter(const InterestFilter& interestFilter,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000294 const InterestCallback& onInterest);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700295
Joao Pereira0b3cac52015-07-02 14:49:49 -0400296 /**
297 * @brief Register prefix with the connected NDN forwarder
298 *
299 * This method only modifies forwarder's RIB and does not associate any
300 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
301 * the right callbacks.
302 *
303 * @param prefix A prefix to register with the connected NDN forwarder
304 * @param onSuccess A callback to be called when prefixRegister command succeeds
305 * @param onFailure A callback to be called when prefixRegister command fails
306 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
307 * used in the signature will be used.
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700308 * @param flags Prefix registration flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400309 *
310 * @return The registered prefix ID which can be used with unregisterPrefix
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700311 * @see nfd::RouteFlags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400312 */
313 const RegisteredPrefixId*
314 registerPrefix(const Name& prefix,
315 const RegisterPrefixSuccessCallback& onSuccess,
316 const RegisterPrefixFailureCallback& onFailure,
317 const security::SigningInfo& signingInfo = security::SigningInfo(),
318 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
319
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700320 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300321 * @brief Remove the registered prefix entry with the registeredPrefixId
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700322 *
323 * This does not affect another registered prefix with a different registeredPrefixId,
324 * even it if has the same prefix name. If there is no entry with the
325 * registeredPrefixId, do nothing.
326 *
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700327 * unsetInterestFilter will use the same credentials as original
328 * setInterestFilter/registerPrefix command
329 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700330 * @param registeredPrefixId The ID returned from registerPrefix
Jeff Thompson11095142013-10-01 16:20:28 -0700331 */
332 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800333 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800334
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700335 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700336 * @brief Remove previously set InterestFilter from library's FIB
337 *
338 * This method always succeeds and will **NOT** send any request to the connected
339 * forwarder.
340 *
341 * @param interestFilterId The ID returned from setInterestFilter.
342 */
343 void
344 unsetInterestFilter(const InterestFilterId* interestFilterId);
345
346 /**
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400347 * @brief Unregister prefix from RIB
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700348 *
349 * unregisterPrefix will use the same credentials as original
350 * setInterestFilter/registerPrefix command
351 *
352 * If registeredPrefixId was obtained using setInterestFilter, the corresponding
353 * InterestFilter will be unset too.
354 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700355 * @param registeredPrefixId The ID returned from registerPrefix
356 * @param onSuccess Callback to be called when operation succeeds
357 * @param onFailure Callback to be called when operation fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700358 */
359 void
360 unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
361 const UnregisterPrefixSuccessCallback& onSuccess,
362 const UnregisterPrefixFailureCallback& onFailure);
363
Junxiao Shib6e276f2017-08-14 20:10:04 +0000364 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800365 * @brief Publish data packet
Junxiao Shib6e276f2017-08-14 20:10:04 +0000366 * @param data the Data; a copy will be made, so that the caller is not required to
367 * maintain the argument unchanged
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800368 *
Junxiao Shib6e276f2017-08-14 20:10:04 +0000369 * This method can be called to satisfy incoming Interests, or to add Data packet into the cache
370 * of the local NDN forwarder if forwarder is configured to accept unsolicited Data.
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700371 *
Junxiao Shib6e276f2017-08-14 20:10:04 +0000372 * @throw OversizedPacketError encoded Data size exceeds MAX_NDN_PACKET_SIZE
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800373 */
374 void
Junxiao Shib6e276f2017-08-14 20:10:04 +0000375 put(Data data);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700376
Eric Newberry83872fd2015-08-06 17:01:24 -0700377 /**
Junxiao Shib6e276f2017-08-14 20:10:04 +0000378 * @brief Send a network NACK
Eric Newberry83872fd2015-08-06 17:01:24 -0700379 * @param nack the Nack; a copy will be made, so that the caller is not required to
380 * maintain the argument unchanged
Junxiao Shib6e276f2017-08-14 20:10:04 +0000381 * @throw OversizedPacketError encoded Nack size exceeds MAX_NDN_PACKET_SIZE
Eric Newberry83872fd2015-08-06 17:01:24 -0700382 */
383 void
Junxiao Shib6e276f2017-08-14 20:10:04 +0000384 put(lp::Nack nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700385
Junxiao Shiedd834e2014-10-28 20:28:58 -0700386public: // IO routine
Jeff Thompson86507bc2013-08-23 20:51:38 -0700387 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700388 * @brief Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800389 *
390 * This call will block forever (default timeout == 0) to process IO on the face.
391 * To exit, one expected to call face.shutdown() from one of the callback methods.
392 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700393 * If positive timeout is specified, then processEvents will exit after this timeout, if
394 * not stopped earlier with face.shutdown() or when all active events finish. The call
395 * can be called repeatedly, if desired.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800396 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700397 * If negative timeout is specified, then processEvents will not block and process only
398 * pending events.
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800399 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700400 * @param timeout maximum time to block the thread
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700401 * @param keepThread Keep thread in a blocked state (in event processing), even when
402 * there are no outstanding events (e.g., no Interest/Data is expected)
403 *
Junxiao Shib6e276f2017-08-14 20:10:04 +0000404 * @note This may throw an exception for reading data or in the callback for processing
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700405 * the data. If you call this from an main event loop, you may want to catch and
406 * log/disregard all exceptions.
Junxiao Shib6e276f2017-08-14 20:10:04 +0000407 *
408 * @throw OversizedPacketError encoded packet size exceeds MAX_NDN_PACKET_SIZE
Jeff Thompson432c8be2013-08-09 16:16:08 -0700409 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700410 void
Junxiao Shi2ed9e072017-08-13 16:45:48 +0000411 processEvents(time::milliseconds timeout = time::milliseconds::zero(),
Junxiao Shic828dfc2016-09-15 13:26:22 +0000412 bool keepThread = false)
413 {
414 this->doProcessEvents(timeout, keepThread);
415 }
Jeff Thompson432c8be2013-08-09 16:16:08 -0700416
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700417 /**
418 * @brief Shutdown face operations
419 *
420 * This method cancels all pending operations and closes connection to NDN Forwarder.
421 *
422 * Note that this method does not stop IO service and if the same IO service is shared
423 * between multiple Faces or with other IO objects (e.g., Scheduler).
424 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700425 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700426 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800427
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700428 /**
Spyridon Mastorakisb42184a2016-11-28 18:03:05 -0800429 * @brief Return nullptr (cannot use IoService in simulations), preserved for API compatibility
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700430 */
Alexander Afanasyevc37bf162018-05-08 19:54:16 -0400431 DummyIoService&
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700432 getIoService()
433 {
Alexander Afanasyevc37bf162018-05-08 19:54:16 -0400434 static DummyIoService io;
435 return io;
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700436 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800437
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800438NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
439 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000440 * @return underlying transport
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800441 */
442 shared_ptr<Transport>
443 getTransport();
444
Junxiao Shic828dfc2016-09-15 13:26:22 +0000445protected:
446 virtual void
Junxiao Shi2ed9e072017-08-13 16:45:48 +0000447 doProcessEvents(time::milliseconds timeout, bool keepThread);
Junxiao Shic828dfc2016-09-15 13:26:22 +0000448
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800449private:
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700450 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000451 * @throw ConfigFile::Error on parse error and unsupported protocols
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700452 */
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800453 shared_ptr<Transport>
454 makeDefaultTransport();
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700455
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600456 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000457 * @throw Face::Error on unsupported protocol
Junxiao Shi2cced062014-11-02 21:27:38 -0700458 * @note shared_ptr is passed by value because ownership is transferred to this function
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600459 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800460 void
Joao Pereira68c0d882015-05-19 14:27:55 -0400461 construct(shared_ptr<Transport> transport, KeyChain& keyChain);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600462
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700463 void
Eric Newberry83872fd2015-08-06 17:01:24 -0700464 onReceiveElement(const Block& blockFromDaemon);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800465
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700466 void
467 asyncShutdown();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700468
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700469private:
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800470 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800471
Joao Pereira68c0d882015-05-19 14:27:55 -0400472 unique_ptr<nfd::Controller> m_nfdController;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600473
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700474 class Impl;
Junxiao Shiae0b4182016-08-08 22:53:17 +0000475 shared_ptr<Impl> m_impl;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700476};
477
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800478} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700479
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800480#endif // NDN_FACE_HPP