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 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 15 | using namespace ndn; |
| 16 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 17 | class Producer |
| 18 | { |
| 19 | public: |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 20 | Producer() |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 21 | {} |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 22 | |
| 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 | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 28 | ndn::Data data(ndn::Name(interest.getName()).append("testApp").appendVersion()); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 29 | data.setFreshnessPeriod(time::seconds(10)); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 30 | |
| 31 | data.setContent((const uint8_t*)"HELLO KITTY", sizeof("HELLO KITTY")); |
| 32 | |
| 33 | keyChain_.sign(data); |
| 34 | |
| 35 | std::cout << ">> D: " << data << std::endl; |
| 36 | face_.put(data); |
| 37 | } |
| 38 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 40 | void |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 41 | onRegisterFailed (const ndn::Name& prefix, const std::string& reason) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 42 | { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 43 | std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl; |
| 44 | face_.shutdown (); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
| 48 | listen() |
| 49 | { |
| 50 | face_.setInterestFilter("/localhost/testApp", |
| 51 | func_lib::bind(&Producer::onInterest, this, _1, _2), |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 52 | func_lib::bind(&Producer::onRegisterFailed, this, _1, _2)); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 53 | face_.processEvents(); |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | ndn::Face face_; |
Alexander Afanasyev | 22a315f | 2014-01-16 21:29:37 -0800 | [diff] [blame] | 58 | KeyChain keyChain_; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 59 | |
| 60 | Buffer ndndId_; |
| 61 | }; |
| 62 | |
| 63 | int main() |
| 64 | { |
| 65 | try { |
| 66 | Producer producer; |
| 67 | producer.listen(); |
| 68 | } |
| 69 | catch(std::exception &e) { |
| 70 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 71 | } |
| 72 | return 0; |
| 73 | } |