blob: 656ed1222c1042e985a411e39820555436fa5260 [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
Alexander Afanasyev766cea72014-04-24 19:16:42 -070024// correct way to include ndn-cxx headers
25// #include <ndn-cxx/face.hpp>
26// #include <ndn-cxx/security/key-chain.hpp>
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080027#include "face.hpp"
28#include "security/key-chain.hpp"
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080029
Davide Pesaventocdcde902017-08-23 15:40:22 -040030#include <iostream>
31
Alexander Afanasyev151a8552014-04-11 00:54:43 -070032// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
33namespace ndn {
Davide Pesaventocdcde902017-08-23 15:40:22 -040034// Additional nested namespaces can be used to prevent/limit name conflicts
Alexander Afanasyev151a8552014-04-11 00:54:43 -070035namespace examples {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080036
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070037class Producer : noncopyable
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080038{
39public:
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080040 void
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070041 run()
42 {
43 m_face.setInterestFilter("/example/testApp",
44 bind(&Producer::onInterest, this, _1, _2),
45 RegisterPrefixSuccessCallback(),
46 bind(&Producer::onRegisterFailed, this, _1, _2));
47 m_face.processEvents();
48 }
49
50private:
51 void
52 onInterest(const InterestFilter& filter, const Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080053 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080054 std::cout << "<< I: " << interest << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070055
Alexander Afanasyev151a8552014-04-11 00:54:43 -070056 // Create new name, based on Interest's name
57 Name dataName(interest.getName());
58 dataName
59 .append("testApp") // add "testApp" component to Interest name
60 .appendVersion(); // add "version" component (current UNIX timestamp in milliseconds)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080061
Alexander Afanasyev151a8552014-04-11 00:54:43 -070062 static const std::string content = "HELLO KITTY";
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080063
Alexander Afanasyev151a8552014-04-11 00:54:43 -070064 // Create Data packet
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070065 shared_ptr<Data> data = make_shared<Data>();
66 data->setName(dataName);
Davide Pesavento0f830802018-01-16 23:58:58 -050067 data->setFreshnessPeriod(10_s); // 10 seconds
68 data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size());
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080069
Alexander Afanasyev151a8552014-04-11 00:54:43 -070070 // Sign Data packet with default identity
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070071 m_keyChain.sign(*data);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070072 // m_keyChain.sign(data, <identityName>);
73 // m_keyChain.sign(data, <certificate>);
74
75 // Return Data packet to the requester
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070076 std::cout << ">> D: " << *data << std::endl;
77 m_face.put(*data);
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080078 }
79
Alexander Afanasyeve289b532014-02-09 22:14:44 -080080
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080081 void
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070082 onRegisterFailed(const Name& prefix, const std::string& reason)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080083 {
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070084 std::cerr << "ERROR: Failed to register prefix \""
85 << prefix << "\" in local hub's daemon (" << reason << ")"
Alexander Afanasyevdbfb16f2014-04-19 17:14:31 -070086 << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070087 m_face.shutdown();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080088 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070089
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080090private:
Alexander Afanasyev151a8552014-04-11 00:54:43 -070091 Face m_face;
92 KeyChain m_keyChain;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080093};
94
Alexander Afanasyev151a8552014-04-11 00:54:43 -070095} // namespace examples
96} // namespace ndn
97
98int
99main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800100{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700101 ndn::examples::Producer producer;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800102 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700103 producer.run();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800104 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700105 catch (const std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800106 std::cerr << "ERROR: " << e.what() << std::endl;
107 }
108 return 0;
109}