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 | /* |
| 3 | * Copyright (c) 2013-2017 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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 24 | // correct way to include ndn-cxx headers |
| 25 | // #include <ndn-cxx/face.hpp> |
| 26 | // #include <ndn-cxx/util/scheduler.hpp> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 27 | #include "face.hpp" |
| 28 | #include "util/scheduler.hpp" |
| 29 | |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 30 | #include <iostream> |
| 31 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 32 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 33 | namespace ndn { |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 34 | // Additional nested namespaces can be used to prevent/limit name conflicts |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 35 | namespace examples { |
| 36 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 37 | class ConsumerWithTimer : noncopyable |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 38 | { |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 39 | public: |
| 40 | ConsumerWithTimer() |
| 41 | : m_face(m_ioService) // Create face with io_service object |
| 42 | , m_scheduler(m_ioService) |
| 43 | { |
| 44 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 45 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 46 | void |
| 47 | run() |
| 48 | { |
| 49 | Interest interest(Name("/example/testApp/randomData")); |
| 50 | interest.setInterestLifetime(time::seconds(1)); |
| 51 | interest.setMustBeFresh(true); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 52 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 53 | m_face.expressInterest(interest, |
| 54 | bind(&ConsumerWithTimer::onData, this, _1, _2), |
Weiwei Liu | ab9aad0 | 2016-10-09 13:56:04 -0700 | [diff] [blame] | 55 | bind(&ConsumerWithTimer::onNack, this, _1, _2), |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 56 | bind(&ConsumerWithTimer::onTimeout, this, _1)); |
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 | std::cout << "Sending " << interest << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 59 | |
| 60 | // Schedule a new event |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 61 | m_scheduler.scheduleEvent(time::seconds(2), |
| 62 | bind(&ConsumerWithTimer::delayedInterest, this)); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 63 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 64 | // m_ioService.run() will block until all events finished or m_ioService.stop() is called |
| 65 | m_ioService.run(); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 66 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 67 | // 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 Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 70 | } |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | void |
| 74 | onData(const Interest& interest, const Data& data) |
| 75 | { |
| 76 | std::cout << data << std::endl; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 77 | } |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 78 | |
| 79 | void |
Weiwei Liu | ab9aad0 | 2016-10-09 13:56:04 -0700 | [diff] [blame] | 80 | onNack(const Interest& interest, const lp::Nack& nack) |
| 81 | { |
| 82 | std::cout << "received Nack with reason " << nack.getReason() |
| 83 | << " for interest " << interest << std::endl; |
| 84 | } |
| 85 | |
| 86 | void |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 87 | onTimeout(const Interest& interest) |
| 88 | { |
| 89 | std::cout << "Timeout " << interest << std::endl; |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | delayedInterest() |
| 94 | { |
| 95 | std::cout << "One more Interest, delayed by the scheduler" << std::endl; |
| 96 | |
| 97 | Interest interest(Name("/example/testApp/randomData")); |
| 98 | interest.setInterestLifetime(time::milliseconds(1000)); |
| 99 | interest.setMustBeFresh(true); |
| 100 | |
| 101 | m_face.expressInterest(interest, |
| 102 | bind(&ConsumerWithTimer::onData, this, _1, _2), |
Weiwei Liu | ab9aad0 | 2016-10-09 13:56:04 -0700 | [diff] [blame] | 103 | bind(&ConsumerWithTimer::onNack, this, _1, _2), |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 104 | bind(&ConsumerWithTimer::onTimeout, this, _1)); |
| 105 | |
| 106 | std::cout << "Sending " << interest << std::endl; |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | // Explicitly create io_service object, which can be shared between Face and Scheduler |
| 111 | boost::asio::io_service m_ioService; |
| 112 | Face m_face; |
| 113 | Scheduler m_scheduler; |
| 114 | }; |
| 115 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 116 | } // namespace examples |
| 117 | } // namespace ndn |
| 118 | |
| 119 | int |
| 120 | main(int argc, char** argv) |
| 121 | { |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 122 | 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 Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 130 | } |