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