Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 2 | /** |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 30 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 31 | namespace ndn { |
| 32 | // Additional nested namespace could be used to prevent/limit name contentions |
| 33 | namespace examples { |
| 34 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 35 | class ConsumerWithTimer : noncopyable |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 36 | { |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 37 | public: |
| 38 | ConsumerWithTimer() |
| 39 | : m_face(m_ioService) // Create face with io_service object |
| 40 | , m_scheduler(m_ioService) |
| 41 | { |
| 42 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 43 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 44 | void |
| 45 | run() |
| 46 | { |
| 47 | Interest interest(Name("/example/testApp/randomData")); |
| 48 | interest.setInterestLifetime(time::seconds(1)); |
| 49 | interest.setMustBeFresh(true); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 50 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 51 | m_face.expressInterest(interest, |
| 52 | bind(&ConsumerWithTimer::onData, this, _1, _2), |
| 53 | bind(&ConsumerWithTimer::onTimeout, this, _1)); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 54 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 55 | std::cout << "Sending " << interest << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 56 | |
| 57 | // Schedule a new event |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 58 | m_scheduler.scheduleEvent(time::seconds(2), |
| 59 | bind(&ConsumerWithTimer::delayedInterest, this)); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 60 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 61 | // m_ioService.run() will block until all events finished or m_ioService.stop() is called |
| 62 | m_ioService.run(); |
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 | // Alternatively, m_face.processEvents() can also be called. |
| 65 | // processEvents will block until the requested data received or timeout occurs. |
| 66 | // m_face.processEvents(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 67 | } |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | void |
| 71 | onData(const Interest& interest, const Data& data) |
| 72 | { |
| 73 | std::cout << data << std::endl; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 74 | } |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 75 | |
| 76 | void |
| 77 | onTimeout(const Interest& interest) |
| 78 | { |
| 79 | std::cout << "Timeout " << interest << std::endl; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | delayedInterest() |
| 84 | { |
| 85 | std::cout << "One more Interest, delayed by the scheduler" << std::endl; |
| 86 | |
| 87 | Interest interest(Name("/example/testApp/randomData")); |
| 88 | interest.setInterestLifetime(time::milliseconds(1000)); |
| 89 | interest.setMustBeFresh(true); |
| 90 | |
| 91 | m_face.expressInterest(interest, |
| 92 | bind(&ConsumerWithTimer::onData, this, _1, _2), |
| 93 | bind(&ConsumerWithTimer::onTimeout, this, _1)); |
| 94 | |
| 95 | std::cout << "Sending " << interest << std::endl; |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | // Explicitly create io_service object, which can be shared between Face and Scheduler |
| 100 | boost::asio::io_service m_ioService; |
| 101 | Face m_face; |
| 102 | Scheduler m_scheduler; |
| 103 | }; |
| 104 | |
| 105 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 106 | |
| 107 | } // namespace examples |
| 108 | } // namespace ndn |
| 109 | |
| 110 | int |
| 111 | main(int argc, char** argv) |
| 112 | { |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 113 | ndn::examples::ConsumerWithTimer consumer; |
| 114 | try { |
| 115 | consumer.run(); |
| 116 | } |
| 117 | catch (const std::exception& e) { |
| 118 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 119 | } |
| 120 | return 0; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 121 | } |