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 | |
| 21 | #include <ndn-cpp/face.hpp> |
| 22 | #include <ndn-cpp/security/key-chain.hpp> |
| 23 | |
| 24 | #if NDN_CPP_HAVE_CXX11 |
| 25 | // In the std library, the placeholders are in a different namespace than boost. |
| 26 | using namespace ndn::func_lib::placeholders; |
| 27 | #endif |
| 28 | |
| 29 | #define MAX_SEG_SIZE 4096 |
| 30 | |
| 31 | class Producer |
| 32 | { |
| 33 | public: |
| 34 | Producer (const char* name) |
| 35 | : m_name (name) |
| 36 | , m_verbose (false) |
| 37 | { |
| 38 | int segnum = 0; |
| 39 | char* buf = new char[MAX_SEG_SIZE]; |
| 40 | do |
| 41 | { |
| 42 | std::cin.read (buf, MAX_SEG_SIZE); |
| 43 | int got = std::cin.gcount (); |
| 44 | |
| 45 | if (got > 0) |
| 46 | { |
| 47 | ndn::ptr_lib::shared_ptr<ndn::Data> data = ndn::ptr_lib::make_shared<ndn::Data> (ndn::Name(m_name).appendSegment (segnum)); |
| 48 | data->setFreshnessPeriod (10000); // 10 sec |
| 49 | data->setContent (reinterpret_cast<const uint8_t*>(buf), got); |
| 50 | |
| 51 | m_keychain.sign(*data); |
| 52 | m_store.push_back(data); |
| 53 | segnum++; |
| 54 | } |
| 55 | } |
| 56 | while (std::cin); |
| 57 | |
| 58 | if (m_verbose) |
| 59 | std::cerr << "Created " << segnum << " chunks for prefix [" << m_name << "]" << std::endl; |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | onInterest (const ndn::ptr_lib::shared_ptr<const ndn::Name>& name, const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest) |
| 64 | { |
| 65 | if (m_verbose) |
| 66 | std::cerr << "<< I: " << *interest << std::endl; |
| 67 | |
| 68 | size_t segnum = static_cast<size_t>(interest->getName ().rbegin ()->toSegment ()); |
| 69 | |
| 70 | if (segnum < m_store.size()) |
| 71 | { |
| 72 | m_face.put (*m_store[segnum]); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | onRegisterFailed (const ndn::ptr_lib::shared_ptr<const ndn::Name>&) |
| 78 | { |
| 79 | std::cerr << "ERROR: Failed to register prefix in local hub's daemon" << std::endl; |
| 80 | m_face.shutdown (); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | run () |
| 85 | { |
| 86 | if (m_store.empty()) |
| 87 | { |
| 88 | std::cerr << "Nothing to serve. Exiting." << std::endl; |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | m_face.setInterestFilter (m_name, |
| 93 | ndn::func_lib::bind (&Producer::onInterest, this, _1, _2), |
| 94 | ndn::func_lib::bind (&Producer::onRegisterFailed, this, _1)); |
| 95 | m_face.processEvents (); |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | ndn::Name m_name; |
| 100 | ndn::Face m_face; |
| 101 | ndn::KeyChain m_keychain; |
| 102 | |
| 103 | std::vector< ndn::ptr_lib::shared_ptr<ndn::Data> > m_store; |
| 104 | |
| 105 | bool m_verbose; |
| 106 | }; |
| 107 | |
| 108 | int |
| 109 | main (int argc, char *argv[]) |
| 110 | { |
| 111 | if (argc < 2) |
| 112 | { |
| 113 | std::cerr << "Usage: ./ndnputchunks [data_prefix]\n"; |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | try |
| 118 | { |
| 119 | ndn::MillisecondsSince1970 time = ndn::getNow(); |
| 120 | |
| 121 | std::cerr << "Preparing the input..." << std::endl; |
| 122 | Producer producer (argv[1]); |
| 123 | std::cerr << "Ready... (took " << ((ndn::getNow() - time)/1000) << " seconds)" << std::endl; |
| 124 | producer.run (); |
| 125 | } |
| 126 | catch (std::exception& e) |
| 127 | { |
| 128 | std::cerr << "ERROR: " << e.what () << std::endl; |
| 129 | } |
| 130 | return 0; |
| 131 | } |