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