Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 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 | |
| 15 | #if NDN_CPP_HAVE_CXX11 |
| 16 | // In the std library, the placeholders are in a different namespace than boost. |
| 17 | using namespace ndn::func_lib::placeholders; |
| 18 | #endif |
| 19 | |
| 20 | using namespace ndn; |
| 21 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 22 | class Producer |
| 23 | { |
| 24 | public: |
| 25 | //////////////////////////////////////////////////////////////////////////////////////// |
| 26 | // CREATE TEST KEYCHAIN (THIS CODE IS ONLY FOR DEBUGGING, NOT TO BE USED IN REAL APPS // |
| 27 | //////////////////////////////////////////////////////////////////////////////////////// |
| 28 | Producer() |
Yingdi Yu | f4330ee | 2014-01-14 14:34:46 -0800 | [diff] [blame] | 29 | : keyChain_() |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 30 | { |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 31 | } |
| 32 | //////////////////////////////////////////////////////////////////////////////////////// |
| 33 | //////////////////////////////////////////////////////////////////////////////////////// |
| 34 | //////////////////////////////////////////////////////////////////////////////////////// |
| 35 | |
| 36 | void |
| 37 | onInterest(const ptr_lib::shared_ptr<const Name> &name, const ptr_lib::shared_ptr<const Interest> &interest) |
| 38 | { |
| 39 | std::cout << "<< I: " << *interest << std::endl; |
| 40 | |
| 41 | ndn::Data data(ndn::Name(interest->getName()).append("testApp").appendVersion()); |
| 42 | data.setFreshnessPeriod(1000); // 10 sec |
| 43 | |
| 44 | data.setContent((const uint8_t*)"HELLO KITTY", sizeof("HELLO KITTY")); |
| 45 | |
| 46 | keyChain_.sign(data); |
| 47 | |
| 48 | std::cout << ">> D: " << data << std::endl; |
| 49 | face_.put(data); |
| 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 | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 54 | onRegisterFailed (const ndn::Name& prefix, const std::string& reason) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 55 | { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 56 | std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl; |
| 57 | face_.shutdown (); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
| 61 | listen() |
| 62 | { |
| 63 | face_.setInterestFilter("/localhost/testApp", |
| 64 | func_lib::bind(&Producer::onInterest, this, _1, _2), |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 65 | func_lib::bind(&Producer::onRegisterFailed, this, _1, _2)); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 66 | face_.processEvents(); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | ndn::Face face_; |
Alexander Afanasyev | 22a315f | 2014-01-16 21:29:37 -0800 | [diff] [blame] | 71 | KeyChain keyChain_; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 72 | |
| 73 | Buffer ndndId_; |
| 74 | }; |
| 75 | |
| 76 | int main() |
| 77 | { |
| 78 | try { |
| 79 | Producer producer; |
| 80 | producer.listen(); |
| 81 | } |
| 82 | catch(std::exception &e) { |
| 83 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 84 | } |
| 85 | return 0; |
| 86 | } |