blob: 1fa2acae31a66ba5e0c44650749c69c42b1ceec1 [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"
Junxiao Shi4b469982015-12-03 18:20:19 +000031#include "encoding/nfd-constants.hpp"
Eric Newberry83872fd2015-08-06 17:01:24 -070032#include "lp/nack.hpp"
Junxiao Shi4b469982015-12-03 18:20:19 +000033#include "security/signing-info.hpp"
Joao Pereira0b3cac52015-07-02 14:49:49 -040034
35#define NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
36
37#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -070038#include "security/identity-certificate.hpp"
Joao Pereira0b3cac52015-07-02 14:49:49 -040039#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080040
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070041namespace boost {
42namespace asio {
43class io_service;
44}
45}
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080046
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070047namespace ndn {
48
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070049class Transport;
50
51class PendingInterestId;
52class RegisteredPrefixId;
53class InterestFilterId;
54
Yingdi Yu1b0311c2015-06-10 14:58:47 -070055namespace security {
56class KeyChain;
57}
58using security::KeyChain;
59
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070060namespace nfd {
61class Controller;
62}
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070063
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070064/**
Eric Newberry83872fd2015-08-06 17:01:24 -070065 * @brief Callback called when expressed Interest gets satisfied with a Data packet
66 */
67typedef function<void(const Interest&, const Data&)> DataCallback;
68
69/**
70 * @brief Callback called when Nack is sent in response to expressed Interest
71 */
72typedef function<void(const Interest&, const lp::Nack&)> NackCallback;
73
74/**
75 * @brief Callback called when expressed Interest times out
76 */
77typedef function<void(const Interest&)> TimeoutCallback;
78
79/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070080 * @brief Callback called when expressed Interest gets satisfied with Data packet
Eric Newberry83872fd2015-08-06 17:01:24 -070081 * @deprecated use DataCallback
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080082 */
83typedef function<void(const Interest&, Data&)> OnData;
84
85/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070086 * @brief Callback called when expressed Interest times out
Eric Newberry83872fd2015-08-06 17:01:24 -070087 * @deprecated use TimeoutCallback
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080088 */
89typedef function<void(const Interest&)> OnTimeout;
90
91/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070092 * @brief Callback called when incoming Interest matches the specified InterestFilter
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080093 */
Alexander Afanasyev90164962014-03-06 08:29:59 +000094typedef function<void (const InterestFilter&, const Interest&)> OnInterest;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080095
96/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070097 * @brief Callback called when registerPrefix or setInterestFilter command succeeds
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080098 */
Alexander Afanasyev984ad192014-05-02 19:11:15 -070099typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800100
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700101/**
102 * @brief Callback called when registerPrefix or setInterestFilter command fails
103 */
104typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
105
106/**
107 * @brief Callback called when unregisterPrefix or unsetInterestFilter command succeeds
108 */
109typedef function<void()> UnregisterPrefixSuccessCallback;
110
111/**
112 * @brief Callback called when unregisterPrefix or unsetInterestFilter command fails
113 */
114typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800115
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800116/**
117 * @brief Abstraction to communicate with local or remote NDN forwarder
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700118 */
Alexander Afanasyev8460afb2014-02-15 20:31:42 -0800119class Face : noncopyable
120{
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700121public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122 class Error : public std::runtime_error
123 {
124 public:
125 explicit
126 Error(const std::string& what)
127 : std::runtime_error(what)
128 {
129 }
130 };
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800131
Junxiao Shiedd834e2014-10-28 20:28:58 -0700132public: // constructors
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800133 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700134 * @brief Create a new Face using the default transport (UnixTransport)
135 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600136 * @throws ConfigFile::Error on configuration file parse failure
137 * @throws Face::Error on unsupported protocol
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800138 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800139 Face();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800140
141 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700142 * @brief Create a new Face using the default transport (UnixTransport)
143 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700144 * @par Usage examples:
145 *
146 * Face face1;
147 * Face face2(face1.getIoService());
148 *
149 * // Now the following ensures that events on both faces are processed
150 * face1.processEvents();
151 * // or face1.getIoService().run();
152 *
153 * @par or
154 *
155 * boost::asio::io_service ioService;
156 * Face face1(ioService);
157 * Face face2(ioService);
158 * ...
159 *
160 * ioService.run();
161 *
162 * @param ioService A reference to boost::io_service object that should control all
163 * IO operations.
164 * @throws ConfigFile::Error on configuration file parse failure
165 * @throws Face::Error on unsupported protocol
166 */
167 explicit
168 Face(boost::asio::io_service& ioService);
169
170 /**
171 * @brief Create a new Face using TcpTransport
172 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700173 * @param host The host of the NDN forwarder
174 * @param port (optional) The port or service name of the NDN forwarder (**default**: "6363")
175 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600176 * @throws Face::Error on unsupported protocol
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700177 */
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800178 Face(const std::string& host, const std::string& port = "6363");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800179
180 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700181 * @brief Create a new Face using the given Transport
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800182 * @param transport the Transport used for communication. If nullptr, then the default
183 * transport will be used.
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700184 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600185 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800186 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800187 explicit
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800188 Face(shared_ptr<Transport> transport);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800189
190 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700191 * @brief Create a new Face using the given Transport and IO service object
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800192 * @param transport the Transport used for communication. If nullptr, then the default
193 * transport will be used.
194 * @param ioService the io_service that controls all IO operations
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800195 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700196 * @sa Face(boost::asio::io_service&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800197 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600198 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800199 */
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800200 Face(shared_ptr<Transport> transport,
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700201 boost::asio::io_service& ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800202
Jeff Thompson4fe45512013-08-23 14:06:38 -0700203 /**
Junxiao Shiedd834e2014-10-28 20:28:58 -0700204 * @brief Create a new Face using the given Transport and IO service object
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800205 * @param transport the Transport used for communication. If nullptr, then the default
206 * transport will be used.
Junxiao Shiedd834e2014-10-28 20:28:58 -0700207 * @param ioService the io_service that controls all IO operations
208 * @param keyChain the KeyChain to sign commands
209 * @throws Face::Error on unsupported protocol
210 * @note shared_ptr is passed by value because ownership is shared with this class
211 */
212 Face(shared_ptr<Transport> transport,
213 boost::asio::io_service& ioService,
214 KeyChain& keyChain);
215
216 ~Face();
217
218public: // consumer
219 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800220 * @brief Express Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700221 * @param interest the Interest; a copy will be made, so that the caller is not
222 * required to maintain the argument unchanged
223 * @param afterSatisfied function to be invoked if Data is returned
224 * @param afterNacked function to be invoked if Network NACK is returned
225 * @param afterTimeout function to be invoked if neither Data nor Network NACK
226 * is returned within InterestLifetime
227 */
228 const PendingInterestId*
229 expressInterest(const Interest& interest,
230 const DataCallback& afterSatisfied,
231 const NackCallback& afterNacked,
232 const TimeoutCallback& afterTimeout);
233
234 /**
235 * @brief Express Interest
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800236 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700237 * @param interest An Interest to be expressed
238 * @param onData Callback to be called when a matching data packet is received
Eric Newberry83872fd2015-08-06 17:01:24 -0700239 * @param onTimeout (optional) A function object to call if the interest times out or is Nacked
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800240 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700241 * @return The pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700242 *
243 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Eric Newberry83872fd2015-08-06 17:01:24 -0700244 *
245 * @deprecated use expressInterest(Interest, DataCallback, NackCallback, TimeoutCallback)
Jeff Thompson4fe45512013-08-23 14:06:38 -0700246 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800247 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800248 expressInterest(const Interest& interest,
Eric Newberry83872fd2015-08-06 17:01:24 -0700249 const OnData& onData,
250 const OnTimeout& onTimeout = nullptr);
Jeff Thompson4fe45512013-08-23 14:06:38 -0700251
252 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800253 * @brief Express Interest using name and Interest template
254 *
255 * @param name Name of the Interest
256 * @param tmpl Interest template to fill parameters
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700257 * @param onData Callback to be called when a matching data packet is received
Eric Newberry83872fd2015-08-06 17:01:24 -0700258 * @param onTimeout (optional) A function object to call if the interest times out or is Nacked
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800259 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700260 * @return Opaque pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700261 *
262 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Eric Newberry83872fd2015-08-06 17:01:24 -0700263 *
264 * @deprecated use expressInterest(Interest, DataCallback, NackCallback, TimeoutCallback)
Jeff Thompson7aec0252013-08-22 17:29:57 -0700265 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800266 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800267 expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800268 const Interest& tmpl,
Eric Newberry83872fd2015-08-06 17:01:24 -0700269 const OnData& onData,
270 const OnTimeout& onTimeout = nullptr);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700271
Jeff Thompson11095142013-10-01 16:20:28 -0700272 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700273 * @brief Cancel previously expressed Interest
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700274 *
Jeff Thompson11095142013-10-01 16:20:28 -0700275 * @param pendingInterestId The ID returned from expressInterest.
276 */
277 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800278 removePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700279
Jeff Thompson432c8be2013-08-09 16:16:08 -0700280 /**
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500281 * @brief Cancel all previously expressed Interests
282 */
283 void
284 removeAllPendingInterests();
285
286 /**
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700287 * @brief Get number of pending Interests
288 */
289 size_t
290 getNPendingInterests() const;
291
Junxiao Shiedd834e2014-10-28 20:28:58 -0700292public: // producer
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700293 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700294 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
295 * callback and register the filtered prefix with the connected NDN forwarder
296 *
297 * This version of setInterestFilter combines setInterestFilter and registerPrefix
298 * operations and is intended to be used when only one filter for the same prefix needed
299 * to be set. When multiple names sharing the same prefix should be dispatched to
300 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
301 * a series of setInterestFilter calls.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700302 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000303 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700304 * @param onInterest A callback to be called when a matching interest is received
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700305 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400306 * @param flags (optional) RIB flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400307 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
308 * used in the signature will be used.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700309 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700310 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
311 * removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700312 */
313 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000314 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700315 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700316 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400317 const security::SigningInfo& signingInfo = security::SigningInfo(),
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700318 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700319
320 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700321 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
322 * callback and register the filtered prefix with the connected NDN forwarder
323 *
324 * This version of setInterestFilter combines setInterestFilter and registerPrefix
325 * operations and is intended to be used when only one filter for the same prefix needed
326 * to be set. When multiple names sharing the same prefix should be dispatched to
327 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
328 * a series of setInterestFilter calls.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700329 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000330 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700331 * @param onInterest A callback to be called when a matching interest is received
332 * @param onSuccess A callback to be called when prefixRegister command succeeds
333 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400334 * @param flags (optional) RIB flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400335 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
336 * used in the signature will be used.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700337 *
Joao Pereira0b3cac52015-07-02 14:49:49 -0400338 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
339 * removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700340 */
341 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000342 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700343 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700344 const RegisterPrefixSuccessCallback& onSuccess,
345 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400346 const security::SigningInfo& signingInfo = security::SigningInfo(),
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700347 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700348
349 /**
350 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest callback
351 *
352 * @param interestFilter Interest
353 * @param onInterest A callback to be called when a matching interest is received
354 *
355 * This method modifies library's FIB only, and does not register the prefix with the
356 * forwarder. It will always succeed. To register prefix with the forwarder, use
357 * registerPrefix, or use the setInterestFilter overload taking two callbacks.
358 *
359 * @return Opaque interest filter ID which can be used with unsetInterestFilter
360 */
361 const InterestFilterId*
362 setInterestFilter(const InterestFilter& interestFilter,
363 const OnInterest& onInterest);
364
Joao Pereira0b3cac52015-07-02 14:49:49 -0400365#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
366 /**
367 * @deprecated Use override with SigningInfo instead of this function
368 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
369 * callback and register the filtered prefix with the connected NDN forwarder
370 *
371 * This version of setInterestFilter combines setInterestFilter and registerPrefix
372 * operations and is intended to be used when only one filter for the same prefix needed
373 * to be set. When multiple names sharing the same prefix should be dispatched to
374 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
375 * a series of setInterestFilter calls.
376 *
377 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
378 * @param onInterest A callback to be called when a matching interest is received
379 * @param onSuccess A callback to be called when prefixRegister command succeeds
380 * @param onFailure A callback to be called when prefixRegister command fails
381 * @param flags (optional) RIB flags
382 * @param certificate (optional) A certificate under which the prefix registration
383 * command is signed. When omitted, a default certificate of
384 * the default identity is used to sign the registration command
385 *
386 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
387 * removeRegisteredPrefix
388 */
philobb778e72015-08-25 14:49:19 -0700389 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400390 const RegisteredPrefixId*
391 setInterestFilter(const InterestFilter& interestFilter,
392 const OnInterest& onInterest,
393 const RegisterPrefixSuccessCallback& onSuccess,
394 const RegisterPrefixFailureCallback& onFailure,
395 const IdentityCertificate& certificate,
philobb778e72015-08-25 14:49:19 -0700396 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700397
398 /**
Joao Pereira0b3cac52015-07-02 14:49:49 -0400399 * @deprecated Use override with SigningInfo instead of this function
400 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
401 * callback and register the filtered prefix with the connected NDN forwarder
402 *
403 * This version of setInterestFilter combines setInterestFilter and registerPrefix
404 * operations and is intended to be used when only one filter for the same prefix needed
405 * to be set. When multiple names sharing the same prefix should be dispatched to
406 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
407 * a series of setInterestFilter calls.
408 *
409 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
410 * @param onInterest A callback to be called when a matching interest is received
411 * @param onFailure A callback to be called when prefixRegister command fails
412 * @param flags (optional) RIB flags
413 * @param certificate (optional) A certificate under which the prefix registration
414 * command is signed. When omitted, a default certificate of
415 * the default identity is used to sign the registration command
416 *
417 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
418 * removeRegisteredPrefix
419 */
philobb778e72015-08-25 14:49:19 -0700420 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400421 const RegisteredPrefixId*
422 setInterestFilter(const InterestFilter& interestFilter,
423 const OnInterest& onInterest,
424 const RegisterPrefixFailureCallback& onFailure,
425 const IdentityCertificate& certificate,
philobb778e72015-08-25 14:49:19 -0700426 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400427
428 /**
429 * @deprecated Use override with SigningInfo instead of this function
430 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
431 * callback and register the filtered prefix with the connected NDN forwarder
432 *
433 * This version of setInterestFilter combines setInterestFilter and registerPrefix
434 * operations and is intended to be used when only one filter for the same prefix needed
435 * to be set. When multiple names sharing the same prefix should be dispatched to
436 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
437 * a series of setInterestFilter calls.
438 *
439 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
440 * @param onInterest A callback to be called when a matching interest is received
441 * @param onSuccess A callback to be called when prefixRegister command succeeds
442 * @param onFailure A callback to be called when prefixRegister command fails
443 * @param identity A signing identity. A prefix registration command is signed
444 * under the default certificate of this identity
445 * @param flags (optional) RIB flags
446 *
447 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
448 */
philobb778e72015-08-25 14:49:19 -0700449 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400450 const RegisteredPrefixId*
451 setInterestFilter(const InterestFilter& interestFilter,
452 const OnInterest& onInterest,
453 const RegisterPrefixSuccessCallback& onSuccess,
454 const RegisterPrefixFailureCallback& onFailure,
455 const Name& identity,
philobb778e72015-08-25 14:49:19 -0700456 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400457
458 /**
459 * @deprecated Use override with SigningInfo instead of this function
460 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
461 * callback and register the filtered prefix with the connected NDN forwarder
462 *
463 * This version of setInterestFilter combines setInterestFilter and registerPrefix
464 * operations and is intended to be used when only one filter for the same prefix needed
465 * to be set. When multiple names sharing the same prefix should be dispatched to
466 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
467 * a series of setInterestFilter calls.
468 *
469 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
470 * @param onInterest A callback to be called when a matching interest is received
471 * @param onFailure A callback to be called when prefixRegister command fails
472 * @param identity A signing identity. A prefix registration command is signed
473 * under the default certificate of this identity
474 * @param flags (optional) RIB flags
475 *
476 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
477 */
philobb778e72015-08-25 14:49:19 -0700478 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400479 const RegisteredPrefixId*
480 setInterestFilter(const InterestFilter& interestFilter,
481 const OnInterest& onInterest,
482 const RegisterPrefixFailureCallback& onFailure,
483 const Name& identity,
philobb778e72015-08-25 14:49:19 -0700484 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400485#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
486
487 /**
488 * @brief Register prefix with the connected NDN forwarder
489 *
490 * This method only modifies forwarder's RIB and does not associate any
491 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
492 * the right callbacks.
493 *
494 * @param prefix A prefix to register with the connected NDN forwarder
495 * @param onSuccess A callback to be called when prefixRegister command succeeds
496 * @param onFailure A callback to be called when prefixRegister command fails
497 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
498 * used in the signature will be used.
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700499 * @param flags Prefix registration flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400500 *
501 * @return The registered prefix ID which can be used with unregisterPrefix
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700502 * @see nfd::RouteFlags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400503 */
504 const RegisteredPrefixId*
505 registerPrefix(const Name& prefix,
506 const RegisterPrefixSuccessCallback& onSuccess,
507 const RegisterPrefixFailureCallback& onFailure,
508 const security::SigningInfo& signingInfo = security::SigningInfo(),
509 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
510
511#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
512 /**
513 * @deprecated Use override with SigningInfo instead of this function
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700514 * @brief Register prefix with the connected NDN forwarder
515 *
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400516 * This method only modifies forwarder's RIB and does not associate any
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700517 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
518 * the right callbacks.
519 *
520 * @param prefix A prefix to register with the connected NDN forwarder
521 * @param onSuccess A callback to be called when prefixRegister command succeeds
522 * @param onFailure A callback to be called when prefixRegister command fails
523 * @param certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700524 * command is signed. When omitted, a default certificate of
525 * the default identity is used to sign the registration command
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400526 * @param flags (optional) RIB flags
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700527 *
528 * @return The registered prefix ID which can be used with unregisterPrefix
529 */
philobb778e72015-08-25 14:49:19 -0700530 DEPRECATED(
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700531 const RegisteredPrefixId*
532 registerPrefix(const Name& prefix,
533 const RegisterPrefixSuccessCallback& onSuccess,
534 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400535 const IdentityCertificate& certificate,
philobb778e72015-08-25 14:49:19 -0700536 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700537
538 /**
Joao Pereira0b3cac52015-07-02 14:49:49 -0400539 * @deprecated Use override with SigningInfo instead of this function
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700540 * @brief Register prefix with the connected NDN forwarder and call onInterest when a matching
541 * interest is received.
542 *
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400543 * This method only modifies forwarder's RIB and does not associate any
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700544 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
545 * the right callbacks.
546 *
547 * @param prefix A prefix to register with the connected NDN forwarder
548 * @param onSuccess A callback to be called when prefixRegister command succeeds
549 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700550 * @param identity A signing identity. A prefix registration command is signed
551 * under the default certificate of this identity
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400552 * @param flags (optional) RIB flags
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700553 *
554 * @return The registered prefix ID which can be used with unregisterPrefix
555 */
philobb778e72015-08-25 14:49:19 -0700556 DEPRECATED(
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700557 const RegisteredPrefixId*
558 registerPrefix(const Name& prefix,
559 const RegisterPrefixSuccessCallback& onSuccess,
560 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700561 const Name& identity,
philobb778e72015-08-25 14:49:19 -0700562 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400563#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700564
565 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300566 * @brief Remove the registered prefix entry with the registeredPrefixId
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700567 *
568 * This does not affect another registered prefix with a different registeredPrefixId,
569 * even it if has the same prefix name. If there is no entry with the
570 * registeredPrefixId, do nothing.
571 *
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700572 * unsetInterestFilter will use the same credentials as original
573 * setInterestFilter/registerPrefix command
574 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700575 * @param registeredPrefixId The ID returned from registerPrefix
Jeff Thompson11095142013-10-01 16:20:28 -0700576 */
577 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800578 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800579
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700580 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700581 * @brief Remove previously set InterestFilter from library's FIB
582 *
583 * This method always succeeds and will **NOT** send any request to the connected
584 * forwarder.
585 *
586 * @param interestFilterId The ID returned from setInterestFilter.
587 */
588 void
589 unsetInterestFilter(const InterestFilterId* interestFilterId);
590
591 /**
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400592 * @brief Unregister prefix from RIB
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700593 *
594 * unregisterPrefix will use the same credentials as original
595 * setInterestFilter/registerPrefix command
596 *
597 * If registeredPrefixId was obtained using setInterestFilter, the corresponding
598 * InterestFilter will be unset too.
599 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700600 * @param registeredPrefixId The ID returned from registerPrefix
601 * @param onSuccess Callback to be called when operation succeeds
602 * @param onFailure Callback to be called when operation fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700603 */
604 void
605 unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
606 const UnregisterPrefixSuccessCallback& onSuccess,
607 const UnregisterPrefixFailureCallback& onFailure);
608
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800609 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800610 * @brief Publish data packet
611 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700612 * This method can be called to satisfy the incoming Interest or to put Data packet into
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700613 * the cache of the local NDN forwarder.
614 *
615 * @param data Data packet to publish. It is highly recommended to use Data packet that
616 * was created using make_shared<Data>(...). Otherwise, put() will make an
617 * extra copy of the Data packet to ensure validity of published Data until
618 * asynchronous put() operation finishes.
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700619 *
620 * @throws Error when Data size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800621 */
622 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800623 put(const Data& data);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700624
Eric Newberry83872fd2015-08-06 17:01:24 -0700625 /**
626 * @brief sends a Network NACK
627 * @param nack the Nack; a copy will be made, so that the caller is not required to
628 * maintain the argument unchanged
629 */
630 void
631 put(const lp::Nack& nack);
632
Junxiao Shiedd834e2014-10-28 20:28:58 -0700633public: // IO routine
Jeff Thompson86507bc2013-08-23 20:51:38 -0700634 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700635 * @brief Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800636 *
637 * This call will block forever (default timeout == 0) to process IO on the face.
638 * To exit, one expected to call face.shutdown() from one of the callback methods.
639 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700640 * If positive timeout is specified, then processEvents will exit after this timeout, if
641 * not stopped earlier with face.shutdown() or when all active events finish. The call
642 * can be called repeatedly, if desired.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800643 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700644 * If negative timeout is specified, then processEvents will not block and process only
645 * pending events.
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800646 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700647 * @param timeout maximum time to block the thread
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700648 * @param keepThread Keep thread in a blocked state (in event processing), even when
649 * there are no outstanding events (e.g., no Interest/Data is expected)
650 *
651 * @throw This may throw an exception for reading data or in the callback for processing
652 * the data. If you call this from an main event loop, you may want to catch and
653 * log/disregard all exceptions.
Jeff Thompson432c8be2013-08-09 16:16:08 -0700654 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700655 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700656 processEvents(const time::milliseconds& timeout = time::milliseconds::zero(),
657 bool keepThread = false);
Jeff Thompson432c8be2013-08-09 16:16:08 -0700658
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700659 /**
660 * @brief Shutdown face operations
661 *
662 * This method cancels all pending operations and closes connection to NDN Forwarder.
663 *
664 * Note that this method does not stop IO service and if the same IO service is shared
665 * between multiple Faces or with other IO objects (e.g., Scheduler).
666 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700667 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700668 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800669
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700670 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700671 * @brief Get reference to IO service object
672 */
673 boost::asio::io_service&
674 getIoService()
675 {
Junxiao Shi2cced062014-11-02 21:27:38 -0700676 return m_ioService;
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700677 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800678
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800679NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
680 /**
681 * @brief Get the underlying transport of the face
682 */
683 shared_ptr<Transport>
684 getTransport();
685
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800686private:
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700687
688 /**
689 * @throws ConfigFile::Error on parse error and unsupported protocols
690 */
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800691 shared_ptr<Transport>
692 makeDefaultTransport();
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700693
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600694 /**
695 * @throws Face::Error on unsupported protocol
Junxiao Shi2cced062014-11-02 21:27:38 -0700696 * @note shared_ptr is passed by value because ownership is transferred to this function
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600697 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800698 void
Joao Pereira68c0d882015-05-19 14:27:55 -0400699 construct(shared_ptr<Transport> transport, KeyChain& keyChain);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600700
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700701 void
Eric Newberry83872fd2015-08-06 17:01:24 -0700702 onReceiveElement(const Block& blockFromDaemon);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800703
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700704 void
705 asyncShutdown();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700706
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700707private:
Junxiao Shi2cced062014-11-02 21:27:38 -0700708 /// the IO service owned by this Face, could be null
709 unique_ptr<boost::asio::io_service> m_internalIoService;
710 /// the IO service used by this Face
711 boost::asio::io_service& m_ioService;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700712
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800713 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800714
Junxiao Shiedd834e2014-10-28 20:28:58 -0700715 /** @brief if not null, a pointer to an internal KeyChain owned by Face
716 * @note if a KeyChain is supplied to constructor, this pointer will be null,
717 * and the passed KeyChain is given to nfdController;
718 * currently Face does not keep the KeyChain passed in constructor
719 * because it's not needed, but this may change in the future
720 */
Joao Pereira68c0d882015-05-19 14:27:55 -0400721 unique_ptr<KeyChain> m_internalKeyChain;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700722
Joao Pereira68c0d882015-05-19 14:27:55 -0400723 unique_ptr<nfd::Controller> m_nfdController;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600724
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700725 class Impl;
Joao Pereira68c0d882015-05-19 14:27:55 -0400726 unique_ptr<Impl> m_impl;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700727};
728
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800729} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700730
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800731#endif // NDN_FACE_HPP