blob: 7f0b2b4cccdd21b99ee5e9485b2fd6bd15f1b083 [file] [log] [blame]
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -07001Trivial applications
2====================
Alexander Afanasyev151a8552014-04-11 00:54:43 -07003
Yingdi Yu4e99f532014-08-25 19:40:57 -07004.. note::
5
6 To successfully run the following examples, please make sure that NFD is properly
7 configured and running. For more information about NFD, refer to `NFD's official
Davide Pesavento933a5672020-07-03 22:32:43 -04008 homepage <https://named-data.net/doc/NFD/>`_.
Yingdi Yu4e99f532014-08-25 19:40:57 -07009
Alexander Afanasyev151a8552014-04-11 00:54:43 -070010Trivial consumer
11----------------
12
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070013In the following trivial example, a consumer creates a :ndn-cxx:`Face` with default
14transport (:ndn-cxx:`UnixTransport`) and sends an Interest for
Weiwei Liuab9aad02016-10-09 13:56:04 -070015``/localhost/testApp/randomData``. While expressing Interest, the app specifies three
16callbacks to be called when Data/Nack is retrieved or Interest times out.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070017
Alexander Afanasyev151a8552014-04-11 00:54:43 -070018.. literalinclude:: ../examples/consumer.cpp
19 :language: c++
20 :linenos:
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040021 :emphasize-lines: 43-49,52-55,63,77,83
Alexander Afanasyev151a8552014-04-11 00:54:43 -070022
23Trivial producer
24----------------
25
26The following example demonstrates how to write a simple producer application.
27
Ivan Yeoba1a4a92015-01-11 00:45:57 -080028First, the application sets an Interest filter for ``/localhost/testApp`` to receive all
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070029Interests that have this prefix. The :ndn-cxx:`Face::setInterestFilter` call accepts two
30callbacks; the first will be called when an Interest is received and the second if prefix
31registration fails.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070032
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070033After an Interest is received, the producer creates a Data packet with the same name as
34the received Interest, adds content, and signs it with the system-default identity. It is
35also possible to specify a particular key to be used during the signing. For more
36information, refer to :ndn-cxx:`KeyChain API documentation <KeyChain>`.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070037
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070038Finally, after Data packet has been created and signed, it is returned to the requester
39using :ndn-cxx:`Face::put` method.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070040
41.. literalinclude:: ../examples/producer.cpp
42 :language: c++
43 :linenos:
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040044 :emphasize-lines: 39-42,55,62-64,79,87,91
Alexander Afanasyev151a8552014-04-11 00:54:43 -070045
Yingdi Yu55ea01a2015-07-21 22:42:17 -070046Consumer that uses Scheduler
47----------------------------
Alexander Afanasyev151a8552014-04-11 00:54:43 -070048
Yingdi Yu55ea01a2015-07-21 22:42:17 -070049The following example demonstrates how to use :ndn-cxx:`Scheduler` to schedule arbitrary
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070050events for execution at specific points of time.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070051
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070052The library internally uses `boost::asio::io_service
Davide Pesavento78338c52020-04-20 23:00:02 -040053<https://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/reference/io_service.html>`_ to
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070054implement fully asynchronous NDN operations (i.e., sending and receiving Interests and
55Data). In addition to network-related operations, ``boost::asio::io_service`` can be used
56to execute any arbitrary callback within the processing thread (run either explicitly via
Davide Pesavento78338c52020-04-20 23:00:02 -040057``io_service::run()`` or implicitly via :ndn-cxx:`Face::processEvents` as in previous
58examples). :ndn-cxx:`Scheduler` is just a wrapper on top of ``io_service``, providing a
59simple interface to schedule tasks at specific times.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070060
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070061The highlighted lines in the example demonstrate all that is needed to express a second
Davide Pesavento78338c52020-04-20 23:00:02 -040062Interest approximately 3 seconds after the first one.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070063
64.. literalinclude:: ../examples/consumer-with-timer.cpp
65 :language: c++
66 :linenos:
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040067 :emphasize-lines: 37-38,54-57,60,91,104-107,112-114