blob: acb214c414f6203daac08ef0697a9934e3f0279b [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),
Alexander Afanasyev9c578182014-05-14 17:28:28 -070073 RegisterPrefixSuccessCallback(),
Alexander Afanasyev151a8552014-04-11 00:54:43 -070074 bind(&Producer::onRegisterFailed, this, _1, _2));
75 m_face.processEvents();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080076 }
77
78private:
Alexander Afanasyev151a8552014-04-11 00:54:43 -070079 Face m_face;
80 KeyChain m_keyChain;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080081};
82
Alexander Afanasyev151a8552014-04-11 00:54:43 -070083} // namespace examples
84} // namespace ndn
85
86int
87main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080088{
89 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070090 ndn::examples::Producer producer;
91 producer.run();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080092 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -070093 catch (std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080094 std::cerr << "ERROR: " << e.what() << std::endl;
95 }
96 return 0;
97}