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 | /* |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Davide Pesavento | 1944281 | 2018-11-23 14:00:04 -0500 | [diff] [blame] | 24 | #include <ndn-cxx/face.hpp> |
| 25 | #include <ndn-cxx/security/key-chain.hpp> |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/security/signing-helpers.hpp> |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 28 | #include <iostream> |
| 29 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 30 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 31 | namespace ndn { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 32 | // Additional nested namespaces should be used to prevent/limit name conflicts |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 33 | namespace examples { |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 34 | |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 35 | class Producer |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 36 | { |
| 37 | public: |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 38 | void |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 39 | run() |
| 40 | { |
| 41 | m_face.setInterestFilter("/example/testApp", |
| 42 | bind(&Producer::onInterest, this, _1, _2), |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 43 | nullptr, // RegisterPrefixSuccessCallback is optional |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 44 | bind(&Producer::onRegisterFailed, this, _1, _2)); |
| 45 | m_face.processEvents(); |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | void |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 50 | onInterest(const InterestFilter&, const Interest& interest) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 51 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 52 | std::cout << ">> I: " << interest << std::endl; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 53 | |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 54 | static const std::string content("Hello, world!"); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 56 | // Create Data packet |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 57 | auto data = make_shared<Data>(interest.getName()); |
| 58 | data->setFreshnessPeriod(10_s); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 59 | data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size()); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 61 | // Sign Data packet with default identity |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 62 | m_keyChain.sign(*data); |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 63 | // 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 Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 67 | |
| 68 | // Return Data packet to the requester |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 69 | std::cout << "<< D: " << *data << std::endl; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 70 | m_face.put(*data); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 74 | onRegisterFailed(const Name& prefix, const std::string& reason) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 75 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 76 | std::cerr << "ERROR: Failed to register prefix '" << prefix |
| 77 | << "' with the local forwarder (" << reason << ")" << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 78 | m_face.shutdown(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 79 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 80 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 81 | private: |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 82 | Face m_face; |
| 83 | KeyChain m_keyChain; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 86 | } // namespace examples |
| 87 | } // namespace ndn |
| 88 | |
| 89 | int |
| 90 | main(int argc, char** argv) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 91 | { |
| 92 | try { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 93 | ndn::examples::Producer producer; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 94 | producer.run(); |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 95 | return 0; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 96 | } |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 97 | catch (const std::exception& e) { |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 98 | std::cerr << "ERROR: " << e.what() << std::endl; |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 99 | return 1; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 100 | } |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 101 | } |