blob: c1ad084c5dd815a1740263b232d349cafedf3996 [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 Afanasyev151a8552014-04-11 00:54:43 -070018.. literalinclude:: ../examples/consumer.cpp
19 :language: c++
20 :linenos:
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070021 :emphasize-lines: 24-27,39,43-46,50,57,67
Alexander Afanasyev151a8552014-04-11 00:54:43 -070022
23
24Trivial producer
25----------------
26
27The following example demonstrates how to write a simple producer application.
28
Ivan Yeoba1a4a92015-01-11 00:45:57 -080029First, the application sets an Interest filter for ``/localhost/testApp`` to receive all
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070030Interests that have this prefix. The :ndn-cxx:`Face::setInterestFilter` call accepts two
31callbacks; the first will be called when an Interest is received and the second if prefix
32registration fails.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070033
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070034After an Interest is received, the producer creates a Data packet with the same name as
35the received Interest, adds content, and signs it with the system-default identity. It is
36also possible to specify a particular key to be used during the signing. For more
37information, refer to :ndn-cxx:`KeyChain API documentation <KeyChain>`.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070038
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070039Finally, after Data packet has been created and signed, it is returned to the requester
40using :ndn-cxx:`Face::put` method.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070041
42.. literalinclude:: ../examples/producer.cpp
43 :language: c++
44 :linenos:
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070045 :emphasize-lines: 42,55-58,61,64,67,70,76
Alexander Afanasyev151a8552014-04-11 00:54:43 -070046
47
Yingdi Yu55ea01a2015-07-21 22:42:17 -070048Consumer that uses Scheduler
49----------------------------
Alexander Afanasyev151a8552014-04-11 00:54:43 -070050
Yingdi Yu55ea01a2015-07-21 22:42:17 -070051The following example demonstrates how to use :ndn-cxx:`Scheduler` to schedule arbitrary
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070052events for execution at specific points of time.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070053
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070054The library internally uses `boost::asio::io_service
Davide Pesaventoe6e6fde2016-04-16 14:44:45 +020055<http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/io_service.html>`_ to
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070056implement fully asynchronous NDN operations (i.e., sending and receiving Interests and
57Data). In addition to network-related operations, ``boost::asio::io_service`` can be used
58to 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 Yu55ea01a2015-07-21 22:42:17 -070060:ndn-cxx:`Scheduler` is just a wrapper on top of ``boost::asio::io_service``,
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070061allowing simple interface to schedule tasks at specific times.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070062
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070063The highlighted lines in the example demonstrate all that is needed to express a second
64Interest approximately 2 seconds after the first one.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070065
66.. literalinclude:: ../examples/consumer-with-timer.cpp
67 :language: c++
68 :linenos:
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070069 :emphasize-lines: 39-40,51-54,58-59,61-62,99-100
70