blob: 532b9524d9dd0186dfc7b027e9f071afc4397195 [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/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043 int 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070065 onInterest(const Name& name, 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 Afanasyeve289b532014-02-09 22:14:44 -080081 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070082 m_face.shutdown();
Wentao Shang38d79682014-01-15 15:55:52 -080083 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084
Wentao Shang38d79682014-01-15 15:55:52 -080085 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070086 run()
Wentao Shang38d79682014-01-15 15:55:52 -080087 {
88 if (m_store.empty())
89 {
90 std::cerr << "Nothing to serve. Exiting." << std::endl;
91 return;
92 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070093
94 m_face.setInterestFilter(m_name,
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095 bind(&Producer::onInterest, this, _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -070096 RegisterPrefixSuccessCallback(),
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097 bind(&Producer::onRegisterFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070098 m_face.processEvents();
Wentao Shang38d79682014-01-15 15:55:52 -080099 }
100
101private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700102 Name m_name;
103 Face m_face;
104 KeyChain m_keychain;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700105
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700106 std::vector< shared_ptr<Data> > m_store;
Wentao Shang38d79682014-01-15 15:55:52 -0800107
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700108 bool m_isVerbose;
Wentao Shang38d79682014-01-15 15:55:52 -0800109};
110
111int
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112main(int argc, char** argv)
Wentao Shang38d79682014-01-15 15:55:52 -0800113{
114 if (argc < 2)
115 {
116 std::cerr << "Usage: ./ndnputchunks [data_prefix]\n";
117 return -1;
118 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700119
Wentao Shang38d79682014-01-15 15:55:52 -0800120 try
121 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122 time::steady_clock::TimePoint startTime = time::steady_clock::now();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700123
Wentao Shang38d79682014-01-15 15:55:52 -0800124 std::cerr << "Preparing the input..." << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700125 Producer producer(argv[1]);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700126 std::cerr << "Ready... (took " << (time::steady_clock::now() - startTime) << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700127
128 while (true)
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800129 {
130 try
131 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700132 // this will exit when daemon dies... so try to connect again if possible
133 producer.run();
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800134 }
135 catch (std::exception& e)
136 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700137 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800138 // and keep going
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700139 sleep(1);
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800140 }
141 }
Wentao Shang38d79682014-01-15 15:55:52 -0800142 }
143 catch (std::exception& e)
144 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700145 std::cerr << "ERROR: " << e.what() << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800146 }
147 return 0;
148}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700149
150} // namespace ndn
151
152int
153main(int argc, char** argv)
154{
155 return ndn::main(argc, argv);
156}