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