blob: 00f3e757c006a726ed0ea21623452a8007fc615b [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
8#include <ndn-cpp/face.hpp>
9
10#include <ndn-cpp/security/key-chain.hpp>
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080011
12#if NDN_CPP_HAVE_CXX11
13// In the std library, the placeholders are in a different namespace than boost.
14using namespace ndn::func_lib::placeholders;
15#endif
16
17using namespace ndn;
18
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080019class Producer
20{
21public:
22 ////////////////////////////////////////////////////////////////////////////////////////
23 // CREATE TEST KEYCHAIN (THIS CODE IS ONLY FOR DEBUGGING, NOT TO BE USED IN REAL APPS //
24 ////////////////////////////////////////////////////////////////////////////////////////
25 Producer()
Yingdi Yuf4330ee2014-01-14 14:34:46 -080026 : keyChain_()
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080027 {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080028 }
29 ////////////////////////////////////////////////////////////////////////////////////////
30 ////////////////////////////////////////////////////////////////////////////////////////
31 ////////////////////////////////////////////////////////////////////////////////////////
32
33 void
34 onInterest(const ptr_lib::shared_ptr<const Name> &name, const ptr_lib::shared_ptr<const Interest> &interest)
35 {
36 std::cout << "<< I: " << *interest << std::endl;
37
38 ndn::Data data(ndn::Name(interest->getName()).append("testApp").appendVersion());
39 data.setFreshnessPeriod(1000); // 10 sec
40
41 data.setContent((const uint8_t*)"HELLO KITTY", sizeof("HELLO KITTY"));
42
43 keyChain_.sign(data);
44
45 std::cout << ">> D: " << data << std::endl;
46 face_.put(data);
47 }
48
49 void
50 onRegisterFailed(const ptr_lib::shared_ptr<const Name>&)
51 {
52 std::cerr << "ERROR: Failed to register prefix in local hub's daemon" << std::endl;
53 face_.shutdown();
54 }
55
56 void
57 listen()
58 {
59 face_.setInterestFilter("/localhost/testApp",
60 func_lib::bind(&Producer::onInterest, this, _1, _2),
61 func_lib::bind(&Producer::onRegisterFailed, this, _1));
62 face_.processEvents();
63 }
64
65private:
66 ndn::Face face_;
Alexander Afanasyev22a315f2014-01-16 21:29:37 -080067 KeyChain keyChain_;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080068
69 Buffer ndndId_;
70};
71
72int main()
73{
74 try {
75 Producer producer;
76 producer.listen();
77 }
78 catch(std::exception &e) {
79 std::cerr << "ERROR: " << e.what() << std::endl;
80 }
81 return 0;
82}