blob: d6317984106231dce8a3fdcd0e8595edab4bc24f [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 Afanasyeve289b532014-02-09 22:14:44 -080042 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data> (ndn::Name(m_name).appendSegment (segnum));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070043 data->setFreshnessPeriod(ndn::time::milliseconds(10000)); // 10 sec
44 data->setContent(reinterpret_cast<const uint8_t*>(buf), got);
45
Wentao Shang38d79682014-01-15 15:55:52 -080046 m_keychain.sign(*data);
47 m_store.push_back(data);
48 segnum++;
49 }
50 }
51 while (std::cin);
52
53 if (m_verbose)
54 std::cerr << "Created " << segnum << " chunks for prefix [" << m_name << "]" << std::endl;
55 }
56
57 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070058 onInterest(const ndn::Name& name, const ndn::Interest& interest)
Wentao Shang38d79682014-01-15 15:55:52 -080059 {
60 if (m_verbose)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080061 std::cerr << "<< I: " << interest << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070062
63 size_t segnum = static_cast<size_t>(interest.getName().rbegin()->toSegment());
Wentao Shang38d79682014-01-15 15:55:52 -080064
65 if (segnum < m_store.size())
66 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067 m_face.put(*m_store[segnum]);
Wentao Shang38d79682014-01-15 15:55:52 -080068 }
69 }
70
71 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072 onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
Wentao Shang38d79682014-01-15 15:55:52 -080073 {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080074 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070075 m_face.shutdown();
Wentao Shang38d79682014-01-15 15:55:52 -080076 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070077
Wentao Shang38d79682014-01-15 15:55:52 -080078 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070079 run()
Wentao Shang38d79682014-01-15 15:55:52 -080080 {
81 if (m_store.empty())
82 {
83 std::cerr << "Nothing to serve. Exiting." << std::endl;
84 return;
85 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070086
87 m_face.setInterestFilter(m_name,
88 ndn::bind(&Producer::onInterest, this, _1, _2),
89 ndn::bind(&Producer::onRegisterFailed, this, _1, _2));
90 m_face.processEvents();
Wentao Shang38d79682014-01-15 15:55:52 -080091 }
92
93private:
94 ndn::Name m_name;
95 ndn::Face m_face;
96 ndn::KeyChain m_keychain;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070097
Alexander Afanasyeve289b532014-02-09 22:14:44 -080098 std::vector< ndn::shared_ptr<ndn::Data> > m_store;
Wentao Shang38d79682014-01-15 15:55:52 -080099
100 bool m_verbose;
101};
102
103int
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700104main(int argc, char *argv[])
Wentao Shang38d79682014-01-15 15:55:52 -0800105{
106 if (argc < 2)
107 {
108 std::cerr << "Usage: ./ndnputchunks [data_prefix]\n";
109 return -1;
110 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700111
Wentao Shang38d79682014-01-15 15:55:52 -0800112 try
113 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700114 ndn::time::steady_clock::TimePoint startTime = ndn::time::steady_clock::now();
115
Wentao Shang38d79682014-01-15 15:55:52 -0800116 std::cerr << "Preparing the input..." << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700117 Producer producer(argv[1]);
118 std::cerr << "Ready... (took " << (ndn::time::steady_clock::now() - startTime) << std::endl;
119
120 while (true)
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800121 {
122 try
123 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700124 producer.run(); // this will exit when daemon dies... so try to connect again if possible
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800125 }
126 catch (std::exception& e)
127 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700128 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800129 // and keep going
130 sleep (1);
131 }
132 }
Wentao Shang38d79682014-01-15 15:55:52 -0800133 }
134 catch (std::exception& e)
135 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700136 std::cerr << "ERROR: " << e.what() << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800137 }
138 return 0;
139}