Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 1 | Trivial applications |
| 2 | ==================== |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 3 | |
Yingdi Yu | 4e99f53 | 2014-08-25 19:40:57 -0700 | [diff] [blame] | 4 | .. 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 Pesavento | 933a567 | 2020-07-03 22:32:43 -0400 | [diff] [blame] | 8 | homepage <https://named-data.net/doc/NFD/>`_. |
Yingdi Yu | 4e99f53 | 2014-08-25 19:40:57 -0700 | [diff] [blame] | 9 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 10 | Trivial consumer |
| 11 | ---------------- |
| 12 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 13 | In the following trivial example, a consumer creates a :ndn-cxx:`Face` with default |
| 14 | transport (:ndn-cxx:`UnixTransport`) and sends an Interest for |
Weiwei Liu | ab9aad0 | 2016-10-09 13:56:04 -0700 | [diff] [blame] | 15 | ``/localhost/testApp/randomData``. While expressing Interest, the app specifies three |
| 16 | callbacks to be called when Data/Nack is retrieved or Interest times out. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 17 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 18 | .. literalinclude:: ../examples/consumer.cpp |
| 19 | :language: c++ |
| 20 | :linenos: |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 21 | :emphasize-lines: 43-49,52-55,63,77,83 |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 22 | |
| 23 | Trivial producer |
| 24 | ---------------- |
| 25 | |
| 26 | The following example demonstrates how to write a simple producer application. |
| 27 | |
Ivan Yeo | ba1a4a9 | 2015-01-11 00:45:57 -0800 | [diff] [blame] | 28 | First, the application sets an Interest filter for ``/localhost/testApp`` to receive all |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 29 | Interests that have this prefix. The :ndn-cxx:`Face::setInterestFilter` call accepts two |
| 30 | callbacks; the first will be called when an Interest is received and the second if prefix |
| 31 | registration fails. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 33 | After an Interest is received, the producer creates a Data packet with the same name as |
| 34 | the received Interest, adds content, and signs it with the system-default identity. It is |
| 35 | also possible to specify a particular key to be used during the signing. For more |
| 36 | information, refer to :ndn-cxx:`KeyChain API documentation <KeyChain>`. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 37 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 38 | Finally, after Data packet has been created and signed, it is returned to the requester |
| 39 | using :ndn-cxx:`Face::put` method. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 40 | |
| 41 | .. literalinclude:: ../examples/producer.cpp |
| 42 | :language: c++ |
| 43 | :linenos: |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 44 | :emphasize-lines: 39-42,55,62-64,79,87,91 |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 45 | |
Yingdi Yu | 55ea01a | 2015-07-21 22:42:17 -0700 | [diff] [blame] | 46 | Consumer that uses Scheduler |
| 47 | ---------------------------- |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 48 | |
Yingdi Yu | 55ea01a | 2015-07-21 22:42:17 -0700 | [diff] [blame] | 49 | The following example demonstrates how to use :ndn-cxx:`Scheduler` to schedule arbitrary |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 50 | events for execution at specific points of time. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 52 | The library internally uses `boost::asio::io_service |
Davide Pesavento | 78338c5 | 2020-04-20 23:00:02 -0400 | [diff] [blame] | 53 | <https://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/reference/io_service.html>`_ to |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 54 | implement fully asynchronous NDN operations (i.e., sending and receiving Interests and |
| 55 | Data). In addition to network-related operations, ``boost::asio::io_service`` can be used |
| 56 | to execute any arbitrary callback within the processing thread (run either explicitly via |
Davide Pesavento | 78338c5 | 2020-04-20 23:00:02 -0400 | [diff] [blame] | 57 | ``io_service::run()`` or implicitly via :ndn-cxx:`Face::processEvents` as in previous |
| 58 | examples). :ndn-cxx:`Scheduler` is just a wrapper on top of ``io_service``, providing a |
| 59 | simple interface to schedule tasks at specific times. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 61 | The highlighted lines in the example demonstrate all that is needed to express a second |
Davide Pesavento | 78338c5 | 2020-04-20 23:00:02 -0400 | [diff] [blame] | 62 | Interest approximately 3 seconds after the first one. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 63 | |
| 64 | .. literalinclude:: ../examples/consumer-with-timer.cpp |
| 65 | :language: c++ |
| 66 | :linenos: |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 67 | :emphasize-lines: 37-38,54-57,60,91,104-107,112-114 |