blob: f524962911c28bde266bbbf7cfd892381dd895b0 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventocdcde902017-08-23 15:40:22 -04002/*
Davide Pesaventoaee2ada2022-02-18 14:43:02 -05003 * Copyright (c) 2013-2022 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevc4b75982014-01-09 14:51:45 -080020 */
21
Davide Pesavento19442812018-11-23 14:00:04 -050022#include <ndn-cxx/face.hpp>
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040023#include <ndn-cxx/security/validator-config.hpp>
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080024
Davide Pesaventocdcde902017-08-23 15:40:22 -040025#include <iostream>
26
Alexander Afanasyev151a8552014-04-11 00:54:43 -070027// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
28namespace ndn {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070029// Additional nested namespaces should be used to prevent/limit name conflicts
Alexander Afanasyev151a8552014-04-11 00:54:43 -070030namespace examples {
31
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070032class Consumer
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080033{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070034public:
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040035 Consumer()
36 {
37 m_validator.load("trust-schema.conf");
38 }
39
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070040 void
41 run()
42 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070043 Name interestName("/example/testApp/randomData");
44 interestName.appendVersion();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080045
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070046 Interest interest(interestName);
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070047 interest.setMustBeFresh(true);
48 interest.setInterestLifetime(6_s); // The default is 4 seconds
49
50 std::cout << "Sending Interest " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070051 m_face.expressInterest(interest,
Davide Pesavento2e481fc2021-07-02 18:20:03 -040052 std::bind(&Consumer::onData, this, _1, _2),
53 std::bind(&Consumer::onNack, this, _1, _2),
54 std::bind(&Consumer::onTimeout, this, _1));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080055
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070056 // processEvents will block until the requested data is received or a timeout occurs
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070057 m_face.processEvents();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080058 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070059
60private:
61 void
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040062 onData(const Interest&, const Data& data)
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070063 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070064 std::cout << "Received Data " << data << std::endl;
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040065
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 Afanasyevc4b75982014-01-09 14:51:45 -080073 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070074
75 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070076 onNack(const Interest&, const lp::Nack& nack) const
Weiwei Liuab9aad02016-10-09 13:56:04 -070077 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070078 std::cout << "Received Nack with reason " << nack.getReason() << std::endl;
Weiwei Liuab9aad02016-10-09 13:56:04 -070079 }
80
81 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070082 onTimeout(const Interest& interest) const
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070083 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070084 std::cout << "Timeout for " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070085 }
86
87private:
88 Face m_face;
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040089 ValidatorConfig m_validator{m_face};
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070090};
Alexander Afanasyev151a8552014-04-11 00:54:43 -070091
92} // namespace examples
93} // namespace ndn
94
95int
96main(int argc, char** argv)
97{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070098 try {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070099 ndn::examples::Consumer consumer;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700100 consumer.run();
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700101 return 0;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700102 }
103 catch (const std::exception& e) {
104 std::cerr << "ERROR: " << e.what() << std::endl;
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700105 return 1;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700106 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700107}