blob: ab7bb7e9700829c874e6bfc250c4b42a5bf8de8c [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 Afanasyev9c578182014-05-14 17:28:28 -0700115 DEPRECATED(
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800116 explicit
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700117 Face(const shared_ptr<boost::asio::io_service>& ioService));
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800118
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -0700119 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700120 * @brief Create a new Face using the default transport (UnixTransport)
121 *
122 * @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 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800179 * @brief Express Interest
180 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700181 * @param interest An Interest to be expressed
182 * @param onData Callback to be called when a matching data packet is received
183 * @param onTimeout (optional) A function object to call if the interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800184 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700185 * @return The pending interest ID which can be used with removePendingInterest
Jeff Thompson4fe45512013-08-23 14:06:38 -0700186 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800187 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800188 expressInterest(const Interest& interest,
189 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Jeff Thompson4fe45512013-08-23 14:06:38 -0700190
191 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800192 * @brief Express Interest using name and Interest template
193 *
194 * @param name Name of the Interest
195 * @param tmpl Interest template to fill parameters
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700196 * @param onData Callback to be called when a matching data packet is received
197 * @param onTimeout (optional) A function object to call if the interest times out
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800198 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700199 * @return Opaque pending interest ID which can be used with removePendingInterest
Jeff Thompson7aec0252013-08-22 17:29:57 -0700200 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800201 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800202 expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800203 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800204 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700205
Jeff Thompson11095142013-10-01 16:20:28 -0700206 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700207 * @brief Cancel previously expressed Interest
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700208 *
Jeff Thompson11095142013-10-01 16:20:28 -0700209 * @param pendingInterestId The ID returned from expressInterest.
210 */
211 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800212 removePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700213
Jeff Thompson432c8be2013-08-09 16:16:08 -0700214 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700215 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
216 * callback and register the filtered prefix with the connected NDN forwarder
217 *
218 * This version of setInterestFilter combines setInterestFilter and registerPrefix
219 * operations and is intended to be used when only one filter for the same prefix needed
220 * to be set. When multiple names sharing the same prefix should be dispatched to
221 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
222 * a series of setInterestFilter calls.
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700223 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000224 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700225 * @param onInterest A callback to be called when a matching interest is received
226 * @param onSuccess A callback to be called when prefixRegister command succeeds
227 * @param onFailure A callback to be called when prefixRegister command fails
228 * @param certificate (optional) A certificate under which the prefix registration
229 * command interest is signed. When omitted, a default certificate
230 * of the default identity is used to sign the registration command
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700231 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700232 * @return Opaque registered prefix ID which can be used with unsetInterestFilter or
233 * removeRegisteredPrefix
Jeff Thompson86507bc2013-08-23 20:51:38 -0700234 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800235 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000236 setInterestFilter(const InterestFilter& interestFilter,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800237 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700238 const RegisterPrefixSuccessCallback& onSuccess,
239 const RegisterPrefixFailureCallback& onFailure,
240 const IdentityCertificate& certificate = IdentityCertificate());
Jeff Thompson11095142013-10-01 16:20:28 -0700241
242 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700243 * @deprecated Use the other overload
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700244 */
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700245 DEPRECATED(
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700246 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000247 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700248 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700249 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700250 const IdentityCertificate& certificate = IdentityCertificate()));
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700251
252 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700253 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
254 * callback and register the filtered prefix with the connected NDN forwarder
255 *
256 * This version of setInterestFilter combines setInterestFilter and registerPrefix
257 * operations and is intended to be used when only one filter for the same prefix needed
258 * to be set. When multiple names sharing the same prefix should be dispatched to
259 * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by
260 * a series of setInterestFilter calls.
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700261 *
Alexander Afanasyev90164962014-03-06 08:29:59 +0000262 * @param interestFilter Interest filter (prefix part will be registered with the forwarder)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700263 * @param onInterest A callback to be called when a matching interest is received
264 * @param onSuccess A callback to be called when prefixRegister command succeeds
265 * @param onFailure A callback to be called when prefixRegister command fails
266 * @param identity A signing identity. A command interest is signed under the default
267 * certificate of this identity
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700268 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700269 * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700270 */
271 const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000272 setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700273 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700274 const RegisterPrefixSuccessCallback& onSuccess,
275 const RegisterPrefixFailureCallback& onFailure,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700276 const Name& identity);
277
278 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700279 * @deprecated Use the other overload
280 */
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700281 DEPRECATED(
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700282 const RegisteredPrefixId*
283 setInterestFilter(const InterestFilter& interestFilter,
284 const OnInterest& onInterest,
285 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700286 const Name& identity));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700287
288 /**
289 * @brief Set InterestFilter to dispatch incoming matching interest to onInterest callback
290 *
291 * @param interestFilter Interest
292 * @param onInterest A callback to be called when a matching interest is received
293 *
294 * This method modifies library's FIB only, and does not register the prefix with the
295 * forwarder. It will always succeed. To register prefix with the forwarder, use
296 * registerPrefix, or use the setInterestFilter overload taking two callbacks.
297 *
298 * @return Opaque interest filter ID which can be used with unsetInterestFilter
299 */
300 const InterestFilterId*
301 setInterestFilter(const InterestFilter& interestFilter,
302 const OnInterest& onInterest);
303
304
305 /**
306 * @brief Register prefix with the connected NDN forwarder
307 *
308 * This method only modifies forwarder's RIB (or FIB) and does not associate any
309 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
310 * the right callbacks.
311 *
312 * @param prefix A prefix to register with the connected NDN forwarder
313 * @param onSuccess A callback to be called when prefixRegister command succeeds
314 * @param onFailure A callback to be called when prefixRegister command fails
315 * @param certificate (optional) A certificate under which the prefix registration
316 * command interest is signed. When omitted, a default certificate
317 * of the default identity is used to sign the registration command
318 *
319 * @return The registered prefix ID which can be used with unregisterPrefix
320 */
321 const RegisteredPrefixId*
322 registerPrefix(const Name& prefix,
323 const RegisterPrefixSuccessCallback& onSuccess,
324 const RegisterPrefixFailureCallback& onFailure,
325 const IdentityCertificate& certificate = IdentityCertificate());
326
327 /**
328 * @brief Register prefix with the connected NDN forwarder and call onInterest when a matching
329 * interest is received.
330 *
331 * This method only modifies forwarder's RIB (or FIB) and does not associate any
332 * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to
333 * the right callbacks.
334 *
335 * @param prefix A prefix to register with the connected NDN forwarder
336 * @param onSuccess A callback to be called when prefixRegister command succeeds
337 * @param onFailure A callback to be called when prefixRegister command fails
338 * @param identity A signing identity. A command interest is signed under the default
339 * certificate of this identity
340 *
341 * @return The registered prefix ID which can be used with unregisterPrefix
342 */
343 const RegisteredPrefixId*
344 registerPrefix(const Name& prefix,
345 const RegisterPrefixSuccessCallback& onSuccess,
346 const RegisterPrefixFailureCallback& onFailure,
347 const Name& identity);
348
349
350 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700351 * @brief Remove the registered prefix entry with the registeredPrefixId from the
352 * pending interest table.
353 *
354 * This does not affect another registered prefix with a different registeredPrefixId,
355 * even it if has the same prefix name. If there is no entry with the
356 * registeredPrefixId, do nothing.
357 *
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700358 * unsetInterestFilter will use the same credentials as original
359 * setInterestFilter/registerPrefix command
360 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700361 * @param registeredPrefixId The ID returned from registerPrefix
Jeff Thompson11095142013-10-01 16:20:28 -0700362 */
363 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800364 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800365
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700366 /**
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700367 * @brief Remove previously set InterestFilter from library's FIB
368 *
369 * This method always succeeds and will **NOT** send any request to the connected
370 * forwarder.
371 *
372 * @param interestFilterId The ID returned from setInterestFilter.
373 */
374 void
375 unsetInterestFilter(const InterestFilterId* interestFilterId);
376
377 /**
378 * @brief Deregister prefix from RIB (or FIB)
379 *
380 * unregisterPrefix will use the same credentials as original
381 * setInterestFilter/registerPrefix command
382 *
383 * If registeredPrefixId was obtained using setInterestFilter, the corresponding
384 * InterestFilter will be unset too.
385 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700386 * @param registeredPrefixId The ID returned from registerPrefix
387 * @param onSuccess Callback to be called when operation succeeds
388 * @param onFailure Callback to be called when operation fails
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700389 */
390 void
391 unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
392 const UnregisterPrefixSuccessCallback& onSuccess,
393 const UnregisterPrefixFailureCallback& onFailure);
394
395 /**
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700396 * @brief (FOR DEBUG PURPOSES ONLY) Request direct NFD FIB management
397 */
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700398 void
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700399 setDirectFibManagement(bool isDirectFibManagementRequested = false);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700400
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800401 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800402 * @brief Publish data packet
403 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700404 * This method can be called to satisfy the incoming Interest or to put Data packet into
405 * the cache of the local NDN forwarder
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800406 */
407 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800408 put(const Data& data);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700409
Jeff Thompson86507bc2013-08-23 20:51:38 -0700410 /**
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700411 * @brief Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800412 *
413 * This call will block forever (default timeout == 0) to process IO on the face.
414 * To exit, one expected to call face.shutdown() from one of the callback methods.
415 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700416 * If positive timeout is specified, then processEvents will exit after this timeout, if
417 * not stopped earlier with face.shutdown() or when all active events finish. The call
418 * can be called repeatedly, if desired.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800419 *
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700420 * If negative timeout is specified, then processEvents will not block and process only
421 * pending events.
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800422 *
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700423 * @param timeout maximum time to block the thread
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700424 * @param keepThread Keep thread in a blocked state (in event processing), even when
425 * there are no outstanding events (e.g., no Interest/Data is expected)
426 *
427 * @throw This may throw an exception for reading data or in the callback for processing
428 * the data. If you call this from an main event loop, you may want to catch and
429 * log/disregard all exceptions.
Jeff Thompson432c8be2013-08-09 16:16:08 -0700430 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700431 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700432 processEvents(const time::milliseconds& timeout = time::milliseconds::zero(),
433 bool keepThread = false);
Jeff Thompson432c8be2013-08-09 16:16:08 -0700434
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700435 /**
436 * @brief Shutdown face operations
437 *
438 * This method cancels all pending operations and closes connection to NDN Forwarder.
439 *
440 * Note that this method does not stop IO service and if the same IO service is shared
441 * between multiple Faces or with other IO objects (e.g., Scheduler).
442 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700443 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700444 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800445
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700446 /**
447 * @brief Get shared_ptr of the IO service object
448 *
449 * @deprecated Use getIoService instead
450 */
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700451 DEPRECATED(
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700452 shared_ptr<boost::asio::io_service>
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700453 ioService())
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700454 {
455 return m_ioService;
456 }
457
458 /**
459 * @brief Get reference to IO service object
460 */
461 boost::asio::io_service&
462 getIoService()
463 {
464 return *m_ioService;
465 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800466
467private:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600468 /**
469 * @throws Face::Error on unsupported protocol
470 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800471 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800472 construct(const shared_ptr<Transport>& transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -0800473 const shared_ptr<boost::asio::io_service>& ioService);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600474
475 bool
476 isSupportedNfdProtocol(const std::string& protocol);
477
478 bool
479 isSupportedNrdProtocol(const std::string& protocol);
480
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700481 class ProcessEventsTimeout
482 {
483 };
484
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700485 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800486 onReceiveElement(const Block& wire);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800487
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700488 void
489 asyncShutdown();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700490
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800491 static void
492 fireProcessEventsTimeout(const boost::system::error_code& error);
493
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700494private:
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800495 shared_ptr<boost::asio::io_service> m_ioService;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700496
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800497 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800498
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700499 shared_ptr<nfd::Controller> m_nfdController;
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700500 bool m_isDirectNfdFibManagementRequested;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600501
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700502 class Impl;
503 shared_ptr<Impl> m_impl;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700504};
505
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600506inline bool
507Face::isSupportedNfdProtocol(const std::string& protocol)
508{
509 return protocol == "nfd-0.1";
510}
511
512inline bool
513Face::isSupportedNrdProtocol(const std::string& protocol)
514{
515 return protocol == "nrd-0.1";
516}
517
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700518inline void
519Face::setDirectFibManagement(bool isDirectFibManagementRequested/* = false*/)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600520{
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700521 m_isDirectNfdFibManagementRequested = isDirectFibManagementRequested;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600522}
523
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800524} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700525
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800526#endif // NDN_FACE_HPP