blob: d88cc86695854a8f20b1fe8d31413248f51bc39c [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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080022 */
23
Davide Pesavento19442812018-11-23 14:00:04 -050024#include <ndn-cxx/face.hpp>
25#include <ndn-cxx/security/key-chain.hpp>
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070026#include <ndn-cxx/security/signing-helpers.hpp>
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080027
Davide Pesaventocdcde902017-08-23 15:40:22 -040028#include <iostream>
29
Alexander Afanasyev151a8552014-04-11 00:54:43 -070030// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
31namespace ndn {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070032// Additional nested namespaces should be used to prevent/limit name conflicts
Alexander Afanasyev151a8552014-04-11 00:54:43 -070033namespace examples {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080034
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070035class Producer
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080036{
37public:
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080038 void
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070039 run()
40 {
41 m_face.setInterestFilter("/example/testApp",
Davide Pesavento2e481fc2021-07-02 18:20:03 -040042 std::bind(&Producer::onInterest, this, _1, _2),
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070043 nullptr, // RegisterPrefixSuccessCallback is optional
Davide Pesavento2e481fc2021-07-02 18:20:03 -040044 std::bind(&Producer::onRegisterFailed, this, _1, _2));
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070045 m_face.processEvents();
46 }
47
48private:
49 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070050 onInterest(const InterestFilter&, const Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080051 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070052 std::cout << ">> I: " << interest << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070053
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070054 static const std::string content("Hello, world!");
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080055
Alexander Afanasyev151a8552014-04-11 00:54:43 -070056 // Create Data packet
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070057 auto data = make_shared<Data>(interest.getName());
58 data->setFreshnessPeriod(10_s);
Davide Pesavento0f830802018-01-16 23:58:58 -050059 data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size());
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080060
Alexander Afanasyev151a8552014-04-11 00:54:43 -070061 // Sign Data packet with default identity
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070062 m_keyChain.sign(*data);
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070063 // m_keyChain.sign(*data, signingByIdentity(<identityName>));
64 // m_keyChain.sign(*data, signingByKey(<keyName>));
65 // m_keyChain.sign(*data, signingByCertificate(<certName>));
66 // m_keyChain.sign(*data, signingWithSha256());
Alexander Afanasyev151a8552014-04-11 00:54:43 -070067
68 // Return Data packet to the requester
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070069 std::cout << "<< D: " << *data << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070070 m_face.put(*data);
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080071 }
72
73 void
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070074 onRegisterFailed(const Name& prefix, const std::string& reason)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080075 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070076 std::cerr << "ERROR: Failed to register prefix '" << prefix
77 << "' with the local forwarder (" << reason << ")" << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070078 m_face.shutdown();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080079 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070080
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080081private:
Alexander Afanasyev151a8552014-04-11 00:54:43 -070082 Face m_face;
83 KeyChain m_keyChain;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080084};
85
Alexander Afanasyev151a8552014-04-11 00:54:43 -070086} // namespace examples
87} // namespace ndn
88
89int
90main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080091{
92 try {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070093 ndn::examples::Producer producer;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070094 producer.run();
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070095 return 0;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080096 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070097 catch (const std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080098 std::cerr << "ERROR: " << e.what() << std::endl;
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070099 return 1;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800100 }
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800101}