blob: ae79848660ba28fa906d9e3a83884e1365794f6b [file] [log] [blame]
Alexander Afanasyevc3d29902018-06-29 18:20:55 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2018, Regents of the University of California
4 *
5 * NAC library is free software: you can redistribute it and/or modify it under the
6 * terms of the GNU Lesser General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option) any later version.
8 *
9 * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12 *
13 * You should have received copies of the GNU General Public License and GNU Lesser
14 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 * See AUTHORS.md for complete list of NAC library authors and contributors.
18 */
19
20#include <ndn-cxx/face.hpp>
21#include <ndn-cxx/security/validator-config.hpp>
22
23// #include <ndn-nac/decryptor.hpp>
24#include "decryptor.hpp"
25
26#include <iostream>
27
28// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
29namespace ndn {
30namespace nac {
31// Additional nested namespaces can be used to prevent/limit name conflicts
32namespace examples {
33
34class Consumer : noncopyable
35{
36public:
37 Consumer()
38 : m_face(nullptr, m_keyChain)
39 , m_validator(m_face)
40 , m_decryptor(m_keyChain.getPib().getDefaultIdentity().getDefaultKey(), m_validator, m_keyChain, m_face)
41 {
42 m_validator.load(R"CONF(
43 trust-anchor
44 {
45 type any
46 }
47 )CONF", "fake-config");
48 }
49
50 void
51 run()
52 {
53 Interest interest(Name("/example/testApp/randomData"));
54 interest.setInterestLifetime(2_s); // 2 seconds
55 interest.setMustBeFresh(true);
56
57 m_face.expressInterest(interest,
58 bind(&Consumer::onData, this, _1, _2),
59 bind(&Consumer::onNack, this, _1, _2),
60 bind(&Consumer::onTimeout, this, _1));
61
62 std::cout << "Sending " << interest << std::endl;
63
64 // processEvents will block until the requested data received or timeout occurs
65 m_face.processEvents();
66 }
67
68private:
69 void
70 onData(const Interest& interest, const Data& data)
71 {
72 m_validator.validate(data,
73 [=] (const Data& data) {
74 m_decryptor.decrypt(data.getContent().blockFromValue(),
75 [=] (ConstBufferPtr content) {
76 std::cout << "Decrypted content: "
77 << std::string(reinterpret_cast<const char*>(content->data()), content->size())
78 << std::endl;
79 },
80 [=] (const ErrorCode&, const std::string& error) {
81 std::cerr << "Cannot decrypt data: " << error << std::endl;
82 });
83 },
84 [=] (const Data& data, const ValidationError& error) {
85 std::cerr << "Cannot validate retrieved data: " << error << std::endl;
86 });
87 }
88
89 void
90 onNack(const Interest& interest, const lp::Nack& nack)
91 {
92 std::cout << "received Nack with reason " << nack.getReason()
93 << " for interest " << interest << std::endl;
94 }
95
96 void
97 onTimeout(const Interest& interest)
98 {
99 std::cout << "Timeout " << interest << std::endl;
100 }
101
102private:
103 KeyChain m_keyChain;
104 Face m_face;
105 ValidatorConfig m_validator;
106 Decryptor m_decryptor;
107};
108
109} // namespace examples
110} // namespace nac
111} // namespace ndn
112
113int
114main(int argc, char** argv)
115{
116 ndn::nac::examples::Consumer consumer;
117 try {
118 consumer.run();
119 }
120 catch (const std::exception& e) {
121 std::cerr << "ERROR: " << e.what() << std::endl;
122 }
123 return 0;
124}