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 |
| 15 | ``/localhost/testApp/randomData``. While expressing Interest, the app specifies two |
| 16 | callbacks to be called when Data is retrieved or Interest times out. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 17 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 18 | ``ndn::bind`` is an alias for either `boost::bind |
| 19 | <http://www.boost.org/doc/libs/1_55_0/libs/bind/bind.html>`_ or `std::bind |
| 20 | <http://en.cppreference.com/w/cpp/utility/functional/bind>`_ when the library is compiled |
| 21 | in C++11 mode. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 22 | |
| 23 | .. literalinclude:: ../examples/consumer.cpp |
| 24 | :language: c++ |
| 25 | :linenos: |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 26 | :emphasize-lines: 24-26,32-34,48-52,54 |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | Trivial producer |
| 30 | ---------------- |
| 31 | |
| 32 | The following example demonstrates how to write a simple producer application. |
| 33 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 34 | First, the application sets an Interset filter for ``/localhost/testApp`` to receive all |
| 35 | Interests that have this prefix. The :ndn-cxx:`Face::setInterestFilter` call accepts two |
| 36 | callbacks; the first will be called when an Interest is received and the second if prefix |
| 37 | registration fails. |
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 | After an Interest is received, the producer creates a Data packet with the same name as |
| 40 | the received Interest, adds content, and signs it with the system-default identity. It is |
| 41 | also possible to specify a particular key to be used during the signing. For more |
| 42 | information, refer to :ndn-cxx:`KeyChain API documentation <KeyChain>`. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 44 | Finally, after Data packet has been created and signed, it is returned to the requester |
| 45 | using :ndn-cxx:`Face::put` method. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 46 | |
| 47 | .. literalinclude:: ../examples/producer.cpp |
| 48 | :language: c++ |
| 49 | :linenos: |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 50 | :emphasize-lines: 44-47,50,56,71-74 |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 51 | |
| 52 | |
| 53 | Consumer that uses ndn::Scheduler |
| 54 | --------------------------------- |
| 55 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 56 | The following example demonstrates how to use :ndn-cxx:`ndn::Scheduler` to schedule arbitrary |
| 57 | events for execution at specific points of time. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 59 | The library internally uses `boost::asio::io_service |
| 60 | <http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/reference/io_service.html>`_ to |
| 61 | implement fully asynchronous NDN operations (i.e., sending and receiving Interests and |
| 62 | Data). In addition to network-related operations, ``boost::asio::io_service`` can be used |
| 63 | to execute any arbitrary callback within the processing thread (run either explicitly via |
| 64 | ``io.run`` or implicitly via :ndn-cxx:`Face::processEvents` as in previous examples). |
| 65 | :ndn-cxx:`ndn::Scheduler` is just a wrapper on top of ``boost::asio::io_service``, |
| 66 | allowing simple interface to schedule tasks at specific times. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 68 | The highlighted lines in the example demonstrate all that is needed to express a second |
| 69 | Interest approximately 2 seconds after the first one. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 70 | |
| 71 | .. literalinclude:: ../examples/consumer-with-timer.cpp |
| 72 | :language: c++ |
| 73 | :linenos: |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 74 | :emphasize-lines: 19,61,76,79-80,83 |