Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 1944281 | 2018-11-23 14:00:04 -0500 | [diff] [blame] | 22 | #include <ndn-cxx/face.hpp> |
| 23 | #include <ndn-cxx/util/scheduler.hpp> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 25 | #include <boost/asio/io_context.hpp> |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 26 | #include <iostream> |
| 27 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 28 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 29 | namespace ndn { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 30 | // Additional nested namespaces should be used to prevent/limit name conflicts |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 31 | namespace examples { |
| 32 | |
Davide Pesavento | 4d0d096 | 2017-12-19 22:23:14 -0500 | [diff] [blame] | 33 | class ConsumerWithTimer |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 34 | { |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 35 | public: |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 36 | void |
| 37 | run() |
| 38 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 39 | Name interestName("/example/testApp/randomData"); |
| 40 | interestName.appendVersion(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 41 | |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 42 | Interest interest(interestName); |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 43 | interest.setMustBeFresh(true); |
| 44 | interest.setInterestLifetime(2_s); |
| 45 | |
| 46 | std::cout << "Sending Interest " << interest << std::endl; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 47 | m_face.expressInterest(interest, |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 48 | std::bind(&ConsumerWithTimer::onData, this, _1, _2), |
| 49 | std::bind(&ConsumerWithTimer::onNack, this, _1, _2), |
| 50 | std::bind(&ConsumerWithTimer::onTimeout, this, _1)); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 52 | // Schedule a new event |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 53 | m_scheduler.schedule(3_s, [this] { delayedInterest(); }); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 54 | |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 55 | // m_ioCtx.run() will block until all events finished or m_ioCtx.stop() is called |
| 56 | m_ioCtx.run(); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 57 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 58 | // Alternatively, m_face.processEvents() can also be called. |
| 59 | // processEvents will block until the requested data received or timeout occurs. |
| 60 | // m_face.processEvents(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 61 | } |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | void |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 65 | onData(const Interest&, const Data& data) const |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 66 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 67 | std::cout << "Received Data " << data << std::endl; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 68 | } |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 69 | |
| 70 | void |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 71 | onNack(const Interest& interest, const lp::Nack& nack) const |
Weiwei Liu | ab9aad0 | 2016-10-09 13:56:04 -0700 | [diff] [blame] | 72 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 73 | std::cout << "Received Nack with reason " << nack.getReason() |
| 74 | << " for " << interest << std::endl; |
Weiwei Liu | ab9aad0 | 2016-10-09 13:56:04 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 78 | onTimeout(const Interest& interest) const |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 79 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 80 | std::cout << "Timeout for " << interest << std::endl; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void |
| 84 | delayedInterest() |
| 85 | { |
| 86 | std::cout << "One more Interest, delayed by the scheduler" << std::endl; |
| 87 | |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 88 | Name interestName("/example/testApp/randomData"); |
| 89 | interestName.appendVersion(); |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 90 | |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 91 | Interest interest(interestName); |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 92 | interest.setMustBeFresh(true); |
| 93 | interest.setInterestLifetime(2_s); |
| 94 | |
| 95 | std::cout << "Sending Interest " << interest << std::endl; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 96 | m_face.expressInterest(interest, |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 97 | std::bind(&ConsumerWithTimer::onData, this, _1, _2), |
| 98 | std::bind(&ConsumerWithTimer::onNack, this, _1, _2), |
| 99 | std::bind(&ConsumerWithTimer::onTimeout, this, _1)); |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private: |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 103 | // Explicitly create io_context object, which will be shared between Face and Scheduler |
| 104 | boost::asio::io_context m_ioCtx; |
| 105 | Face m_face{m_ioCtx}; |
| 106 | Scheduler m_scheduler{m_ioCtx}; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 109 | } // namespace examples |
| 110 | } // namespace ndn |
| 111 | |
| 112 | int |
| 113 | main(int argc, char** argv) |
| 114 | { |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 115 | try { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 116 | ndn::examples::ConsumerWithTimer consumer; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 117 | consumer.run(); |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 118 | return 0; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 119 | } |
| 120 | catch (const std::exception& e) { |
| 121 | std::cerr << "ERROR: " << e.what() << std::endl; |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 122 | return 1; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 123 | } |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 124 | } |