blob: c09873908209c9bf0e4554b3dbbff5c37e9f66bb [file] [log] [blame]
Alexander Afanasyevc4b75982014-01-09 14:51:45 -08001/* -*- 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 Afanasyev09c613f2014-01-29 00:23:58 -08008// correct way to include NDN-CPP headers
9// #include <ndn-cpp-dev/face.hpp>
10// #include <ndn-cpp-dev/security/key-chain.hpp>
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080011
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080012#include "face.hpp"
13#include "security/key-chain.hpp"
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080014
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080015using namespace ndn;
16
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080017class Producer
18{
19public:
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080020 Producer()
Yingdi Yube4150e2014-02-18 13:02:46 -080021 {}
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080022
23 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080024 onInterest(const Name& name, const Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080025 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080026 std::cout << "<< I: " << interest << std::endl;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080027
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080028 ndn::Data data(ndn::Name(interest.getName()).append("testApp").appendVersion());
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080029 data.setFreshnessPeriod(1000); // 10 sec
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 Afanasyeve289b532014-02-09 22:14:44 -080039
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080040 void
Alexander Afanasyeve289b532014-02-09 22:14:44 -080041 onRegisterFailed (const ndn::Name& prefix, const std::string& reason)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080042 {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080043 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl;
44 face_.shutdown ();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080045 }
46
47 void
48 listen()
49 {
50 face_.setInterestFilter("/localhost/testApp",
51 func_lib::bind(&Producer::onInterest, this, _1, _2),
Alexander Afanasyeve289b532014-02-09 22:14:44 -080052 func_lib::bind(&Producer::onRegisterFailed, this, _1, _2));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080053 face_.processEvents();
54 }
55
56private:
57 ndn::Face face_;
Alexander Afanasyev22a315f2014-01-16 21:29:37 -080058 KeyChain keyChain_;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080059
60 Buffer ndndId_;
61};
62
63int 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}