blob: 9fe9cd6616bd0bb5a4920905078d2abd6109298b [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 Afanasyev766cea72014-04-24 19:16:42 -07008// correct way to include ndn-cxx headers
9// #include <ndn-cxx/face.hpp>
10// #include <ndn-cxx/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));
Alexander Afanasyevdbfb16f2014-04-19 17:14:31 -070040 data.setContent(reinterpret_cast<const uint8_t*>(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 Afanasyevdbfb16f2014-04-19 17:14:31 -070056 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")"
57 << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070058 m_face.shutdown();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080059 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070060
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080061 void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070062 run()
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080063 {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070064 m_face.setInterestFilter("/localhost/testApp",
65 bind(&Producer::onInterest, this, _1, _2),
66 bind(&Producer::onRegisterFailed, this, _1, _2));
67 m_face.processEvents();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080068 }
69
70private:
Alexander Afanasyev151a8552014-04-11 00:54:43 -070071 Face m_face;
72 KeyChain m_keyChain;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080073};
74
Alexander Afanasyev151a8552014-04-11 00:54:43 -070075} // namespace examples
76} // namespace ndn
77
78int
79main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080080{
81 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070082 ndn::examples::Producer producer;
83 producer.run();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080084 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -070085 catch (std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080086 std::cerr << "ERROR: " << e.what() << std::endl;
87 }
88 return 0;
89}