blob: 4a24bac4fdb6f78d0828f757513f0cedcd5f14a2 [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
15#if NDN_CPP_HAVE_CXX11
16// In the std library, the placeholders are in a different namespace than boost.
17using namespace ndn::func_lib::placeholders;
18#endif
19
20using namespace ndn;
21
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080022class Producer
23{
24public:
25 ////////////////////////////////////////////////////////////////////////////////////////
26 // CREATE TEST KEYCHAIN (THIS CODE IS ONLY FOR DEBUGGING, NOT TO BE USED IN REAL APPS //
27 ////////////////////////////////////////////////////////////////////////////////////////
28 Producer()
Yingdi Yuf4330ee2014-01-14 14:34:46 -080029 : keyChain_()
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080030 {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080031 }
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
52 void
53 onRegisterFailed(const ptr_lib::shared_ptr<const Name>&)
54 {
55 std::cerr << "ERROR: Failed to register prefix in local hub's daemon" << std::endl;
56 face_.shutdown();
57 }
58
59 void
60 listen()
61 {
62 face_.setInterestFilter("/localhost/testApp",
63 func_lib::bind(&Producer::onInterest, this, _1, _2),
64 func_lib::bind(&Producer::onRegisterFailed, this, _1));
65 face_.processEvents();
66 }
67
68private:
69 ndn::Face face_;
Alexander Afanasyev22a315f2014-01-16 21:29:37 -080070 KeyChain keyChain_;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080071
72 Buffer ndndId_;
73};
74
75int main()
76{
77 try {
78 Producer producer;
79 producer.listen();
80 }
81 catch(std::exception &e) {
82 std::cerr << "ERROR: " << e.what() << std::endl;
83 }
84 return 0;
85}