blob: 4defc29eda45118df442bfb350f04505cba75087 [file] [log] [blame]
Alexander Afanasyevc4b75982014-01-09 14:51:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyev151a8552014-04-11 00:54:43 -07003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevc4b75982014-01-09 14:51:45 -08004 * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev151a8552014-04-11 00:54:43 -07005 * BSD License, see COPYING for copyright and distribution information.
Alexander Afanasyevc4b75982014-01-09 14:51:45 -08006 */
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 Afanasyev151a8552014-04-11 00:54:43 -070015// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
16namespace ndn {
17// Additional nested namespace could be used to prevent/limit name contentions
18namespace examples {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080019
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080020class Producer
21{
22public:
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080023 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 Afanasyevb1db7c62014-04-03 14:57:25 -070027
Alexander Afanasyev151a8552014-04-11 00:54:43 -070028 // Create new name, based on Interest's name
29 Name dataName(interest.getName());
30 dataName
31 .append("testApp") // add "testApp" component to Interest name
32 .appendVersion(); // add "version" component (current UNIX timestamp in milliseconds)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080033
Alexander Afanasyev151a8552014-04-11 00:54:43 -070034 static const std::string content = "HELLO KITTY";
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080035
Alexander Afanasyev151a8552014-04-11 00:54:43 -070036 // Create Data packet
37 Data data;
38 data.setName(dataName);
39 data.setFreshnessPeriod(time::seconds(10));
40 data.setContent(Block(content.c_str(), content.size()));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080041
Alexander Afanasyev151a8552014-04-11 00:54:43 -070042 // Sign Data packet with default identity
43 m_keyChain.sign(data);
44 // m_keyChain.sign(data, <identityName>);
45 // m_keyChain.sign(data, <certificate>);
46
47 // Return Data packet to the requester
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080048 std::cout << ">> D: " << data << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070049 m_face.put(data);
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080050 }
51
Alexander Afanasyeve289b532014-02-09 22:14:44 -080052
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080053 void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070054 onRegisterFailed(const 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;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070057 m_face.shutdown();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080058 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070059
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080060 void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070061 run()
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080062 {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070063 m_face.setInterestFilter("/localhost/testApp",
64 bind(&Producer::onInterest, this, _1, _2),
65 bind(&Producer::onRegisterFailed, this, _1, _2));
66 m_face.processEvents();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080067 }
68
69private:
Alexander Afanasyev151a8552014-04-11 00:54:43 -070070 Face m_face;
71 KeyChain m_keyChain;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080072};
73
Alexander Afanasyev151a8552014-04-11 00:54:43 -070074} // namespace examples
75} // namespace ndn
76
77int
78main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080079{
80 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070081 ndn::examples::Producer producer;
82 producer.run();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080083 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -070084 catch (std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080085 std::cerr << "ERROR: " << e.what() << std::endl;
86 }
87 return 0;
88}