blob: 236b2969a89ce431508db1d61f12023816779383 [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"
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080031#include "security/key-chain.hpp"
Junxiao Shib6e276f2017-08-14 20:10:04 +000032#include "security/signing-info.hpp"
Joao Pereira0b3cac52015-07-02 14:49:49 -040033
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070034namespace boost {
35namespace asio {
36class io_service;
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070037} // namespace asio
38} // namespace boost
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080039
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070040namespace ndn {
41
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070042class Transport;
43
44class PendingInterestId;
45class RegisteredPrefixId;
46class InterestFilterId;
47
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070048namespace nfd {
49class Controller;
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070050} // namespace nfd
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070051
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070052/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000053 * @brief Callback invoked when expressed Interest gets satisfied with a Data packet
Eric Newberry83872fd2015-08-06 17:01:24 -070054 */
55typedef function<void(const Interest&, const Data&)> DataCallback;
56
57/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000058 * @brief Callback invoked when Nack is sent in response to expressed Interest
Eric Newberry83872fd2015-08-06 17:01:24 -070059 */
60typedef function<void(const Interest&, const lp::Nack&)> NackCallback;
61
62/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000063 * @brief Callback invoked when expressed Interest times out
Eric Newberry83872fd2015-08-06 17:01:24 -070064 */
65typedef function<void(const Interest&)> TimeoutCallback;
66
67/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000068 * @brief Callback invoked when incoming Interest matches the specified InterestFilter
69 */
70typedef function<void(const InterestFilter&, const Interest&)> InterestCallback;
71
72/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000073 * @brief Callback invoked when registerPrefix or setInterestFilter command succeeds
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080074 */
Alexander Afanasyev984ad192014-05-02 19:11:15 -070075typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080076
Alexander Afanasyev984ad192014-05-02 19:11:15 -070077/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000078 * @brief Callback invoked when registerPrefix or setInterestFilter command fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -070079 */
80typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
81
82/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000083 * @brief Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds
Alexander Afanasyev984ad192014-05-02 19:11:15 -070084 */
85typedef function<void()> UnregisterPrefixSuccessCallback;
86
87/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000088 * @brief Callback invoked when unregisterPrefix or unsetInterestFilter command fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -070089 */
90typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080091
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080092/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000093 * @brief Provide a communication channel with local or remote NDN forwarder
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070094 */
Alexander Afanasyev8460afb2014-02-15 20:31:42 -080095class Face : noncopyable
96{
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070097public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098 class Error : public std::runtime_error
99 {
100 public:
101 explicit
102 Error(const std::string& what)
103 : std::runtime_error(what)
104 {
105 }
106 };
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800107
Junxiao Shib6e276f2017-08-14 20:10:04 +0000108 /**
109 * @brief Exception thrown when attempting to send a packet over size limit
110 */
111 class OversizedPacketError : public Error
112 {
113 public:
114 /**
115 * @brief Constructor
116 * @param pktType packet type, 'I' for Interest, 'D' for Data, 'N' for Nack
117 * @param name packet name
118 * @param wireSize wire encoding size
119 */
120 OversizedPacketError(char pktType, const Name& name, size_t wireSize);
121
122 public:
123 const char pktType;
124 const Name name;
125 const size_t wireSize;
126 };
127
Junxiao Shiedd834e2014-10-28 20:28:58 -0700128public: // constructors
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800129 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000130 * @brief Create Face using given transport (or default transport if omitted)
131 * @param transport the transport for lower layer communication. If nullptr,
132 * a default transport will be used. The default transport is
133 * determined from a FaceUri in NDN_CLIENT_TRANSPORT environ,
134 * a FaceUri in configuration file 'transport' key, or UnixTransport.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700135 *
Junxiao Shib6e276f2017-08-14 20:10:04 +0000136 * @throw ConfigFile::Error @p transport is nullptr, and the configuration file cannot be
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000137 * parsed or specifies an unsupported protocol
138 * @note shared_ptr is passed by value because ownership is shared with this class
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800139 */
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000140 explicit
141 Face(shared_ptr<Transport> transport = nullptr);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800142
143 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000144 * @brief Create Face using default transport and given io_service
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700145 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000146 * Usage examples:
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700147 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000148 * @code
149 * Face face1;
150 * Face face2(face1.getIoService());
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700151 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000152 * // Now the following ensures that events on both faces are processed
153 * face1.processEvents();
154 * // or face1.getIoService().run();
155 * @endcode
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700156 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000157 * or
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700158 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000159 * @code
160 * boost::asio::io_service ioService;
161 * Face face1(ioService);
162 * Face face2(ioService);
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700163 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000164 * ioService.run();
165 * @endcode
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700166 *
167 * @param ioService A reference to boost::io_service object that should control all
168 * IO operations.
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000169 * @throw ConfigFile::Error the configuration file cannot be parsed or specifies an unsupported protocol
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700170 */
171 explicit
172 Face(boost::asio::io_service& ioService);
173
174 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000175 * @brief Create Face using TcpTransport
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700176 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000177 * @param host IP address or hostname of the NDN forwarder
178 * @param port port number or service name of the NDN forwarder (**default**: "6363")
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700179 */
Junxiao Shiae0b4182016-08-08 22:53:17 +0000180 explicit
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800181 Face(const std::string& host, const std::string& port = "6363");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800182
183 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000184 * @brief Create Face using given transport and KeyChain
185 * @param transport the transport for lower layer communication. If nullptr,
186 * a default transport will be used.
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700187 * @param keyChain the KeyChain to sign commands
188 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000189 * @sa Face(shared_ptr<Transport>)
190 *
Junxiao Shib6e276f2017-08-14 20:10:04 +0000191 * @throw ConfigFile::Error @p transport is nullptr, and the configuration file cannot be
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000192 * parsed or specifies an unsupported protocol
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700193 * @note shared_ptr is passed by value because ownership is shared with this class
194 */
195 Face(shared_ptr<Transport> transport, KeyChain& keyChain);
196
197 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000198 * @brief Create Face using given transport and IO service
199 * @param transport the transport for lower layer communication. If nullptr,
200 * a default transport will be used.
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800201 * @param ioService the io_service that controls all IO operations
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800202 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700203 * @sa Face(boost::asio::io_service&)
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000204 * @sa Face(shared_ptr<Transport>)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800205 *
Junxiao Shib6e276f2017-08-14 20:10:04 +0000206 * @throw ConfigFile::Error @p transport is nullptr, and the configuration file cannot be
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000207 * parsed or specifies an unsupported protocol
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700208 * @note shared_ptr is passed by value because ownership is shared with this class
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800209 */
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700210 Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800211
Jeff Thompson4fe45512013-08-23 14:06:38 -0700212 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000213 * @brief Create a new Face using given Transport and IO service
214 * @param transport the transport for lower layer communication. If nullptr,
215 * a default transport will be used.
Junxiao Shiedd834e2014-10-28 20:28:58 -0700216 * @param ioService the io_service that controls all IO operations
217 * @param keyChain the KeyChain to sign commands
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700218 *
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000219 * @sa Face(boost::asio::io_service&)
220 * @sa Face(shared_ptr<Transport>, KeyChain&)
221 *
Junxiao Shib6e276f2017-08-14 20:10:04 +0000222 * @throw ConfigFile::Error @p transport is nullptr, and the configuration file cannot be
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000223 * parsed or specifies an unsupported protocol
Junxiao Shiedd834e2014-10-28 20:28:58 -0700224 * @note shared_ptr is passed by value because ownership is shared with this class
225 */
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700226 Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain);
Junxiao Shiedd834e2014-10-28 20:28:58 -0700227
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500228 virtual
Junxiao Shiedd834e2014-10-28 20:28:58 -0700229 ~Face();
230
231public: // consumer
232 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800233 * @brief Express Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700234 * @param interest the Interest; a copy will be made, so that the caller is not
235 * required to maintain the argument unchanged
236 * @param afterSatisfied function to be invoked if Data is returned
237 * @param afterNacked function to be invoked if Network NACK is returned
238 * @param afterTimeout function to be invoked if neither Data nor Network NACK
239 * is returned within InterestLifetime
Junxiao Shib6e276f2017-08-14 20:10:04 +0000240 * @throw OversizedPacketError encoded Interest size exceeds MAX_NDN_PACKET_SIZE
Eric Newberry83872fd2015-08-06 17:01:24 -0700241 */
242 const PendingInterestId*
243 expressInterest(const Interest& interest,
244 const DataCallback& afterSatisfied,
245 const NackCallback& afterNacked,
246 const TimeoutCallback& afterTimeout);
247
248 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700249 * @brief Cancel previously expressed Interest
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700250 *
Jeff Thompson11095142013-10-01 16:20:28 -0700251 * @param pendingInterestId The ID returned from expressInterest.
252 */
253 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800254 removePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700255
Jeff Thompson432c8be2013-08-09 16:16:08 -0700256 /**
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500257 * @brief Cancel all previously expressed Interests
258 */
259 void
260 removeAllPendingInterests();
261
262 /**
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700263 * @brief Get number of pending Interests
264 */
265 size_t
266 getNPendingInterests() const;
267
Junxiao Shiedd834e2014-10-28 20:28:58 -0700268public: // producer
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700269 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700270 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
271 * callback and register the filtered prefix with the connected NDN forwarder
272 *
273 * This version of setInterestFilter combines setInterestFilter and registerPrefix
274 * operations and is intended to be used when only one filter for the same prefix needed
275 * to be set. When multiple names sharing the same prefix should be dispatched to
276 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
277 * a series of setInterestFilter calls.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700278 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000279 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700280 * @param onInterest A callback to be called when a matching interest is received
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700281 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400282 * @param flags (optional) RIB flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400283 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
284 * used in the signature will be used.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700285 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700286 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
287 * removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700288 */
289 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000290 setInterestFilter(const InterestFilter& interestFilter,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000291 const InterestCallback& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700292 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400293 const security::SigningInfo& signingInfo = security::SigningInfo(),
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700294 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700295
296 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700297 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
298 * callback and register the filtered prefix with the connected NDN forwarder
299 *
300 * This version of setInterestFilter combines setInterestFilter and registerPrefix
301 * operations and is intended to be used when only one filter for the same prefix needed
302 * to be set. When multiple names sharing the same prefix should be dispatched to
303 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
304 * a series of setInterestFilter calls.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700305 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000306 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700307 * @param onInterest A callback to be called when a matching interest is received
308 * @param onSuccess A callback to be called when prefixRegister command succeeds
309 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400310 * @param flags (optional) RIB flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400311 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
312 * used in the signature will be used.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700313 *
Joao Pereira0b3cac52015-07-02 14:49:49 -0400314 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
315 * removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700316 */
317 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000318 setInterestFilter(const InterestFilter& interestFilter,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000319 const InterestCallback& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700320 const RegisterPrefixSuccessCallback& onSuccess,
321 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400322 const security::SigningInfo& signingInfo = security::SigningInfo(),
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700323 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700324
325 /**
326 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest callback
327 *
328 * @param interestFilter Interest
329 * @param onInterest A callback to be called when a matching interest is received
330 *
331 * This method modifies library's FIB only, and does not register the prefix with the
332 * forwarder. It will always succeed. To register prefix with the forwarder, use
333 * registerPrefix, or use the setInterestFilter overload taking two callbacks.
334 *
335 * @return Opaque interest filter ID which can be used with unsetInterestFilter
336 */
337 const InterestFilterId*
338 setInterestFilter(const InterestFilter& interestFilter,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000339 const InterestCallback& onInterest);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700340
Joao Pereira0b3cac52015-07-02 14:49:49 -0400341 /**
342 * @brief Register prefix with the connected NDN forwarder
343 *
344 * This method only modifies forwarder's RIB and does not associate any
345 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
346 * the right callbacks.
347 *
348 * @param prefix A prefix to register with the connected NDN forwarder
349 * @param onSuccess A callback to be called when prefixRegister command succeeds
350 * @param onFailure A callback to be called when prefixRegister command fails
351 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
352 * used in the signature will be used.
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700353 * @param flags Prefix registration flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400354 *
355 * @return The registered prefix ID which can be used with unregisterPrefix
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700356 * @see nfd::RouteFlags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400357 */
358 const RegisteredPrefixId*
359 registerPrefix(const Name& prefix,
360 const RegisterPrefixSuccessCallback& onSuccess,
361 const RegisterPrefixFailureCallback& onFailure,
362 const security::SigningInfo& signingInfo = security::SigningInfo(),
363 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
364
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700365 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300366 * @brief Remove the registered prefix entry with the registeredPrefixId
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700367 *
368 * This does not affect another registered prefix with a different registeredPrefixId,
369 * even it if has the same prefix name. If there is no entry with the
370 * registeredPrefixId, do nothing.
371 *
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700372 * unsetInterestFilter will use the same credentials as original
373 * setInterestFilter/registerPrefix command
374 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700375 * @param registeredPrefixId The ID returned from registerPrefix
Jeff Thompson11095142013-10-01 16:20:28 -0700376 */
377 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800378 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800379
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700380 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700381 * @brief Remove previously set InterestFilter from library's FIB
382 *
383 * This method always succeeds and will **NOT** send any request to the connected
384 * forwarder.
385 *
386 * @param interestFilterId The ID returned from setInterestFilter.
387 */
388 void
389 unsetInterestFilter(const InterestFilterId* interestFilterId);
390
391 /**
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400392 * @brief Unregister prefix from RIB
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700393 *
394 * unregisterPrefix will use the same credentials as original
395 * setInterestFilter/registerPrefix command
396 *
397 * If registeredPrefixId was obtained using setInterestFilter, the corresponding
398 * InterestFilter will be unset too.
399 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700400 * @param registeredPrefixId The ID returned from registerPrefix
401 * @param onSuccess Callback to be called when operation succeeds
402 * @param onFailure Callback to be called when operation fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700403 */
404 void
405 unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
406 const UnregisterPrefixSuccessCallback& onSuccess,
407 const UnregisterPrefixFailureCallback& onFailure);
408
Junxiao Shib6e276f2017-08-14 20:10:04 +0000409 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800410 * @brief Publish data packet
Junxiao Shib6e276f2017-08-14 20:10:04 +0000411 * @param data the Data; a copy will be made, so that the caller is not required to
412 * maintain the argument unchanged
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800413 *
Junxiao Shib6e276f2017-08-14 20:10:04 +0000414 * This method can be called to satisfy incoming Interests, or to add Data packet into the cache
415 * of the local NDN forwarder if forwarder is configured to accept unsolicited Data.
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700416 *
Junxiao Shib6e276f2017-08-14 20:10:04 +0000417 * @throw OversizedPacketError encoded Data size exceeds MAX_NDN_PACKET_SIZE
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800418 */
419 void
Junxiao Shib6e276f2017-08-14 20:10:04 +0000420 put(Data data);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700421
Eric Newberry83872fd2015-08-06 17:01:24 -0700422 /**
Junxiao Shib6e276f2017-08-14 20:10:04 +0000423 * @brief Send a network NACK
Eric Newberry83872fd2015-08-06 17:01:24 -0700424 * @param nack the Nack; a copy will be made, so that the caller is not required to
425 * maintain the argument unchanged
Junxiao Shib6e276f2017-08-14 20:10:04 +0000426 * @throw OversizedPacketError encoded Nack size exceeds MAX_NDN_PACKET_SIZE
Eric Newberry83872fd2015-08-06 17:01:24 -0700427 */
428 void
Junxiao Shib6e276f2017-08-14 20:10:04 +0000429 put(lp::Nack nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700430
Junxiao Shiedd834e2014-10-28 20:28:58 -0700431public: // IO routine
Jeff Thompson86507bc2013-08-23 20:51:38 -0700432 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700433 * @brief Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800434 *
435 * This call will block forever (default timeout == 0) to process IO on the face.
436 * To exit, one expected to call face.shutdown() from one of the callback methods.
437 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700438 * If positive timeout is specified, then processEvents will exit after this timeout, if
439 * not stopped earlier with face.shutdown() or when all active events finish. The call
440 * can be called repeatedly, if desired.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800441 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700442 * If negative timeout is specified, then processEvents will not block and process only
443 * pending events.
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800444 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700445 * @param timeout maximum time to block the thread
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700446 * @param keepThread Keep thread in a blocked state (in event processing), even when
447 * there are no outstanding events (e.g., no Interest/Data is expected)
448 *
Junxiao Shib6e276f2017-08-14 20:10:04 +0000449 * @note This may throw an exception for reading data or in the callback for processing
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700450 * the data. If you call this from an main event loop, you may want to catch and
451 * log/disregard all exceptions.
Junxiao Shib6e276f2017-08-14 20:10:04 +0000452 *
453 * @throw OversizedPacketError encoded packet size exceeds MAX_NDN_PACKET_SIZE
Jeff Thompson432c8be2013-08-09 16:16:08 -0700454 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700455 void
Junxiao Shi2ed9e072017-08-13 16:45:48 +0000456 processEvents(time::milliseconds timeout = time::milliseconds::zero(),
Junxiao Shic828dfc2016-09-15 13:26:22 +0000457 bool keepThread = false)
458 {
459 this->doProcessEvents(timeout, keepThread);
460 }
Jeff Thompson432c8be2013-08-09 16:16:08 -0700461
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700462 /**
463 * @brief Shutdown face operations
464 *
465 * This method cancels all pending operations and closes connection to NDN Forwarder.
466 *
467 * Note that this method does not stop IO service and if the same IO service is shared
468 * between multiple Faces or with other IO objects (e.g., Scheduler).
469 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700470 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700471 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800472
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700473 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000474 * @return reference to IO service object
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700475 */
476 boost::asio::io_service&
477 getIoService()
478 {
Junxiao Shi2cced062014-11-02 21:27:38 -0700479 return m_ioService;
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700480 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800481
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800482NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
483 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000484 * @return underlying transport
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800485 */
486 shared_ptr<Transport>
487 getTransport();
488
Junxiao Shic828dfc2016-09-15 13:26:22 +0000489protected:
490 virtual void
Junxiao Shi2ed9e072017-08-13 16:45:48 +0000491 doProcessEvents(time::milliseconds timeout, bool keepThread);
Junxiao Shic828dfc2016-09-15 13:26:22 +0000492
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800493private:
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700494 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000495 * @throw ConfigFile::Error on parse error and unsupported protocols
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700496 */
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800497 shared_ptr<Transport>
498 makeDefaultTransport();
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700499
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600500 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000501 * @throw Face::Error on unsupported protocol
Junxiao Shi2cced062014-11-02 21:27:38 -0700502 * @note shared_ptr is passed by value because ownership is transferred to this function
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600503 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800504 void
Joao Pereira68c0d882015-05-19 14:27:55 -0400505 construct(shared_ptr<Transport> transport, KeyChain& keyChain);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600506
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700507 void
Eric Newberry83872fd2015-08-06 17:01:24 -0700508 onReceiveElement(const Block& blockFromDaemon);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800509
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700510 void
511 asyncShutdown();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700512
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700513private:
Junxiao Shi2cced062014-11-02 21:27:38 -0700514 /// the IO service owned by this Face, could be null
515 unique_ptr<boost::asio::io_service> m_internalIoService;
516 /// the IO service used by this Face
517 boost::asio::io_service& m_ioService;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700518
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800519 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800520
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000521 /**
522 * @brief if not null, a pointer to an internal KeyChain owned by Face
523 * @note if a KeyChain is supplied to constructor, this pointer will be null,
524 * and the passed KeyChain is given to nfdController;
525 * currently Face does not keep the KeyChain passed in constructor
526 * because it's not needed, but this may change in the future
Junxiao Shiedd834e2014-10-28 20:28:58 -0700527 */
Joao Pereira68c0d882015-05-19 14:27:55 -0400528 unique_ptr<KeyChain> m_internalKeyChain;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700529
Joao Pereira68c0d882015-05-19 14:27:55 -0400530 unique_ptr<nfd::Controller> m_nfdController;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600531
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700532 class Impl;
Junxiao Shiae0b4182016-08-08 22:53:17 +0000533 shared_ptr<Impl> m_impl;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700534};
535
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800536} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700537
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800538#endif // NDN_FACE_HPP