blob: bb6435b1421674f668298111fc7f78f00476f12c [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
Wentao Shang38d79682014-01-15 15:55:52 -080024#define MAX_SEG_SIZE 4096
25
26class Producer
27{
28public:
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070029 Producer(const char* name)
30 : m_name(name)
31 , m_verbose(false)
Wentao Shang38d79682014-01-15 15:55:52 -080032 {
33 int segnum = 0;
34 char* buf = new char[MAX_SEG_SIZE];
35 do
36 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070037 std::cin.read(buf, MAX_SEG_SIZE);
Wentao Shang38d79682014-01-15 15:55:52 -080038 int got = std::cin.gcount ();
39
40 if (got > 0)
41 {
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070042 ndn::shared_ptr<ndn::Data> data =
43 ndn::make_shared<ndn::Data>(ndn::Name(m_name).appendSegment(segnum));
44
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070045 data->setFreshnessPeriod(ndn::time::milliseconds(10000)); // 10 sec
46 data->setContent(reinterpret_cast<const uint8_t*>(buf), got);
47
Wentao Shang38d79682014-01-15 15:55:52 -080048 m_keychain.sign(*data);
49 m_store.push_back(data);
50 segnum++;
51 }
52 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070053 while (static_cast<bool>(std::cin));
Wentao Shang38d79682014-01-15 15:55:52 -080054
55 if (m_verbose)
56 std::cerr << "Created " << segnum << " chunks for prefix [" << m_name << "]" << std::endl;
57 }
58
59 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060 onInterest(const ndn::Name& name, const ndn::Interest& interest)
Wentao Shang38d79682014-01-15 15:55:52 -080061 {
62 if (m_verbose)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080063 std::cerr << "<< I: " << interest << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064
65 size_t segnum = static_cast<size_t>(interest.getName().rbegin()->toSegment());
Wentao Shang38d79682014-01-15 15:55:52 -080066
67 if (segnum < m_store.size())
68 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069 m_face.put(*m_store[segnum]);
Wentao Shang38d79682014-01-15 15:55:52 -080070 }
71 }
72
73 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
Wentao Shang38d79682014-01-15 15:55:52 -080075 {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080076 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070077 m_face.shutdown();
Wentao Shang38d79682014-01-15 15:55:52 -080078 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070079
Wentao Shang38d79682014-01-15 15:55:52 -080080 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070081 run()
Wentao Shang38d79682014-01-15 15:55:52 -080082 {
83 if (m_store.empty())
84 {
85 std::cerr << "Nothing to serve. Exiting." << std::endl;
86 return;
87 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088
89 m_face.setInterestFilter(m_name,
90 ndn::bind(&Producer::onInterest, this, _1, _2),
91 ndn::bind(&Producer::onRegisterFailed, this, _1, _2));
92 m_face.processEvents();
Wentao Shang38d79682014-01-15 15:55:52 -080093 }
94
95private:
96 ndn::Name m_name;
97 ndn::Face m_face;
98 ndn::KeyChain m_keychain;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070099
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800100 std::vector< ndn::shared_ptr<ndn::Data> > m_store;
Wentao Shang38d79682014-01-15 15:55:52 -0800101
102 bool m_verbose;
103};
104
105int
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700106main(int argc, char *argv[])
Wentao Shang38d79682014-01-15 15:55:52 -0800107{
108 if (argc < 2)
109 {
110 std::cerr << "Usage: ./ndnputchunks [data_prefix]\n";
111 return -1;
112 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700113
Wentao Shang38d79682014-01-15 15:55:52 -0800114 try
115 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700116 ndn::time::steady_clock::TimePoint startTime = ndn::time::steady_clock::now();
117
Wentao Shang38d79682014-01-15 15:55:52 -0800118 std::cerr << "Preparing the input..." << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700119 Producer producer(argv[1]);
120 std::cerr << "Ready... (took " << (ndn::time::steady_clock::now() - startTime) << std::endl;
121
122 while (true)
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800123 {
124 try
125 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700126 producer.run(); // this will exit when daemon dies... so try to connect again if possible
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800127 }
128 catch (std::exception& e)
129 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700130 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800131 // and keep going
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700132 sleep(1);
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800133 }
134 }
Wentao Shang38d79682014-01-15 15:55:52 -0800135 }
136 catch (std::exception& e)
137 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700138 std::cerr << "ERROR: " << e.what() << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800139 }
140 return 0;
141}