blob: 53ccdc0a6963a97f0cbee2c06c7039d9c0643920 [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
Alexander Afanasyeve289b532014-02-09 22:14:44 -080052
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080053 void
Alexander Afanasyeve289b532014-02-09 22:14:44 -080054 onRegisterFailed (const ndn::Name& prefix, const std::string& reason)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080055 {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080056 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl;
57 face_.shutdown ();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080058 }
59
60 void
61 listen()
62 {
63 face_.setInterestFilter("/localhost/testApp",
64 func_lib::bind(&Producer::onInterest, this, _1, _2),
Alexander Afanasyeve289b532014-02-09 22:14:44 -080065 func_lib::bind(&Producer::onRegisterFailed, this, _1, _2));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080066 face_.processEvents();
67 }
68
69private:
70 ndn::Face face_;
Alexander Afanasyev22a315f2014-01-16 21:29:37 -080071 KeyChain keyChain_;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080072
73 Buffer ndndId_;
74};
75
76int main()
77{
78 try {
79 Producer producer;
80 producer.listen();
81 }
82 catch(std::exception &e) {
83 std::cerr << "ERROR: " << e.what() << std::endl;
84 }
85 return 0;
86}