Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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 | /** |
Alexander Afanasyev | 73e3004 | 2015-09-17 17:09:51 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 20 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 21 | * @author Wentao Shang <http://irl.cs.ucla.edu/~wentao/> |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 24 | #include "face.hpp" |
| 25 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 26 | namespace ndn { |
| 27 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 28 | class Consumer |
| 29 | { |
| 30 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 31 | Consumer(const std::string& dataName, |
| 32 | size_t pipeSize, size_t nTotalSegments, |
Alexander Afanasyev | 117f5ef | 2015-06-03 15:07:24 -0700 | [diff] [blame] | 33 | bool mustBeFresh = true) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 34 | : m_dataName(dataName) |
| 35 | , m_pipeSize(pipeSize) |
| 36 | , m_nTotalSegments(nTotalSegments) |
| 37 | , m_nextSegment(0) |
| 38 | , m_totalSize(0) |
| 39 | , m_isOutputEnabled(false) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 40 | , m_mustBeFresh(mustBeFresh) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
| 44 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 45 | enableOutput() |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 46 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 47 | m_isOutputEnabled = true; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 51 | run(); |
| 52 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 53 | private: |
| 54 | void |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 55 | onData(Data& data); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 56 | |
| 57 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 58 | onTimeout(const Interest& interest); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 60 | Face m_face; |
| 61 | Name m_dataName; |
| 62 | size_t m_pipeSize; |
| 63 | size_t m_nTotalSegments; |
| 64 | size_t m_nextSegment; |
| 65 | size_t m_totalSize; |
| 66 | bool m_isOutputEnabled; // set to false by default |
Alexander Afanasyev | c1ebbe9 | 2014-01-16 22:24:34 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | c1ebbe9 | 2014-01-16 22:24:34 -0800 | [diff] [blame] | 68 | bool m_mustBeFresh; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 72 | Consumer::run() |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 73 | { |
| 74 | try |
| 75 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 76 | for (size_t i = 0; i < m_pipeSize; i++) |
| 77 | { |
| 78 | Interest interest(Name(m_dataName).appendSegment(m_nextSegment++)); |
| 79 | interest.setInterestLifetime(time::milliseconds(4000)); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 80 | interest.setMustBeFresh(m_mustBeFresh); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 82 | m_face.expressInterest(interest, |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 83 | bind(&Consumer::onData, this, _2), |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 84 | bind(&Consumer::onTimeout, this, _1)); |
| 85 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 86 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 87 | // processEvents will block until the requested data received or timeout occurs |
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 | catch (std::exception& e) |
| 91 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 92 | std::cerr << "ERROR: " << e.what() << std::endl; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | void |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 97 | Consumer::onData(Data& data) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 98 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 99 | const Block& content = data.getContent(); |
| 100 | const Name& name = data.getName(); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 102 | if (m_isOutputEnabled) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 103 | { |
| 104 | std::cout.write(reinterpret_cast<const char*>(content.value()), content.value_size()); |
| 105 | } |
| 106 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 107 | m_totalSize += content.value_size(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 109 | if (name[-1].toSegment() + 1 == m_nTotalSegments) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 110 | { |
| 111 | std::cerr << "Last segment received." << std::endl; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 112 | std::cerr << "Total # bytes of content received: " << m_totalSize << std::endl; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 113 | } |
| 114 | else |
| 115 | { |
| 116 | // Send interest for next segment |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 117 | Interest interest(Name(m_dataName).appendSegment(m_nextSegment++)); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 118 | interest.setInterestLifetime(time::milliseconds(4000)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 119 | interest.setMustBeFresh(m_mustBeFresh); |
| 120 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 121 | m_face.expressInterest(interest, |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 122 | bind(&Consumer::onData, this, _2), |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 123 | bind(&Consumer::onTimeout, this, _1)); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 129 | Consumer::onTimeout(const Interest& interest) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 130 | { |
| 131 | //XXX: currently no retrans |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 132 | std::cerr << "TIMEOUT: last interest sent for segment #" << (m_nextSegment - 1) << std::endl; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
| 136 | int |
| 137 | usage(const std::string &filename) |
| 138 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 139 | std::cerr << "Usage: \n " |
| 140 | << filename << " [-p pipeSize] [-c nTotalSegmentsment] [-o] /ndn/name\n"; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | int |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | main(int argc, char** argv) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 147 | { |
| 148 | std::string name; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 149 | int pipeSize = 1; |
| 150 | int nTotalSegments = std::numeric_limits<int>::max(); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 151 | bool output = false; |
| 152 | |
| 153 | int opt; |
| 154 | while ((opt = getopt(argc, argv, "op:c:")) != -1) |
| 155 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 156 | switch (opt) |
| 157 | { |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 158 | case 'p': |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 159 | pipeSize = atoi(optarg); |
| 160 | if (pipeSize <= 0) |
| 161 | pipeSize = 1; |
| 162 | std::cerr << "main(): set pipe size = " << pipeSize << std::endl; |
| 163 | break; |
| 164 | case 'c': |
| 165 | nTotalSegments = atoi(optarg); |
| 166 | if (nTotalSegments <= 0) |
| 167 | nTotalSegments = 1; |
| 168 | std::cerr << "main(): set total seg = " << nTotalSegments << std::endl; |
| 169 | break; |
| 170 | case 'o': |
| 171 | output = true; |
| 172 | break; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 173 | default: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 174 | return usage(argv[0]); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | if (optind < argc) |
| 179 | { |
| 180 | name = argv[optind]; |
| 181 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 182 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 183 | if (name.empty()) |
| 184 | { |
| 185 | return usage(argv[0]); |
| 186 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 187 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 188 | Consumer consumer(name, pipeSize, nTotalSegments); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 189 | |
| 190 | if (output) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 191 | consumer.enableOutput(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 192 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 193 | consumer.run(); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 194 | |
| 195 | return 0; |
| 196 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 197 | |
| 198 | } // namespace ndn |
| 199 | |
| 200 | int |
| 201 | main(int argc, char** argv) |
| 202 | { |
| 203 | return ndn::main(argc, argv); |
| 204 | } |