Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 21 | #include "face.hpp" |
| 22 | #include "security/key-chain.hpp" |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 23 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 24 | #define MAX_SEG_SIZE 4096 |
| 25 | |
| 26 | class Producer |
| 27 | { |
| 28 | public: |
| 29 | Producer (const char* name) |
| 30 | : m_name (name) |
| 31 | , m_verbose (false) |
| 32 | { |
| 33 | int segnum = 0; |
| 34 | char* buf = new char[MAX_SEG_SIZE]; |
| 35 | do |
| 36 | { |
| 37 | std::cin.read (buf, MAX_SEG_SIZE); |
| 38 | int got = std::cin.gcount (); |
| 39 | |
| 40 | if (got > 0) |
| 41 | { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 42 | ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data> (ndn::Name(m_name).appendSegment (segnum)); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 43 | data->setFreshnessPeriod (10000); // 10 sec |
| 44 | data->setContent (reinterpret_cast<const uint8_t*>(buf), got); |
| 45 | |
| 46 | 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 Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 58 | onInterest (const ndn::shared_ptr<const ndn::Name>& name, const ndn::shared_ptr<const ndn::Interest>& interest) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 59 | { |
| 60 | if (m_verbose) |
| 61 | std::cerr << "<< I: " << *interest << std::endl; |
| 62 | |
| 63 | size_t segnum = static_cast<size_t>(interest->getName ().rbegin ()->toSegment ()); |
| 64 | |
| 65 | if (segnum < m_store.size()) |
| 66 | { |
| 67 | m_face.put (*m_store[segnum]); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 72 | onRegisterFailed (const ndn::Name& prefix, const std::string& reason) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 73 | { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 74 | std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 75 | m_face.shutdown (); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | run () |
| 80 | { |
| 81 | if (m_store.empty()) |
| 82 | { |
| 83 | std::cerr << "Nothing to serve. Exiting." << std::endl; |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | m_face.setInterestFilter (m_name, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 88 | ndn::bind (&Producer::onInterest, this, _1, _2), |
| 89 | ndn::bind (&Producer::onRegisterFailed, this, _1, _2)); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 90 | m_face.processEvents (); |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | ndn::Name m_name; |
| 95 | ndn::Face m_face; |
| 96 | ndn::KeyChain m_keychain; |
| 97 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 98 | std::vector< ndn::shared_ptr<ndn::Data> > m_store; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 99 | |
| 100 | bool m_verbose; |
| 101 | }; |
| 102 | |
| 103 | int |
| 104 | main (int argc, char *argv[]) |
| 105 | { |
| 106 | if (argc < 2) |
| 107 | { |
| 108 | std::cerr << "Usage: ./ndnputchunks [data_prefix]\n"; |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | try |
| 113 | { |
| 114 | ndn::MillisecondsSince1970 time = ndn::getNow(); |
| 115 | |
| 116 | std::cerr << "Preparing the input..." << std::endl; |
| 117 | Producer producer (argv[1]); |
| 118 | std::cerr << "Ready... (took " << ((ndn::getNow() - time)/1000) << " seconds)" << std::endl; |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 119 | while(true) |
| 120 | { |
| 121 | try |
| 122 | { |
| 123 | producer.run (); // this will exit when daemon dies... so try to connect again if possible |
| 124 | } |
| 125 | catch (std::exception& e) |
| 126 | { |
| 127 | std::cerr << "ERROR: " << e.what () << std::endl; |
| 128 | // and keep going |
| 129 | sleep (1); |
| 130 | } |
| 131 | } |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 132 | } |
| 133 | catch (std::exception& e) |
| 134 | { |
| 135 | std::cerr << "ERROR: " << e.what () << std::endl; |
| 136 | } |
| 137 | return 0; |
| 138 | } |