blob: d6ca24808deffaff62d421c79e2cbc6de15b68d6 [file] [log] [blame]
Alexander Afanasyevc4b75982014-01-09 14:51:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080013 */
14
Alexander Afanasyev766cea72014-04-24 19:16:42 -070015// correct way to include ndn-cxx headers
16// #include <ndn-cxx/face.hpp>
17// #include <ndn-cxx/security/key-chain.hpp>
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080018
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080019#include "face.hpp"
20#include "security/key-chain.hpp"
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080021
Alexander Afanasyev151a8552014-04-11 00:54:43 -070022// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
23namespace ndn {
24// Additional nested namespace could be used to prevent/limit name contentions
25namespace examples {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080026
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080027class Producer
28{
29public:
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080030 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080031 onInterest(const Name& name, const Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080032 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080033 std::cout << "<< I: " << interest << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070034
Alexander Afanasyev151a8552014-04-11 00:54:43 -070035 // Create new name, based on Interest's name
36 Name dataName(interest.getName());
37 dataName
38 .append("testApp") // add "testApp" component to Interest name
39 .appendVersion(); // add "version" component (current UNIX timestamp in milliseconds)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080040
Alexander Afanasyev151a8552014-04-11 00:54:43 -070041 static const std::string content = "HELLO KITTY";
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080042
Alexander Afanasyev151a8552014-04-11 00:54:43 -070043 // Create Data packet
44 Data data;
45 data.setName(dataName);
46 data.setFreshnessPeriod(time::seconds(10));
Alexander Afanasyevdbfb16f2014-04-19 17:14:31 -070047 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size());
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080048
Alexander Afanasyev151a8552014-04-11 00:54:43 -070049 // Sign Data packet with default identity
50 m_keyChain.sign(data);
51 // m_keyChain.sign(data, <identityName>);
52 // m_keyChain.sign(data, <certificate>);
53
54 // Return Data packet to the requester
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080055 std::cout << ">> D: " << data << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070056 m_face.put(data);
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080057 }
58
Alexander Afanasyeve289b532014-02-09 22:14:44 -080059
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080060 void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070061 onRegisterFailed(const Name& prefix, const std::string& reason)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080062 {
Alexander Afanasyevdbfb16f2014-04-19 17:14:31 -070063 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")"
64 << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070065 m_face.shutdown();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080066 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070067
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080068 void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070069 run()
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080070 {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070071 m_face.setInterestFilter("/localhost/testApp",
72 bind(&Producer::onInterest, this, _1, _2),
73 bind(&Producer::onRegisterFailed, this, _1, _2));
74 m_face.processEvents();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080075 }
76
77private:
Alexander Afanasyev151a8552014-04-11 00:54:43 -070078 Face m_face;
79 KeyChain m_keyChain;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080080};
81
Alexander Afanasyev151a8552014-04-11 00:54:43 -070082} // namespace examples
83} // namespace ndn
84
85int
86main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080087{
88 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070089 ndn::examples::Producer producer;
90 producer.run();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080091 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -070092 catch (std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080093 std::cerr << "ERROR: " << e.what() << std::endl;
94 }
95 return 0;
96}