blob: cdc2df2074c00ec0a01bd96586e684e17eec568b [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 Pesavento02ed3322023-02-23 19:40:22 -05008 homepage <https://docs.named-data.net/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:
Davide Pesaventoaee2ada2022-02-18 14:43:02 -050021 :emphasize-lines: 43-48,51-54,62,76,82
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:
Davide Pesavento296c3a12023-05-04 21:40:40 -040044 :emphasize-lines: 39-42,55,60-63,78,86,90
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
Davide Pesavento2f46d652023-11-09 23:40:01 -050052The library internally uses `boost::asio::io_context
53<https://www.boost.org/doc/libs/1_71_0/doc/html/boost_asio/reference/io_context.html>`__
54to implement fully asynchronous NDN operations (i.e., sending and receiving Interests and
55Data). In addition to network-related operations, ``boost::asio::io_context`` can be used
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070056to execute any arbitrary callback within the processing thread (run either explicitly via
Davide Pesavento2f46d652023-11-09 23:40:01 -050057``io_context::run()`` or implicitly via :ndn-cxx:`Face::processEvents` as in previous
58examples). :ndn-cxx:`Scheduler` is just a wrapper on top of ``io_context``, providing a
Davide Pesavento78338c52020-04-20 23:00:02 -040059simple 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:
Davide Pesaventoaee2ada2022-02-18 14:43:02 -050067 :emphasize-lines: 47-50,53,84,96-99,104-106