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 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 36 | class Producer |
| 37 | { |
| 38 | public: |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 39 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 40 | onInterest(const Name& name, const Interest& interest) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 41 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 42 | std::cout << "<< I: " << interest << std::endl; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 44 | // Create new name, based on Interest's name |
| 45 | Name dataName(interest.getName()); |
| 46 | dataName |
| 47 | .append("testApp") // add "testApp" component to Interest name |
| 48 | .appendVersion(); // add "version" component (current UNIX timestamp in milliseconds) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 50 | static const std::string content = "HELLO KITTY"; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 52 | // Create Data packet |
| 53 | Data data; |
| 54 | data.setName(dataName); |
| 55 | data.setFreshnessPeriod(time::seconds(10)); |
Alexander Afanasyev | dbfb16f | 2014-04-19 17:14:31 -0700 | [diff] [blame] | 56 | data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size()); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 58 | // Sign Data packet with default identity |
| 59 | m_keyChain.sign(data); |
| 60 | // m_keyChain.sign(data, <identityName>); |
| 61 | // m_keyChain.sign(data, <certificate>); |
| 62 | |
| 63 | // Return Data packet to the requester |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 64 | std::cout << ">> D: " << data << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 65 | m_face.put(data); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 69 | void |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 70 | onRegisterFailed(const Name& prefix, const std::string& reason) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 71 | { |
Alexander Afanasyev | dbfb16f | 2014-04-19 17:14:31 -0700 | [diff] [blame] | 72 | std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" |
| 73 | << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 74 | m_face.shutdown(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 75 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 76 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 77 | void |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 78 | run() |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 79 | { |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 80 | m_face.setInterestFilter("/localhost/testApp", |
| 81 | bind(&Producer::onInterest, this, _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 82 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 83 | bind(&Producer::onRegisterFailed, this, _1, _2)); |
| 84 | m_face.processEvents(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 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 | { |
| 98 | try { |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 99 | ndn::examples::Producer producer; |
| 100 | producer.run(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 101 | } |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 102 | catch (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 | } |