blob: 664381210a0d059fab5da5d4a7de32f3cb050874 [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 Pesavento2e481fc2021-07-02 18:20:03 -04003 * Copyright (c) 2013-2021 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);
47 interest.setCanBePrefix(false);
48 interest.setMustBeFresh(true);
49 interest.setInterestLifetime(6_s); // The default is 4 seconds
50
51 std::cout << "Sending Interest " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070052 m_face.expressInterest(interest,
Davide Pesavento2e481fc2021-07-02 18:20:03 -040053 std::bind(&Consumer::onData, this, _1, _2),
54 std::bind(&Consumer::onNack, this, _1, _2),
55 std::bind(&Consumer::onTimeout, this, _1));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080056
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070057 // processEvents will block until the requested data is received or a timeout occurs
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070058 m_face.processEvents();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080059 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070060
61private:
62 void
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040063 onData(const Interest&, const Data& data)
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070064 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070065 std::cout << "Received Data " << data << std::endl;
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040066
67 m_validator.validate(data,
68 [] (const Data&) {
69 std::cout << "Data conforms to trust schema" << std::endl;
70 },
71 [] (const Data&, const security::ValidationError& error) {
72 std::cout << "Error authenticating data: " << error << std::endl;
73 });
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080074 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070075
76 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070077 onNack(const Interest&, const lp::Nack& nack) const
Weiwei Liuab9aad02016-10-09 13:56:04 -070078 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070079 std::cout << "Received Nack with reason " << nack.getReason() << std::endl;
Weiwei Liuab9aad02016-10-09 13:56:04 -070080 }
81
82 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070083 onTimeout(const Interest& interest) const
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070084 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070085 std::cout << "Timeout for " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070086 }
87
88private:
89 Face m_face;
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040090 ValidatorConfig m_validator{m_face};
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070091};
Alexander Afanasyev151a8552014-04-11 00:54:43 -070092
93} // namespace examples
94} // namespace ndn
95
96int
97main(int argc, char** argv)
98{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070099 try {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700100 ndn::examples::Consumer consumer;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700101 consumer.run();
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700102 return 0;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700103 }
104 catch (const std::exception& e) {
105 std::cerr << "ERROR: " << e.what() << std::endl;
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700106 return 1;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700107 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700108}