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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 33 | int scope = -1, 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_scope(scope) |
| 41 | , m_mustBeFresh(mustBeFresh) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 46 | enableOutput() |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 47 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 48 | m_isOutputEnabled = true; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 52 | run(); |
| 53 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 54 | private: |
| 55 | void |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 56 | onData(Data& data); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 57 | |
| 58 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | onTimeout(const Interest& interest); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 61 | Face m_face; |
| 62 | Name m_dataName; |
| 63 | size_t m_pipeSize; |
| 64 | size_t m_nTotalSegments; |
| 65 | size_t m_nextSegment; |
| 66 | size_t m_totalSize; |
| 67 | bool m_isOutputEnabled; // set to false by default |
Alexander Afanasyev | c1ebbe9 | 2014-01-16 22:24:34 -0800 | [diff] [blame] | 68 | |
| 69 | int m_scope; |
| 70 | bool m_mustBeFresh; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 74 | Consumer::run() |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 75 | { |
| 76 | try |
| 77 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 78 | for (size_t i = 0; i < m_pipeSize; i++) |
| 79 | { |
| 80 | Interest interest(Name(m_dataName).appendSegment(m_nextSegment++)); |
| 81 | interest.setInterestLifetime(time::milliseconds(4000)); |
| 82 | if (m_scope >= 0) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 83 | interest.setScope(m_scope); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 84 | interest.setMustBeFresh(m_mustBeFresh); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 86 | m_face.expressInterest(interest, |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 87 | bind(&Consumer::onData, this, _2), |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 88 | bind(&Consumer::onTimeout, this, _1)); |
| 89 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 90 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 91 | // processEvents will block until the requested data received or timeout occurs |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 92 | m_face.processEvents(); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 93 | } |
| 94 | catch (std::exception& e) |
| 95 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 96 | std::cerr << "ERROR: " << e.what() << std::endl; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | void |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 101 | Consumer::onData(Data& data) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 103 | const Block& content = data.getContent(); |
| 104 | const Name& name = data.getName(); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 106 | if (m_isOutputEnabled) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 107 | { |
| 108 | std::cout.write(reinterpret_cast<const char*>(content.value()), content.value_size()); |
| 109 | } |
| 110 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 111 | m_totalSize += content.value_size(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 112 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 113 | if (name[-1].toSegment() + 1 == m_nTotalSegments) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 114 | { |
| 115 | std::cerr << "Last segment received." << std::endl; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 116 | std::cerr << "Total # bytes of content received: " << m_totalSize << std::endl; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 117 | } |
| 118 | else |
| 119 | { |
| 120 | // Send interest for next segment |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 121 | Interest interest(Name(m_dataName).appendSegment(m_nextSegment++)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 122 | if (m_scope >= 0) |
| 123 | interest.setScope(m_scope); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 124 | interest.setInterestLifetime(time::milliseconds(4000)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 125 | interest.setMustBeFresh(m_mustBeFresh); |
| 126 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 127 | m_face.expressInterest(interest, |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 128 | bind(&Consumer::onData, this, _2), |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 129 | bind(&Consumer::onTimeout, this, _1)); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 135 | Consumer::onTimeout(const Interest& interest) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 136 | { |
| 137 | //XXX: currently no retrans |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 138 | 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] | 139 | } |
| 140 | |
| 141 | |
| 142 | int |
| 143 | usage(const std::string &filename) |
| 144 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 145 | std::cerr << "Usage: \n " |
| 146 | << filename << " [-p pipeSize] [-c nTotalSegmentsment] [-o] /ndn/name\n"; |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | int |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 152 | main(int argc, char** argv) |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 153 | { |
| 154 | std::string name; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 155 | int pipeSize = 1; |
| 156 | int nTotalSegments = std::numeric_limits<int>::max(); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 157 | bool output = false; |
| 158 | |
| 159 | int opt; |
| 160 | while ((opt = getopt(argc, argv, "op:c:")) != -1) |
| 161 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 162 | switch (opt) |
| 163 | { |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 164 | case 'p': |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 165 | pipeSize = atoi(optarg); |
| 166 | if (pipeSize <= 0) |
| 167 | pipeSize = 1; |
| 168 | std::cerr << "main(): set pipe size = " << pipeSize << std::endl; |
| 169 | break; |
| 170 | case 'c': |
| 171 | nTotalSegments = atoi(optarg); |
| 172 | if (nTotalSegments <= 0) |
| 173 | nTotalSegments = 1; |
| 174 | std::cerr << "main(): set total seg = " << nTotalSegments << std::endl; |
| 175 | break; |
| 176 | case 'o': |
| 177 | output = true; |
| 178 | break; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 179 | default: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 180 | return usage(argv[0]); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | if (optind < argc) |
| 185 | { |
| 186 | name = argv[optind]; |
| 187 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 188 | |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 189 | if (name.empty()) |
| 190 | { |
| 191 | return usage(argv[0]); |
| 192 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 193 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 194 | Consumer consumer(name, pipeSize, nTotalSegments); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 195 | |
| 196 | if (output) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 197 | consumer.enableOutput(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 199 | consumer.run(); |
Wentao Shang | 38d7968 | 2014-01-15 15:55:52 -0800 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 203 | |
| 204 | } // namespace ndn |
| 205 | |
| 206 | int |
| 207 | main(int argc, char** argv) |
| 208 | { |
| 209 | return ndn::main(argc, argv); |
| 210 | } |