blob: 4544ec07bac4eea829e71019dbb9692a305869c9 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventocdcde902017-08-23 15:40:22 -04002/*
Davide Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080022 */
23
Davide Pesavento19442812018-11-23 14:00:04 -050024#include <ndn-cxx/face.hpp>
25#include <ndn-cxx/security/key-chain.hpp>
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080026
Davide Pesaventocdcde902017-08-23 15:40:22 -040027#include <iostream>
28
Alexander Afanasyev151a8552014-04-11 00:54:43 -070029// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
30namespace ndn {
Davide Pesaventocdcde902017-08-23 15:40:22 -040031// Additional nested namespaces can be used to prevent/limit name conflicts
Alexander Afanasyev151a8552014-04-11 00:54:43 -070032namespace examples {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080033
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070034class Producer : noncopyable
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080035{
36public:
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080037 void
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070038 run()
39 {
40 m_face.setInterestFilter("/example/testApp",
41 bind(&Producer::onInterest, this, _1, _2),
42 RegisterPrefixSuccessCallback(),
43 bind(&Producer::onRegisterFailed, this, _1, _2));
44 m_face.processEvents();
45 }
46
47private:
48 void
49 onInterest(const InterestFilter& filter, const Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080050 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080051 std::cout << "<< I: " << interest << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070052
Alexander Afanasyev151a8552014-04-11 00:54:43 -070053 // Create new name, based on Interest's name
54 Name dataName(interest.getName());
55 dataName
56 .append("testApp") // add "testApp" component to Interest name
57 .appendVersion(); // add "version" component (current UNIX timestamp in milliseconds)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080058
Alexander Afanasyev151a8552014-04-11 00:54:43 -070059 static const std::string content = "HELLO KITTY";
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080060
Alexander Afanasyev151a8552014-04-11 00:54:43 -070061 // Create Data packet
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070062 shared_ptr<Data> data = make_shared<Data>();
63 data->setName(dataName);
Davide Pesavento0f830802018-01-16 23:58:58 -050064 data->setFreshnessPeriod(10_s); // 10 seconds
65 data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size());
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080066
Alexander Afanasyev151a8552014-04-11 00:54:43 -070067 // Sign Data packet with default identity
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070068 m_keyChain.sign(*data);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070069 // m_keyChain.sign(data, <identityName>);
70 // m_keyChain.sign(data, <certificate>);
71
72 // Return Data packet to the requester
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070073 std::cout << ">> D: " << *data << std::endl;
74 m_face.put(*data);
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080075 }
76
Alexander Afanasyeve289b532014-02-09 22:14:44 -080077
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080078 void
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070079 onRegisterFailed(const Name& prefix, const std::string& reason)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080080 {
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070081 std::cerr << "ERROR: Failed to register prefix \""
82 << prefix << "\" in local hub's daemon (" << reason << ")"
Alexander Afanasyevdbfb16f2014-04-19 17:14:31 -070083 << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070084 m_face.shutdown();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080085 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070086
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080087private:
Alexander Afanasyev151a8552014-04-11 00:54:43 -070088 Face m_face;
89 KeyChain m_keyChain;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080090};
91
Alexander Afanasyev151a8552014-04-11 00:54:43 -070092} // namespace examples
93} // namespace ndn
94
95int
96main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080097{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070098 ndn::examples::Producer producer;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080099 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700100 producer.run();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800101 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700102 catch (const std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800103 std::cerr << "ERROR: " << e.what() << std::endl;
104 }
105 return 0;
106}