blob: 81df7df982777b35f51d277ce912248863111de3 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevc4b75982014-01-09 14:51:45 -08002/**
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -07003 * Copyright (c) 2013-2015 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 Afanasyevc4b75982014-01-09 14:51:45 -080027
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080028#include "face.hpp"
29#include "security/key-chain.hpp"
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080030
Alexander Afanasyev151a8552014-04-11 00:54:43 -070031// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
32namespace ndn {
33// Additional nested namespace could be used to prevent/limit name contentions
34namespace examples {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080035
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070036class Producer : noncopyable
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080037{
38public:
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080039 void
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070040 run()
41 {
42 m_face.setInterestFilter("/example/testApp",
43 bind(&Producer::onInterest, this, _1, _2),
44 RegisterPrefixSuccessCallback(),
45 bind(&Producer::onRegisterFailed, this, _1, _2));
46 m_face.processEvents();
47 }
48
49private:
50 void
51 onInterest(const InterestFilter& filter, const Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080052 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080053 std::cout << "<< I: " << interest << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070054
Alexander Afanasyev151a8552014-04-11 00:54:43 -070055 // Create new name, based on Interest's name
56 Name dataName(interest.getName());
57 dataName
58 .append("testApp") // add "testApp" component to Interest name
59 .appendVersion(); // add "version" component (current UNIX timestamp in milliseconds)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080060
Alexander Afanasyev151a8552014-04-11 00:54:43 -070061 static const std::string content = "HELLO KITTY";
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080062
Alexander Afanasyev151a8552014-04-11 00:54:43 -070063 // Create Data packet
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070064 shared_ptr<Data> data = make_shared<Data>();
65 data->setName(dataName);
66 data->setFreshnessPeriod(time::seconds(10));
67 data->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size());
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080068
Alexander Afanasyev151a8552014-04-11 00:54:43 -070069 // Sign Data packet with default identity
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070070 m_keyChain.sign(*data);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070071 // m_keyChain.sign(data, <identityName>);
72 // m_keyChain.sign(data, <certificate>);
73
74 // Return Data packet to the requester
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070075 std::cout << ">> D: " << *data << std::endl;
76 m_face.put(*data);
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080077 }
78
Alexander Afanasyeve289b532014-02-09 22:14:44 -080079
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080080 void
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070081 onRegisterFailed(const Name& prefix, const std::string& reason)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080082 {
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070083 std::cerr << "ERROR: Failed to register prefix \""
84 << prefix << "\" in local hub's daemon (" << reason << ")"
Alexander Afanasyevdbfb16f2014-04-19 17:14:31 -070085 << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070086 m_face.shutdown();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080087 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070088
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080089private:
Alexander Afanasyev151a8552014-04-11 00:54:43 -070090 Face m_face;
91 KeyChain m_keyChain;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080092};
93
Alexander Afanasyev151a8552014-04-11 00:54:43 -070094} // namespace examples
95} // namespace ndn
96
97int
98main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080099{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700100 ndn::examples::Producer producer;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800101 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700102 producer.run();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800103 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700104 catch (const std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800105 std::cerr << "ERROR: " << e.what() << std::endl;
106 }
107 return 0;
108}