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