blob: fe6af03c351098a0684bbdf15db4c46f6881483f [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 Afanasyev8cf1c562016-06-23 16:01:55 -07003 * Copyright (c) 2013-2016 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;
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070044} // namespace asio
45} // namespace boost
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;
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070057} // namespace security
Yingdi Yu1b0311c2015-06-10 14:58:47 -070058using security::KeyChain;
59
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070060namespace nfd {
61class Controller;
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070062} // namespace nfd
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
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700137 * @throws ConfigFile::Error if configuration file specifies an 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
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700165 * @throws ConfigFile::Error if configuration file specifies an unsupported protocol
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700166 */
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")
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700175 */
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800176 Face(const std::string& host, const std::string& port = "6363");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800177
178 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700179 * @brief Create a new Face using the given Transport
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800180 * @param transport the Transport used for communication. If nullptr, then the default
181 * transport will be used.
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700182 *
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700183 * @throws ConfigFile::Error if @p transport is nullptr on configuration file parse failure
184 * @throws ConfigFile::Error if @p transport is nullptr and the configuration file
185 * specifies an unsupported protocol
186 * @note shared_ptr is passed by value because ownership is shared with this class
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800187 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800188 explicit
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800189 Face(shared_ptr<Transport> transport);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800190
191 /**
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700192 * @brief Create a new Face using the given Transport and KeyChain instance
193 * @param transport the Transport used for communication. If nullptr, then the default
194 * transport will be used.
195 * @param keyChain the KeyChain to sign commands
196 *
197 * @throws ConfigFile::Error if @p transport is nullptr on configuration file parse failure
198 * @throws ConfigFile::Error if @p transport is nullptr and the configuration file
199 * specifies an unsupported protocol
200 * @note shared_ptr is passed by value because ownership is shared with this class
201 */
202 Face(shared_ptr<Transport> transport, KeyChain& keyChain);
203
204 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700205 * @brief Create a new Face using the given Transport and IO service object
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800206 * @param transport the Transport used for communication. If nullptr, then the default
207 * transport will be used.
208 * @param ioService the io_service that controls all IO operations
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800209 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700210 * @sa Face(boost::asio::io_service&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800211 *
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700212 * @throws ConfigFile::Error if @p transport is nullptr on configuration file parse failure
213 * @throws ConfigFile::Error if @p transport is nullptr and the configuration file
214 * specifies an unsupported protocol
215 * @note shared_ptr is passed by value because ownership is shared with this class
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800216 */
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700217 Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800218
Jeff Thompson4fe45512013-08-23 14:06:38 -0700219 /**
Junxiao Shiedd834e2014-10-28 20:28:58 -0700220 * @brief Create a new Face using the given Transport and IO service object
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800221 * @param transport the Transport used for communication. If nullptr, then the default
222 * transport will be used.
Junxiao Shiedd834e2014-10-28 20:28:58 -0700223 * @param ioService the io_service that controls all IO operations
224 * @param keyChain the KeyChain to sign commands
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700225 *
226 * @throws ConfigFile::Error if @p transport is nullptr on configuration file parse failure
227 * @throws ConfigFile::Error if @p transport is nullptr and the configuration file
228 * specifies an unsupported protocol
Junxiao Shiedd834e2014-10-28 20:28:58 -0700229 * @note shared_ptr is passed by value because ownership is shared with this class
230 */
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -0700231 Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain);
Junxiao Shiedd834e2014-10-28 20:28:58 -0700232
233 ~Face();
234
235public: // consumer
236 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800237 * @brief Express Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700238 * @param interest the Interest; a copy will be made, so that the caller is not
239 * required to maintain the argument unchanged
240 * @param afterSatisfied function to be invoked if Data is returned
241 * @param afterNacked function to be invoked if Network NACK is returned
242 * @param afterTimeout function to be invoked if neither Data nor Network NACK
243 * is returned within InterestLifetime
244 */
245 const PendingInterestId*
246 expressInterest(const Interest& interest,
247 const DataCallback& afterSatisfied,
248 const NackCallback& afterNacked,
249 const TimeoutCallback& afterTimeout);
250
251 /**
252 * @brief Express Interest
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800253 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700254 * @param interest An Interest to be expressed
255 * @param onData Callback to be called when a matching data packet is received
Eric Newberry83872fd2015-08-06 17:01:24 -0700256 * @param onTimeout (optional) A function object to call if the interest times out or is Nacked
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800257 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700258 * @return The pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700259 *
260 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Eric Newberry83872fd2015-08-06 17:01:24 -0700261 *
262 * @deprecated use expressInterest(Interest, DataCallback, NackCallback, TimeoutCallback)
Jeff Thompson4fe45512013-08-23 14:06:38 -0700263 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800264 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800265 expressInterest(const Interest& interest,
Eric Newberry83872fd2015-08-06 17:01:24 -0700266 const OnData& onData,
267 const OnTimeout& onTimeout = nullptr);
Jeff Thompson4fe45512013-08-23 14:06:38 -0700268
269 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800270 * @brief Express Interest using name and Interest template
271 *
272 * @param name Name of the Interest
273 * @param tmpl Interest template to fill parameters
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700274 * @param onData Callback to be called when a matching data packet is received
Eric Newberry83872fd2015-08-06 17:01:24 -0700275 * @param onTimeout (optional) A function object to call if the interest times out or is Nacked
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800276 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700277 * @return Opaque pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700278 *
279 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Eric Newberry83872fd2015-08-06 17:01:24 -0700280 *
281 * @deprecated use expressInterest(Interest, DataCallback, NackCallback, TimeoutCallback)
Jeff Thompson7aec0252013-08-22 17:29:57 -0700282 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800283 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800284 expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800285 const Interest& tmpl,
Eric Newberry83872fd2015-08-06 17:01:24 -0700286 const OnData& onData,
287 const OnTimeout& onTimeout = nullptr);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700288
Jeff Thompson11095142013-10-01 16:20:28 -0700289 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700290 * @brief Cancel previously expressed Interest
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700291 *
Jeff Thompson11095142013-10-01 16:20:28 -0700292 * @param pendingInterestId The ID returned from expressInterest.
293 */
294 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800295 removePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700296
Jeff Thompson432c8be2013-08-09 16:16:08 -0700297 /**
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500298 * @brief Cancel all previously expressed Interests
299 */
300 void
301 removeAllPendingInterests();
302
303 /**
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700304 * @brief Get number of pending Interests
305 */
306 size_t
307 getNPendingInterests() const;
308
Junxiao Shiedd834e2014-10-28 20:28:58 -0700309public: // producer
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700310 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700311 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
312 * callback and register the filtered prefix with the connected NDN forwarder
313 *
314 * This version of setInterestFilter combines setInterestFilter and registerPrefix
315 * operations and is intended to be used when only one filter for the same prefix needed
316 * to be set. When multiple names sharing the same prefix should be dispatched to
317 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
318 * a series of setInterestFilter calls.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700319 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000320 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700321 * @param onInterest A callback to be called when a matching interest is received
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700322 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400323 * @param flags (optional) RIB flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400324 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
325 * used in the signature will be used.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700326 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700327 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
328 * removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700329 */
330 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000331 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700332 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700333 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400334 const security::SigningInfo& signingInfo = security::SigningInfo(),
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700335 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700336
337 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700338 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
339 * callback and register the filtered prefix with the connected NDN forwarder
340 *
341 * This version of setInterestFilter combines setInterestFilter and registerPrefix
342 * operations and is intended to be used when only one filter for the same prefix needed
343 * to be set. When multiple names sharing the same prefix should be dispatched to
344 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
345 * a series of setInterestFilter calls.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700346 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000347 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700348 * @param onInterest A callback to be called when a matching interest is received
349 * @param onSuccess A callback to be called when prefixRegister command succeeds
350 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400351 * @param flags (optional) RIB flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400352 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
353 * used in the signature will be used.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700354 *
Joao Pereira0b3cac52015-07-02 14:49:49 -0400355 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
356 * removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700357 */
358 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000359 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700360 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700361 const RegisterPrefixSuccessCallback& onSuccess,
362 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400363 const security::SigningInfo& signingInfo = security::SigningInfo(),
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700364 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700365
366 /**
367 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest callback
368 *
369 * @param interestFilter Interest
370 * @param onInterest A callback to be called when a matching interest is received
371 *
372 * This method modifies library's FIB only, and does not register the prefix with the
373 * forwarder. It will always succeed. To register prefix with the forwarder, use
374 * registerPrefix, or use the setInterestFilter overload taking two callbacks.
375 *
376 * @return Opaque interest filter ID which can be used with unsetInterestFilter
377 */
378 const InterestFilterId*
379 setInterestFilter(const InterestFilter& interestFilter,
380 const OnInterest& onInterest);
381
Joao Pereira0b3cac52015-07-02 14:49:49 -0400382#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
383 /**
384 * @deprecated Use override with SigningInfo instead of this function
385 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
386 * callback and register the filtered prefix with the connected NDN forwarder
387 *
388 * This version of setInterestFilter combines setInterestFilter and registerPrefix
389 * operations and is intended to be used when only one filter for the same prefix needed
390 * to be set. When multiple names sharing the same prefix should be dispatched to
391 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
392 * a series of setInterestFilter calls.
393 *
394 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
395 * @param onInterest A callback to be called when a matching interest is received
396 * @param onSuccess A callback to be called when prefixRegister command succeeds
397 * @param onFailure A callback to be called when prefixRegister command fails
398 * @param flags (optional) RIB flags
399 * @param certificate (optional) A certificate under which the prefix registration
400 * command is signed. When omitted, a default certificate of
401 * the default identity is used to sign the registration command
402 *
403 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
404 * removeRegisteredPrefix
405 */
philobb778e72015-08-25 14:49:19 -0700406 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400407 const RegisteredPrefixId*
408 setInterestFilter(const InterestFilter& interestFilter,
409 const OnInterest& onInterest,
410 const RegisterPrefixSuccessCallback& onSuccess,
411 const RegisterPrefixFailureCallback& onFailure,
412 const IdentityCertificate& certificate,
philobb778e72015-08-25 14:49:19 -0700413 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700414
415 /**
Joao Pereira0b3cac52015-07-02 14:49:49 -0400416 * @deprecated Use override with SigningInfo instead of this function
417 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
418 * callback and register the filtered prefix with the connected NDN forwarder
419 *
420 * This version of setInterestFilter combines setInterestFilter and registerPrefix
421 * operations and is intended to be used when only one filter for the same prefix needed
422 * to be set. When multiple names sharing the same prefix should be dispatched to
423 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
424 * a series of setInterestFilter calls.
425 *
426 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
427 * @param onInterest A callback to be called when a matching interest is received
428 * @param onFailure A callback to be called when prefixRegister command fails
429 * @param flags (optional) RIB flags
430 * @param certificate (optional) A certificate under which the prefix registration
431 * command is signed. When omitted, a default certificate of
432 * the default identity is used to sign the registration command
433 *
434 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
435 * removeRegisteredPrefix
436 */
philobb778e72015-08-25 14:49:19 -0700437 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400438 const RegisteredPrefixId*
439 setInterestFilter(const InterestFilter& interestFilter,
440 const OnInterest& onInterest,
441 const RegisterPrefixFailureCallback& onFailure,
442 const IdentityCertificate& certificate,
philobb778e72015-08-25 14:49:19 -0700443 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400444
445 /**
446 * @deprecated Use override with SigningInfo instead of this function
447 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
448 * callback and register the filtered prefix with the connected NDN forwarder
449 *
450 * This version of setInterestFilter combines setInterestFilter and registerPrefix
451 * operations and is intended to be used when only one filter for the same prefix needed
452 * to be set. When multiple names sharing the same prefix should be dispatched to
453 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
454 * a series of setInterestFilter calls.
455 *
456 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
457 * @param onInterest A callback to be called when a matching interest is received
458 * @param onSuccess A callback to be called when prefixRegister command succeeds
459 * @param onFailure A callback to be called when prefixRegister command fails
460 * @param identity A signing identity. A prefix registration command is signed
461 * under the default certificate of this identity
462 * @param flags (optional) RIB flags
463 *
464 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
465 */
philobb778e72015-08-25 14:49:19 -0700466 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400467 const RegisteredPrefixId*
468 setInterestFilter(const InterestFilter& interestFilter,
469 const OnInterest& onInterest,
470 const RegisterPrefixSuccessCallback& onSuccess,
471 const RegisterPrefixFailureCallback& onFailure,
472 const Name& identity,
philobb778e72015-08-25 14:49:19 -0700473 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400474
475 /**
476 * @deprecated Use override with SigningInfo instead of this function
477 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
478 * callback and register the filtered prefix with the connected NDN forwarder
479 *
480 * This version of setInterestFilter combines setInterestFilter and registerPrefix
481 * operations and is intended to be used when only one filter for the same prefix needed
482 * to be set. When multiple names sharing the same prefix should be dispatched to
483 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
484 * a series of setInterestFilter calls.
485 *
486 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
487 * @param onInterest A callback to be called when a matching interest is received
488 * @param onFailure A callback to be called when prefixRegister command fails
489 * @param identity A signing identity. A prefix registration command is signed
490 * under the default certificate of this identity
491 * @param flags (optional) RIB flags
492 *
493 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
494 */
philobb778e72015-08-25 14:49:19 -0700495 DEPRECATED(
Joao Pereira0b3cac52015-07-02 14:49:49 -0400496 const RegisteredPrefixId*
497 setInterestFilter(const InterestFilter& interestFilter,
498 const OnInterest& onInterest,
499 const RegisterPrefixFailureCallback& onFailure,
500 const Name& identity,
philobb778e72015-08-25 14:49:19 -0700501 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400502#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
503
504 /**
505 * @brief Register prefix with the connected NDN forwarder
506 *
507 * This method only modifies forwarder's RIB and does not associate any
508 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
509 * the right callbacks.
510 *
511 * @param prefix A prefix to register with the connected NDN forwarder
512 * @param onSuccess A callback to be called when prefixRegister command succeeds
513 * @param onFailure A callback to be called when prefixRegister command fails
514 * @param signingInfo (optional) Signing parameters. When omitted, a default parameters
515 * used in the signature will be used.
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700516 * @param flags Prefix registration flags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400517 *
518 * @return The registered prefix ID which can be used with unregisterPrefix
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700519 * @see nfd::RouteFlags
Joao Pereira0b3cac52015-07-02 14:49:49 -0400520 */
521 const RegisteredPrefixId*
522 registerPrefix(const Name& prefix,
523 const RegisterPrefixSuccessCallback& onSuccess,
524 const RegisterPrefixFailureCallback& onFailure,
525 const security::SigningInfo& signingInfo = security::SigningInfo(),
526 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
527
528#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
529 /**
530 * @deprecated Use override with SigningInfo instead of this function
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700531 * @brief Register prefix with the connected NDN forwarder
532 *
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400533 * This method only modifies forwarder's RIB and does not associate any
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700534 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
535 * the right callbacks.
536 *
537 * @param prefix A prefix to register with the connected NDN forwarder
538 * @param onSuccess A callback to be called when prefixRegister command succeeds
539 * @param onFailure A callback to be called when prefixRegister command fails
540 * @param certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700541 * command is signed. When omitted, a default certificate of
542 * the default identity is used to sign the registration command
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400543 * @param flags (optional) RIB flags
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700544 *
545 * @return The registered prefix ID which can be used with unregisterPrefix
546 */
philobb778e72015-08-25 14:49:19 -0700547 DEPRECATED(
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700548 const RegisteredPrefixId*
549 registerPrefix(const Name& prefix,
550 const RegisterPrefixSuccessCallback& onSuccess,
551 const RegisterPrefixFailureCallback& onFailure,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400552 const IdentityCertificate& certificate,
philobb778e72015-08-25 14:49:19 -0700553 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700554
555 /**
Joao Pereira0b3cac52015-07-02 14:49:49 -0400556 * @deprecated Use override with SigningInfo instead of this function
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700557 * @brief Register prefix with the connected NDN forwarder and call onInterest when a matching
558 * interest is received.
559 *
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400560 * This method only modifies forwarder's RIB and does not associate any
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700561 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
562 * the right callbacks.
563 *
564 * @param prefix A prefix to register with the connected NDN forwarder
565 * @param onSuccess A callback to be called when prefixRegister command succeeds
566 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700567 * @param identity A signing identity. A prefix registration command is signed
568 * under the default certificate of this identity
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400569 * @param flags (optional) RIB flags
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700570 *
571 * @return The registered prefix ID which can be used with unregisterPrefix
572 */
philobb778e72015-08-25 14:49:19 -0700573 DEPRECATED(
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700574 const RegisteredPrefixId*
575 registerPrefix(const Name& prefix,
576 const RegisterPrefixSuccessCallback& onSuccess,
577 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700578 const Name& identity,
philobb778e72015-08-25 14:49:19 -0700579 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400580#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700581
582 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300583 * @brief Remove the registered prefix entry with the registeredPrefixId
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700584 *
585 * This does not affect another registered prefix with a different registeredPrefixId,
586 * even it if has the same prefix name. If there is no entry with the
587 * registeredPrefixId, do nothing.
588 *
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700589 * unsetInterestFilter will use the same credentials as original
590 * setInterestFilter/registerPrefix command
591 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700592 * @param registeredPrefixId The ID returned from registerPrefix
Jeff Thompson11095142013-10-01 16:20:28 -0700593 */
594 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800595 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800596
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700597 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700598 * @brief Remove previously set InterestFilter from library's FIB
599 *
600 * This method always succeeds and will **NOT** send any request to the connected
601 * forwarder.
602 *
603 * @param interestFilterId The ID returned from setInterestFilter.
604 */
605 void
606 unsetInterestFilter(const InterestFilterId* interestFilterId);
607
608 /**
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400609 * @brief Unregister prefix from RIB
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700610 *
611 * unregisterPrefix will use the same credentials as original
612 * setInterestFilter/registerPrefix command
613 *
614 * If registeredPrefixId was obtained using setInterestFilter, the corresponding
615 * InterestFilter will be unset too.
616 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700617 * @param registeredPrefixId The ID returned from registerPrefix
618 * @param onSuccess Callback to be called when operation succeeds
619 * @param onFailure Callback to be called when operation fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700620 */
621 void
622 unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
623 const UnregisterPrefixSuccessCallback& onSuccess,
624 const UnregisterPrefixFailureCallback& onFailure);
625
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800626 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800627 * @brief Publish data packet
628 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700629 * This method can be called to satisfy the incoming Interest or to put Data packet into
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700630 * the cache of the local NDN forwarder.
631 *
632 * @param data Data packet to publish. It is highly recommended to use Data packet that
633 * was created using make_shared<Data>(...). Otherwise, put() will make an
634 * extra copy of the Data packet to ensure validity of published Data until
635 * asynchronous put() operation finishes.
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700636 *
637 * @throws Error when Data size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800638 */
639 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800640 put(const Data& data);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700641
Eric Newberry83872fd2015-08-06 17:01:24 -0700642 /**
643 * @brief sends a Network NACK
644 * @param nack the Nack; a copy will be made, so that the caller is not required to
645 * maintain the argument unchanged
646 */
647 void
648 put(const lp::Nack& nack);
649
Junxiao Shiedd834e2014-10-28 20:28:58 -0700650public: // IO routine
Jeff Thompson86507bc2013-08-23 20:51:38 -0700651 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700652 * @brief Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800653 *
654 * This call will block forever (default timeout == 0) to process IO on the face.
655 * To exit, one expected to call face.shutdown() from one of the callback methods.
656 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700657 * If positive timeout is specified, then processEvents will exit after this timeout, if
658 * not stopped earlier with face.shutdown() or when all active events finish. The call
659 * can be called repeatedly, if desired.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800660 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700661 * If negative timeout is specified, then processEvents will not block and process only
662 * pending events.
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800663 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700664 * @param timeout maximum time to block the thread
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700665 * @param keepThread Keep thread in a blocked state (in event processing), even when
666 * there are no outstanding events (e.g., no Interest/Data is expected)
667 *
668 * @throw This may throw an exception for reading data or in the callback for processing
669 * the data. If you call this from an main event loop, you may want to catch and
670 * log/disregard all exceptions.
Jeff Thompson432c8be2013-08-09 16:16:08 -0700671 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700672 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700673 processEvents(const time::milliseconds& timeout = time::milliseconds::zero(),
674 bool keepThread = false);
Jeff Thompson432c8be2013-08-09 16:16:08 -0700675
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700676 /**
677 * @brief Shutdown face operations
678 *
679 * This method cancels all pending operations and closes connection to NDN Forwarder.
680 *
681 * Note that this method does not stop IO service and if the same IO service is shared
682 * between multiple Faces or with other IO objects (e.g., Scheduler).
683 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700684 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700685 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800686
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700687 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700688 * @brief Get reference to IO service object
689 */
690 boost::asio::io_service&
691 getIoService()
692 {
Junxiao Shi2cced062014-11-02 21:27:38 -0700693 return m_ioService;
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700694 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800695
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800696NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
697 /**
698 * @brief Get the underlying transport of the face
699 */
700 shared_ptr<Transport>
701 getTransport();
702
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800703private:
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700704
705 /**
706 * @throws ConfigFile::Error on parse error and unsupported protocols
707 */
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800708 shared_ptr<Transport>
709 makeDefaultTransport();
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700710
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600711 /**
712 * @throws Face::Error on unsupported protocol
Junxiao Shi2cced062014-11-02 21:27:38 -0700713 * @note shared_ptr is passed by value because ownership is transferred to this function
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600714 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800715 void
Joao Pereira68c0d882015-05-19 14:27:55 -0400716 construct(shared_ptr<Transport> transport, KeyChain& keyChain);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600717
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700718 void
Eric Newberry83872fd2015-08-06 17:01:24 -0700719 onReceiveElement(const Block& blockFromDaemon);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800720
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700721 void
722 asyncShutdown();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700723
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700724private:
Junxiao Shi2cced062014-11-02 21:27:38 -0700725 /// the IO service owned by this Face, could be null
726 unique_ptr<boost::asio::io_service> m_internalIoService;
727 /// the IO service used by this Face
728 boost::asio::io_service& m_ioService;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700729
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800730 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800731
Junxiao Shiedd834e2014-10-28 20:28:58 -0700732 /** @brief if not null, a pointer to an internal KeyChain owned by Face
733 * @note if a KeyChain is supplied to constructor, this pointer will be null,
734 * and the passed KeyChain is given to nfdController;
735 * currently Face does not keep the KeyChain passed in constructor
736 * because it's not needed, but this may change in the future
737 */
Joao Pereira68c0d882015-05-19 14:27:55 -0400738 unique_ptr<KeyChain> m_internalKeyChain;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700739
Joao Pereira68c0d882015-05-19 14:27:55 -0400740 unique_ptr<nfd::Controller> m_nfdController;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600741
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700742 class Impl;
Joao Pereira68c0d882015-05-19 14:27:55 -0400743 unique_ptr<Impl> m_impl;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700744};
745
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800746} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700747
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800748#endif // NDN_FACE_HPP