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