blob: 3b5ff07d5cd2d249cca028cc890ae495ba173d5f [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/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080036class Producer
37{
38public:
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080039 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080040 onInterest(const Name& name, const Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080041 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080042 std::cout << "<< I: " << interest << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070043
Alexander Afanasyev151a8552014-04-11 00:54:43 -070044 // Create new name, based on Interest's name
45 Name dataName(interest.getName());
46 dataName
47 .append("testApp") // add "testApp" component to Interest name
48 .appendVersion(); // add "version" component (current UNIX timestamp in milliseconds)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080049
Alexander Afanasyev151a8552014-04-11 00:54:43 -070050 static const std::string content = "HELLO KITTY";
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080051
Alexander Afanasyev151a8552014-04-11 00:54:43 -070052 // Create Data packet
53 Data data;
54 data.setName(dataName);
55 data.setFreshnessPeriod(time::seconds(10));
Alexander Afanasyevdbfb16f2014-04-19 17:14:31 -070056 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size());
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080057
Alexander Afanasyev151a8552014-04-11 00:54:43 -070058 // Sign Data packet with default identity
59 m_keyChain.sign(data);
60 // m_keyChain.sign(data, <identityName>);
61 // m_keyChain.sign(data, <certificate>);
62
63 // Return Data packet to the requester
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080064 std::cout << ">> D: " << data << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070065 m_face.put(data);
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080066 }
67
Alexander Afanasyeve289b532014-02-09 22:14:44 -080068
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080069 void
Alexander Afanasyev24b75c82014-05-31 15:59:31 +030070 onRegisterFailed(const std::string& reason)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080071 {
Alexander Afanasyevdbfb16f2014-04-19 17:14:31 -070072 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")"
73 << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070074 m_face.shutdown();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080075 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070076
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080077 void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070078 run()
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080079 {
Alexander Afanasyevb6783062014-06-13 13:03:25 -070080 m_face.setInterestFilter("/example/testApp",
Alexander Afanasyev151a8552014-04-11 00:54:43 -070081 bind(&Producer::onInterest, this, _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -070082 RegisterPrefixSuccessCallback(),
Alexander Afanasyev24b75c82014-05-31 15:59:31 +030083 bind(&Producer::onRegisterFailed, this, _2));
Alexander Afanasyev151a8552014-04-11 00:54:43 -070084 m_face.processEvents();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080085 }
86
87private:
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{
98 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070099 ndn::examples::Producer producer;
100 producer.run();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800101 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700102 catch (std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800103 std::cerr << "ERROR: " << e.what() << std::endl;
104 }
105 return 0;
106}