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 | /* |
Davide Pesavento | aee2ada | 2022-02-18 14:43:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 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 | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 1944281 | 2018-11-23 14:00:04 -0500 | [diff] [blame] | 22 | #include <ndn-cxx/face.hpp> |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 23 | #include <ndn-cxx/security/validator-config.hpp> |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 25 | #include <iostream> |
| 26 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 27 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 28 | namespace ndn { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 29 | // Additional nested namespaces should be used to prevent/limit name conflicts |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 30 | namespace examples { |
| 31 | |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 32 | class Consumer |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 33 | { |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 34 | public: |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 35 | Consumer() |
| 36 | { |
| 37 | m_validator.load("trust-schema.conf"); |
| 38 | } |
| 39 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 40 | void |
| 41 | run() |
| 42 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 43 | Name interestName("/example/testApp/randomData"); |
| 44 | interestName.appendVersion(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 45 | |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 46 | Interest interest(interestName); |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 47 | interest.setMustBeFresh(true); |
| 48 | interest.setInterestLifetime(6_s); // The default is 4 seconds |
| 49 | |
| 50 | std::cout << "Sending Interest " << interest << std::endl; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 51 | m_face.expressInterest(interest, |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 52 | std::bind(&Consumer::onData, this, _1, _2), |
| 53 | std::bind(&Consumer::onNack, this, _1, _2), |
| 54 | std::bind(&Consumer::onTimeout, this, _1)); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 55 | |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 56 | // processEvents will block until the requested data is received or a timeout occurs |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 57 | m_face.processEvents(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 58 | } |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | void |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 62 | onData(const Interest&, const Data& data) |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 63 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 64 | std::cout << "Received Data " << data << std::endl; |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 65 | |
| 66 | m_validator.validate(data, |
| 67 | [] (const Data&) { |
| 68 | std::cout << "Data conforms to trust schema" << std::endl; |
| 69 | }, |
| 70 | [] (const Data&, const security::ValidationError& error) { |
| 71 | std::cout << "Error authenticating data: " << error << std::endl; |
| 72 | }); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 73 | } |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 74 | |
| 75 | void |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 76 | onNack(const Interest&, const lp::Nack& nack) const |
Weiwei Liu | ab9aad0 | 2016-10-09 13:56:04 -0700 | [diff] [blame] | 77 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 78 | std::cout << "Received Nack with reason " << nack.getReason() << std::endl; |
Weiwei Liu | ab9aad0 | 2016-10-09 13:56:04 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 82 | onTimeout(const Interest& interest) const |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 83 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 84 | std::cout << "Timeout for " << interest << std::endl; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private: |
| 88 | Face m_face; |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 89 | ValidatorConfig m_validator{m_face}; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 90 | }; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 91 | |
| 92 | } // namespace examples |
| 93 | } // namespace ndn |
| 94 | |
| 95 | int |
| 96 | main(int argc, char** argv) |
| 97 | { |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 98 | try { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 99 | ndn::examples::Consumer consumer; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 100 | consumer.run(); |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 101 | return 0; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 102 | } |
| 103 | catch (const std::exception& e) { |
| 104 | std::cerr << "ERROR: " << e.what() << std::endl; |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 105 | return 1; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 106 | } |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 107 | } |