blob: e512c72a08c17b599f13423ce220544a3d4608b7 [file] [log] [blame]
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -07001Trivial applications
2====================
Alexander Afanasyev151a8552014-04-11 00:54:43 -07003
Yingdi Yu4e99f532014-08-25 19:40:57 -07004.. 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 Afanasyev151a8552014-04-11 00:54:43 -070010Trivial consumer
11----------------
12
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070013In the following trivial example, a consumer creates a :ndn-cxx:`Face` with default
14transport (:ndn-cxx:`UnixTransport`) and sends an Interest for
15``/localhost/testApp/randomData``. While expressing Interest, the app specifies two
16callbacks to be called when Data is retrieved or Interest times out.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070017
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070018``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
21in C++11 mode.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070022
23.. literalinclude:: ../examples/consumer.cpp
24 :language: c++
25 :linenos:
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070026 :emphasize-lines: 24-26,32-34,48-52,54
Alexander Afanasyev151a8552014-04-11 00:54:43 -070027
28
29Trivial producer
30----------------
31
32The following example demonstrates how to write a simple producer application.
33
Ivan Yeoba1a4a92015-01-11 00:45:57 -080034First, the application sets an Interest filter for ``/localhost/testApp`` to receive all
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070035Interests that have this prefix. The :ndn-cxx:`Face::setInterestFilter` call accepts two
36callbacks; the first will be called when an Interest is received and the second if prefix
37registration fails.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070038
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070039After an Interest is received, the producer creates a Data packet with the same name as
40the received Interest, adds content, and signs it with the system-default identity. It is
41also possible to specify a particular key to be used during the signing. For more
42information, refer to :ndn-cxx:`KeyChain API documentation <KeyChain>`.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070043
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070044Finally, after Data packet has been created and signed, it is returned to the requester
45using :ndn-cxx:`Face::put` method.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070046
47.. literalinclude:: ../examples/producer.cpp
48 :language: c++
49 :linenos:
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070050 :emphasize-lines: 44-47,50,56,71-74
Alexander Afanasyev151a8552014-04-11 00:54:43 -070051
52
53Consumer that uses ndn::Scheduler
54---------------------------------
55
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070056The following example demonstrates how to use :ndn-cxx:`ndn::Scheduler` to schedule arbitrary
57events for execution at specific points of time.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070058
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070059The 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
61implement fully asynchronous NDN operations (i.e., sending and receiving Interests and
62Data). In addition to network-related operations, ``boost::asio::io_service`` can be used
63to 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``,
66allowing simple interface to schedule tasks at specific times.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070067
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070068The highlighted lines in the example demonstrate all that is needed to express a second
69Interest approximately 2 seconds after the first one.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070070
71.. literalinclude:: ../examples/consumer-with-timer.cpp
72 :language: c++
73 :linenos:
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070074 :emphasize-lines: 19,61,76,79-80,83