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