blob: 8fbef0cd8a95a16ccaa59988f120011e9f0726ac [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
27 const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo
28
29 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080030
31:[JavaScript]:
32
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080033 .. code-block:: javascript
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080034
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080035 var Face = function Face(
36
37 [settings // associative array]
38
39 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080040
41:[Python]:
42
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080043 .. code-block:: python
44
45 def __init__(self)
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080046
47:Parameters:
48
49 - ``transport``
50 An object of a subclass of Transport to use for communication.
51
52 - ``connectionInfo``
53 This must be a ConnectionInfo from the same subclass of Transport as transport.
54
55 - ``settings``
56 (JavaScript only) An associative array with the following defaults:
57
58 .. code-block:: javascript
59
60 getTransport: function() { return new WebSocketTransport(); },
61 getHostAndPort: transport.defaultGetHostAndPort,
Alexander Afanasyev57e20ed2013-11-26 19:14:19 -080062 // a function, on each call it returns
63 // a new { host: host, port: port } or null if there are no more hosts.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080064 host: null, // If null, use getHostAndPort when connecting.
65 port: 9696
66
67Face Constructor (default Transport)
Alexander Afanasyev06e798c2013-11-26 19:10:29 -080068^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080069
70Create a new Face object with optional settings to manage NDN communication.
71
72:[C++]:
73
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080074 .. code-block:: c++
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080075
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080076 Face(
77
78 [const char* host]
79 [, unsigned short port]
80
81 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080082
83:[JavaScript]:
84
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080085 .. code-block:: javascript
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080086
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080087 var Face = function Face(
88
89 [settings // associative array]
90
91 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080092
93:[Python]:
94
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -080095 .. code-block:: python
96
97 def __init__(self)
Alexander Afanasyev2d600f72013-11-21 18:20:37 -080098
99:Parameters:
100
101 - ``host``
102 (optional) The host to connect to. If omitted, use localhost with the default TcpTransport.
103
104 - ``port``
105 (optional) The port to connect to. If omitted, use 6363 with the default TcpTransport.
106
107 - ``settings``
108 (JavaScript only) (optional) An associative array with the following defaults:
109
110 .. code-block:: javascript
111
112 getTransport: function() { return new WebSocketTransport(); },
113 getHostAndPort: transport.defaultGetHostAndPort,
Alexander Afanasyev57e20ed2013-11-26 19:14:19 -0800114 // a function, on each call it returns a new
115 // { host: host, port: port } or null if there are no more hosts.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800116 host: null, // If null, use getHostAndPort when connecting.
117 port: 9696
118
Alexander Afanasyev06e798c2013-11-26 19:10:29 -0800119Face.expressInterest Methods
120----------------------------
121
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800122Face.expressInterest Method (from Interest)
Alexander Afanasyev06e798c2013-11-26 19:10:29 -0800123^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800124
125Send 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).
126
127C++ only: Your application must call processEvents.
128
129:[C++]:
130
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800131 .. code-block:: c++
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800132
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800133 unsigned int expressInterest(
134
135 const Interest& interest,
136 const OnData& onData,
137 [, const OnTimeout& onTimeout]
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800138
139 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800140
141:[JavaScript]:
142
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800143 .. code-block:: javascript
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800144
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800145 Face.prototype.expressInterest = function(
146
147 interest // Interest
148 onData, // function
149 [, onTimeout // function]
150
151 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800152
153:Parameters:
154
155 - ``interest``
156 The Interest to send which includes the interest lifetime for the timeout.
157
158 - ``onData``
159 When a matching data packet is received, this calls ``onData(interest, data)`` where:
160
161 - ``interest`` is the interest given to expressInterest.
162 - ``data`` is the received Data object.
163
164 - ``onTimeout``
165 (optional) If the interest times out according to the interest lifetime, this calls ``onTimeout(interest)`` where:
166
167 - ``interest`` is the interest given to expressInterest.
168
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800169:Returns:
170
171 The pending interest ID which can be used with removePendingInterest.
172
173Face.expressInterest Method (from Name)
Alexander Afanasyev06e798c2013-11-26 19:10:29 -0800174^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800175
176Encode 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).
177C++ only: Your application must call processEvents.
178
179:[C++]:
180
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800181 .. code-block:: c++
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800182
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800183 unsigned int expressInterest(
184
185 const Name& name,
186 [, const Interest* interestTemplate]
187 const OnData& onData,
188 [, const OnTimeout& onTimeout]
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800189
190 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800191
192:[JavaScript]:
193
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800194 .. code-block:: javascript
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800195
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800196 Face.prototype.expressInterest = function(
197
198 name, // Name
199 [, interestTemplate // Interest]
200 onData, // function
201 [, onTimeout // function]
202
203 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800204
205:[Python]:
206
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800207 .. code-block:: python
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800208
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800209 def expressInterest(self,
210
211 name # Name
212 closure # Closure
213 [, interestTemplate # Interest]
214
215 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800216
217:Parameters:
218
219 - ``name``
220 The Name for the interest.
221
222 - ``interestTemplate``
223 (optional) If not omitted, copy the interest selectors from this Interest. If omitted, use a default interest lifetime.
224
225 - ``onData``
226 When a matching data packet is received, this calls ``onData(interest, data)`` where:
227
228 - ``interest`` is the interest given to expressInterest.
229 - ``data`` is the received Data object.
230
231 - ``onTimeout``
232 (optional) If the interest times out according to the interest lifetime, this calls ``onTimeout(interest)`` where:
233
234 - ``interest`` is the interest given to expressInterest.
235
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800236:Returns:
237
238 The pending interest ID which can be used with removePendingInterest.
239
240Face.removePendingInterest Method
241---------------------------------
242
243Remove 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.
244
245:[C++]:
246
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800247 .. code-block:: c++
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800248
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800249 void removePendingInterest(
250
251 unsigned int pendingInterestId
252
253 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800254
255:Parameters:
256
257 - ``pendingInterestId``
258 The ID returned from expressInterest.
259
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800260.. _registerPrefix:
261
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800262Face.registerPrefix Method
263--------------------------
264
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800265Request NDN network to forward Interests for the specified :ref:`name prefix <Name>` towards the face.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800266
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800267.. note::
268
269 The current API is limited to registering the specified prefix only on a direclty connected NDN hub (e.g., local NDN daemon).
270
271.. note::
272
273 **C++ only**: Your application must call :ref:`processEvents`.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800274
275:[C++]:
276
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800277 .. code-block:: c++
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800278
279 void registerPrefix(
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800280
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800281 const Name &prefix,
282 const OnRegisterSucceed &onRegisterSucceed,
283 const onRegisterFailed &onRegisterFailed
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800284 [, ForwardingFlags flags]
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800285 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800286
287:[JavaScript]:
288
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800289 .. code-block:: javascript
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800290
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800291 Face.prototype.registerPrefix = function(
292
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800293 prefix, // Name
294 OnRegisterSucceed, // function
295 onRegisterFailed // function
296 [, flags] // ForwardingFlags
297
298 )
299
300:[Python]:
301
302 .. code-block:: python
303
304 def registerPrefix(self,
305
306 prefix, # Name
307 OnRegisterSucceed, # function
308 OnRegisterFailed # function
309 [, flags] # ForwardingFlags
310 )
311
312:Parameters:
313
314 - ``prefix``
315 The :ref:`Name prefix <Name>` to register in NDN network.
316
317 - ``onRegisterSucceed``
318 Callback that is fired when the prefix is successfully registered within the NDN network.
319 The prototype for the callback is ``onRegisterSucceed(prefix)``, where:
320 - ``prefix`` is the prefix given to registerPrefix.
321
322 - ``onRegisterFailed``
323 If failed to set Interest filter for any reason, this calls ``onRegisterFailed(prefix)`` where:
324 - ``prefix`` is the prefix given to registerPrefix.
325
326 - ``flags``
327 (optional) The flags for finer control of how and which Interests should be forwarded towards the face.
328 If omitted, use the default flags defined by the default :ref:`ForwardingFlags <ForwardingFlags>` constructor.
329
330.. _deregisterPrefix:
331
332Face.deregisterPrefix Method
333----------------------------
334
335Deregister the previously registered :ref:`prefix <Name>` from the NDN network.
336
337.. note::
338
339 The current API is limited to deregistering the specified prefix only on a direclty connected NDN hub (e.g., local NDN daemon).
340
341:[C++]:
342
343 .. code-block:: c++
344
345 void deregisterPrefix(
346
347 const Name &prefix
348 [, const onDeregisterSucceed &onDeregisterSucceed]
349 [, const onDeregisterFailed &onDeregisterFailed]
350 )
351
352:[JavaScript]:
353
354 .. code-block:: javascript
355
356 Face.prototype.deregisterPrefix = function(
357
358 prefix // Name
359 [, OnDeregisterSucceed] // function
360 [, OnDeregisterFailed] // function
361
362 )
363
364:[Python]:
365
366 .. code-block:: python
367
368 def deregisterPrefix(self,
369
370 prefix # Name
371 [, OnDeregisterSucceed] # function
372 [, OnDeregisterFailed] # function
373 )
374
375:Parameters:
376
377 - ``prefix``
378 The :ref:`Name prefix <Name>` to deregister in NDN network.
379
380 - ``onDeregisterSucceed``
381 (Optional) Callback that is fired when the prefix is successfully deregistered within the NDN network.
382 The prototype for the callback is ``onDeregisterSucceed(prefix)``, where:
383 - ``prefix`` is the prefix given to ``deregisterPrefix``.
384
385 - ``onRegisterFailed``
386 (Optional) If failed to set Interest filter for any reason, this calls ``onDeregisterFailed(prefix)`` where:
387 - ``prefix`` is the prefix given to ``deregisterPrefix``.
388
389.. _setInterestFilter:
390
391Face.setInterestFilter Method
392-----------------------------
393
394Register ``onInterest`` callback when an Interest mathing the :ref:`filter <InterestFilter>` is received on the face.
395
396
397:[C++]:
398
399 .. code-block:: c++
400
401 unsigned int setInterestFilter(
402
403 const InterestFilter& filter,
404 const OnInterest& onInterest,
405
406 );
407
408:[JavaScript]:
409
410 .. code-block:: javascript
411
412 Face.prototype.setInterestFilter = function(
413
414 filter, // InterestFilter
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800415 onInterest // function
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800416
417 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800418
419:[Python]:
420
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800421 .. code-block:: python
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800422
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800423 def setInterestFilter(self,
424
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800425 filter, # InterestFilter
426 onInterest # function
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800427
428 )
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800429
430:Parameters:
431
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800432 - ``filter``
433 The :ref:`InterestFilter <InterestFilter>` to match Interests.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800434
435 - ``onInterest``
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800436 When an interest is received which matches the name prefix, this calls ``onInterest(face, filter, interest)`` where:
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800437
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800438 - ``face`` is the :ref:`Face` on which the Interest is received.
439 An application can satisfy this Interest using :ref:`put`
440 - ``filter`` is the filter given to ``setInterestFilter``.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800441 - ``interest`` is the received interest.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800442
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800443:Returns:
444
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800445 The interest filter ID which can be used with :ref:`removeInterestFilter`.
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800446
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800447.. _removeInterestFilter:
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800448
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800449Face.removeInterestFilter Method
450--------------------------------
451
452Remove 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 -0800453
454:[C++]:
455
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800456 .. code-block:: c++
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800457
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800458 void removeInterestFilter(
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800459
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800460 unsigned int interestFilterId
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800461
462 );
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800463
464:Parameters:
465
Alexander Afanasyevce6daac2013-11-26 19:49:37 -0800466 - ``interestFilterId``
467 The ID returned from :ref:`setInterestFilter`.
468
469.. _put:
470
471Face.put Method
472---------------
473
474Put (publish) the encoded and signed Data packet on a Face.
475
476This method essentially satisfies any pending Interest that matches the name of the published Data packet.
477If there are no outstanding Interests, directly connected NDN daemon (e.g., local NDN daemon) puts the Data packet into its packet buffer.
478
479:[C++]:
480
481 .. code-block:: c++
482
483 void put(
484
485 ptr_lib::shared_ptr<const Data> data
486
487 );
488
489:Parameters:
490
491 - ``data``
492 The encoded and signed Data packet
493
494
495.. _processEvents:
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800496
497Face.processEvents Method
498-------------------------
499
500C++ 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).
501
502:[C++]:
503
Alexander Afanasyev1efe4a32013-11-21 18:46:34 -0800504 .. code-block:: c++
505
506 void processEvents();
Alexander Afanasyev2d600f72013-11-21 18:20:37 -0800507
508:Throw:
509
510 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.
511