blob: 793cd8931dac6d2b59d086e2a153b0df44a02072 [file] [log] [blame]
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -07001Trivial applications
2====================
Alexander Afanasyev151a8552014-04-11 00:54:43 -07003
4Trivial consumer
5----------------
6
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -07007In the following trivial example, a consumer creates a :ndn-cxx:`Face` with default
8transport (:ndn-cxx:`UnixTransport`) and sends an Interest for
9``/localhost/testApp/randomData``. While expressing Interest, the app specifies two
10callbacks to be called when Data is retrieved or Interest times out.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070011
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070012``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
15in C++11 mode.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070016
17.. literalinclude:: ../examples/consumer.cpp
18 :language: c++
19 :linenos:
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 :emphasize-lines: 24-26,32-34,48-52,54
Alexander Afanasyev151a8552014-04-11 00:54:43 -070021
22
23Trivial producer
24----------------
25
26The following example demonstrates how to write a simple producer application.
27
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070028First, the application sets an Interset filter for ``/localhost/testApp`` to receive all
29Interests that have this prefix. The :ndn-cxx:`Face::setInterestFilter` call accepts two
30callbacks; the first will be called when an Interest is received and the second if prefix
31registration fails.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070032
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070033After an Interest is received, the producer creates a Data packet with the same name as
34the received Interest, adds content, and signs it with the system-default identity. It is
35also possible to specify a particular key to be used during the signing. For more
36information, refer to :ndn-cxx:`KeyChain API documentation <KeyChain>`.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070037
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070038Finally, after Data packet has been created and signed, it is returned to the requester
39using :ndn-cxx:`Face::put` method.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070040
41.. literalinclude:: ../examples/producer.cpp
42 :language: c++
43 :linenos:
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070044 :emphasize-lines: 44-47,50,56,71-74
Alexander Afanasyev151a8552014-04-11 00:54:43 -070045
46
47Consumer that uses ndn::Scheduler
48---------------------------------
49
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070050The following example demonstrates how to use :ndn-cxx:`ndn::Scheduler` to schedule arbitrary
51events for execution at specific points of time.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070052
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070053The 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
55implement fully asynchronous NDN operations (i.e., sending and receiving Interests and
56Data). In addition to network-related operations, ``boost::asio::io_service`` can be used
57to 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``,
60allowing simple interface to schedule tasks at specific times.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070061
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -070062The highlighted lines in the example demonstrate all that is needed to express a second
63Interest approximately 2 seconds after the first one.
Alexander Afanasyev151a8552014-04-11 00:54:43 -070064
65.. literalinclude:: ../examples/consumer-with-timer.cpp
66 :language: c++
67 :linenos:
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070068 :emphasize-lines: 19,61,76,79-80,83