blob: a977322bc06e5db26fb579992711e302ed0e832f [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Weiwei Liu1e787c42016-04-22 10:13:41 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Wentao Shang38d79682014-01-15 15:55:52 -08004 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Wentao Shang38d79682014-01-15 15:55:52 -08006 *
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.
Wentao Shang38d79682014-01-15 15:55:52 -080020 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070021 * @author Wentao Shang <http://irl.cs.ucla.edu/~wentao/>
Wentao Shang38d79682014-01-15 15:55:52 -080022 */
23
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080024#include "face.hpp"
25#include "security/key-chain.hpp"
Wentao Shang38d79682014-01-15 15:55:52 -080026
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070027namespace ndn {
28
29const size_t MAX_SEG_SIZE = 4096;
Wentao Shang38d79682014-01-15 15:55:52 -080030
31class Producer
32{
33public:
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070034 Producer(const char* name)
35 : m_name(name)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070036 , m_isVerbose(false)
Wentao Shang38d79682014-01-15 15:55:52 -080037 {
38 int segnum = 0;
39 char* buf = new char[MAX_SEG_SIZE];
40 do
41 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070042 std::cin.read(buf, MAX_SEG_SIZE);
Davide Pesavento8f5cbdc2015-09-13 00:59:28 +020043 ssize_t got = std::cin.gcount();
Wentao Shang38d79682014-01-15 15:55:52 -080044
45 if (got > 0)
46 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047 shared_ptr<Data> data =
48 make_shared<Data>(Name(m_name).appendSegment(segnum));
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070049
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070050 data->setFreshnessPeriod(time::milliseconds(10000)); // 10 sec
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070051 data->setContent(reinterpret_cast<const uint8_t*>(buf), got);
52
Wentao Shang38d79682014-01-15 15:55:52 -080053 m_keychain.sign(*data);
54 m_store.push_back(data);
55 segnum++;
56 }
57 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070058 while (static_cast<bool>(std::cin));
Wentao Shang38d79682014-01-15 15:55:52 -080059
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060 if (m_isVerbose)
Wentao Shang38d79682014-01-15 15:55:52 -080061 std::cerr << "Created " << segnum << " chunks for prefix [" << m_name << "]" << std::endl;
62 }
63
64 void
Alexander Afanasyev24b75c82014-05-31 15:59:31 +030065 onInterest(const Interest& interest)
Wentao Shang38d79682014-01-15 15:55:52 -080066 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067 if (m_isVerbose)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080068 std::cerr << "<< I: " << interest << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069
70 size_t segnum = static_cast<size_t>(interest.getName().rbegin()->toSegment());
Wentao Shang38d79682014-01-15 15:55:52 -080071
72 if (segnum < m_store.size())
73 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 m_face.put(*m_store[segnum]);
Wentao Shang38d79682014-01-15 15:55:52 -080075 }
76 }
77
78 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070079 onRegisterFailed(const Name& prefix, const std::string& reason)
Wentao Shang38d79682014-01-15 15:55:52 -080080 {
Alexander Afanasyev24b75c82014-05-31 15:59:31 +030081 std::cerr << "ERROR: Failed to register prefix '"
82 << prefix << "' in local hub's daemon (" << reason << ")"
83 << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084 m_face.shutdown();
Wentao Shang38d79682014-01-15 15:55:52 -080085 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070086
Wentao Shang38d79682014-01-15 15:55:52 -080087 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088 run()
Wentao Shang38d79682014-01-15 15:55:52 -080089 {
90 if (m_store.empty())
91 {
92 std::cerr << "Nothing to serve. Exiting." << std::endl;
93 return;
94 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070095
96 m_face.setInterestFilter(m_name,
Alexander Afanasyev24b75c82014-05-31 15:59:31 +030097 bind(&Producer::onInterest, this, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -070098 RegisterPrefixSuccessCallback(),
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070099 bind(&Producer::onRegisterFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700100 m_face.processEvents();
Wentao Shang38d79682014-01-15 15:55:52 -0800101 }
102
103private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104 Name m_name;
105 Face m_face;
106 KeyChain m_keychain;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700107
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700108 std::vector< shared_ptr<Data> > m_store;
Wentao Shang38d79682014-01-15 15:55:52 -0800109
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700110 bool m_isVerbose;
Wentao Shang38d79682014-01-15 15:55:52 -0800111};
112
113int
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700114main(int argc, char** argv)
Wentao Shang38d79682014-01-15 15:55:52 -0800115{
116 if (argc < 2)
117 {
118 std::cerr << "Usage: ./ndnputchunks [data_prefix]\n";
119 return -1;
120 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700121
Wentao Shang38d79682014-01-15 15:55:52 -0800122 try
123 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124 time::steady_clock::TimePoint startTime = time::steady_clock::now();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700125
Wentao Shang38d79682014-01-15 15:55:52 -0800126 std::cerr << "Preparing the input..." << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700127 Producer producer(argv[1]);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700128 std::cerr << "Ready... (took " << (time::steady_clock::now() - startTime) << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700129
130 while (true)
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800131 {
132 try
133 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134 // this will exit when daemon dies... so try to connect again if possible
135 producer.run();
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800136 }
137 catch (std::exception& e)
138 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700139 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800140 // and keep going
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700141 sleep(1);
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800142 }
143 }
Wentao Shang38d79682014-01-15 15:55:52 -0800144 }
145 catch (std::exception& e)
146 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700147 std::cerr << "ERROR: " << e.what() << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800148 }
149 return 0;
150}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700151
152} // namespace ndn
153
154int
155main(int argc, char** argv)
156{
Weiwei Liu1e787c42016-04-22 10:13:41 -0700157 std::cerr << "ndnputchunks3 is deprecated. Use ndnputchunks program from ndn-tools repository.\n" << std::endl;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700158 return ndn::main(argc, argv);
159}