blob: d95d04a1b1e349fcaffeb282a7b0884502e67d4a [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070022 */
23
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -070024#ifndef NDN_FACE_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -070025#define NDN_FACE_HPP
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070026
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080027#include "common.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070028
29#include "name.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080030#include "interest.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070031#include "interest-filter.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080032#include "data.hpp"
Alexander Afanasyev984ad192014-05-02 19:11:15 -070033#include "security/identity-certificate.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080034
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070035namespace boost {
36namespace asio {
37class io_service;
38}
39}
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080040
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070041namespace ndn {
42
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070043class Transport;
44
45class PendingInterestId;
46class RegisteredPrefixId;
47class InterestFilterId;
48
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070049namespace nfd {
50class Controller;
51}
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070052
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070053/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070054 * @brief Callback called when expressed Interest gets satisfied with Data packet
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080055 */
56typedef function<void(const Interest&, Data&)> OnData;
57
58/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070059 * @brief Callback called when expressed Interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080060 */
61typedef function<void(const Interest&)> OnTimeout;
62
63/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070064 * @brief Callback called when incoming Interest matches the specified InterestFilter
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080065 */
Alexander Afanasyev90164962014-03-06 08:29:59 +000066typedef function<void (const InterestFilter&, const Interest&)> OnInterest;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080067
68/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070069 * @brief Callback called when registerPrefix or setInterestFilter command succeeds
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080070 */
Alexander Afanasyev984ad192014-05-02 19:11:15 -070071typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080072
Alexander Afanasyev984ad192014-05-02 19:11:15 -070073/**
74 * @brief Callback called when registerPrefix or setInterestFilter command fails
75 */
76typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
77
78/**
79 * @brief Callback called when unregisterPrefix or unsetInterestFilter command succeeds
80 */
81typedef function<void()> UnregisterPrefixSuccessCallback;
82
83/**
84 * @brief Callback called when unregisterPrefix or unsetInterestFilter command fails
85 */
86typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080087
88
89/**
90 * @brief Abstraction to communicate with local or remote NDN forwarder
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070091 */
Alexander Afanasyev8460afb2014-02-15 20:31:42 -080092class Face : noncopyable
93{
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070094public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095 class Error : public std::runtime_error
96 {
97 public:
98 explicit
99 Error(const std::string& what)
100 : std::runtime_error(what)
101 {
102 }
103 };
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800104
105 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700106 * @brief Create a new Face using the default transport (UnixTransport)
107 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600108 * @throws ConfigFile::Error on configuration file parse failure
109 * @throws Face::Error on unsupported protocol
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800110 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800111 Face();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800112
113 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700114 * @brief Create a new Face using the default transport (UnixTransport)
115 *
116 * @deprecated This constructor is deprecated. Use `Face(boost::asio::io_service&)`
117 * instead.
118 *
119 * @param ioService A shared pointer to boost::io_service object that should control all
120 * IO operations
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600121 * @throws ConfigFile::Error on configuration file parse failure
122 * @throws Face::Error on unsupported protocol
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700123 */
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700124 DEPRECATED(
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800125 explicit
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700126 Face(const shared_ptr<boost::asio::io_service>& ioService));
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800127
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700128 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700129 * @brief Create a new Face using the default transport (UnixTransport)
130 *
131 * @par Usage examples:
132 *
133 * Face face1;
134 * Face face2(face1.getIoService());
135 *
136 * // Now the following ensures that events on both faces are processed
137 * face1.processEvents();
138 * // or face1.getIoService().run();
139 *
140 * @par or
141 *
142 * boost::asio::io_service ioService;
143 * Face face1(ioService);
144 * Face face2(ioService);
145 * ...
146 *
147 * ioService.run();
148 *
149 * @param ioService A reference to boost::io_service object that should control all
150 * IO operations.
151 * @throws ConfigFile::Error on configuration file parse failure
152 * @throws Face::Error on unsupported protocol
153 */
154 explicit
155 Face(boost::asio::io_service& ioService);
156
157 /**
158 * @brief Create a new Face using TcpTransport
159 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700160 * @param host The host of the NDN forwarder
161 * @param port (optional) The port or service name of the NDN forwarder (**default**: "6363")
162 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600163 * @throws Face::Error on unsupported protocol
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700164 */
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800165 Face(const std::string& host, const std::string& port = "6363");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800166
167 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700168 * @brief Create a new Face using the given Transport
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700169 *
170 * @param transport A shared_ptr to a Transport object used for communication
171 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600172 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800173 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800174 explicit
175 Face(const shared_ptr<Transport>& transport);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800176
177 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700178 * @brief Create a new Face using the given Transport and IO service object
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800179 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700180 * @sa Face(boost::asio::io_service&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800181 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600182 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800183 */
184 Face(const shared_ptr<Transport>& transport,
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700185 boost::asio::io_service& ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800186
Jeff Thompson4fe45512013-08-23 14:06:38 -0700187 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800188 * @brief Express Interest
189 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700190 * @param interest An Interest to be expressed
191 * @param onData Callback to be called when a matching data packet is received
192 * @param onTimeout (optional) A function object to call if the interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800193 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700194 * @return The pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700195 *
196 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Jeff Thompson4fe45512013-08-23 14:06:38 -0700197 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800198 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800199 expressInterest(const Interest& interest,
200 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Jeff Thompson4fe45512013-08-23 14:06:38 -0700201
202 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800203 * @brief Express Interest using name and Interest template
204 *
205 * @param name Name of the Interest
206 * @param tmpl Interest template to fill parameters
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700207 * @param onData Callback to be called when a matching data packet is received
208 * @param onTimeout (optional) A function object to call if the interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800209 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700210 * @return Opaque pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700211 *
212 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Jeff Thompson7aec0252013-08-22 17:29:57 -0700213 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800214 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800215 expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800216 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800217 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700218
Jeff Thompson11095142013-10-01 16:20:28 -0700219 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700220 * @brief Cancel previously expressed Interest
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700221 *
Jeff Thompson11095142013-10-01 16:20:28 -0700222 * @param pendingInterestId The ID returned from expressInterest.
223 */
224 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800225 removePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700226
Jeff Thompson432c8be2013-08-09 16:16:08 -0700227 /**
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700228 * @brief Get number of pending Interests
229 */
230 size_t
231 getNPendingInterests() const;
232
233 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700234 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
235 * callback and register the filtered prefix with the connected NDN forwarder
236 *
237 * This version of setInterestFilter combines setInterestFilter and registerPrefix
238 * operations and is intended to be used when only one filter for the same prefix needed
239 * to be set. When multiple names sharing the same prefix should be dispatched to
240 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
241 * a series of setInterestFilter calls.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700242 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000243 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700244 * @param onInterest A callback to be called when a matching interest is received
245 * @param onSuccess A callback to be called when prefixRegister command succeeds
246 * @param onFailure A callback to be called when prefixRegister command fails
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700247 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700248 * @param certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700249 * command is signed. When omitted, a default certificate of
250 * the default identity is used to sign the registration command
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700251 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700252 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
253 * removeRegisteredPrefix
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400254 *
255 * @note IdentityCertificate() creates a certificate with an empty name, which is an invalid
256 * certificate. A valid IdentityCertificate has at least 4 name components, as it follows
257 * `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700258 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800259 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000260 setInterestFilter(const InterestFilter& interestFilter,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800261 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700262 const RegisterPrefixSuccessCallback& onSuccess,
263 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700264 const IdentityCertificate& certificate = IdentityCertificate(),
265 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Jeff Thompson11095142013-10-01 16:20:28 -0700266
267 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300268 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
269 * callback and register the filtered prefix with the connected NDN forwarder
270 *
271 * This version of setInterestFilter combines setInterestFilter and registerPrefix
272 * operations and is intended to be used when only one filter for the same prefix needed
273 * to be set. When multiple names sharing the same prefix should be dispatched to
274 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
275 * a series of setInterestFilter calls.
276 *
277 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
278 * @param onInterest A callback to be called when a matching interest is received
279 * @param onFailure A callback to be called when prefixRegister command fails
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700280 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300281 * @param certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700282 * command is signed. When omitted, a default certificate of
283 * the default identity is used to sign the registration command
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300284 *
285 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
286 * removeRegisteredPrefix
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400287 *
288 * @note IdentityCertificate() creates a certificate with an empty name, which is an invalid
289 * certificate. A valid IdentityCertificate has at least 4 name components, as it follows
290 * `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700291 */
292 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000293 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700294 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700295 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700296 const IdentityCertificate& certificate = IdentityCertificate(),
297 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700298
299 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700300 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
301 * callback and register the filtered prefix with the connected NDN forwarder
302 *
303 * This version of setInterestFilter combines setInterestFilter and registerPrefix
304 * operations and is intended to be used when only one filter for the same prefix needed
305 * to be set. When multiple names sharing the same prefix should be dispatched to
306 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
307 * a series of setInterestFilter calls.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700308 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000309 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700310 * @param onInterest A callback to be called when a matching interest is received
311 * @param onSuccess A callback to be called when prefixRegister command succeeds
312 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700313 * @param identity A signing identity. A prefix registration command is signed
314 * under the default certificate of this identity
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700315 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700316 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700317 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700318 */
319 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000320 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700321 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700322 const RegisterPrefixSuccessCallback& onSuccess,
323 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700324 const Name& identity,
325 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700326
327 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300328 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
329 * callback and register the filtered prefix with the connected NDN forwarder
330 *
331 * This version of setInterestFilter combines setInterestFilter and registerPrefix
332 * operations and is intended to be used when only one filter for the same prefix needed
333 * to be set. When multiple names sharing the same prefix should be dispatched to
334 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
335 * a series of setInterestFilter calls.
336 *
337 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
338 * @param onInterest A callback to be called when a matching interest is received
339 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700340 * @param identity A signing identity. A prefix registration command is signed
341 * under the default certificate of this identity
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700342 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300343 *
344 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700345 */
346 const RegisteredPrefixId*
347 setInterestFilter(const InterestFilter& interestFilter,
348 const OnInterest& onInterest,
349 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700350 const Name& identity,
351 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700352
353 /**
354 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest callback
355 *
356 * @param interestFilter Interest
357 * @param onInterest A callback to be called when a matching interest is received
358 *
359 * This method modifies library's FIB only, and does not register the prefix with the
360 * forwarder. It will always succeed. To register prefix with the forwarder, use
361 * registerPrefix, or use the setInterestFilter overload taking two callbacks.
362 *
363 * @return Opaque interest filter ID which can be used with unsetInterestFilter
364 */
365 const InterestFilterId*
366 setInterestFilter(const InterestFilter& interestFilter,
367 const OnInterest& onInterest);
368
369
370 /**
371 * @brief Register prefix with the connected NDN forwarder
372 *
373 * This method only modifies forwarder's RIB (or FIB) and does not associate any
374 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
375 * the right callbacks.
376 *
377 * @param prefix A prefix to register with the connected NDN forwarder
378 * @param onSuccess A callback to be called when prefixRegister command succeeds
379 * @param onFailure A callback to be called when prefixRegister command fails
380 * @param certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700381 * command is signed. When omitted, a default certificate of
382 * the default identity is used to sign the registration command
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700383 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700384 *
385 * @return The registered prefix ID which can be used with unregisterPrefix
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400386 *
387 * @note IdentityCertificate() creates a certificate with an empty name, which is an invalid
388 * certificate. A valid IdentityCertificate has at least 4 name components, as it follows
389 * `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700390 */
391 const RegisteredPrefixId*
392 registerPrefix(const Name& prefix,
393 const RegisterPrefixSuccessCallback& onSuccess,
394 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700395 const IdentityCertificate& certificate = IdentityCertificate(),
396 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700397
398 /**
399 * @brief Register prefix with the connected NDN forwarder and call onInterest when a matching
400 * interest is received.
401 *
402 * This method only modifies forwarder's RIB (or FIB) and does not associate any
403 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
404 * the right callbacks.
405 *
406 * @param prefix A prefix to register with the connected NDN forwarder
407 * @param onSuccess A callback to be called when prefixRegister command succeeds
408 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700409 * @param identity A signing identity. A prefix registration command is signed
410 * under the default certificate of this identity
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700411 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700412 *
413 * @return The registered prefix ID which can be used with unregisterPrefix
414 */
415 const RegisteredPrefixId*
416 registerPrefix(const Name& prefix,
417 const RegisterPrefixSuccessCallback& onSuccess,
418 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700419 const Name& identity,
420 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700421
422 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300423 * @brief Remove the registered prefix entry with the registeredPrefixId
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700424 *
425 * This does not affect another registered prefix with a different registeredPrefixId,
426 * even it if has the same prefix name. If there is no entry with the
427 * registeredPrefixId, do nothing.
428 *
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700429 * unsetInterestFilter will use the same credentials as original
430 * setInterestFilter/registerPrefix command
431 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700432 * @param registeredPrefixId The ID returned from registerPrefix
Jeff Thompson11095142013-10-01 16:20:28 -0700433 */
434 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800435 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800436
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700437 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700438 * @brief Remove previously set InterestFilter from library's FIB
439 *
440 * This method always succeeds and will **NOT** send any request to the connected
441 * forwarder.
442 *
443 * @param interestFilterId The ID returned from setInterestFilter.
444 */
445 void
446 unsetInterestFilter(const InterestFilterId* interestFilterId);
447
448 /**
449 * @brief Deregister prefix from RIB (or FIB)
450 *
451 * unregisterPrefix will use the same credentials as original
452 * setInterestFilter/registerPrefix command
453 *
454 * If registeredPrefixId was obtained using setInterestFilter, the corresponding
455 * InterestFilter will be unset too.
456 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700457 * @param registeredPrefixId The ID returned from registerPrefix
458 * @param onSuccess Callback to be called when operation succeeds
459 * @param onFailure Callback to be called when operation fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700460 */
461 void
462 unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
463 const UnregisterPrefixSuccessCallback& onSuccess,
464 const UnregisterPrefixFailureCallback& onFailure);
465
466 /**
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700467 * @brief (FOR DEBUG PURPOSES ONLY) Request direct NFD FIB management
468 */
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700469 void
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700470 setDirectFibManagement(bool isDirectFibManagementRequested = false);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700471
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800472 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800473 * @brief Publish data packet
474 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700475 * This method can be called to satisfy the incoming Interest or to put Data packet into
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700476 * the cache of the local NDN forwarder.
477 *
478 * @param data Data packet to publish. It is highly recommended to use Data packet that
479 * was created using make_shared<Data>(...). Otherwise, put() will make an
480 * extra copy of the Data packet to ensure validity of published Data until
481 * asynchronous put() operation finishes.
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700482 *
483 * @throws Error when Data size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800484 */
485 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800486 put(const Data& data);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700487
Jeff Thompson86507bc2013-08-23 20:51:38 -0700488 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700489 * @brief Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800490 *
491 * This call will block forever (default timeout == 0) to process IO on the face.
492 * To exit, one expected to call face.shutdown() from one of the callback methods.
493 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700494 * If positive timeout is specified, then processEvents will exit after this timeout, if
495 * not stopped earlier with face.shutdown() or when all active events finish. The call
496 * can be called repeatedly, if desired.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800497 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700498 * If negative timeout is specified, then processEvents will not block and process only
499 * pending events.
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800500 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700501 * @param timeout maximum time to block the thread
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700502 * @param keepThread Keep thread in a blocked state (in event processing), even when
503 * there are no outstanding events (e.g., no Interest/Data is expected)
504 *
505 * @throw This may throw an exception for reading data or in the callback for processing
506 * the data. If you call this from an main event loop, you may want to catch and
507 * log/disregard all exceptions.
Jeff Thompson432c8be2013-08-09 16:16:08 -0700508 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700509 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700510 processEvents(const time::milliseconds& timeout = time::milliseconds::zero(),
511 bool keepThread = false);
Jeff Thompson432c8be2013-08-09 16:16:08 -0700512
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700513 /**
514 * @brief Shutdown face operations
515 *
516 * This method cancels all pending operations and closes connection to NDN Forwarder.
517 *
518 * Note that this method does not stop IO service and if the same IO service is shared
519 * between multiple Faces or with other IO objects (e.g., Scheduler).
520 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700521 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700522 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800523
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700524 /**
525 * @brief Get shared_ptr of the IO service object
526 *
527 * @deprecated Use getIoService instead
528 */
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700529 DEPRECATED(
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700530 shared_ptr<boost::asio::io_service>
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700531 ioService())
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700532 {
533 return m_ioService;
534 }
535
536 /**
537 * @brief Get reference to IO service object
538 */
539 boost::asio::io_service&
540 getIoService()
541 {
542 return *m_ioService;
543 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800544
545private:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600546 /**
547 * @throws Face::Error on unsupported protocol
548 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800549 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800550 construct(const shared_ptr<Transport>& transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -0800551 const shared_ptr<boost::asio::io_service>& ioService);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600552
553 bool
554 isSupportedNfdProtocol(const std::string& protocol);
555
556 bool
557 isSupportedNrdProtocol(const std::string& protocol);
558
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700559 class ProcessEventsTimeout
560 {
561 };
562
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700563 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800564 onReceiveElement(const Block& wire);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800565
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700566 void
567 asyncShutdown();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700568
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800569 static void
570 fireProcessEventsTimeout(const boost::system::error_code& error);
571
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700572private:
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800573 shared_ptr<boost::asio::io_service> m_ioService;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700574
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800575 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800576
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700577 shared_ptr<nfd::Controller> m_nfdController;
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700578 bool m_isDirectNfdFibManagementRequested;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600579
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700580 class Impl;
581 shared_ptr<Impl> m_impl;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700582};
583
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600584inline bool
585Face::isSupportedNfdProtocol(const std::string& protocol)
586{
587 return protocol == "nfd-0.1";
588}
589
590inline bool
591Face::isSupportedNrdProtocol(const std::string& protocol)
592{
593 return protocol == "nrd-0.1";
594}
595
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700596inline void
597Face::setDirectFibManagement(bool isDirectFibManagementRequested/* = false*/)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600598{
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700599 m_isDirectNfdFibManagementRequested = isDirectFibManagementRequested;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600600}
601
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800602} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700603
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800604#endif // NDN_FACE_HPP