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 | /* |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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> |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 26 | |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 27 | #include <iostream> |
| 28 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 29 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 30 | namespace ndn { |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 31 | // Additional nested namespaces can be used to prevent/limit name conflicts |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 32 | namespace examples { |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 33 | |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 34 | class Producer : noncopyable |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 35 | { |
| 36 | public: |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 37 | void |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 38 | run() |
| 39 | { |
| 40 | m_face.setInterestFilter("/example/testApp", |
| 41 | bind(&Producer::onInterest, this, _1, _2), |
| 42 | RegisterPrefixSuccessCallback(), |
| 43 | bind(&Producer::onRegisterFailed, this, _1, _2)); |
| 44 | m_face.processEvents(); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | void |
| 49 | onInterest(const InterestFilter& filter, const Interest& interest) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 50 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 51 | std::cout << "<< I: " << interest << std::endl; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 52 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 53 | // Create new name, based on Interest's name |
| 54 | Name dataName(interest.getName()); |
| 55 | dataName |
| 56 | .append("testApp") // add "testApp" component to Interest name |
| 57 | .appendVersion(); // add "version" component (current UNIX timestamp in milliseconds) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 59 | static const std::string content = "HELLO KITTY"; |
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 | // Create Data packet |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 62 | shared_ptr<Data> data = make_shared<Data>(); |
| 63 | data->setName(dataName); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 64 | data->setFreshnessPeriod(10_s); // 10 seconds |
| 65 | data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size()); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 67 | // Sign Data packet with default identity |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 68 | m_keyChain.sign(*data); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 69 | // m_keyChain.sign(data, <identityName>); |
| 70 | // m_keyChain.sign(data, <certificate>); |
| 71 | |
| 72 | // Return Data packet to the requester |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 73 | std::cout << ">> D: " << *data << std::endl; |
| 74 | m_face.put(*data); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 78 | void |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 79 | onRegisterFailed(const Name& prefix, const std::string& reason) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 80 | { |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 81 | std::cerr << "ERROR: Failed to register prefix \"" |
| 82 | << prefix << "\" in local hub's daemon (" << reason << ")" |
Alexander Afanasyev | dbfb16f | 2014-04-19 17:14:31 -0700 | [diff] [blame] | 83 | << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 84 | m_face.shutdown(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 85 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 87 | private: |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 88 | Face m_face; |
| 89 | KeyChain m_keyChain; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 92 | } // namespace examples |
| 93 | } // namespace ndn |
| 94 | |
| 95 | int |
| 96 | main(int argc, char** argv) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 97 | { |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 98 | ndn::examples::Producer producer; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 99 | try { |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 100 | producer.run(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 101 | } |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 102 | catch (const std::exception& e) { |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 103 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 104 | } |
| 105 | return 0; |
| 106 | } |