Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 15 | // correct way to include ndn-cxx headers |
| 16 | // #include <ndn-cxx/face.hpp> |
| 17 | // #include <ndn-cxx/security/key-chain.hpp> |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 18 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 19 | #include "face.hpp" |
| 20 | #include "security/key-chain.hpp" |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 22 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 23 | namespace ndn { |
| 24 | // Additional nested namespace could be used to prevent/limit name contentions |
| 25 | namespace examples { |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 27 | class Producer |
| 28 | { |
| 29 | public: |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 30 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 31 | onInterest(const Name& name, const Interest& interest) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 32 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 33 | std::cout << "<< I: " << interest << std::endl; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 35 | // Create new name, based on Interest's name |
| 36 | Name dataName(interest.getName()); |
| 37 | dataName |
| 38 | .append("testApp") // add "testApp" component to Interest name |
| 39 | .appendVersion(); // add "version" component (current UNIX timestamp in milliseconds) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 40 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 41 | static const std::string content = "HELLO KITTY"; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 43 | // Create Data packet |
| 44 | Data data; |
| 45 | data.setName(dataName); |
| 46 | data.setFreshnessPeriod(time::seconds(10)); |
Alexander Afanasyev | dbfb16f | 2014-04-19 17:14:31 -0700 | [diff] [blame] | 47 | data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size()); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 49 | // Sign Data packet with default identity |
| 50 | m_keyChain.sign(data); |
| 51 | // m_keyChain.sign(data, <identityName>); |
| 52 | // m_keyChain.sign(data, <certificate>); |
| 53 | |
| 54 | // Return Data packet to the requester |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 55 | std::cout << ">> D: " << data << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 56 | m_face.put(data); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 60 | void |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 61 | onRegisterFailed(const Name& prefix, const std::string& reason) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 62 | { |
Alexander Afanasyev | dbfb16f | 2014-04-19 17:14:31 -0700 | [diff] [blame] | 63 | std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" |
| 64 | << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 65 | m_face.shutdown(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 66 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 68 | void |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 69 | run() |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 70 | { |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 71 | m_face.setInterestFilter("/localhost/testApp", |
| 72 | bind(&Producer::onInterest, this, _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 73 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 74 | bind(&Producer::onRegisterFailed, this, _1, _2)); |
| 75 | m_face.processEvents(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | private: |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 79 | Face m_face; |
| 80 | KeyChain m_keyChain; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 83 | } // namespace examples |
| 84 | } // namespace ndn |
| 85 | |
| 86 | int |
| 87 | main(int argc, char** argv) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 88 | { |
| 89 | try { |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 90 | ndn::examples::Producer producer; |
| 91 | producer.run(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 92 | } |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 93 | catch (std::exception& e) { |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 94 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 95 | } |
| 96 | return 0; |
| 97 | } |