blob: 19587dc37e1d85f851afddfb8a57807524f99b6a [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;
Junxiao Shiedd834e2014-10-28 20:28:58 -070044class KeyChain;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070045
46class PendingInterestId;
47class RegisteredPrefixId;
48class InterestFilterId;
49
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070050namespace nfd {
51class Controller;
52}
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070053
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070054/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070055 * @brief Callback called when expressed Interest gets satisfied with Data packet
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080056 */
57typedef function<void(const Interest&, Data&)> OnData;
58
59/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070060 * @brief Callback called when expressed Interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080061 */
62typedef function<void(const Interest&)> OnTimeout;
63
64/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070065 * @brief Callback called when incoming Interest matches the specified InterestFilter
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080066 */
Alexander Afanasyev90164962014-03-06 08:29:59 +000067typedef function<void (const InterestFilter&, const Interest&)> OnInterest;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080068
69/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070070 * @brief Callback called when registerPrefix or setInterestFilter command succeeds
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080071 */
Alexander Afanasyev984ad192014-05-02 19:11:15 -070072typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080073
Alexander Afanasyev984ad192014-05-02 19:11:15 -070074/**
75 * @brief Callback called when registerPrefix or setInterestFilter command fails
76 */
77typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
78
79/**
80 * @brief Callback called when unregisterPrefix or unsetInterestFilter command succeeds
81 */
82typedef function<void()> UnregisterPrefixSuccessCallback;
83
84/**
85 * @brief Callback called when unregisterPrefix or unsetInterestFilter command fails
86 */
87typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080088
89
90/**
91 * @brief Abstraction to communicate with local or remote NDN forwarder
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070092 */
Alexander Afanasyev8460afb2014-02-15 20:31:42 -080093class Face : noncopyable
94{
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070095public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070096 class Error : public std::runtime_error
97 {
98 public:
99 explicit
100 Error(const std::string& what)
101 : std::runtime_error(what)
102 {
103 }
104 };
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800105
Junxiao Shiedd834e2014-10-28 20:28:58 -0700106public: // constructors
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800107 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700108 * @brief Create a new Face using the default transport (UnixTransport)
109 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600110 * @throws ConfigFile::Error on configuration file parse failure
111 * @throws Face::Error on unsupported protocol
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800112 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800113 Face();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800114
115 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700116 * @brief Create a new Face using the default transport (UnixTransport)
117 *
118 * @deprecated This constructor is deprecated. Use `Face(boost::asio::io_service&)`
119 * instead.
120 *
121 * @param ioService A shared pointer to boost::io_service object that should control all
122 * IO operations
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600123 * @throws ConfigFile::Error on configuration file parse failure
124 * @throws Face::Error on unsupported protocol
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700125 */
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700126 DEPRECATED(
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800127 explicit
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700128 Face(const shared_ptr<boost::asio::io_service>& ioService));
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800129
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700130 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700131 * @brief Create a new Face using the default transport (UnixTransport)
132 *
133 * @par Usage examples:
134 *
135 * Face face1;
136 * Face face2(face1.getIoService());
137 *
138 * // Now the following ensures that events on both faces are processed
139 * face1.processEvents();
140 * // or face1.getIoService().run();
141 *
142 * @par or
143 *
144 * boost::asio::io_service ioService;
145 * Face face1(ioService);
146 * Face face2(ioService);
147 * ...
148 *
149 * ioService.run();
150 *
151 * @param ioService A reference to boost::io_service object that should control all
152 * IO operations.
153 * @throws ConfigFile::Error on configuration file parse failure
154 * @throws Face::Error on unsupported protocol
155 */
156 explicit
157 Face(boost::asio::io_service& ioService);
158
159 /**
160 * @brief Create a new Face using TcpTransport
161 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700162 * @param host The host of the NDN forwarder
163 * @param port (optional) The port or service name of the NDN forwarder (**default**: "6363")
164 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600165 * @throws Face::Error on unsupported protocol
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700166 */
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800167 Face(const std::string& host, const std::string& port = "6363");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800168
169 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700170 * @brief Create a new Face using the given Transport
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700171 *
172 * @param transport A shared_ptr to a Transport object used for communication
173 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600174 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800175 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800176 explicit
177 Face(const shared_ptr<Transport>& transport);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800178
179 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700180 * @brief Create a new Face using the given Transport and IO service object
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800181 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700182 * @sa Face(boost::asio::io_service&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800183 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600184 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800185 */
186 Face(const shared_ptr<Transport>& transport,
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700187 boost::asio::io_service& ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800188
Jeff Thompson4fe45512013-08-23 14:06:38 -0700189 /**
Junxiao Shiedd834e2014-10-28 20:28:58 -0700190 * @brief Create a new Face using the given Transport and IO service object
191 * @param transport the Transport used for communication
192 * @param ioService the io_service that controls all IO operations
193 * @param keyChain the KeyChain to sign commands
194 * @throws Face::Error on unsupported protocol
195 * @note shared_ptr is passed by value because ownership is shared with this class
196 */
197 Face(shared_ptr<Transport> transport,
198 boost::asio::io_service& ioService,
199 KeyChain& keyChain);
200
201 ~Face();
202
203public: // consumer
204 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800205 * @brief Express Interest
206 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700207 * @param interest An Interest to be expressed
208 * @param onData Callback to be called when a matching data packet is received
209 * @param onTimeout (optional) A function object to call if the interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800210 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700211 * @return The pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700212 *
213 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Jeff Thompson4fe45512013-08-23 14:06:38 -0700214 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800215 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800216 expressInterest(const Interest& interest,
217 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Jeff Thompson4fe45512013-08-23 14:06:38 -0700218
219 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800220 * @brief Express Interest using name and Interest template
221 *
222 * @param name Name of the Interest
223 * @param tmpl Interest template to fill parameters
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700224 * @param onData Callback to be called when a matching data packet is received
225 * @param onTimeout (optional) A function object to call if the interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800226 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700227 * @return Opaque pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700228 *
229 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Jeff Thompson7aec0252013-08-22 17:29:57 -0700230 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800231 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800232 expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800233 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800234 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700235
Jeff Thompson11095142013-10-01 16:20:28 -0700236 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700237 * @brief Cancel previously expressed Interest
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700238 *
Jeff Thompson11095142013-10-01 16:20:28 -0700239 * @param pendingInterestId The ID returned from expressInterest.
240 */
241 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800242 removePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700243
Jeff Thompson432c8be2013-08-09 16:16:08 -0700244 /**
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700245 * @brief Get number of pending Interests
246 */
247 size_t
248 getNPendingInterests() const;
249
Junxiao Shiedd834e2014-10-28 20:28:58 -0700250public: // producer
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700251 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700252 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
253 * callback and register the filtered prefix with the connected NDN forwarder
254 *
255 * This version of setInterestFilter combines setInterestFilter and registerPrefix
256 * operations and is intended to be used when only one filter for the same prefix needed
257 * to be set. When multiple names sharing the same prefix should be dispatched to
258 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
259 * a series of setInterestFilter calls.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700260 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000261 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700262 * @param onInterest A callback to be called when a matching interest is received
263 * @param onSuccess A callback to be called when prefixRegister command succeeds
264 * @param onFailure A callback to be called when prefixRegister command fails
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700265 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700266 * @param certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700267 * command is signed. When omitted, a default certificate of
268 * the default identity is used to sign the registration command
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700269 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700270 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
271 * removeRegisteredPrefix
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400272 *
273 * @note IdentityCertificate() creates a certificate with an empty name, which is an invalid
274 * certificate. A valid IdentityCertificate has at least 4 name components, as it follows
275 * `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700276 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800277 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000278 setInterestFilter(const InterestFilter& interestFilter,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800279 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700280 const RegisterPrefixSuccessCallback& onSuccess,
281 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700282 const IdentityCertificate& certificate = IdentityCertificate(),
283 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Jeff Thompson11095142013-10-01 16:20:28 -0700284
285 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300286 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
287 * callback and register the filtered prefix with the connected NDN forwarder
288 *
289 * This version of setInterestFilter combines setInterestFilter and registerPrefix
290 * operations and is intended to be used when only one filter for the same prefix needed
291 * to be set. When multiple names sharing the same prefix should be dispatched to
292 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
293 * a series of setInterestFilter calls.
294 *
295 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
296 * @param onInterest A callback to be called when a matching interest is received
297 * @param onFailure A callback to be called when prefixRegister command fails
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700298 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300299 * @param certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700300 * command is signed. When omitted, a default certificate of
301 * the default identity is used to sign the registration command
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300302 *
303 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
304 * removeRegisteredPrefix
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400305 *
306 * @note IdentityCertificate() creates a certificate with an empty name, which is an invalid
307 * certificate. A valid IdentityCertificate has at least 4 name components, as it follows
308 * `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700309 */
310 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000311 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700312 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700313 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700314 const IdentityCertificate& certificate = IdentityCertificate(),
315 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700316
317 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700318 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
319 * callback and register the filtered prefix with the connected NDN forwarder
320 *
321 * This version of setInterestFilter combines setInterestFilter and registerPrefix
322 * operations and is intended to be used when only one filter for the same prefix needed
323 * to be set. When multiple names sharing the same prefix should be dispatched to
324 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
325 * a series of setInterestFilter calls.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700326 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000327 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700328 * @param onInterest A callback to be called when a matching interest is received
329 * @param onSuccess A callback to be called when prefixRegister command succeeds
330 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700331 * @param identity A signing identity. A prefix registration command is signed
332 * under the default certificate of this identity
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700333 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700334 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700335 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700336 */
337 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000338 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700339 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700340 const RegisterPrefixSuccessCallback& onSuccess,
341 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700342 const Name& identity,
343 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700344
345 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300346 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
347 * callback and register the filtered prefix with the connected NDN forwarder
348 *
349 * This version of setInterestFilter combines setInterestFilter and registerPrefix
350 * operations and is intended to be used when only one filter for the same prefix needed
351 * to be set. When multiple names sharing the same prefix should be dispatched to
352 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
353 * a series of setInterestFilter calls.
354 *
355 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
356 * @param onInterest A callback to be called when a matching interest is received
357 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700358 * @param identity A signing identity. A prefix registration command is signed
359 * under the default certificate of this identity
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700360 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300361 *
362 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700363 */
364 const RegisteredPrefixId*
365 setInterestFilter(const InterestFilter& interestFilter,
366 const OnInterest& onInterest,
367 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700368 const Name& identity,
369 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700370
371 /**
372 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest callback
373 *
374 * @param interestFilter Interest
375 * @param onInterest A callback to be called when a matching interest is received
376 *
377 * This method modifies library's FIB only, and does not register the prefix with the
378 * forwarder. It will always succeed. To register prefix with the forwarder, use
379 * registerPrefix, or use the setInterestFilter overload taking two callbacks.
380 *
381 * @return Opaque interest filter ID which can be used with unsetInterestFilter
382 */
383 const InterestFilterId*
384 setInterestFilter(const InterestFilter& interestFilter,
385 const OnInterest& onInterest);
386
387
388 /**
389 * @brief Register prefix with the connected NDN forwarder
390 *
391 * This method only modifies forwarder's RIB (or FIB) and does not associate any
392 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
393 * the right callbacks.
394 *
395 * @param prefix A prefix to register with the connected NDN forwarder
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 certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700399 * command is signed. When omitted, a default certificate of
400 * the default identity is used to sign the registration command
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700401 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700402 *
403 * @return The registered prefix ID which can be used with unregisterPrefix
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400404 *
405 * @note IdentityCertificate() creates a certificate with an empty name, which is an invalid
406 * certificate. A valid IdentityCertificate has at least 4 name components, as it follows
407 * `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700408 */
409 const RegisteredPrefixId*
410 registerPrefix(const Name& prefix,
411 const RegisterPrefixSuccessCallback& onSuccess,
412 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700413 const IdentityCertificate& certificate = IdentityCertificate(),
414 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700415
416 /**
417 * @brief Register prefix with the connected NDN forwarder and call onInterest when a matching
418 * interest is received.
419 *
420 * This method only modifies forwarder's RIB (or FIB) and does not associate any
421 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
422 * the right callbacks.
423 *
424 * @param prefix A prefix to register with the connected NDN forwarder
425 * @param onSuccess A callback to be called when prefixRegister command succeeds
426 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700427 * @param identity A signing identity. A prefix registration command is signed
428 * under the default certificate of this identity
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700429 * @param flags (optional) RIB flags (not used when direct FIB management is requested)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700430 *
431 * @return The registered prefix ID which can be used with unregisterPrefix
432 */
433 const RegisteredPrefixId*
434 registerPrefix(const Name& prefix,
435 const RegisterPrefixSuccessCallback& onSuccess,
436 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700437 const Name& identity,
438 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700439
440 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300441 * @brief Remove the registered prefix entry with the registeredPrefixId
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700442 *
443 * This does not affect another registered prefix with a different registeredPrefixId,
444 * even it if has the same prefix name. If there is no entry with the
445 * registeredPrefixId, do nothing.
446 *
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700447 * unsetInterestFilter will use the same credentials as original
448 * setInterestFilter/registerPrefix command
449 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700450 * @param registeredPrefixId The ID returned from registerPrefix
Jeff Thompson11095142013-10-01 16:20:28 -0700451 */
452 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800453 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800454
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700455 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700456 * @brief Remove previously set InterestFilter from library's FIB
457 *
458 * This method always succeeds and will **NOT** send any request to the connected
459 * forwarder.
460 *
461 * @param interestFilterId The ID returned from setInterestFilter.
462 */
463 void
464 unsetInterestFilter(const InterestFilterId* interestFilterId);
465
466 /**
467 * @brief Deregister prefix from RIB (or FIB)
468 *
469 * unregisterPrefix will use the same credentials as original
470 * setInterestFilter/registerPrefix command
471 *
472 * If registeredPrefixId was obtained using setInterestFilter, the corresponding
473 * InterestFilter will be unset too.
474 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700475 * @param registeredPrefixId The ID returned from registerPrefix
476 * @param onSuccess Callback to be called when operation succeeds
477 * @param onFailure Callback to be called when operation fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700478 */
479 void
480 unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
481 const UnregisterPrefixSuccessCallback& onSuccess,
482 const UnregisterPrefixFailureCallback& onFailure);
483
484 /**
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700485 * @brief (FOR DEBUG PURPOSES ONLY) Request direct NFD FIB management
486 */
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700487 void
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700488 setDirectFibManagement(bool isDirectFibManagementRequested = false);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700489
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800490 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800491 * @brief Publish data packet
492 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700493 * This method can be called to satisfy the incoming Interest or to put Data packet into
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700494 * the cache of the local NDN forwarder.
495 *
496 * @param data Data packet to publish. It is highly recommended to use Data packet that
497 * was created using make_shared<Data>(...). Otherwise, put() will make an
498 * extra copy of the Data packet to ensure validity of published Data until
499 * asynchronous put() operation finishes.
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700500 *
501 * @throws Error when Data size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800502 */
503 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800504 put(const Data& data);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700505
Junxiao Shiedd834e2014-10-28 20:28:58 -0700506public: // IO routine
Jeff Thompson86507bc2013-08-23 20:51:38 -0700507 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700508 * @brief Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800509 *
510 * This call will block forever (default timeout == 0) to process IO on the face.
511 * To exit, one expected to call face.shutdown() from one of the callback methods.
512 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700513 * If positive timeout is specified, then processEvents will exit after this timeout, if
514 * not stopped earlier with face.shutdown() or when all active events finish. The call
515 * can be called repeatedly, if desired.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800516 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700517 * If negative timeout is specified, then processEvents will not block and process only
518 * pending events.
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800519 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700520 * @param timeout maximum time to block the thread
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700521 * @param keepThread Keep thread in a blocked state (in event processing), even when
522 * there are no outstanding events (e.g., no Interest/Data is expected)
523 *
524 * @throw This may throw an exception for reading data or in the callback for processing
525 * the data. If you call this from an main event loop, you may want to catch and
526 * log/disregard all exceptions.
Jeff Thompson432c8be2013-08-09 16:16:08 -0700527 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700528 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700529 processEvents(const time::milliseconds& timeout = time::milliseconds::zero(),
530 bool keepThread = false);
Jeff Thompson432c8be2013-08-09 16:16:08 -0700531
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700532 /**
533 * @brief Shutdown face operations
534 *
535 * This method cancels all pending operations and closes connection to NDN Forwarder.
536 *
537 * Note that this method does not stop IO service and if the same IO service is shared
538 * between multiple Faces or with other IO objects (e.g., Scheduler).
539 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700540 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700541 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800542
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700543 /**
544 * @brief Get shared_ptr of the IO service object
545 *
546 * @deprecated Use getIoService instead
547 */
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700548 DEPRECATED(
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700549 shared_ptr<boost::asio::io_service>
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700550 ioService())
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700551 {
552 return m_ioService;
553 }
554
555 /**
556 * @brief Get reference to IO service object
557 */
558 boost::asio::io_service&
559 getIoService()
560 {
561 return *m_ioService;
562 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800563
564private:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600565 /**
566 * @throws Face::Error on unsupported protocol
Junxiao Shiedd834e2014-10-28 20:28:58 -0700567 * @note shared_ptrs are passed by value because ownership is transferred to this function
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600568 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800569 void
Junxiao Shiedd834e2014-10-28 20:28:58 -0700570 construct(shared_ptr<Transport> transport,
571 shared_ptr<boost::asio::io_service> ioService,
572 KeyChain* keyChain);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600573
574 bool
575 isSupportedNfdProtocol(const std::string& protocol);
576
577 bool
578 isSupportedNrdProtocol(const std::string& protocol);
579
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700580 class ProcessEventsTimeout
581 {
582 };
583
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700584 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800585 onReceiveElement(const Block& wire);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800586
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700587 void
588 asyncShutdown();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700589
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800590 static void
591 fireProcessEventsTimeout(const boost::system::error_code& error);
592
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700593private:
Junxiao Shiedd834e2014-10-28 20:28:58 -0700594 /// @todo change to regular pointer after #2097
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800595 shared_ptr<boost::asio::io_service> m_ioService;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700596
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800597 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800598
Junxiao Shiedd834e2014-10-28 20:28:58 -0700599 /** @brief if not null, a pointer to an internal KeyChain owned by Face
600 * @note if a KeyChain is supplied to constructor, this pointer will be null,
601 * and the passed KeyChain is given to nfdController;
602 * currently Face does not keep the KeyChain passed in constructor
603 * because it's not needed, but this may change in the future
604 */
605 KeyChain* m_internalKeyChain;
606
607 nfd::Controller* m_nfdController;
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700608 bool m_isDirectNfdFibManagementRequested;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600609
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700610 class Impl;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700611 Impl* m_impl;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700612};
613
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600614inline bool
615Face::isSupportedNfdProtocol(const std::string& protocol)
616{
617 return protocol == "nfd-0.1";
618}
619
620inline bool
621Face::isSupportedNrdProtocol(const std::string& protocol)
622{
623 return protocol == "nrd-0.1";
624}
625
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700626inline void
627Face::setDirectFibManagement(bool isDirectFibManagementRequested/* = false*/)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600628{
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700629 m_isDirectNfdFibManagementRequested = isDirectFibManagementRequested;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600630}
631
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800632} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700633
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800634#endif // NDN_FACE_HPP