blob: 2f9c4d4f21ddcec8abf7bc08cb7a36fe839fe245 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventocdcde902017-08-23 15:40:22 -04002/*
Davide Pesavento2e481fc2021-07-02 18:20:03 -04003 * Copyright (c) 2013-2021 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
Davide Pesavento19442812018-11-23 14:00:04 -050024#include <ndn-cxx/face.hpp>
25#include <ndn-cxx/util/scheduler.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080026
Davide Pesavento4d0d0962017-12-19 22:23:14 -050027#include <boost/asio/io_service.hpp>
Davide Pesaventocdcde902017-08-23 15:40:22 -040028#include <iostream>
29
Alexander Afanasyev151a8552014-04-11 00:54:43 -070030// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
31namespace ndn {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070032// Additional nested namespaces should be used to prevent/limit name conflicts
Alexander Afanasyev151a8552014-04-11 00:54:43 -070033namespace examples {
34
Davide Pesavento4d0d0962017-12-19 22:23:14 -050035class ConsumerWithTimer
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 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070047 Name interestName("/example/testApp/randomData");
48 interestName.appendVersion();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080049
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070050 Interest interest(interestName);
51 interest.setCanBePrefix(false);
52 interest.setMustBeFresh(true);
53 interest.setInterestLifetime(2_s);
54
55 std::cout << "Sending Interest " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070056 m_face.expressInterest(interest,
Davide Pesavento2e481fc2021-07-02 18:20:03 -040057 std::bind(&ConsumerWithTimer::onData, this, _1, _2),
58 std::bind(&ConsumerWithTimer::onNack, this, _1, _2),
59 std::bind(&ConsumerWithTimer::onTimeout, this, _1));
Alexander Afanasyev151a8552014-04-11 00:54:43 -070060
Alexander Afanasyev151a8552014-04-11 00:54:43 -070061 // Schedule a new event
Junxiao Shia5f233e2019-03-18 09:39:22 -060062 m_scheduler.schedule(3_s, [this] { delayedInterest(); });
Alexander Afanasyev151a8552014-04-11 00:54:43 -070063
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070064 // m_ioService.run() will block until all events finished or m_ioService.stop() is called
65 m_ioService.run();
Alexander Afanasyev151a8552014-04-11 00:54:43 -070066
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070067 // Alternatively, m_face.processEvents() can also be called.
68 // processEvents will block until the requested data received or timeout occurs.
69 // m_face.processEvents();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080070 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070071
72private:
73 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070074 onData(const Interest&, const Data& data) const
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070075 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070076 std::cout << "Received Data " << data << std::endl;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080077 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070078
79 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070080 onNack(const Interest& interest, const lp::Nack& nack) const
Weiwei Liuab9aad02016-10-09 13:56:04 -070081 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070082 std::cout << "Received Nack with reason " << nack.getReason()
83 << " for " << interest << std::endl;
Weiwei Liuab9aad02016-10-09 13:56:04 -070084 }
85
86 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070087 onTimeout(const Interest& interest) const
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070088 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070089 std::cout << "Timeout for " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070090 }
91
92 void
93 delayedInterest()
94 {
95 std::cout << "One more Interest, delayed by the scheduler" << std::endl;
96
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070097 Name interestName("/example/testApp/randomData");
98 interestName.appendVersion();
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070099
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700100 Interest interest(interestName);
101 interest.setCanBePrefix(false);
102 interest.setMustBeFresh(true);
103 interest.setInterestLifetime(2_s);
104
105 std::cout << "Sending Interest " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700106 m_face.expressInterest(interest,
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400107 std::bind(&ConsumerWithTimer::onData, this, _1, _2),
108 std::bind(&ConsumerWithTimer::onNack, this, _1, _2),
109 std::bind(&ConsumerWithTimer::onTimeout, this, _1));
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700110 }
111
112private:
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700113 // Explicitly create io_service object, which will be shared between Face and Scheduler
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700114 boost::asio::io_service m_ioService;
115 Face m_face;
116 Scheduler m_scheduler;
117};
118
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700119} // namespace examples
120} // namespace ndn
121
122int
123main(int argc, char** argv)
124{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700125 try {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700126 ndn::examples::ConsumerWithTimer consumer;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700127 consumer.run();
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700128 return 0;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700129 }
130 catch (const std::exception& e) {
131 std::cerr << "ERROR: " << e.what() << std::endl;
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700132 return 1;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700133 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700134}