Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | 1688ded | 2015-03-29 10:09:26 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 12 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 13 | * |
| 14 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | /** |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 26 | * Copyright (c) 2014, Regents of the University of California, |
| 27 | * Arizona Board of Regents, |
| 28 | * Colorado State University, |
| 29 | * University Pierre & Marie Curie, Sorbonne University, |
| 30 | * Washington University in St. Louis, |
| 31 | * Beijing Institute of Technology, |
| 32 | * The University of Memphis |
| 33 | * |
| 34 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 35 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 36 | * |
| 37 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 38 | * of the GNU General Public License as published by the Free Software Foundation, |
| 39 | * either version 3 of the License, or (at your option) any later version. |
| 40 | * |
| 41 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 42 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 43 | * PURPOSE. See the GNU General Public License for more details. |
| 44 | * |
| 45 | * You should have received a copy of the GNU General Public License along with |
| 46 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 47 | * |
| 48 | * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 49 | */ |
| 50 | |
Junxiao Shi | 1753afb | 2015-04-17 20:59:50 -0700 | [diff] [blame] | 51 | #include "core/version.hpp" |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 52 | #include "core/common.hpp" |
| 53 | |
| 54 | #include <ndn-cxx/util/io.hpp> |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 56 | namespace ndn { |
| 57 | namespace peek { |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 58 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 59 | class NdnPeek : boost::noncopyable |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 60 | { |
| 61 | public: |
| 62 | explicit |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 63 | NdnPeek(char* programName) |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 64 | : isVerbose(false) |
| 65 | , mustBeFresh(false) |
| 66 | , wantRightmostChild(false) |
| 67 | , wantPayloadOnly(false) |
| 68 | , m_programName(programName) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 69 | , m_minSuffixComponents(-1) |
| 70 | , m_maxSuffixComponents(-1) |
| 71 | , m_interestLifetime(-1) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 72 | , m_timeout(-1) |
| 73 | , m_prefixName("") |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 74 | , m_didReceiveData(false) |
Teng Liang | 2864b9d | 2016-03-17 11:17:38 -0700 | [diff] [blame^] | 75 | , m_didReceiveNack(false) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 76 | { |
| 77 | } |
| 78 | |
| 79 | void |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 80 | usage(std::ostream& os ,const boost::program_options::options_description& options) const |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 81 | { |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 82 | os << "Usage: " << m_programName << " [options] ndn:/name\n" |
| 83 | " Fetch one data item matching the name prefix and write it to standard output\n" |
| 84 | "\n" |
| 85 | << options; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void |
| 89 | setMinSuffixComponents(int minSuffixComponents) |
| 90 | { |
| 91 | if (minSuffixComponents < 0) |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 92 | throw std::out_of_range("'minSuffixComponents' must be a non-negative integer"); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 94 | m_minSuffixComponents = minSuffixComponents; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | setMaxSuffixComponents(int maxSuffixComponents) |
| 99 | { |
| 100 | if (maxSuffixComponents < 0) |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 101 | throw std::out_of_range("'maxSuffixComponents' must be a non-negative integer"); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 102 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 103 | m_maxSuffixComponents = maxSuffixComponents; |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | setInterestLifetime(int interestLifetime) |
| 108 | { |
| 109 | if (interestLifetime < 0) |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 110 | throw std::out_of_range("'lifetime' must be a non-negative integer"); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 111 | |
| 112 | m_interestLifetime = time::milliseconds(interestLifetime); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 116 | setTimeout(int timeout) |
| 117 | { |
| 118 | if (timeout < 0) |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 119 | throw std::out_of_range("'timeout' must be a non-negative integer"); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 120 | |
| 121 | m_timeout = time::milliseconds(timeout); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void |
Teng Liang | 799390a | 2016-03-12 23:14:29 -0700 | [diff] [blame] | 125 | setLink(const std::string& file) |
| 126 | { |
| 127 | m_link = io::load<Link>(file); |
| 128 | if (m_link == nullptr) |
| 129 | throw std::runtime_error(file + " is either nonreadable or nonparseable"); |
| 130 | } |
| 131 | |
| 132 | void |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 133 | setPrefixName(const std::string& prefixName) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 134 | { |
| 135 | m_prefixName = prefixName; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 138 | time::milliseconds |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 139 | getDefaultInterestLifetime() |
| 140 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 141 | return time::seconds(4); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 144 | Interest |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 145 | createInterestPacket() |
| 146 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 147 | Name interestName(m_prefixName); |
| 148 | Interest interestPacket(interestName); |
| 149 | |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 150 | if (mustBeFresh) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 151 | interestPacket.setMustBeFresh(true); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 152 | |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 153 | if (wantRightmostChild) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 154 | interestPacket.setChildSelector(1); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 155 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 156 | if (m_minSuffixComponents >= 0) |
| 157 | interestPacket.setMinSuffixComponents(m_minSuffixComponents); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 158 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 159 | if (m_maxSuffixComponents >= 0) |
| 160 | interestPacket.setMaxSuffixComponents(m_maxSuffixComponents); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 161 | |
| 162 | if (m_interestLifetime < time::milliseconds::zero()) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 163 | interestPacket.setInterestLifetime(getDefaultInterestLifetime()); |
| 164 | else |
| 165 | interestPacket.setInterestLifetime(m_interestLifetime); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 166 | |
Teng Liang | 799390a | 2016-03-12 23:14:29 -0700 | [diff] [blame] | 167 | if (m_link != nullptr) |
| 168 | interestPacket.setLink(m_link->wireEncode()); |
| 169 | |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 170 | if (isVerbose) { |
Junxiao Shi | e02fb52 | 2015-10-18 08:45:09 -0700 | [diff] [blame] | 171 | std::cerr << "INTEREST: " << interestPacket << std::endl; |
| 172 | } |
| 173 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 174 | return interestPacket; |
| 175 | } |
| 176 | |
| 177 | void |
Teng Liang | 2864b9d | 2016-03-17 11:17:38 -0700 | [diff] [blame^] | 178 | onData(const Interest& interest, const Data& data) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 179 | { |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 180 | m_didReceiveData = true; |
Junxiao Shi | e02fb52 | 2015-10-18 08:45:09 -0700 | [diff] [blame] | 181 | |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 182 | if (isVerbose) { |
Junxiao Shi | e02fb52 | 2015-10-18 08:45:09 -0700 | [diff] [blame] | 183 | std::cerr << "DATA, RTT: " |
| 184 | << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count() |
| 185 | << "ms" << std::endl; |
| 186 | } |
| 187 | |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 188 | if (wantPayloadOnly) { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 189 | const Block& block = data.getContent(); |
| 190 | std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size()); |
| 191 | } |
| 192 | else { |
| 193 | const Block& block = data.wireEncode(); |
| 194 | std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size()); |
| 195 | } |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void |
Teng Liang | 2864b9d | 2016-03-17 11:17:38 -0700 | [diff] [blame^] | 199 | onNack(const Interest& interest, const lp::Nack& nack) |
| 200 | { |
| 201 | m_didReceiveNack = true; |
| 202 | lp::NackHeader header = nack.getHeader(); |
| 203 | |
| 204 | if (isVerbose) { |
| 205 | std::cerr << "NACK, RTT: " |
| 206 | << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count() |
| 207 | << "ms" << std::endl; |
| 208 | } |
| 209 | |
| 210 | if (wantPayloadOnly) { |
| 211 | std::cout << header.getReason() << std::endl; |
| 212 | } |
| 213 | else { |
| 214 | const Block& block = header.wireEncode(); |
| 215 | std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 220 | onTimeout(const Interest& interest) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 221 | { |
| 222 | } |
| 223 | |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 224 | int |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 225 | run() |
| 226 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 227 | try { |
| 228 | m_face.expressInterest(createInterestPacket(), |
| 229 | bind(&NdnPeek::onData, this, _1, _2), |
Teng Liang | 2864b9d | 2016-03-17 11:17:38 -0700 | [diff] [blame^] | 230 | bind(&NdnPeek::onNack, this, _1, _2), |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 231 | bind(&NdnPeek::onTimeout, this, _1)); |
Junxiao Shi | e02fb52 | 2015-10-18 08:45:09 -0700 | [diff] [blame] | 232 | m_expressInterestTime = time::steady_clock::now(); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 233 | if (m_timeout < time::milliseconds::zero()) { |
Junxiao Shi | e02fb52 | 2015-10-18 08:45:09 -0700 | [diff] [blame] | 234 | m_timeout = m_interestLifetime < time::milliseconds::zero() ? |
| 235 | getDefaultInterestLifetime() : m_interestLifetime; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 236 | } |
Junxiao Shi | e02fb52 | 2015-10-18 08:45:09 -0700 | [diff] [blame] | 237 | m_face.processEvents(m_timeout); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 238 | } |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 239 | catch (const std::exception& e) { |
Junxiao Shi | e02fb52 | 2015-10-18 08:45:09 -0700 | [diff] [blame] | 240 | std::cerr << "ERROR: " << e.what() << std::endl; |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 241 | return 1; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 242 | } |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 243 | |
Teng Liang | 2864b9d | 2016-03-17 11:17:38 -0700 | [diff] [blame^] | 244 | if (m_didReceiveNack) |
| 245 | return 4; |
| 246 | |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 247 | if (isVerbose && !m_didReceiveData) { |
Junxiao Shi | e02fb52 | 2015-10-18 08:45:09 -0700 | [diff] [blame] | 248 | std::cerr << "TIMEOUT" << std::endl; |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 249 | return 3; |
Junxiao Shi | e02fb52 | 2015-10-18 08:45:09 -0700 | [diff] [blame] | 250 | } |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 251 | |
Teng Liang | 2864b9d | 2016-03-17 11:17:38 -0700 | [diff] [blame^] | 252 | if (!m_didReceiveData) |
| 253 | return 3; |
| 254 | |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 255 | return 0; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 258 | public: |
| 259 | bool isVerbose; |
| 260 | bool mustBeFresh; |
| 261 | bool wantRightmostChild; |
| 262 | bool wantPayloadOnly; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 263 | |
| 264 | private: |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 265 | std::string m_programName; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 266 | int m_minSuffixComponents; |
| 267 | int m_maxSuffixComponents; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 268 | time::milliseconds m_interestLifetime; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 269 | time::milliseconds m_timeout; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 270 | std::string m_prefixName; |
Junxiao Shi | e02fb52 | 2015-10-18 08:45:09 -0700 | [diff] [blame] | 271 | time::steady_clock::TimePoint m_expressInterestTime; |
Teng Liang | 799390a | 2016-03-12 23:14:29 -0700 | [diff] [blame] | 272 | shared_ptr<Link> m_link; |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 273 | bool m_didReceiveData; |
Teng Liang | 2864b9d | 2016-03-17 11:17:38 -0700 | [diff] [blame^] | 274 | bool m_didReceiveNack; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 275 | Face m_face; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 276 | }; |
| 277 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 278 | int |
| 279 | main(int argc, char* argv[]) |
| 280 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 281 | NdnPeek program(argv[0]); |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 282 | |
| 283 | namespace po = boost::program_options; |
| 284 | |
| 285 | po::options_description visibleOptDesc("Allowed options"); |
| 286 | visibleOptDesc.add_options() |
| 287 | ("help,h", "print help and exit") |
| 288 | ("version,V", "print version and exit") |
| 289 | ("fresh,f", po::bool_switch(&program.mustBeFresh), |
| 290 | "set MustBeFresh") |
| 291 | ("rightmost,r", po::bool_switch(&program.wantRightmostChild), |
| 292 | "set ChildSelector to rightmost") |
| 293 | ("minsuffix,m", po::value<int>()->notifier(bind(&NdnPeek::setMinSuffixComponents, &program, _1)), |
| 294 | "set MinSuffixComponents") |
| 295 | ("maxsuffix,M", po::value<int>()->notifier(bind(&NdnPeek::setMaxSuffixComponents, &program, _1)), |
| 296 | "set MaxSuffixComponents") |
| 297 | ("lifetime,l", po::value<int>()->notifier(bind(&NdnPeek::setInterestLifetime, &program, _1)), |
| 298 | "set InterestLifetime (in milliseconds)") |
| 299 | ("payload,p", po::bool_switch(&program.wantPayloadOnly), |
| 300 | "print payload only, instead of full packet") |
| 301 | ("timeout,w", po::value<int>()->notifier(bind(&NdnPeek::setTimeout, &program, _1)), |
| 302 | "set timeout (in milliseconds)") |
| 303 | ("verbose,v", po::bool_switch(&program.isVerbose), |
| 304 | "turn on verbose output") |
Teng Liang | 799390a | 2016-03-12 23:14:29 -0700 | [diff] [blame] | 305 | ("link-file", po::value<std::string>()->notifier(bind(&NdnPeek::setLink, &program, _1)), |
| 306 | "set Link from a file") |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 307 | ; |
| 308 | |
| 309 | po::options_description hiddenOptDesc("Hidden options"); |
| 310 | hiddenOptDesc.add_options() |
| 311 | ("prefix", po::value<std::string>(), "Interest name"); |
| 312 | |
| 313 | po::options_description optDesc("Allowed options"); |
| 314 | optDesc.add(visibleOptDesc).add(hiddenOptDesc); |
| 315 | |
| 316 | po::positional_options_description optPos; |
| 317 | optPos.add("prefix", -1); |
| 318 | |
| 319 | try { |
| 320 | po::variables_map vm; |
| 321 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm); |
| 322 | po::notify(vm); |
| 323 | |
| 324 | if (vm.count("help") > 0) { |
| 325 | program.usage(std::cout, visibleOptDesc); |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | if (vm.count("version") > 0) { |
Junxiao Shi | 1753afb | 2015-04-17 20:59:50 -0700 | [diff] [blame] | 330 | std::cout << "ndnpeek " << tools::VERSION << std::endl; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 331 | return 0; |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | if (vm.count("prefix") > 0) { |
| 335 | std::string prefixName = vm["prefix"].as<std::string>(); |
| 336 | program.setPrefixName(prefixName); |
| 337 | } |
| 338 | else { |
| 339 | throw std::runtime_error("Required argument 'prefix' is missing"); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 342 | catch (const std::exception& e) { |
| 343 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 344 | program.usage(std::cerr, visibleOptDesc); |
| 345 | return 2; |
| 346 | } |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 347 | |
Teng Liang | d4ab87f | 2016-03-08 18:50:47 -0700 | [diff] [blame] | 348 | return program.run(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 349 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 350 | |
| 351 | } // namespace peek |
| 352 | } // namespace ndn |
| 353 | |
| 354 | int |
| 355 | main(int argc, char** argv) |
| 356 | { |
| 357 | return ndn::peek::main(argc, argv); |
| 358 | } |