blob: 6ef49e0353cecc66fd2df0780bf09d9099e0cbeb [file] [log] [blame]
Alexander Afanasyev2d600f72013-11-21 18:20:37 -08001.. _Face:
2
3Face Class
4==========
5
6:[C++]:
7 Namespace: ``ndn``
8
9:[Python]:
10 Module: ``pyndn``
11
Alexander Afanasyev06e798c2013-11-26 19:10:29 -080012Face Constructors
13-----------------
14
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080015Face Constructor (explicit Transport)
Alexander Afanasyev06e798c2013-11-26 19:10:29 -080016^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080017
18Create a new Face object with the given Transport to manage NDN communication.
19
20:[C++]:
21
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080022 .. code-block:: c++
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080023
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080024 Face(
25
26 const ptr_lib::shared_ptr<Transport>& transport
Alexander Afanasyevd2657342013-12-17 16:07:14 -080027 const ptr_lib::shared_ptr<const Transport::ConnectionInfo>&
28 connectionInfo
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080029
30 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080031
32:[JavaScript]:
33
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080034 .. code-block:: javascript
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080035
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080036 var Face = function Face(
37
38 [settings // associative array]
39
40 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080041
42:[Python]:
43
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080044 .. code-block:: python
45
46 def __init__(self)
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080047
48:Parameters:
49
50 - ``transport``
51 An object of a subclass of Transport to use for communication.
52
53 - ``connectionInfo``
54 This must be a ConnectionInfo from the same subclass of Transport as transport.
55
56 - ``settings``
57 (JavaScript only) An associative array with the following defaults:
58
59 .. code-block:: javascript
60
Alexander Afanasyevd2657342013-12-17 16:07:14 -080061 getTransport: function()
62 { return new WebSocketTransport(); },
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080063 getHostAndPort: transport.defaultGetHostAndPort,
Alexander Afanasyev57e20ed2013-11-26 19:14:19 -080064 // a function, on each call it returns
Alexander Afanasyevd2657342013-12-17 16:07:14 -080065 // a new { host: host, port: port }
66 // or null if there are no more hosts.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080067 host: null, // If null, use getHostAndPort when connecting.
68 port: 9696
69
70Face Constructor (default Transport)
Alexander Afanasyev06e798c2013-11-26 19:10:29 -080071^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080072
73Create a new Face object with optional settings to manage NDN communication.
74
75:[C++]:
76
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080077 .. code-block:: c++
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080078
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080079 Face(
80
81 [const char* host]
82 [, unsigned short port]
83
84 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080085
86:[JavaScript]:
87
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080088 .. code-block:: javascript
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080089
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080090 var Face = function Face(
91
92 [settings // associative array]
93
94 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080095
96:[Python]:
97
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080098 .. code-block:: python
99
100 def __init__(self)
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800101
102:Parameters:
103
104 - ``host``
105 (optional) The host to connect to. If omitted, use localhost with the default TcpTransport.
106
107 - ``port``
108 (optional) The port to connect to. If omitted, use 6363 with the default TcpTransport.
109
110 - ``settings``
111 (JavaScript only) (optional) An associative array with the following defaults:
112
113 .. code-block:: javascript
114
Alexander Afanasyevd2657342013-12-17 16:07:14 -0800115 getTransport: function()
116 { return new WebSocketTransport(); },
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800117 getHostAndPort: transport.defaultGetHostAndPort,
Alexander Afanasyev57e20ed2013-11-26 19:14:19 -0800118 // a function, on each call it returns a new
Alexander Afanasyevd2657342013-12-17 16:07:14 -0800119 // { host: host, port: port }
120 // or null if there are no more hosts.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800121 host: null, // If null, use getHostAndPort when connecting.
122 port: 9696
123
Alexander Afanasyev06e798c2013-11-26 19:10:29 -0800124Face.expressInterest Methods
125----------------------------
126
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800127Face.expressInterest Method (from Interest)
Alexander Afanasyev06e798c2013-11-26 19:10:29 -0800128^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800129
130Send the interest through the transport, read the entire response and call onData. If the interest times out according to interest lifetime, call onTimeout (if not omitted).
131
132C++ only: Your application must call processEvents.
133
134:[C++]:
135
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800136 .. code-block:: c++
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800137
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800138 unsigned int expressInterest(
139
140 const Interest& interest,
141 const OnData& onData,
142 [, const OnTimeout& onTimeout]
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800143
144 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800145
146:[JavaScript]:
147
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800148 .. code-block:: javascript
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800149
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800150 Face.prototype.expressInterest = function(
151
152 interest // Interest
153 onData, // function
154 [, onTimeout // function]
155
156 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800157
158:Parameters:
159
160 - ``interest``
161 The Interest to send which includes the interest lifetime for the timeout.
162
163 - ``onData``
164 When a matching data packet is received, this calls ``onData(interest, data)`` where:
165
166 - ``interest`` is the interest given to expressInterest.
167 - ``data`` is the received Data object.
168
169 - ``onTimeout``
170 (optional) If the interest times out according to the interest lifetime, this calls ``onTimeout(interest)`` where:
171
172 - ``interest`` is the interest given to expressInterest.
173
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800174:Returns:
175
176 The pending interest ID which can be used with removePendingInterest.
177
178Face.expressInterest Method (from Name)
Alexander Afanasyev06e798c2013-11-26 19:10:29 -0800179^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800180
181Encode name as an Interest, using the interestTemplate if supplied, send the interest through the transport, read the entire response and call onData. If the interest times out according to interest lifetime, call onTimeout (if not omitted).
182C++ only: Your application must call processEvents.
183
184:[C++]:
185
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800186 .. code-block:: c++
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800187
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800188 unsigned int expressInterest(
189
190 const Name& name,
191 [, const Interest* interestTemplate]
192 const OnData& onData,
193 [, const OnTimeout& onTimeout]
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800194
195 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800196
197:[JavaScript]:
198
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800199 .. code-block:: javascript
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800200
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800201 Face.prototype.expressInterest = function(
202
203 name, // Name
204 [, interestTemplate // Interest]
205 onData, // function
206 [, onTimeout // function]
207
208 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800209
210:[Python]:
211
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800212 .. code-block:: python
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800213
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800214 def expressInterest(self,
215
216 name # Name
217 closure # Closure
218 [, interestTemplate # Interest]
219
220 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800221
222:Parameters:
223
224 - ``name``
225 The Name for the interest.
226
227 - ``interestTemplate``
228 (optional) If not omitted, copy the interest selectors from this Interest. If omitted, use a default interest lifetime.
229
230 - ``onData``
231 When a matching data packet is received, this calls ``onData(interest, data)`` where:
232
233 - ``interest`` is the interest given to expressInterest.
234 - ``data`` is the received Data object.
235
236 - ``onTimeout``
237 (optional) If the interest times out according to the interest lifetime, this calls ``onTimeout(interest)`` where:
238
239 - ``interest`` is the interest given to expressInterest.
240
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800241:Returns:
242
243 The pending interest ID which can be used with removePendingInterest.
244
245Face.removePendingInterest Method
246---------------------------------
247
248Remove the pending interest entry with the pendingInterestId from the pending interest table. This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name. If there is no entry with the pendingInterestId, do nothing.
249
250:[C++]:
251
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800252 .. code-block:: c++
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800253
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800254 void removePendingInterest(
255
256 unsigned int pendingInterestId
257
258 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800259
260:Parameters:
261
262 - ``pendingInterestId``
263 The ID returned from expressInterest.
264
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800265.. _registerPrefix:
266
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800267Face.registerPrefix Method
268--------------------------
269
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800270Request NDN network to forward Interests for the specified :ref:`name prefix <Name>` towards the face.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800271
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800272.. note::
273
274 The current API is limited to registering the specified prefix only on a direclty connected NDN hub (e.g., local NDN daemon).
275
276.. note::
277
278 **C++ only**: Your application must call :ref:`processEvents`.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800279
280:[C++]:
281
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800282 .. code-block:: c++
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800283
284 void registerPrefix(
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800285
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800286 const Name &prefix,
287 const OnRegisterSucceed &onRegisterSucceed,
288 const onRegisterFailed &onRegisterFailed
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800289 [, ForwardingFlags flags]
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800290 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800291
292:[JavaScript]:
293
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800294 .. code-block:: javascript
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800295
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800296 Face.prototype.registerPrefix = function(
297
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800298 prefix, // Name
299 OnRegisterSucceed, // function
300 onRegisterFailed // function
301 [, flags] // ForwardingFlags
302
303 )
304
305:[Python]:
306
307 .. code-block:: python
308
309 def registerPrefix(self,
310
311 prefix, # Name
312 OnRegisterSucceed, # function
313 OnRegisterFailed # function
314 [, flags] # ForwardingFlags
315 )
316
317:Parameters:
318
319 - ``prefix``
320 The :ref:`Name prefix <Name>` to register in NDN network.
321
322 - ``onRegisterSucceed``
323 Callback that is fired when the prefix is successfully registered within the NDN network.
324 The prototype for the callback is ``onRegisterSucceed(prefix)``, where:
325 - ``prefix`` is the prefix given to registerPrefix.
326
327 - ``onRegisterFailed``
328 If failed to set Interest filter for any reason, this calls ``onRegisterFailed(prefix)`` where:
329 - ``prefix`` is the prefix given to registerPrefix.
330
331 - ``flags``
332 (optional) The flags for finer control of how and which Interests should be forwarded towards the face.
333 If omitted, use the default flags defined by the default :ref:`ForwardingFlags <ForwardingFlags>` constructor.
334
335.. _deregisterPrefix:
336
337Face.deregisterPrefix Method
338----------------------------
339
340Deregister the previously registered :ref:`prefix <Name>` from the NDN network.
341
342.. note::
343
344 The current API is limited to deregistering the specified prefix only on a direclty connected NDN hub (e.g., local NDN daemon).
345
346:[C++]:
347
348 .. code-block:: c++
349
350 void deregisterPrefix(
351
352 const Name &prefix
353 [, const onDeregisterSucceed &onDeregisterSucceed]
354 [, const onDeregisterFailed &onDeregisterFailed]
355 )
356
357:[JavaScript]:
358
359 .. code-block:: javascript
360
361 Face.prototype.deregisterPrefix = function(
362
363 prefix // Name
364 [, OnDeregisterSucceed] // function
365 [, OnDeregisterFailed] // function
366
367 )
368
369:[Python]:
370
371 .. code-block:: python
372
373 def deregisterPrefix(self,
374
375 prefix # Name
376 [, OnDeregisterSucceed] # function
377 [, OnDeregisterFailed] # function
378 )
379
380:Parameters:
381
382 - ``prefix``
383 The :ref:`Name prefix <Name>` to deregister in NDN network.
384
385 - ``onDeregisterSucceed``
386 (Optional) Callback that is fired when the prefix is successfully deregistered within the NDN network.
387 The prototype for the callback is ``onDeregisterSucceed(prefix)``, where:
388 - ``prefix`` is the prefix given to ``deregisterPrefix``.
389
390 - ``onRegisterFailed``
391 (Optional) If failed to set Interest filter for any reason, this calls ``onDeregisterFailed(prefix)`` where:
392 - ``prefix`` is the prefix given to ``deregisterPrefix``.
393
394.. _setInterestFilter:
395
396Face.setInterestFilter Method
397-----------------------------
398
399Register ``onInterest`` callback when an Interest mathing the :ref:`filter <InterestFilter>` is received on the face.
400
401
402:[C++]:
403
404 .. code-block:: c++
405
406 unsigned int setInterestFilter(
407
408 const InterestFilter& filter,
409 const OnInterest& onInterest,
410
411 );
412
413:[JavaScript]:
414
415 .. code-block:: javascript
416
417 Face.prototype.setInterestFilter = function(
418
419 filter, // InterestFilter
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800420 onInterest // function
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800421
422 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800423
424:[Python]:
425
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800426 .. code-block:: python
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800427
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800428 def setInterestFilter(self,
429
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800430 filter, # InterestFilter
431 onInterest # function
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800432
433 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800434
435:Parameters:
436
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800437 - ``filter``
438 The :ref:`InterestFilter <InterestFilter>` to match Interests.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800439
440 - ``onInterest``
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800441 When an interest is received which matches the name prefix, this calls ``onInterest(face, filter, interest)`` where:
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800442
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800443 - ``face`` is the :ref:`Face` on which the Interest is received.
444 An application can satisfy this Interest using :ref:`put`
445 - ``filter`` is the filter given to ``setInterestFilter``.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800446 - ``interest`` is the received interest.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800447
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800448:Returns:
449
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800450 The interest filter ID which can be used with :ref:`removeInterestFilter`.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800451
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800452.. _removeInterestFilter:
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800453
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800454Face.removeInterestFilter Method
455--------------------------------
456
457Remove the previously set Interest filter with the ``interestFilterId`` from the pending interest table. This does not affect any other interest filters with different IDs, even it if has the same prefix name. If there is no entry with the ``interestFilterId``, do nothing.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800458
459:[C++]:
460
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800461 .. code-block:: c++
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800462
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800463 void removeInterestFilter(
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800464
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800465 unsigned int interestFilterId
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800466
467 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800468
469:Parameters:
470
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800471 - ``interestFilterId``
472 The ID returned from :ref:`setInterestFilter`.
473
474.. _put:
475
476Face.put Method
477---------------
478
479Put (publish) the encoded and signed Data packet on a Face.
480
481This method essentially satisfies any pending Interest that matches the name of the published Data packet.
482If there are no outstanding Interests, directly connected NDN daemon (e.g., local NDN daemon) puts the Data packet into its packet buffer.
483
484:[C++]:
485
486 .. code-block:: c++
487
488 void put(
489
490 ptr_lib::shared_ptr<const Data> data
491
492 );
493
494:Parameters:
495
496 - ``data``
497 The encoded and signed Data packet
498
499
500.. _processEvents:
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800501
502Face.processEvents Method
503-------------------------
504
505C++ only: Process any data to receive and call data or timeout callbacks. This is non-blocking and will return immediately if there is no data to receive. You should repeatedly call this from an event loop, with calls to sleep as needed so that the loop doesn't use 100% of the CPU. Since processEvents modifies the pending interest table, your application should make sure that it calls processEvents in the same thread as expressInterest (which also modifies the pending interest table).
506
507:[C++]:
508
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800509 .. code-block:: c++
510
511 void processEvents();
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800512
513:Throw:
514
515 This may throw an exception for reading data or in the callback for processing the data. If you call this from an main event loop, you may want to catch and log/disregard all exceptions.
516