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