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 | |
| 4 | Trivial consumer |
| 5 | ---------------- |
| 6 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 7 | In the following trivial example, a consumer creates a :ndn-cxx:`Face` with default |
| 8 | transport (:ndn-cxx:`UnixTransport`) and sends an Interest for |
| 9 | ``/localhost/testApp/randomData``. While expressing Interest, the app specifies two |
| 10 | callbacks to be called when Data is retrieved or Interest times out. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 11 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 12 | ``ndn::bind`` is an alias for either `boost::bind |
| 13 | <http://www.boost.org/doc/libs/1_55_0/libs/bind/bind.html>`_ or `std::bind |
| 14 | <http://en.cppreference.com/w/cpp/utility/functional/bind>`_ when the library is compiled |
| 15 | in C++11 mode. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 16 | |
| 17 | .. literalinclude:: ../examples/consumer.cpp |
| 18 | :language: c++ |
| 19 | :linenos: |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | :emphasize-lines: 24-26,32-34,48-52,54 |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | Trivial producer |
| 24 | ---------------- |
| 25 | |
| 26 | The following example demonstrates how to write a simple producer application. |
| 27 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 28 | First, the application sets an Interset filter for ``/localhost/testApp`` to receive all |
| 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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 44 | :emphasize-lines: 44-47,50,56,71-74 |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 45 | |
| 46 | |
| 47 | Consumer that uses ndn::Scheduler |
| 48 | --------------------------------- |
| 49 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 50 | The following example demonstrates how to use :ndn-cxx:`ndn::Scheduler` to schedule arbitrary |
| 51 | events for execution at specific points of time. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 52 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 53 | The library internally uses `boost::asio::io_service |
| 54 | <http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/reference/io_service.html>`_ to |
| 55 | implement fully asynchronous NDN operations (i.e., sending and receiving Interests and |
| 56 | Data). In addition to network-related operations, ``boost::asio::io_service`` can be used |
| 57 | to execute any arbitrary callback within the processing thread (run either explicitly via |
| 58 | ``io.run`` or implicitly via :ndn-cxx:`Face::processEvents` as in previous examples). |
| 59 | :ndn-cxx:`ndn::Scheduler` is just a wrapper on top of ``boost::asio::io_service``, |
| 60 | allowing simple interface to schedule tasks at specific times. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 61 | |
Alexander Afanasyev | 9b0e114 | 2014-05-08 00:17:34 -0700 | [diff] [blame] | 62 | The highlighted lines in the example demonstrate all that is needed to express a second |
| 63 | Interest approximately 2 seconds after the first one. |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 64 | |
| 65 | .. literalinclude:: ../examples/consumer-with-timer.cpp |
| 66 | :language: c++ |
| 67 | :linenos: |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 68 | :emphasize-lines: 19,61,76,79-80,83 |