blob: a42a95f58dceba0592ba15898bc21621a7916d3b [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
Yingdi Yu1b0311c2015-06-10 14:58:47 -070049namespace security {
50class KeyChain;
51}
52using security::KeyChain;
53
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070054namespace nfd {
55class Controller;
56}
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070057
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070058/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070059 * @brief Callback called when expressed Interest gets satisfied with Data packet
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080060 */
61typedef function<void(const Interest&, Data&)> OnData;
62
63/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070064 * @brief Callback called when expressed Interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080065 */
66typedef function<void(const Interest&)> OnTimeout;
67
68/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070069 * @brief Callback called when incoming Interest matches the specified InterestFilter
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080070 */
Alexander Afanasyev90164962014-03-06 08:29:59 +000071typedef function<void (const InterestFilter&, const Interest&)> OnInterest;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080072
73/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070074 * @brief Callback called when registerPrefix or setInterestFilter command succeeds
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080075 */
Alexander Afanasyev984ad192014-05-02 19:11:15 -070076typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080077
Alexander Afanasyev984ad192014-05-02 19:11:15 -070078/**
79 * @brief Callback called when registerPrefix or setInterestFilter command fails
80 */
81typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
82
83/**
84 * @brief Callback called when unregisterPrefix or unsetInterestFilter command succeeds
85 */
86typedef function<void()> UnregisterPrefixSuccessCallback;
87
88/**
89 * @brief Callback called when unregisterPrefix or unsetInterestFilter command fails
90 */
91typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080092
93
94/**
95 * @brief Abstraction to communicate with local or remote NDN forwarder
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070096 */
Alexander Afanasyev8460afb2014-02-15 20:31:42 -080097class Face : noncopyable
98{
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070099public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700100 class Error : public std::runtime_error
101 {
102 public:
103 explicit
104 Error(const std::string& what)
105 : std::runtime_error(what)
106 {
107 }
108 };
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800109
Junxiao Shiedd834e2014-10-28 20:28:58 -0700110public: // constructors
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800111 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700112 * @brief Create a new Face using the default transport (UnixTransport)
113 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600114 * @throws ConfigFile::Error on configuration file parse failure
115 * @throws Face::Error on unsupported protocol
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800116 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800117 Face();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800118
119 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700120 * @brief Create a new Face using the default transport (UnixTransport)
121 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700122 * @par Usage examples:
123 *
124 * Face face1;
125 * Face face2(face1.getIoService());
126 *
127 * // Now the following ensures that events on both faces are processed
128 * face1.processEvents();
129 * // or face1.getIoService().run();
130 *
131 * @par or
132 *
133 * boost::asio::io_service ioService;
134 * Face face1(ioService);
135 * Face face2(ioService);
136 * ...
137 *
138 * ioService.run();
139 *
140 * @param ioService A reference to boost::io_service object that should control all
141 * IO operations.
142 * @throws ConfigFile::Error on configuration file parse failure
143 * @throws Face::Error on unsupported protocol
144 */
145 explicit
146 Face(boost::asio::io_service& ioService);
147
148 /**
149 * @brief Create a new Face using TcpTransport
150 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700151 * @param host The host of the NDN forwarder
152 * @param port (optional) The port or service name of the NDN forwarder (**default**: "6363")
153 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600154 * @throws Face::Error on unsupported protocol
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700155 */
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800156 Face(const std::string& host, const std::string& port = "6363");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800157
158 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700159 * @brief Create a new Face using the given Transport
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700160 *
161 * @param transport A shared_ptr to a Transport object used for communication
162 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600163 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800164 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800165 explicit
166 Face(const shared_ptr<Transport>& transport);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800167
168 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700169 * @brief Create a new Face using the given Transport and IO service object
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800170 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700171 * @sa Face(boost::asio::io_service&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800172 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600173 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800174 */
175 Face(const shared_ptr<Transport>& transport,
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700176 boost::asio::io_service& ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800177
Jeff Thompson4fe45512013-08-23 14:06:38 -0700178 /**
Junxiao Shiedd834e2014-10-28 20:28:58 -0700179 * @brief Create a new Face using the given Transport and IO service object
180 * @param transport the Transport used for communication
181 * @param ioService the io_service that controls all IO operations
182 * @param keyChain the KeyChain to sign commands
183 * @throws Face::Error on unsupported protocol
184 * @note shared_ptr is passed by value because ownership is shared with this class
185 */
186 Face(shared_ptr<Transport> transport,
187 boost::asio::io_service& ioService,
188 KeyChain& keyChain);
189
190 ~Face();
191
192public: // consumer
193 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800194 * @brief Express Interest
195 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700196 * @param interest An Interest to be expressed
197 * @param onData Callback to be called when a matching data packet is received
198 * @param onTimeout (optional) A function object to call if the interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800199 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700200 * @return The pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700201 *
202 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Jeff Thompson4fe45512013-08-23 14:06:38 -0700203 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800204 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800205 expressInterest(const Interest& interest,
206 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Jeff Thompson4fe45512013-08-23 14:06:38 -0700207
208 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800209 * @brief Express Interest using name and Interest template
210 *
211 * @param name Name of the Interest
212 * @param tmpl Interest template to fill parameters
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700213 * @param onData Callback to be called when a matching data packet is received
214 * @param onTimeout (optional) A function object to call if the interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800215 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700216 * @return Opaque pending interest ID which can be used with removePendingInterest
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700217 *
218 * @throws Error when Interest size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Jeff Thompson7aec0252013-08-22 17:29:57 -0700219 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800220 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800221 expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800222 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800223 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700224
Jeff Thompson11095142013-10-01 16:20:28 -0700225 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700226 * @brief Cancel previously expressed Interest
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700227 *
Jeff Thompson11095142013-10-01 16:20:28 -0700228 * @param pendingInterestId The ID returned from expressInterest.
229 */
230 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800231 removePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700232
Jeff Thompson432c8be2013-08-09 16:16:08 -0700233 /**
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700234 * @brief Get number of pending Interests
235 */
236 size_t
237 getNPendingInterests() const;
238
Junxiao Shiedd834e2014-10-28 20:28:58 -0700239public: // producer
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700240 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700241 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
242 * callback and register the filtered prefix with the connected NDN forwarder
243 *
244 * This version of setInterestFilter combines setInterestFilter and registerPrefix
245 * operations and is intended to be used when only one filter for the same prefix needed
246 * to be set. When multiple names sharing the same prefix should be dispatched to
247 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
248 * a series of setInterestFilter calls.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700249 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000250 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700251 * @param onInterest A callback to be called when a matching interest is received
252 * @param onSuccess A callback to be called when prefixRegister command succeeds
253 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400254 * @param flags (optional) RIB flags
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700255 * @param certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700256 * command is signed. When omitted, a default certificate of
257 * the default identity is used to sign the registration command
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700258 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700259 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
260 * removeRegisteredPrefix
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400261 *
262 * @note IdentityCertificate() creates a certificate with an empty name, which is an invalid
263 * certificate. A valid IdentityCertificate has at least 4 name components, as it follows
264 * `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700265 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800266 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000267 setInterestFilter(const InterestFilter& interestFilter,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800268 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700269 const RegisterPrefixSuccessCallback& onSuccess,
270 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700271 const IdentityCertificate& certificate = IdentityCertificate(),
272 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Jeff Thompson11095142013-10-01 16:20:28 -0700273
274 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300275 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
276 * callback and register the filtered prefix with the connected NDN forwarder
277 *
278 * This version of setInterestFilter combines setInterestFilter and registerPrefix
279 * operations and is intended to be used when only one filter for the same prefix needed
280 * to be set. When multiple names sharing the same prefix should be dispatched to
281 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
282 * a series of setInterestFilter calls.
283 *
284 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
285 * @param onInterest A callback to be called when a matching interest is received
286 * @param onFailure A callback to be called when prefixRegister command fails
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400287 * @param flags (optional) RIB flags
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300288 * @param certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700289 * command is signed. When omitted, a default certificate of
290 * the default identity is used to sign the registration command
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300291 *
292 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
293 * removeRegisteredPrefix
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400294 *
295 * @note IdentityCertificate() creates a certificate with an empty name, which is an invalid
296 * certificate. A valid IdentityCertificate has at least 4 name components, as it follows
297 * `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700298 */
299 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000300 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700301 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700302 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700303 const IdentityCertificate& certificate = IdentityCertificate(),
304 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700305
306 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700307 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
308 * callback and register the filtered prefix with the connected NDN forwarder
309 *
310 * This version of setInterestFilter combines setInterestFilter and registerPrefix
311 * operations and is intended to be used when only one filter for the same prefix needed
312 * to be set. When multiple names sharing the same prefix should be dispatched to
313 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
314 * a series of setInterestFilter calls.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700315 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000316 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700317 * @param onInterest A callback to be called when a matching interest is received
318 * @param onSuccess A callback to be called when prefixRegister command succeeds
319 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700320 * @param identity A signing identity. A prefix registration command is signed
321 * under the default certificate of this identity
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400322 * @param flags (optional) RIB flags
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700323 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700324 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700325 */
326 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000327 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700328 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700329 const RegisterPrefixSuccessCallback& onSuccess,
330 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700331 const Name& identity,
332 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700333
334 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300335 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
336 * callback and register the filtered prefix with the connected NDN forwarder
337 *
338 * This version of setInterestFilter combines setInterestFilter and registerPrefix
339 * operations and is intended to be used when only one filter for the same prefix needed
340 * to be set. When multiple names sharing the same prefix should be dispatched to
341 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
342 * a series of setInterestFilter calls.
343 *
344 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
345 * @param onInterest A callback to be called when a matching interest is received
346 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700347 * @param identity A signing identity. A prefix registration command is signed
348 * under the default certificate of this identity
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400349 * @param flags (optional) RIB flags
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300350 *
351 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700352 */
353 const RegisteredPrefixId*
354 setInterestFilter(const InterestFilter& interestFilter,
355 const OnInterest& onInterest,
356 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700357 const Name& identity,
358 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700359
360 /**
361 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest callback
362 *
363 * @param interestFilter Interest
364 * @param onInterest A callback to be called when a matching interest is received
365 *
366 * This method modifies library's FIB only, and does not register the prefix with the
367 * forwarder. It will always succeed. To register prefix with the forwarder, use
368 * registerPrefix, or use the setInterestFilter overload taking two callbacks.
369 *
370 * @return Opaque interest filter ID which can be used with unsetInterestFilter
371 */
372 const InterestFilterId*
373 setInterestFilter(const InterestFilter& interestFilter,
374 const OnInterest& onInterest);
375
376
377 /**
378 * @brief Register prefix with the connected NDN forwarder
379 *
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400380 * This method only modifies forwarder's RIB and does not associate any
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700381 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
382 * the right callbacks.
383 *
384 * @param prefix A prefix to register with the connected NDN forwarder
385 * @param onSuccess A callback to be called when prefixRegister command succeeds
386 * @param onFailure A callback to be called when prefixRegister command fails
387 * @param certificate (optional) A certificate under which the prefix registration
Junxiao Shi6a90f372014-10-13 20:29:30 -0700388 * command is signed. When omitted, a default certificate of
389 * the default identity is used to sign the registration command
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400390 * @param flags (optional) RIB flags
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700391 *
392 * @return The registered prefix ID which can be used with unregisterPrefix
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400393 *
394 * @note IdentityCertificate() creates a certificate with an empty name, which is an invalid
395 * certificate. A valid IdentityCertificate has at least 4 name components, as it follows
396 * `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700397 */
398 const RegisteredPrefixId*
399 registerPrefix(const Name& prefix,
400 const RegisterPrefixSuccessCallback& onSuccess,
401 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700402 const IdentityCertificate& certificate = IdentityCertificate(),
403 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700404
405 /**
406 * @brief Register prefix with the connected NDN forwarder and call onInterest when a matching
407 * interest is received.
408 *
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400409 * This method only modifies forwarder's RIB and does not associate any
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700410 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
411 * the right callbacks.
412 *
413 * @param prefix A prefix to register with the connected NDN forwarder
414 * @param onSuccess A callback to be called when prefixRegister command succeeds
415 * @param onFailure A callback to be called when prefixRegister command fails
Junxiao Shi6a90f372014-10-13 20:29:30 -0700416 * @param identity A signing identity. A prefix registration command is signed
417 * under the default certificate of this identity
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400418 * @param flags (optional) RIB flags
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700419 *
420 * @return The registered prefix ID which can be used with unregisterPrefix
421 */
422 const RegisteredPrefixId*
423 registerPrefix(const Name& prefix,
424 const RegisterPrefixSuccessCallback& onSuccess,
425 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700426 const Name& identity,
427 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700428
429 /**
Alexander Afanasyev1b01c312014-05-26 16:58:10 +0300430 * @brief Remove the registered prefix entry with the registeredPrefixId
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700431 *
432 * This does not affect another registered prefix with a different registeredPrefixId,
433 * even it if has the same prefix name. If there is no entry with the
434 * registeredPrefixId, do nothing.
435 *
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700436 * unsetInterestFilter will use the same credentials as original
437 * setInterestFilter/registerPrefix command
438 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700439 * @param registeredPrefixId The ID returned from registerPrefix
Jeff Thompson11095142013-10-01 16:20:28 -0700440 */
441 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800442 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800443
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700444 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700445 * @brief Remove previously set InterestFilter from library's FIB
446 *
447 * This method always succeeds and will **NOT** send any request to the connected
448 * forwarder.
449 *
450 * @param interestFilterId The ID returned from setInterestFilter.
451 */
452 void
453 unsetInterestFilter(const InterestFilterId* interestFilterId);
454
455 /**
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400456 * @brief Unregister prefix from RIB
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700457 *
458 * unregisterPrefix will use the same credentials as original
459 * setInterestFilter/registerPrefix command
460 *
461 * If registeredPrefixId was obtained using setInterestFilter, the corresponding
462 * InterestFilter will be unset too.
463 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700464 * @param registeredPrefixId The ID returned from registerPrefix
465 * @param onSuccess Callback to be called when operation succeeds
466 * @param onFailure Callback to be called when operation fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700467 */
468 void
469 unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
470 const UnregisterPrefixSuccessCallback& onSuccess,
471 const UnregisterPrefixFailureCallback& onFailure);
472
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800473 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800474 * @brief Publish data packet
475 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700476 * This method can be called to satisfy the incoming Interest or to put Data packet into
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700477 * the cache of the local NDN forwarder.
478 *
479 * @param data Data packet to publish. It is highly recommended to use Data packet that
480 * was created using make_shared<Data>(...). Otherwise, put() will make an
481 * extra copy of the Data packet to ensure validity of published Data until
482 * asynchronous put() operation finishes.
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700483 *
484 * @throws Error when Data size exceeds maximum limit (MAX_NDN_PACKET_SIZE)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800485 */
486 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800487 put(const Data& data);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700488
Junxiao Shiedd834e2014-10-28 20:28:58 -0700489public: // IO routine
Jeff Thompson86507bc2013-08-23 20:51:38 -0700490 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700491 * @brief Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800492 *
493 * This call will block forever (default timeout == 0) to process IO on the face.
494 * To exit, one expected to call face.shutdown() from one of the callback methods.
495 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700496 * If positive timeout is specified, then processEvents will exit after this timeout, if
497 * not stopped earlier with face.shutdown() or when all active events finish. The call
498 * can be called repeatedly, if desired.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800499 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700500 * If negative timeout is specified, then processEvents will not block and process only
501 * pending events.
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800502 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700503 * @param timeout maximum time to block the thread
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700504 * @param keepThread Keep thread in a blocked state (in event processing), even when
505 * there are no outstanding events (e.g., no Interest/Data is expected)
506 *
507 * @throw This may throw an exception for reading data or in the callback for processing
508 * the data. If you call this from an main event loop, you may want to catch and
509 * log/disregard all exceptions.
Jeff Thompson432c8be2013-08-09 16:16:08 -0700510 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700511 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700512 processEvents(const time::milliseconds& timeout = time::milliseconds::zero(),
513 bool keepThread = false);
Jeff Thompson432c8be2013-08-09 16:16:08 -0700514
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700515 /**
516 * @brief Shutdown face operations
517 *
518 * This method cancels all pending operations and closes connection to NDN Forwarder.
519 *
520 * Note that this method does not stop IO service and if the same IO service is shared
521 * between multiple Faces or with other IO objects (e.g., Scheduler).
522 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700523 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700524 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800525
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700526 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700527 * @brief Get reference to IO service object
528 */
529 boost::asio::io_service&
530 getIoService()
531 {
Junxiao Shi2cced062014-11-02 21:27:38 -0700532 return m_ioService;
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700533 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800534
535private:
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700536
537 /**
538 * @throws ConfigFile::Error on parse error and unsupported protocols
539 */
540 void
Joao Pereira68c0d882015-05-19 14:27:55 -0400541 construct(KeyChain& keyChain);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700542
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600543 /**
544 * @throws Face::Error on unsupported protocol
Junxiao Shi2cced062014-11-02 21:27:38 -0700545 * @note shared_ptr is passed by value because ownership is transferred to this function
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600546 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800547 void
Joao Pereira68c0d882015-05-19 14:27:55 -0400548 construct(shared_ptr<Transport> transport, KeyChain& keyChain);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600549
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700550 class ProcessEventsTimeout
551 {
552 };
553
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700554 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800555 onReceiveElement(const Block& wire);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800556
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700557 void
558 asyncShutdown();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700559
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800560 static void
561 fireProcessEventsTimeout(const boost::system::error_code& error);
562
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700563private:
Junxiao Shi2cced062014-11-02 21:27:38 -0700564 /// the IO service owned by this Face, could be null
565 unique_ptr<boost::asio::io_service> m_internalIoService;
566 /// the IO service used by this Face
567 boost::asio::io_service& m_ioService;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700568
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800569 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800570
Junxiao Shiedd834e2014-10-28 20:28:58 -0700571 /** @brief if not null, a pointer to an internal KeyChain owned by Face
572 * @note if a KeyChain is supplied to constructor, this pointer will be null,
573 * and the passed KeyChain is given to nfdController;
574 * currently Face does not keep the KeyChain passed in constructor
575 * because it's not needed, but this may change in the future
576 */
Joao Pereira68c0d882015-05-19 14:27:55 -0400577 unique_ptr<KeyChain> m_internalKeyChain;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700578
Joao Pereira68c0d882015-05-19 14:27:55 -0400579 unique_ptr<nfd::Controller> m_nfdController;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600580
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700581 class Impl;
Joao Pereira68c0d882015-05-19 14:27:55 -0400582 unique_ptr<Impl> m_impl;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700583};
584
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800585} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700586
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800587#endif // NDN_FACE_HPP