blob: 8c82cf6a7f68a905979b0842979b3062ac7ec935 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Weiwei Liuab9aad02016-10-09 13:56:04 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080022 */
23
Alexander Afanasyev766cea72014-04-24 19:16:42 -070024// correct way to include ndn-cxx headers
25// #include <ndn-cxx/face.hpp>
26// #include <ndn-cxx/util/scheduler.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080027#include "face.hpp"
28#include "util/scheduler.hpp"
29
Alexander Afanasyev151a8552014-04-11 00:54:43 -070030// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
31namespace ndn {
32// Additional nested namespace could be used to prevent/limit name contentions
33namespace examples {
34
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070035class ConsumerWithTimer : noncopyable
Alexander Afanasyevf6468892014-01-29 01:04:14 -080036{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070037public:
38 ConsumerWithTimer()
39 : m_face(m_ioService) // Create face with io_service object
40 , m_scheduler(m_ioService)
41 {
42 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -080043
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070044 void
45 run()
46 {
47 Interest interest(Name("/example/testApp/randomData"));
48 interest.setInterestLifetime(time::seconds(1));
49 interest.setMustBeFresh(true);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080050
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070051 m_face.expressInterest(interest,
52 bind(&ConsumerWithTimer::onData, this, _1, _2),
Weiwei Liuab9aad02016-10-09 13:56:04 -070053 bind(&ConsumerWithTimer::onNack, this, _1, _2),
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070054 bind(&ConsumerWithTimer::onTimeout, this, _1));
Alexander Afanasyev151a8552014-04-11 00:54:43 -070055
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070056 std::cout << "Sending " << interest << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070057
58 // Schedule a new event
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070059 m_scheduler.scheduleEvent(time::seconds(2),
60 bind(&ConsumerWithTimer::delayedInterest, this));
Alexander Afanasyev151a8552014-04-11 00:54:43 -070061
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070062 // m_ioService.run() will block until all events finished or m_ioService.stop() is called
63 m_ioService.run();
Alexander Afanasyev151a8552014-04-11 00:54:43 -070064
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070065 // Alternatively, m_face.processEvents() can also be called.
66 // processEvents will block until the requested data received or timeout occurs.
67 // m_face.processEvents();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080068 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070069
70private:
71 void
72 onData(const Interest& interest, const Data& data)
73 {
74 std::cout << data << std::endl;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080075 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070076
77 void
Weiwei Liuab9aad02016-10-09 13:56:04 -070078 onNack(const Interest& interest, const lp::Nack& nack)
79 {
80 std::cout << "received Nack with reason " << nack.getReason()
81 << " for interest " << interest << std::endl;
82 }
83
84 void
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070085 onTimeout(const Interest& interest)
86 {
87 std::cout << "Timeout " << interest << std::endl;
88 }
89
90 void
91 delayedInterest()
92 {
93 std::cout << "One more Interest, delayed by the scheduler" << std::endl;
94
95 Interest interest(Name("/example/testApp/randomData"));
96 interest.setInterestLifetime(time::milliseconds(1000));
97 interest.setMustBeFresh(true);
98
99 m_face.expressInterest(interest,
100 bind(&ConsumerWithTimer::onData, this, _1, _2),
Weiwei Liuab9aad02016-10-09 13:56:04 -0700101 bind(&ConsumerWithTimer::onNack, this, _1, _2),
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700102 bind(&ConsumerWithTimer::onTimeout, this, _1));
103
104 std::cout << "Sending " << interest << std::endl;
105 }
106
107private:
108 // Explicitly create io_service object, which can be shared between Face and Scheduler
109 boost::asio::io_service m_ioService;
110 Face m_face;
111 Scheduler m_scheduler;
112};
113
114
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700115
116} // namespace examples
117} // namespace ndn
118
119int
120main(int argc, char** argv)
121{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700122 ndn::examples::ConsumerWithTimer consumer;
123 try {
124 consumer.run();
125 }
126 catch (const std::exception& e) {
127 std::cerr << "ERROR: " << e.what() << std::endl;
128 }
129 return 0;
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700130}