blob: 985b77e4df7453d27c446c35b2f96a872178e64f [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 Afanasyevf6468892014-01-29 01:04:14 -080020 */
21
Davide Pesavento19442812018-11-23 14:00:04 -050022#include <ndn-cxx/face.hpp>
23#include <ndn-cxx/util/scheduler.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080024
Davide Pesavento4d0d0962017-12-19 22:23:14 -050025#include <boost/asio/io_service.hpp>
Davide Pesaventocdcde902017-08-23 15:40:22 -040026#include <iostream>
27
Alexander Afanasyev151a8552014-04-11 00:54:43 -070028// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
29namespace ndn {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070030// Additional nested namespaces should be used to prevent/limit name conflicts
Alexander Afanasyev151a8552014-04-11 00:54:43 -070031namespace examples {
32
Davide Pesavento4d0d0962017-12-19 22:23:14 -050033class ConsumerWithTimer
Alexander Afanasyevf6468892014-01-29 01:04:14 -080034{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070035public:
36 ConsumerWithTimer()
37 : m_face(m_ioService) // Create face with io_service object
38 , m_scheduler(m_ioService)
39 {
40 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -080041
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070042 void
43 run()
44 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070045 Name interestName("/example/testApp/randomData");
46 interestName.appendVersion();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080047
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070048 Interest interest(interestName);
49 interest.setCanBePrefix(false);
50 interest.setMustBeFresh(true);
51 interest.setInterestLifetime(2_s);
52
53 std::cout << "Sending Interest " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070054 m_face.expressInterest(interest,
Davide Pesavento2e481fc2021-07-02 18:20:03 -040055 std::bind(&ConsumerWithTimer::onData, this, _1, _2),
56 std::bind(&ConsumerWithTimer::onNack, this, _1, _2),
57 std::bind(&ConsumerWithTimer::onTimeout, this, _1));
Alexander Afanasyev151a8552014-04-11 00:54:43 -070058
Alexander Afanasyev151a8552014-04-11 00:54:43 -070059 // Schedule a new event
Junxiao Shia5f233e2019-03-18 09:39:22 -060060 m_scheduler.schedule(3_s, [this] { delayedInterest(); });
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
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070072 onData(const Interest&, const Data& data) const
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070073 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070074 std::cout << "Received Data " << data << std::endl;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080075 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070076
77 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070078 onNack(const Interest& interest, const lp::Nack& nack) const
Weiwei Liuab9aad02016-10-09 13:56:04 -070079 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070080 std::cout << "Received Nack with reason " << nack.getReason()
81 << " for " << interest << std::endl;
Weiwei Liuab9aad02016-10-09 13:56:04 -070082 }
83
84 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070085 onTimeout(const Interest& interest) const
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070086 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070087 std::cout << "Timeout for " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070088 }
89
90 void
91 delayedInterest()
92 {
93 std::cout << "One more Interest, delayed by the scheduler" << std::endl;
94
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070095 Name interestName("/example/testApp/randomData");
96 interestName.appendVersion();
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070097
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070098 Interest interest(interestName);
99 interest.setCanBePrefix(false);
100 interest.setMustBeFresh(true);
101 interest.setInterestLifetime(2_s);
102
103 std::cout << "Sending Interest " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700104 m_face.expressInterest(interest,
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400105 std::bind(&ConsumerWithTimer::onData, this, _1, _2),
106 std::bind(&ConsumerWithTimer::onNack, this, _1, _2),
107 std::bind(&ConsumerWithTimer::onTimeout, this, _1));
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700108 }
109
110private:
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700111 // Explicitly create io_service object, which will be shared between Face and Scheduler
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700112 boost::asio::io_service m_ioService;
113 Face m_face;
114 Scheduler m_scheduler;
115};
116
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700117} // namespace examples
118} // namespace ndn
119
120int
121main(int argc, char** argv)
122{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700123 try {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700124 ndn::examples::ConsumerWithTimer consumer;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700125 consumer.run();
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700126 return 0;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700127 }
128 catch (const std::exception& e) {
129 std::cerr << "ERROR: " << e.what() << std::endl;
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700130 return 1;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700131 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700132}