blob: e07f5418f4a1d0e20bbb8ed0c096bca1ce7d6d52 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07002/**
Alexander Afanasyev9d158f02015-02-17 21:30:19 -08003 * Copyright (c) 2013-2015 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
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080025#include "common.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070026
27#include "name.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080028#include "interest.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070029#include "interest-filter.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080030#include "data.hpp"
Joao Pereira0b3cac52015-07-02 14:49:49 -040031#include "security/signing-info.hpp"
Eric Newberry83872fd2015-08-06 17:01:24 -070032#include "lp/nack.hpp"
Joao Pereira0b3cac52015-07-02 14:49:49 -040033
34#define NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
35
36#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -070037#include "security/identity-certificate.hpp"
Joao Pereira0b3cac52015-07-02 14:49:49 -040038#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080039
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070040namespace boost {
41namespace asio {
42class io_service;
43}
44}
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080045
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070046namespace ndn {
47
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070048class Transport;
49
50class PendingInterestId;
51class RegisteredPrefixId;
52class InterestFilterId;
53
Yingdi Yu1b0311c2015-06-10 14:58:47 -070054namespace security {
55class KeyChain;
56}
57using security::KeyChain;
58
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070059namespace nfd {
60class Controller;
61}
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070062
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070063/**
Eric Newberry83872fd2015-08-06 17:01:24 -070064 * @brief Callback called when expressed Interest gets satisfied with a Data packet
65 */
66typedef function<void(const Interest&, const Data&)> DataCallback;
67
68/**
69 * @brief Callback called when Nack is sent in response to expressed Interest
70 */
71typedef function<void(const Interest&, const lp::Nack&)> NackCallback;
72
73/**
74 * @brief Callback called when expressed Interest times out
75 */
76typedef function<void(const Interest&)> TimeoutCallback;
77
78/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070079 * @brief Callback called when expressed Interest gets satisfied with Data packet
Eric Newberry83872fd2015-08-06 17:01:24 -070080 * @deprecated use DataCallback
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080081 */
82typedef function<void(const Interest&, Data&)> OnData;
83
84/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070085 * @brief Callback called when expressed Interest times out
Eric Newberry83872fd2015-08-06 17:01:24 -070086 * @deprecated use TimeoutCallback
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080087 */
88typedef function<void(const Interest&)> OnTimeout;
89
90/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070091 * @brief Callback called when incoming Interest matches the specified InterestFilter
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080092 */
Alexander Afanasyev90164962014-03-06 08:29:59 +000093typedef function<void (const InterestFilter&, const Interest&)> OnInterest;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080094
95/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070096 * @brief Callback called when registerPrefix or setInterestFilter command succeeds
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080097 */
Alexander Afanasyev984ad192014-05-02 19:11:15 -070098typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080099
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700100/**
101 * @brief Callback called when registerPrefix or setInterestFilter command fails
102 */
103typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
104
105/**
106 * @brief Callback called when unregisterPrefix or unsetInterestFilter command succeeds
107 */
108typedef function<void()> UnregisterPrefixSuccessCallback;
109
110/**
111 * @brief Callback called when unregisterPrefix or unsetInterestFilter command fails
112 */
113typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800114
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800115/**
116 * @brief Abstraction to communicate with local or remote NDN forwarder
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700117 */
Alexander Afanasyev8460afb2014-02-15 20:31:42 -0800118class Face : noncopyable
119{
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700120public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700121 class Error : public std::runtime_error
122 {
123 public:
124 explicit
125 Error(const std::string& what)
126 : std::runtime_error(what)
127 {
128 }
129 };
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800130
Junxiao Shiedd834e2014-10-28 20:28:58 -0700131public: // constructors
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800132 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700133 * @brief Create a new Face using the default transport (UnixTransport)
134 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600135 * @throws ConfigFile::Error on configuration file parse failure
136 * @throws Face::Error on unsupported protocol
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800137 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800138 Face();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800139
140 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700141 * @brief Create a new Face using the default transport (UnixTransport)
142 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700143 * @par Usage examples:
144 *
145 * Face face1;
146 * Face face2(face1.getIoService());
147 *
148 * // Now the following ensures that events on both faces are processed
149 * face1.processEvents();
150 * // or face1.getIoService().run();
151 *
152 * @par or
153 *
154 * boost::asio::io_service ioService;
155 * Face face1(ioService);
156 * Face face2(ioService);
157 * ...
158 *
159 * ioService.run();
160 *
161 * @param ioService A reference to boost::io_service object that should control all
162 * IO operations.
163 * @throws ConfigFile::Error on configuration file parse failure
164 * @throws Face::Error on unsupported protocol
165 */
166 explicit
167 Face(boost::asio::io_service& ioService);
168
169 /**
170 * @brief Create a new Face using TcpTransport
171 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700172 * @param host The host of the NDN forwarder
173 * @param port (optional) The port or service name of the NDN forwarder (**default**: "6363")
174 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600175 * @throws Face::Error on unsupported protocol
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700176 */
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800177 Face(const std::string& host, const std::string& port = "6363");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800178
179 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700180 * @brief Create a new Face using the given Transport
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700181 *
182 * @param transport A shared_ptr to a Transport object used for communication
183 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600184 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800185 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800186 explicit
187 Face(const shared_ptr<Transport>& transport);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800188
189 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700190 * @brief Create a new Face using the given Transport and IO service object
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800191 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700192 * @sa Face(boost::asio::io_service&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800193 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600194 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800195 */
196 Face(const shared_ptr<Transport>& transport,
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700197 boost::asio::io_service& ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800198
Jeff Thompson4fe45512013-08-23 14:06:38 -0700199 /**
Junxiao Shiedd834e2014-10-28 20:28:58 -0700200 * @brief Create a new Face using the given Transport and IO service object
201 * @param transport the Transport used for communication
202 * @param ioService the io_service that controls all IO operations
203 * @param keyChain the KeyChain to sign commands
204 * @throws Face::Error on unsupported protocol
205 * @note shared_ptr is passed by value because ownership is shared with this class
206 */
207 Face(shared_ptr<Transport> transport,
208 boost::asio::io_service& ioService,
209 KeyChain& keyChain);
210
211 ~Face();
212
213public: // consumer
214 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800215 * @brief Express Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700216 * @param interest the Interest; a copy will be made, so that the caller is not
217 * required to maintain the argument unchanged
218 * @param afterSatisfied function to be invoked if Data is returned
219 * @param afterNacked function to be invoked if Network NACK is returned
220 * @param afterTimeout function to be invoked if neither Data nor Network NACK
221 * is returned within InterestLifetime
222 */
223 const PendingInterestId*
224 expressInterest(const Interest& interest,
225 const DataCallback& afterSatisfied,
226 const NackCallback& afterNacked,
227 const TimeoutCallback& afterTimeout);
228
229 /**
230 * @brief Express Interest
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800231 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700232 * @param interest An Interest to be expressed
233 * @param onData Callback to be called when a matching data packet is received
Eric Newberry83872fd2015-08-06 17:01:24 -0700234 * @param onTimeout (optional) A function object to call if the interest times out or is Nacked
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800235 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700236 * @return The pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700237 *
238 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Eric Newberry83872fd2015-08-06 17:01:24 -0700239 *
240 * @deprecated use expressInterest(Interest, DataCallback, NackCallback, TimeoutCallback)
Jeff Thompson4fe45512013-08-23 14:06:38 -0700241 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800242 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800243 expressInterest(const Interest& interest,
Eric Newberry83872fd2015-08-06 17:01:24 -0700244 const OnData& onData,
245 const OnTimeout& onTimeout = nullptr);
Jeff Thompson4fe45512013-08-23 14:06:38 -0700246
247 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800248 * @brief Express Interest using name and Interest template
249 *
250 * @param name Name of the Interest
251 * @param tmpl Interest template to fill parameters
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700252 * @param onData Callback to be called when a matching data packet is received
Eric Newberry83872fd2015-08-06 17:01:24 -0700253 * @param onTimeout (optional) A function object to call if the interest times out or is Nacked
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800254 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700255 * @return Opaque pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700256 *
257 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Eric Newberry83872fd2015-08-06 17:01:24 -0700258 *
259 * @deprecated use expressInterest(Interest, DataCallback, NackCallback, TimeoutCallback)
Jeff Thompson7aec0252013-08-22 17:29:57 -0700260 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800261 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800262 expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800263 const Interest& tmpl,
Eric Newberry83872fd2015-08-06 17:01:24 -0700264 const OnData& onData,
265 const OnTimeout& onTimeout = nullptr);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700266
Jeff Thompson11095142013-10-01 16:20:28 -0700267 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700268 * @brief Cancel previously expressed Interest
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700269 *
Jeff Thompson11095142013-10-01 16:20:28 -0700270 * @param pendingInterestId The ID returned from expressInterest.
271 */
272 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800273 removePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700274
Jeff Thompson432c8be2013-08-09 16:16:08 -0700275 /**
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500276 * @brief Cancel all previously expressed Interests
277 */
278 void
279 removeAllPendingInterests();
280
281 /**
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700282 * @brief Get number of pending Interests
283 */
284 size_t
285 getNPendingInterests() const;
286
Junxiao Shiedd834e2014-10-28 20:28:58 -0700287public: // producer
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700288 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700289 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
290 * callback and register the filtered prefix with the connected NDN forwarder
291 *
292 * This version of setInterestFilter combines setInterestFilter and registerPrefix
293 * operations and is intended to be used when only one filter for the same prefix needed
294 * to be set. When multiple names sharing the same prefix should be dispatched to
295 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
296 * a series of setInterestFilter calls.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700297 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000298 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700299 * @param onInterest A callback to be called when a matching interest is received
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700300 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400301 * @param flags (optional) RIB flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400302 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
303 * used in the signature will be used.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700304 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700305 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
306 * removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700307 */
308 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000309 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700310 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700311 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400312 const security::SigningInfo& signingInfo = security::SigningInfo(),
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700313 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700314
315 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700316 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
317 * callback and register the filtered prefix with the connected NDN forwarder
318 *
319 * This version of setInterestFilter combines setInterestFilter and registerPrefix
320 * operations and is intended to be used when only one filter for the same prefix needed
321 * to be set. When multiple names sharing the same prefix should be dispatched to
322 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
323 * a series of setInterestFilter calls.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700324 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000325 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700326 * @param onInterest A callback to be called when a matching interest is received
327 * @param onSuccess A callback to be called when prefixRegister command succeeds
328 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400329 * @param flags (optional) RIB flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400330 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
331 * used in the signature will be used.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700332 *
Joao Pereira0b3cac52015-07-02 14:49:49 -0400333 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
334 * removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700335 */
336 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000337 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700338 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700339 const RegisterPrefixSuccessCallback& onSuccess,
340 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400341 const security::SigningInfo& signingInfo = security::SigningInfo(),
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700342 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700343
344 /**
345 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest callback
346 *
347 * @param interestFilter Interest
348 * @param onInterest A callback to be called when a matching interest is received
349 *
350 * This method modifies library's FIB only, and does not register the prefix with the
351 * forwarder. It will always succeed. To register prefix with the forwarder, use
352 * registerPrefix, or use the setInterestFilter overload taking two callbacks.
353 *
354 * @return Opaque interest filter ID which can be used with unsetInterestFilter
355 */
356 const InterestFilterId*
357 setInterestFilter(const InterestFilter& interestFilter,
358 const OnInterest& onInterest);
359
Joao Pereira0b3cac52015-07-02 14:49:49 -0400360#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
361 /**
362 * @deprecated Use override with SigningInfo instead of this function
363 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
364 * callback and register the filtered prefix with the connected NDN forwarder
365 *
366 * This version of setInterestFilter combines setInterestFilter and registerPrefix
367 * operations and is intended to be used when only one filter for the same prefix needed
368 * to be set. When multiple names sharing the same prefix should be dispatched to
369 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
370 * a series of setInterestFilter calls.
371 *
372 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
373 * @param onInterest A callback to be called when a matching interest is received
374 * @param onSuccess A callback to be called when prefixRegister command succeeds
375 * @param onFailure A callback to be called when prefixRegister command fails
376 * @param flags (optional) RIB flags
377 * @param certificate (optional) A certificate under which the prefix registration
378 * command is signed. When omitted, a default certificate of
379 * the default identity is used to sign the registration command
380 *
381 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
382 * removeRegisteredPrefix
383 */
philobb778e72015-08-25 14:49:19 -0700384 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400385 const RegisteredPrefixId*
386 setInterestFilter(const InterestFilter& interestFilter,
387 const OnInterest& onInterest,
388 const RegisterPrefixSuccessCallback& onSuccess,
389 const RegisterPrefixFailureCallback& onFailure,
390 const IdentityCertificate& certificate,
philobb778e72015-08-25 14:49:19 -0700391 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700392
393 /**
Joao Pereira0b3cac52015-07-02 14:49:49 -0400394 * @deprecated Use override with SigningInfo instead of this function
395 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
396 * callback and register the filtered prefix with the connected NDN forwarder
397 *
398 * This version of setInterestFilter combines setInterestFilter and registerPrefix
399 * operations and is intended to be used when only one filter for the same prefix needed
400 * to be set. When multiple names sharing the same prefix should be dispatched to
401 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
402 * a series of setInterestFilter calls.
403 *
404 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
405 * @param onInterest A callback to be called when a matching interest is received
406 * @param onFailure A callback to be called when prefixRegister command fails
407 * @param flags (optional) RIB flags
408 * @param certificate (optional) A certificate under which the prefix registration
409 * command is signed. When omitted, a default certificate of
410 * the default identity is used to sign the registration command
411 *
412 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
413 * removeRegisteredPrefix
414 */
philobb778e72015-08-25 14:49:19 -0700415 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400416 const RegisteredPrefixId*
417 setInterestFilter(const InterestFilter& interestFilter,
418 const OnInterest& onInterest,
419 const RegisterPrefixFailureCallback& onFailure,
420 const IdentityCertificate& certificate,
philobb778e72015-08-25 14:49:19 -0700421 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400422
423 /**
424 * @deprecated Use override with SigningInfo instead of this function
425 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
426 * callback and register the filtered prefix with the connected NDN forwarder
427 *
428 * This version of setInterestFilter combines setInterestFilter and registerPrefix
429 * operations and is intended to be used when only one filter for the same prefix needed
430 * to be set. When multiple names sharing the same prefix should be dispatched to
431 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
432 * a series of setInterestFilter calls.
433 *
434 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
435 * @param onInterest A callback to be called when a matching interest is received
436 * @param onSuccess A callback to be called when prefixRegister command succeeds
437 * @param onFailure A callback to be called when prefixRegister command fails
438 * @param identity A signing identity. A prefix registration command is signed
439 * under the default certificate of this identity
440 * @param flags (optional) RIB flags
441 *
442 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
443 */
philobb778e72015-08-25 14:49:19 -0700444 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400445 const RegisteredPrefixId*
446 setInterestFilter(const InterestFilter& interestFilter,
447 const OnInterest& onInterest,
448 const RegisterPrefixSuccessCallback& onSuccess,
449 const RegisterPrefixFailureCallback& onFailure,
450 const Name& identity,
philobb778e72015-08-25 14:49:19 -0700451 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400452
453 /**
454 * @deprecated Use override with SigningInfo instead of this function
455 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
456 * callback and register the filtered prefix with the connected NDN forwarder
457 *
458 * This version of setInterestFilter combines setInterestFilter and registerPrefix
459 * operations and is intended to be used when only one filter for the same prefix needed
460 * to be set. When multiple names sharing the same prefix should be dispatched to
461 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
462 * a series of setInterestFilter calls.
463 *
464 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
465 * @param onInterest A callback to be called when a matching interest is received
466 * @param onFailure A callback to be called when prefixRegister command fails
467 * @param identity A signing identity. A prefix registration command is signed
468 * under the default certificate of this identity
469 * @param flags (optional) RIB flags
470 *
471 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
472 */
philobb778e72015-08-25 14:49:19 -0700473 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400474 const RegisteredPrefixId*
475 setInterestFilter(const InterestFilter& interestFilter,
476 const OnInterest& onInterest,
477 const RegisterPrefixFailureCallback& onFailure,
478 const Name& identity,
philobb778e72015-08-25 14:49:19 -0700479 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400480#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
481
482 /**
483 * @brief Register prefix with the connected NDN forwarder
484 *
485 * This method only modifies forwarder's RIB and does not associate any
486 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
487 * the right callbacks.
488 *
489 * @param prefix A prefix to register with the connected NDN forwarder
490 * @param onSuccess A callback to be called when prefixRegister command succeeds
491 * @param onFailure A callback to be called when prefixRegister command fails
492 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
493 * used in the signature will be used.
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700494 * @param flags Prefix registration flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400495 *
496 * @return The registered prefix ID which can be used with unregisterPrefix
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700497 * @see nfd::RouteFlags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400498 */
499 const RegisteredPrefixId*
500 registerPrefix(const Name& prefix,
501 const RegisterPrefixSuccessCallback& onSuccess,
502 const RegisterPrefixFailureCallback& onFailure,
503 const security::SigningInfo& signingInfo = security::SigningInfo(),
504 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
505
506#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
507 /**
508 * @deprecated Use override with SigningInfo instead of this function
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700509 * @brief Register prefix with the connected NDN forwarder
510 *
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400511 * This method only modifies forwarder's RIB and does not associate any
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700512 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
513 * the right callbacks.
514 *
515 * @param prefix A prefix to register with the connected NDN forwarder
516 * @param onSuccess A callback to be called when prefixRegister command succeeds
517 * @param onFailure A callback to be called when prefixRegister command fails
518 * @param certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700519 * command is signed. When omitted, a default certificate of
520 * the default identity is used to sign the registration command
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400521 * @param flags (optional) RIB flags
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700522 *
523 * @return The registered prefix ID which can be used with unregisterPrefix
524 */
philobb778e72015-08-25 14:49:19 -0700525 DEPRECATED(
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700526 const RegisteredPrefixId*
527 registerPrefix(const Name& prefix,
528 const RegisterPrefixSuccessCallback& onSuccess,
529 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400530 const IdentityCertificate& certificate,
philobb778e72015-08-25 14:49:19 -0700531 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700532
533 /**
Joao Pereira0b3cac52015-07-02 14:49:49 -0400534 * @deprecated Use override with SigningInfo instead of this function
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700535 * @brief Register prefix with the connected NDN forwarder and call onInterest when a matching
536 * interest is received.
537 *
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400538 * This method only modifies forwarder's RIB and does not associate any
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700539 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
540 * the right callbacks.
541 *
542 * @param prefix A prefix to register with the connected NDN forwarder
543 * @param onSuccess A callback to be called when prefixRegister command succeeds
544 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700545 * @param identity A signing identity. A prefix registration command is signed
546 * under the default certificate of this identity
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400547 * @param flags (optional) RIB flags
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700548 *
549 * @return The registered prefix ID which can be used with unregisterPrefix
550 */
philobb778e72015-08-25 14:49:19 -0700551 DEPRECATED(
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700552 const RegisteredPrefixId*
553 registerPrefix(const Name& prefix,
554 const RegisterPrefixSuccessCallback& onSuccess,
555 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700556 const Name& identity,
philobb778e72015-08-25 14:49:19 -0700557 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400558#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700559
560 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300561 * @brief Remove the registered prefix entry with the registeredPrefixId
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700562 *
563 * This does not affect another registered prefix with a different registeredPrefixId,
564 * even it if has the same prefix name. If there is no entry with the
565 * registeredPrefixId, do nothing.
566 *
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700567 * unsetInterestFilter will use the same credentials as original
568 * setInterestFilter/registerPrefix command
569 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700570 * @param registeredPrefixId The ID returned from registerPrefix
Jeff Thompson11095142013-10-01 16:20:28 -0700571 */
572 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800573 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800574
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700575 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700576 * @brief Remove previously set InterestFilter from library's FIB
577 *
578 * This method always succeeds and will **NOT** send any request to the connected
579 * forwarder.
580 *
581 * @param interestFilterId The ID returned from setInterestFilter.
582 */
583 void
584 unsetInterestFilter(const InterestFilterId* interestFilterId);
585
586 /**
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400587 * @brief Unregister prefix from RIB
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700588 *
589 * unregisterPrefix will use the same credentials as original
590 * setInterestFilter/registerPrefix command
591 *
592 * If registeredPrefixId was obtained using setInterestFilter, the corresponding
593 * InterestFilter will be unset too.
594 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700595 * @param registeredPrefixId The ID returned from registerPrefix
596 * @param onSuccess Callback to be called when operation succeeds
597 * @param onFailure Callback to be called when operation fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700598 */
599 void
600 unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
601 const UnregisterPrefixSuccessCallback& onSuccess,
602 const UnregisterPrefixFailureCallback& onFailure);
603
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800604 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800605 * @brief Publish data packet
606 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700607 * This method can be called to satisfy the incoming Interest or to put Data packet into
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700608 * the cache of the local NDN forwarder.
609 *
610 * @param data Data packet to publish. It is highly recommended to use Data packet that
611 * was created using make_shared<Data>(...). Otherwise, put() will make an
612 * extra copy of the Data packet to ensure validity of published Data until
613 * asynchronous put() operation finishes.
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700614 *
615 * @throws Error when Data size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800616 */
617 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800618 put(const Data& data);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700619
Eric Newberry83872fd2015-08-06 17:01:24 -0700620 /**
621 * @brief sends a Network NACK
622 * @param nack the Nack; a copy will be made, so that the caller is not required to
623 * maintain the argument unchanged
624 */
625 void
626 put(const lp::Nack& nack);
627
Junxiao Shiedd834e2014-10-28 20:28:58 -0700628public: // IO routine
Jeff Thompson86507bc2013-08-23 20:51:38 -0700629 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700630 * @brief Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800631 *
632 * This call will block forever (default timeout == 0) to process IO on the face.
633 * To exit, one expected to call face.shutdown() from one of the callback methods.
634 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700635 * If positive timeout is specified, then processEvents will exit after this timeout, if
636 * not stopped earlier with face.shutdown() or when all active events finish. The call
637 * can be called repeatedly, if desired.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800638 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700639 * If negative timeout is specified, then processEvents will not block and process only
640 * pending events.
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800641 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700642 * @param timeout maximum time to block the thread
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700643 * @param keepThread Keep thread in a blocked state (in event processing), even when
644 * there are no outstanding events (e.g., no Interest/Data is expected)
645 *
646 * @throw This may throw an exception for reading data or in the callback for processing
647 * the data. If you call this from an main event loop, you may want to catch and
648 * log/disregard all exceptions.
Jeff Thompson432c8be2013-08-09 16:16:08 -0700649 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700650 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700651 processEvents(const time::milliseconds& timeout = time::milliseconds::zero(),
652 bool keepThread = false);
Jeff Thompson432c8be2013-08-09 16:16:08 -0700653
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700654 /**
655 * @brief Shutdown face operations
656 *
657 * This method cancels all pending operations and closes connection to NDN Forwarder.
658 *
659 * Note that this method does not stop IO service and if the same IO service is shared
660 * between multiple Faces or with other IO objects (e.g., Scheduler).
661 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700662 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700663 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800664
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700665 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700666 * @brief Get reference to IO service object
667 */
668 boost::asio::io_service&
669 getIoService()
670 {
Junxiao Shi2cced062014-11-02 21:27:38 -0700671 return m_ioService;
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700672 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800673
674private:
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700675
676 /**
677 * @throws ConfigFile::Error on parse error and unsupported protocols
678 */
679 void
Joao Pereira68c0d882015-05-19 14:27:55 -0400680 construct(KeyChain& keyChain);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700681
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600682 /**
683 * @throws Face::Error on unsupported protocol
Junxiao Shi2cced062014-11-02 21:27:38 -0700684 * @note shared_ptr is passed by value because ownership is transferred to this function
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600685 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800686 void
Joao Pereira68c0d882015-05-19 14:27:55 -0400687 construct(shared_ptr<Transport> transport, KeyChain& keyChain);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600688
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700689 void
Eric Newberry83872fd2015-08-06 17:01:24 -0700690 onReceiveElement(const Block& blockFromDaemon);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800691
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700692 void
693 asyncShutdown();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700694
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700695private:
Junxiao Shi2cced062014-11-02 21:27:38 -0700696 /// the IO service owned by this Face, could be null
697 unique_ptr<boost::asio::io_service> m_internalIoService;
698 /// the IO service used by this Face
699 boost::asio::io_service& m_ioService;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700700
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800701 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800702
Junxiao Shiedd834e2014-10-28 20:28:58 -0700703 /** @brief if not null, a pointer to an internal KeyChain owned by Face
704 * @note if a KeyChain is supplied to constructor, this pointer will be null,
705 * and the passed KeyChain is given to nfdController;
706 * currently Face does not keep the KeyChain passed in constructor
707 * because it's not needed, but this may change in the future
708 */
Joao Pereira68c0d882015-05-19 14:27:55 -0400709 unique_ptr<KeyChain> m_internalKeyChain;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700710
Joao Pereira68c0d882015-05-19 14:27:55 -0400711 unique_ptr<nfd::Controller> m_nfdController;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600712
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700713 class Impl;
Joao Pereira68c0d882015-05-19 14:27:55 -0400714 unique_ptr<Impl> m_impl;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700715};
716
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800717} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700718
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800719#endif // NDN_FACE_HPP