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