Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
| 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 5 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 8 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 12 | * @author Wentao Shang <http://irl.cs.ucla.edu/~wentao/> |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 15 | #include "face.hpp" |
| 16 | #include "security/key-chain.hpp" |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 17 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 18 | namespace ndn { |
| 19 | |
| 20 | const size_t MAX_SEG_SIZE = 4096; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 21 | |
| 22 | class Producer |
| 23 | { |
| 24 | public: |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 25 | Producer(const char* name) |
| 26 | : m_name(name) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 27 | , m_isVerbose(false) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 28 | { |
| 29 | int segnum = 0; |
| 30 | char* buf = new char[MAX_SEG_SIZE]; |
| 31 | do |
| 32 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 33 | std::cin.read(buf, MAX_SEG_SIZE); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 34 | int got = std::cin.gcount(); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 35 | |
| 36 | if (got > 0) |
| 37 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 38 | shared_ptr<Data> data = |
| 39 | make_shared<Data>(Name(m_name).appendSegment(segnum)); |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 40 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 41 | data->setFreshnessPeriod(time::milliseconds(10000)); // 10 sec |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 42 | data->setContent(reinterpret_cast<const uint8_t*>(buf), got); |
| 43 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 44 | m_keychain.sign(*data); |
| 45 | m_store.push_back(data); |
| 46 | segnum++; |
| 47 | } |
| 48 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 49 | while (static_cast<bool>(std::cin)); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 51 | if (m_isVerbose) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 52 | std::cerr << "Created " << segnum << " chunks for prefix [" << m_name << "]" << std::endl; |
| 53 | } |
| 54 | |
| 55 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 56 | onInterest(const Name& name, const Interest& interest) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 57 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 58 | if (m_isVerbose) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 59 | std::cerr << "<< I: " << interest << std::endl; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 60 | |
| 61 | size_t segnum = static_cast<size_t>(interest.getName().rbegin()->toSegment()); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 62 | |
| 63 | if (segnum < m_store.size()) |
| 64 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 65 | m_face.put(*m_store[segnum]); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 70 | onRegisterFailed(const Name& prefix, const std::string& reason) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 71 | { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 72 | std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 73 | m_face.shutdown(); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 74 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 75 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 76 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 77 | run() |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 78 | { |
| 79 | if (m_store.empty()) |
| 80 | { |
| 81 | std::cerr << "Nothing to serve. Exiting." << std::endl; |
| 82 | return; |
| 83 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 84 | |
| 85 | m_face.setInterestFilter(m_name, |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 86 | bind(&Producer::onInterest, this, _1, _2), |
| 87 | bind(&Producer::onRegisterFailed, this, _1, _2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 88 | m_face.processEvents(); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 92 | Name m_name; |
| 93 | Face m_face; |
| 94 | KeyChain m_keychain; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 95 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 96 | std::vector< shared_ptr<Data> > m_store; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 98 | bool m_isVerbose; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | int |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 102 | main(int argc, char** argv) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 103 | { |
| 104 | if (argc < 2) |
| 105 | { |
| 106 | std::cerr << "Usage: ./ndnputchunks [data_prefix]\n"; |
| 107 | return -1; |
| 108 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 109 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 110 | try |
| 111 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 112 | time::steady_clock::TimePoint startTime = time::steady_clock::now(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 113 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 114 | std::cerr << "Preparing the input..." << std::endl; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 115 | Producer producer(argv[1]); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 116 | std::cerr << "Ready... (took " << (time::steady_clock::now() - startTime) << std::endl; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 117 | |
| 118 | while (true) |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 119 | { |
| 120 | try |
| 121 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 122 | // this will exit when daemon dies... so try to connect again if possible |
| 123 | producer.run(); |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 124 | } |
| 125 | catch (std::exception& e) |
| 126 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 127 | std::cerr << "ERROR: " << e.what() << std::endl; |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 128 | // and keep going |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 129 | sleep(1); |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 132 | } |
| 133 | catch (std::exception& e) |
| 134 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 135 | std::cerr << "ERROR: " << e.what() << std::endl; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 136 | } |
| 137 | return 0; |
| 138 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 139 | |
| 140 | } // namespace ndn |
| 141 | |
| 142 | int |
| 143 | main(int argc, char** argv) |
| 144 | { |
| 145 | return ndn::main(argc, argv); |
| 146 | } |