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 |
| 45 | m_scheduler.scheduleEvent(ndn::time::seconds(2), [this] { |
| 46 | m_faceConsumer.expressInterest(ndn::Interest("/hello/world"), |
| 47 | std::bind([] { std::cout << "Hello!" << std::endl; }), |
| 48 | std::bind([] { std::cout << "Bye!.." << std::endl; })); |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | run() |
| 54 | { |
| 55 | m_faceConsumer.processEvents(); // ok (will not block and do nothing) |
| 56 | // m_faceConsumer.getIoService().run(); // will crash |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | void |
| 61 | respondToAnyInterest(const ndn::Interest& interest) |
| 62 | { |
| 63 | auto data = std::make_shared<ndn::Data>(interest.getName()); |
| 64 | m_keyChain.sign(*data); |
| 65 | m_faceProducer.put(*data); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | ndn::KeyChain& m_keyChain; |
| 70 | ndn::Face m_faceConsumer; |
| 71 | ndn::Face m_faceProducer; |
| 72 | ndn::Scheduler m_scheduler; |
| 73 | }; |
| 74 | |
| 75 | } // namespace app |
| 76 | |
| 77 | #endif // NDNSIM_EXAMPLES_NDN_CXX_SIMPLE_REAL_APP_HPP |