blob: e03e6cf31eff1b2f98ea2e9c040e739609ba9e77 [file] [log] [blame]
Wentao Shang38d79682014-01-15 15:55:52 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Wentao Shang <wentao@cs.ucla.edu>
19 */
20
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080021#include "face.hpp"
22#include "security/key-chain.hpp"
Wentao Shang38d79682014-01-15 15:55:52 -080023
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070024namespace ndn {
25
26const size_t MAX_SEG_SIZE = 4096;
Wentao Shang38d79682014-01-15 15:55:52 -080027
28class Producer
29{
30public:
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070031 Producer(const char* name)
32 : m_name(name)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070033 , m_isVerbose(false)
Wentao Shang38d79682014-01-15 15:55:52 -080034 {
35 int segnum = 0;
36 char* buf = new char[MAX_SEG_SIZE];
37 do
38 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070039 std::cin.read(buf, MAX_SEG_SIZE);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070040 int got = std::cin.gcount();
Wentao Shang38d79682014-01-15 15:55:52 -080041
42 if (got > 0)
43 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044 shared_ptr<Data> data =
45 make_shared<Data>(Name(m_name).appendSegment(segnum));
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070046
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047 data->setFreshnessPeriod(time::milliseconds(10000)); // 10 sec
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070048 data->setContent(reinterpret_cast<const uint8_t*>(buf), got);
49
Wentao Shang38d79682014-01-15 15:55:52 -080050 m_keychain.sign(*data);
51 m_store.push_back(data);
52 segnum++;
53 }
54 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070055 while (static_cast<bool>(std::cin));
Wentao Shang38d79682014-01-15 15:55:52 -080056
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070057 if (m_isVerbose)
Wentao Shang38d79682014-01-15 15:55:52 -080058 std::cerr << "Created " << segnum << " chunks for prefix [" << m_name << "]" << std::endl;
59 }
60
61 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070062 onInterest(const Name& name, const Interest& interest)
Wentao Shang38d79682014-01-15 15:55:52 -080063 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070064 if (m_isVerbose)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080065 std::cerr << "<< I: " << interest << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066
67 size_t segnum = static_cast<size_t>(interest.getName().rbegin()->toSegment());
Wentao Shang38d79682014-01-15 15:55:52 -080068
69 if (segnum < m_store.size())
70 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071 m_face.put(*m_store[segnum]);
Wentao Shang38d79682014-01-15 15:55:52 -080072 }
73 }
74
75 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076 onRegisterFailed(const Name& prefix, const std::string& reason)
Wentao Shang38d79682014-01-15 15:55:52 -080077 {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080078 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070079 m_face.shutdown();
Wentao Shang38d79682014-01-15 15:55:52 -080080 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070081
Wentao Shang38d79682014-01-15 15:55:52 -080082 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070083 run()
Wentao Shang38d79682014-01-15 15:55:52 -080084 {
85 if (m_store.empty())
86 {
87 std::cerr << "Nothing to serve. Exiting." << std::endl;
88 return;
89 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070090
91 m_face.setInterestFilter(m_name,
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070092 bind(&Producer::onInterest, this, _1, _2),
93 bind(&Producer::onRegisterFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070094 m_face.processEvents();
Wentao Shang38d79682014-01-15 15:55:52 -080095 }
96
97private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098 Name m_name;
99 Face m_face;
100 KeyChain m_keychain;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700101
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700102 std::vector< shared_ptr<Data> > m_store;
Wentao Shang38d79682014-01-15 15:55:52 -0800103
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104 bool m_isVerbose;
Wentao Shang38d79682014-01-15 15:55:52 -0800105};
106
107int
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700108main(int argc, char** argv)
Wentao Shang38d79682014-01-15 15:55:52 -0800109{
110 if (argc < 2)
111 {
112 std::cerr << "Usage: ./ndnputchunks [data_prefix]\n";
113 return -1;
114 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700115
Wentao Shang38d79682014-01-15 15:55:52 -0800116 try
117 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700118 time::steady_clock::TimePoint startTime = time::steady_clock::now();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700119
Wentao Shang38d79682014-01-15 15:55:52 -0800120 std::cerr << "Preparing the input..." << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700121 Producer producer(argv[1]);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122 std::cerr << "Ready... (took " << (time::steady_clock::now() - startTime) << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700123
124 while (true)
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800125 {
126 try
127 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700128 // this will exit when daemon dies... so try to connect again if possible
129 producer.run();
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800130 }
131 catch (std::exception& e)
132 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700133 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800134 // and keep going
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700135 sleep(1);
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800136 }
137 }
Wentao Shang38d79682014-01-15 15:55:52 -0800138 }
139 catch (std::exception& e)
140 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700141 std::cerr << "ERROR: " << e.what() << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800142 }
143 return 0;
144}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700145
146} // namespace ndn
147
148int
149main(int argc, char** argv)
150{
151 return ndn::main(argc, argv);
152}