blob: 139ff2819dd6992b1a48dc71e64088c15907b938 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070013 */
14
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -070015#ifndef NDN_FACE_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -070016#define NDN_FACE_HPP
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070017
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080018#include "common.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070019
20#include "name.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080021#include "interest.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070022#include "interest-filter.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080023#include "data.hpp"
Alexander Afanasyev984ad192014-05-02 19:11:15 -070024#include "security/identity-certificate.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080025
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070026namespace boost {
27namespace asio {
28class io_service;
29}
30}
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080031
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070032namespace ndn {
33
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070034class Transport;
35
36class PendingInterestId;
37class RegisteredPrefixId;
38class InterestFilterId;
39
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070040namespace nfd {
41class Controller;
42}
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070043
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070044/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070045 * @brief Callback called when expressed Interest gets satisfied with Data packet
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080046 */
47typedef function<void(const Interest&, Data&)> OnData;
48
49/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070050 * @brief Callback called when expressed Interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080051 */
52typedef function<void(const Interest&)> OnTimeout;
53
54/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070055 * @brief Callback called when incoming Interest matches the specified InterestFilter
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080056 */
Alexander Afanasyev90164962014-03-06 08:29:59 +000057typedef function<void (const InterestFilter&, const Interest&)> OnInterest;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080058
59/**
Alexander Afanasyev984ad192014-05-02 19:11:15 -070060 * @brief Callback called when registerPrefix or setInterestFilter command succeeds
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080061 */
Alexander Afanasyev984ad192014-05-02 19:11:15 -070062typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080063
Alexander Afanasyev984ad192014-05-02 19:11:15 -070064/**
65 * @brief Callback called when registerPrefix or setInterestFilter command fails
66 */
67typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
68
69/**
70 * @brief Callback called when unregisterPrefix or unsetInterestFilter command succeeds
71 */
72typedef function<void()> UnregisterPrefixSuccessCallback;
73
74/**
75 * @brief Callback called when unregisterPrefix or unsetInterestFilter command fails
76 */
77typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080078
79
80/**
81 * @brief Abstraction to communicate with local or remote NDN forwarder
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070082 */
Alexander Afanasyev8460afb2014-02-15 20:31:42 -080083class Face : noncopyable
84{
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070085public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070086 class Error : public std::runtime_error
87 {
88 public:
89 explicit
90 Error(const std::string& what)
91 : std::runtime_error(what)
92 {
93 }
94 };
Alexander Afanasyevb790d952014-01-24 12:07:53 -080095
96 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070097 * @brief Create a new Face using the default transport (UnixTransport)
98 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060099 * @throws ConfigFile::Error on configuration file parse failure
100 * @throws Face::Error on unsupported protocol
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800101 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800102 Face();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800103
104 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700105 * @brief Create a new Face using the default transport (UnixTransport)
106 *
107 * @deprecated This constructor is deprecated. Use `Face(boost::asio::io_service&)`
108 * instead.
109 *
110 * @param ioService A shared pointer to boost::io_service object that should control all
111 * IO operations
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600112 * @throws ConfigFile::Error on configuration file parse failure
113 * @throws Face::Error on unsupported protocol
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700114 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800115 explicit
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800116 Face(const shared_ptr<boost::asio::io_service>& ioService);
117
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700118 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700119 * @brief Create a new Face using the default transport (UnixTransport)
120 *
121 * @par Usage examples:
122 *
123 * Face face1;
124 * Face face2(face1.getIoService());
125 *
126 * // Now the following ensures that events on both faces are processed
127 * face1.processEvents();
128 * // or face1.getIoService().run();
129 *
130 * @par or
131 *
132 * boost::asio::io_service ioService;
133 * Face face1(ioService);
134 * Face face2(ioService);
135 * ...
136 *
137 * ioService.run();
138 *
139 * @param ioService A reference to boost::io_service object that should control all
140 * IO operations.
141 * @throws ConfigFile::Error on configuration file parse failure
142 * @throws Face::Error on unsupported protocol
143 */
144 explicit
145 Face(boost::asio::io_service& ioService);
146
147 /**
148 * @brief Create a new Face using TcpTransport
149 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700150 * @param host The host of the NDN forwarder
151 * @param port (optional) The port or service name of the NDN forwarder (**default**: "6363")
152 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600153 * @throws Face::Error on unsupported protocol
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700154 */
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800155 Face(const std::string& host, const std::string& port = "6363");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800156
157 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700158 * @brief Create a new Face using the given Transport
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700159 *
160 * @param transport A shared_ptr to a Transport object used for communication
161 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600162 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800163 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800164 explicit
165 Face(const shared_ptr<Transport>& transport);
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 and IO service object
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800169 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700170 * @sa Face(boost::asio::io_service&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800171 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600172 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800173 */
174 Face(const shared_ptr<Transport>& transport,
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700175 boost::asio::io_service& ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800176
Jeff Thompson4fe45512013-08-23 14:06:38 -0700177 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800178 * @brief Express Interest
179 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700180 * @param interest An Interest to be expressed
181 * @param onData Callback to be called when a matching data packet is received
182 * @param onTimeout (optional) A function object to call if the interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800183 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700184 * @return The pending interest ID which can be used with removePendingInterest
Jeff Thompson4fe45512013-08-23 14:06:38 -0700185 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800186 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800187 expressInterest(const Interest& interest,
188 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Jeff Thompson4fe45512013-08-23 14:06:38 -0700189
190 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800191 * @brief Express Interest using name and Interest template
192 *
193 * @param name Name of the Interest
194 * @param tmpl Interest template to fill parameters
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700195 * @param onData Callback to be called when a matching data packet is received
196 * @param onTimeout (optional) A function object to call if the interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800197 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700198 * @return Opaque pending interest ID which can be used with removePendingInterest
Jeff Thompson7aec0252013-08-22 17:29:57 -0700199 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800200 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800201 expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800202 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800203 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700204
Jeff Thompson11095142013-10-01 16:20:28 -0700205 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700206 * @brief Cancel previously expressed Interest
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700207 *
Jeff Thompson11095142013-10-01 16:20:28 -0700208 * @param pendingInterestId The ID returned from expressInterest.
209 */
210 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800211 removePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700212
Jeff Thompson432c8be2013-08-09 16:16:08 -0700213 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700214 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
215 * callback and register the filtered prefix with the connected NDN forwarder
216 *
217 * This version of setInterestFilter combines setInterestFilter and registerPrefix
218 * operations and is intended to be used when only one filter for the same prefix needed
219 * to be set. When multiple names sharing the same prefix should be dispatched to
220 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
221 * a series of setInterestFilter calls.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700222 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000223 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700224 * @param onInterest A callback to be called when a matching interest is received
225 * @param onSuccess A callback to be called when prefixRegister command succeeds
226 * @param onFailure A callback to be called when prefixRegister command fails
227 * @param certificate (optional) A certificate under which the prefix registration
228 * command interest is signed. When omitted, a default certificate
229 * of the default identity is used to sign the registration command
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700230 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700231 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
232 * removeRegisteredPrefix
Jeff Thompson86507bc2013-08-23 20:51:38 -0700233 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800234 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000235 setInterestFilter(const InterestFilter& interestFilter,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800236 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700237 const RegisterPrefixSuccessCallback& onSuccess,
238 const RegisterPrefixFailureCallback& onFailure,
239 const IdentityCertificate& certificate = IdentityCertificate());
Jeff Thompson11095142013-10-01 16:20:28 -0700240
241 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700242 * @deprecated Use the other overload
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700243 */
244 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000245 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700246 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700247 const RegisterPrefixFailureCallback& onFailure,
248 const IdentityCertificate& certificate = IdentityCertificate());
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700249
250 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700251 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
252 * callback and register the filtered prefix with the connected NDN forwarder
253 *
254 * This version of setInterestFilter combines setInterestFilter and registerPrefix
255 * operations and is intended to be used when only one filter for the same prefix needed
256 * to be set. When multiple names sharing the same prefix should be dispatched to
257 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
258 * a series of setInterestFilter calls.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700259 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000260 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700261 * @param onInterest A callback to be called when a matching interest is received
262 * @param onSuccess A callback to be called when prefixRegister command succeeds
263 * @param onFailure A callback to be called when prefixRegister command fails
264 * @param identity A signing identity. A command interest is signed under the default
265 * certificate of this identity
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700266 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700267 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700268 */
269 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000270 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700271 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700272 const RegisterPrefixSuccessCallback& onSuccess,
273 const RegisterPrefixFailureCallback& onFailure,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700274 const Name& identity);
275
276 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700277 * @deprecated Use the other overload
278 */
279 const RegisteredPrefixId*
280 setInterestFilter(const InterestFilter& interestFilter,
281 const OnInterest& onInterest,
282 const RegisterPrefixFailureCallback& onFailure,
283 const Name& identity);
284
285 /**
286 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest callback
287 *
288 * @param interestFilter Interest
289 * @param onInterest A callback to be called when a matching interest is received
290 *
291 * This method modifies library's FIB only, and does not register the prefix with the
292 * forwarder. It will always succeed. To register prefix with the forwarder, use
293 * registerPrefix, or use the setInterestFilter overload taking two callbacks.
294 *
295 * @return Opaque interest filter ID which can be used with unsetInterestFilter
296 */
297 const InterestFilterId*
298 setInterestFilter(const InterestFilter& interestFilter,
299 const OnInterest& onInterest);
300
301
302 /**
303 * @brief Register prefix with the connected NDN forwarder
304 *
305 * This method only modifies forwarder's RIB (or FIB) and does not associate any
306 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
307 * the right callbacks.
308 *
309 * @param prefix A prefix to register with the connected NDN forwarder
310 * @param onSuccess A callback to be called when prefixRegister command succeeds
311 * @param onFailure A callback to be called when prefixRegister command fails
312 * @param certificate (optional) A certificate under which the prefix registration
313 * command interest is signed. When omitted, a default certificate
314 * of the default identity is used to sign the registration command
315 *
316 * @return The registered prefix ID which can be used with unregisterPrefix
317 */
318 const RegisteredPrefixId*
319 registerPrefix(const Name& prefix,
320 const RegisterPrefixSuccessCallback& onSuccess,
321 const RegisterPrefixFailureCallback& onFailure,
322 const IdentityCertificate& certificate = IdentityCertificate());
323
324 /**
325 * @brief Register prefix with the connected NDN forwarder and call onInterest when a matching
326 * interest is received.
327 *
328 * This method only modifies forwarder's RIB (or FIB) and does not associate any
329 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
330 * the right callbacks.
331 *
332 * @param prefix A prefix to register with the connected NDN forwarder
333 * @param onSuccess A callback to be called when prefixRegister command succeeds
334 * @param onFailure A callback to be called when prefixRegister command fails
335 * @param identity A signing identity. A command interest is signed under the default
336 * certificate of this identity
337 *
338 * @return The registered prefix ID which can be used with unregisterPrefix
339 */
340 const RegisteredPrefixId*
341 registerPrefix(const Name& prefix,
342 const RegisterPrefixSuccessCallback& onSuccess,
343 const RegisterPrefixFailureCallback& onFailure,
344 const Name& identity);
345
346
347 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700348 * @brief Remove the registered prefix entry with the registeredPrefixId from the
349 * pending interest table.
350 *
351 * This does not affect another registered prefix with a different registeredPrefixId,
352 * even it if has the same prefix name. If there is no entry with the
353 * registeredPrefixId, do nothing.
354 *
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700355 * unsetInterestFilter will use the same credentials as original
356 * setInterestFilter/registerPrefix command
357 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700358 * @param registeredPrefixId The ID returned from registerPrefix
Jeff Thompson11095142013-10-01 16:20:28 -0700359 */
360 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800361 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800362
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700363 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700364 * @brief Remove previously set InterestFilter from library's FIB
365 *
366 * This method always succeeds and will **NOT** send any request to the connected
367 * forwarder.
368 *
369 * @param interestFilterId The ID returned from setInterestFilter.
370 */
371 void
372 unsetInterestFilter(const InterestFilterId* interestFilterId);
373
374 /**
375 * @brief Deregister prefix from RIB (or FIB)
376 *
377 * unregisterPrefix will use the same credentials as original
378 * setInterestFilter/registerPrefix command
379 *
380 * If registeredPrefixId was obtained using setInterestFilter, the corresponding
381 * InterestFilter will be unset too.
382 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700383 * @param registeredPrefixId The ID returned from registerPrefix
384 * @param onSuccess Callback to be called when operation succeeds
385 * @param onFailure Callback to be called when operation fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700386 */
387 void
388 unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
389 const UnregisterPrefixSuccessCallback& onSuccess,
390 const UnregisterPrefixFailureCallback& onFailure);
391
392 /**
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700393 * @brief (FOR DEBUG PURPOSES ONLY) Request direct NFD FIB management
394 */
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700395 void
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700396 setDirectFibManagement(bool isDirectFibManagementRequested = false);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700397
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800398 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800399 * @brief Publish data packet
400 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700401 * This method can be called to satisfy the incoming Interest or to put Data packet into
402 * the cache of the local NDN forwarder
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800403 */
404 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800405 put(const Data& data);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700406
Jeff Thompson86507bc2013-08-23 20:51:38 -0700407 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700408 * @brief Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800409 *
410 * This call will block forever (default timeout == 0) to process IO on the face.
411 * To exit, one expected to call face.shutdown() from one of the callback methods.
412 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700413 * If positive timeout is specified, then processEvents will exit after this timeout, if
414 * not stopped earlier with face.shutdown() or when all active events finish. The call
415 * can be called repeatedly, if desired.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800416 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700417 * If negative timeout is specified, then processEvents will not block and process only
418 * pending events.
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800419 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700420 * @param timeout maximum time to block the thread
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700421 * @param keepThread Keep thread in a blocked state (in event processing), even when
422 * there are no outstanding events (e.g., no Interest/Data is expected)
423 *
424 * @throw This may throw an exception for reading data or in the callback for processing
425 * the data. If you call this from an main event loop, you may want to catch and
426 * log/disregard all exceptions.
Jeff Thompson432c8be2013-08-09 16:16:08 -0700427 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700428 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700429 processEvents(const time::milliseconds& timeout = time::milliseconds::zero(),
430 bool keepThread = false);
Jeff Thompson432c8be2013-08-09 16:16:08 -0700431
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700432 /**
433 * @brief Shutdown face operations
434 *
435 * This method cancels all pending operations and closes connection to NDN Forwarder.
436 *
437 * Note that this method does not stop IO service and if the same IO service is shared
438 * between multiple Faces or with other IO objects (e.g., Scheduler).
439 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700440 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700441 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800442
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700443 /**
444 * @brief Get shared_ptr of the IO service object
445 *
446 * @deprecated Use getIoService instead
447 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700448 shared_ptr<boost::asio::io_service>
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700449 ioService()
450 {
451 return m_ioService;
452 }
453
454 /**
455 * @brief Get reference to IO service object
456 */
457 boost::asio::io_service&
458 getIoService()
459 {
460 return *m_ioService;
461 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800462
463private:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600464 /**
465 * @throws Face::Error on unsupported protocol
466 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800467 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800468 construct(const shared_ptr<Transport>& transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -0800469 const shared_ptr<boost::asio::io_service>& ioService);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600470
471 bool
472 isSupportedNfdProtocol(const std::string& protocol);
473
474 bool
475 isSupportedNrdProtocol(const std::string& protocol);
476
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700477 class ProcessEventsTimeout
478 {
479 };
480
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700481 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800482 onReceiveElement(const Block& wire);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800483
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700484 void
485 asyncShutdown();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700486
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800487 static void
488 fireProcessEventsTimeout(const boost::system::error_code& error);
489
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700490private:
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800491 shared_ptr<boost::asio::io_service> m_ioService;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700492
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800493 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800494
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700495 shared_ptr<nfd::Controller> m_nfdController;
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700496 bool m_isDirectNfdFibManagementRequested;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600497
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700498 class Impl;
499 shared_ptr<Impl> m_impl;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700500};
501
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600502inline bool
503Face::isSupportedNfdProtocol(const std::string& protocol)
504{
505 return protocol == "nfd-0.1";
506}
507
508inline bool
509Face::isSupportedNrdProtocol(const std::string& protocol)
510{
511 return protocol == "nrd-0.1";
512}
513
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700514inline void
515Face::setDirectFibManagement(bool isDirectFibManagementRequested/* = false*/)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600516{
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700517 m_isDirectNfdFibManagementRequested = isDirectFibManagementRequested;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600518}
519
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800520} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700521
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800522#endif // NDN_FACE_HPP