Spyridon Mastorakis | 95be509 | 2015-08-26 15:07:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
| 7 | * |
| 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
| 20 | #ifndef NDNSIM_EXAMPLES_NDN_CXX_SIMPLE_REAL_APP_HPP |
| 21 | #define NDNSIM_EXAMPLES_NDN_CXX_SIMPLE_REAL_APP_HPP |
| 22 | |
| 23 | #include <ndn-cxx/face.hpp> |
| 24 | #include <ndn-cxx/interest.hpp> |
| 25 | #include <ndn-cxx/security/key-chain.hpp> |
| 26 | #include <ndn-cxx/util/scheduler.hpp> |
| 27 | |
| 28 | #include <iostream> |
| 29 | |
| 30 | namespace app { |
| 31 | |
| 32 | class RealApp |
| 33 | { |
| 34 | public: |
| 35 | RealApp(ndn::KeyChain& keyChain) |
| 36 | : m_keyChain(keyChain) |
| 37 | , m_faceProducer(m_faceConsumer.getIoService()) |
| 38 | , m_scheduler(m_faceConsumer.getIoService()) |
| 39 | { |
| 40 | // register prefix and set interest filter on producer face |
| 41 | m_faceProducer.setInterestFilter("/hello", std::bind(&RealApp::respondToAnyInterest, this, _2), |
| 42 | std::bind([]{}), std::bind([]{})); |
| 43 | |
| 44 | // use scheduler to send interest later on consumer face |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 45 | m_scheduler.schedule(ndn::time::seconds(2), [this] { |
Spyridon Mastorakis | 95be509 | 2015-08-26 15:07:37 -0700 | [diff] [blame] | 46 | m_faceConsumer.expressInterest(ndn::Interest("/hello/world"), |
| 47 | std::bind([] { std::cout << "Hello!" << std::endl; }), |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 48 | std::bind([] { std::cout << "NACK!" << std::endl; }), |
Spyridon Mastorakis | 95be509 | 2015-08-26 15:07:37 -0700 | [diff] [blame] | 49 | std::bind([] { std::cout << "Bye!.." << std::endl; })); |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | run() |
| 55 | { |
| 56 | m_faceConsumer.processEvents(); // ok (will not block and do nothing) |
| 57 | // m_faceConsumer.getIoService().run(); // will crash |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | void |
| 62 | respondToAnyInterest(const ndn::Interest& interest) |
| 63 | { |
| 64 | auto data = std::make_shared<ndn::Data>(interest.getName()); |
| 65 | m_keyChain.sign(*data); |
| 66 | m_faceProducer.put(*data); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | ndn::KeyChain& m_keyChain; |
| 71 | ndn::Face m_faceConsumer; |
| 72 | ndn::Face m_faceProducer; |
| 73 | ndn::Scheduler m_scheduler; |
| 74 | }; |
| 75 | |
| 76 | } // namespace app |
| 77 | |
| 78 | #endif // NDNSIM_EXAMPLES_NDN_CXX_SIMPLE_REAL_APP_HPP |