blob: b7b2a87443727ec7c65a755ddd29e5ab068e17d4 [file] [log] [blame]
Spyridon Mastorakis95be5092015-08-26 15:07:37 -07001/* -*- 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
30namespace app {
31
32class RealApp
33{
34public:
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; }),
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -070048 std::bind([] { std::cout << "NACK!" << std::endl; }),
Spyridon Mastorakis95be5092015-08-26 15:07:37 -070049 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
60private:
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
69private:
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