Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 2 | /** |
Junxiao Shi | 5620fb2 | 2015-06-07 20:59:38 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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. |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD 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 | * NFD 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 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 2d2cde1 | 2014-03-31 00:32:42 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 26 | #include "version.hpp" |
| 27 | |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 28 | #include <boost/noncopyable.hpp> |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/face.hpp> |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndntlvpeek { |
| 33 | |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 34 | class NdnTlvPeek : boost::noncopyable |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 35 | { |
| 36 | public: |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 37 | explicit |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 38 | NdnTlvPeek(char* programName) |
| 39 | : m_programName(programName) |
| 40 | , m_mustBeFresh(false) |
| 41 | , m_isChildSelectorRightmost(false) |
| 42 | , m_minSuffixComponents(-1) |
| 43 | , m_maxSuffixComponents(-1) |
| 44 | , m_interestLifetime(-1) |
| 45 | , m_isPayloadOnlySet(false) |
| 46 | , m_timeout(-1) |
| 47 | , m_prefixName("") |
| 48 | , m_isDataReceived(false) |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | usage() |
| 54 | { |
| 55 | std::cout << "\n Usage:\n " << m_programName << " " |
| 56 | "[-f] [-r] [-m min] [-M max] [-l lifetime] [-p] [-w timeout] ndn:/name\n" |
| 57 | " Get one data item matching the name prefix and write it to stdout\n" |
| 58 | " [-f] - set MustBeFresh\n" |
| 59 | " [-r] - set ChildSelector to select rightmost child\n" |
| 60 | " [-m min] - set MinSuffixComponents\n" |
| 61 | " [-M max] - set MaxSuffixComponents\n" |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 62 | " [-l lifetime] - set InterestLifetime in time::milliseconds\n" |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 63 | " [-p] - print payload only, not full packet\n" |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 64 | " [-w timeout] - set Timeout in time::milliseconds\n" |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 65 | " [-h] - print help and exit\n" |
| 66 | " [-V] - print version and exit\n" |
| 67 | "\n"; |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 68 | exit(1); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | setMustBeFresh() |
| 73 | { |
| 74 | m_mustBeFresh = true; |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | setRightmostChildSelector() |
| 79 | { |
| 80 | m_isChildSelectorRightmost = true; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | setMinSuffixComponents(int minSuffixComponents) |
| 85 | { |
| 86 | if (minSuffixComponents < 0) |
| 87 | usage(); |
| 88 | m_minSuffixComponents = minSuffixComponents; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | setMaxSuffixComponents(int maxSuffixComponents) |
| 93 | { |
| 94 | if (maxSuffixComponents < 0) |
| 95 | usage(); |
| 96 | m_maxSuffixComponents = maxSuffixComponents; |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | setInterestLifetime(int interestLifetime) |
| 101 | { |
| 102 | if (interestLifetime < 0) |
| 103 | usage(); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 104 | m_interestLifetime = ndn::time::milliseconds(interestLifetime); |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void |
| 108 | setPayloadOnly() |
| 109 | { |
| 110 | m_isPayloadOnlySet = true; |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | setTimeout(int timeout) |
| 115 | { |
| 116 | if (timeout < 0) |
| 117 | usage(); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 118 | m_timeout = ndn::time::milliseconds(timeout); |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
| 122 | setPrefixName(char* prefixName) |
| 123 | { |
| 124 | m_prefixName = prefixName; |
| 125 | if (m_prefixName.length() == 0) |
| 126 | usage(); |
| 127 | } |
| 128 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 129 | ndn::time::milliseconds |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 130 | getDefaultInterestLifetime() |
| 131 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 132 | return ndn::time::seconds(4); |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | ndn::Interest |
| 136 | createInterestPacket() |
| 137 | { |
| 138 | ndn::Name interestName(m_prefixName); |
| 139 | ndn::Interest interestPacket(interestName); |
| 140 | if (m_mustBeFresh) |
| 141 | interestPacket.setMustBeFresh(true); |
| 142 | if (m_isChildSelectorRightmost) |
| 143 | interestPacket.setChildSelector(1); |
| 144 | if (m_minSuffixComponents >= 0) |
| 145 | interestPacket.setMinSuffixComponents(m_minSuffixComponents); |
| 146 | if (m_maxSuffixComponents >= 0) |
| 147 | interestPacket.setMaxSuffixComponents(m_maxSuffixComponents); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 148 | if (m_interestLifetime < ndn::time::milliseconds::zero()) |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 149 | interestPacket.setInterestLifetime(getDefaultInterestLifetime()); |
| 150 | else |
| 151 | interestPacket.setInterestLifetime(m_interestLifetime); |
| 152 | return interestPacket; |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | onData(const ndn::Interest& interest, ndn::Data& data) |
| 157 | { |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 158 | m_isDataReceived = true; |
| 159 | if (m_isPayloadOnlySet) |
| 160 | { |
Junxiao Shi | 2d2cde1 | 2014-03-31 00:32:42 -0700 | [diff] [blame] | 161 | const ndn::Block& block = data.getContent(); |
| 162 | std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size()); |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 163 | } |
| 164 | else |
| 165 | { |
Junxiao Shi | 2d2cde1 | 2014-03-31 00:32:42 -0700 | [diff] [blame] | 166 | const ndn::Block& block = data.wireEncode(); |
| 167 | std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size()); |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | onTimeout(const ndn::Interest& interest) |
| 173 | { |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | run() |
| 178 | { |
| 179 | try |
| 180 | { |
| 181 | m_face.expressInterest(createInterestPacket(), |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 182 | bind(&NdnTlvPeek::onData, this, _1, _2), |
| 183 | bind(&NdnTlvPeek::onTimeout, this, _1)); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 184 | if (m_timeout < ndn::time::milliseconds::zero()) |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 185 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 186 | if (m_interestLifetime < ndn::time::milliseconds::zero()) |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 187 | m_face.processEvents(getDefaultInterestLifetime()); |
| 188 | else |
| 189 | m_face.processEvents(m_interestLifetime); |
| 190 | } |
| 191 | else |
| 192 | m_face.processEvents(m_timeout); |
| 193 | } |
| 194 | catch (std::exception& e) |
| 195 | { |
| 196 | std::cerr << "ERROR: " << e.what() << "\n" << std::endl; |
| 197 | exit(1); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | bool |
| 202 | isDataReceived() const |
| 203 | { |
| 204 | return m_isDataReceived; |
| 205 | } |
| 206 | |
| 207 | private: |
| 208 | |
| 209 | std::string m_programName; |
| 210 | bool m_mustBeFresh; |
| 211 | bool m_isChildSelectorRightmost; |
| 212 | int m_minSuffixComponents; |
| 213 | int m_maxSuffixComponents; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 214 | ndn::time::milliseconds m_interestLifetime; |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 215 | bool m_isPayloadOnlySet; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 216 | ndn::time::milliseconds m_timeout; |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 217 | std::string m_prefixName; |
| 218 | bool m_isDataReceived; |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 219 | ndn::Face m_face; |
| 220 | }; |
| 221 | |
| 222 | } |
| 223 | |
| 224 | int |
| 225 | main(int argc, char* argv[]) |
| 226 | { |
Junxiao Shi | 5620fb2 | 2015-06-07 20:59:38 -0700 | [diff] [blame^] | 227 | std::cerr << "ndn-tlv-peek is deprecated. Use ndnpeek program from ndn-tools repository.\n" |
| 228 | "See `man ndn-tlv-peek` for details." << std::endl; |
| 229 | |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 230 | ndntlvpeek::NdnTlvPeek ndnTlvPeek(argv[0]); |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 231 | int option; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 232 | while ((option = getopt(argc, argv, "hfrm:M:l:pw:V")) != -1) { |
| 233 | switch (option) { |
| 234 | case 'h': |
| 235 | ndnTlvPeek.usage(); |
| 236 | break; |
| 237 | case 'f': |
| 238 | ndnTlvPeek.setMustBeFresh(); |
| 239 | break; |
| 240 | case 'r': |
| 241 | ndnTlvPeek.setRightmostChildSelector(); |
| 242 | break; |
| 243 | case 'm': |
| 244 | ndnTlvPeek.setMinSuffixComponents(atoi(optarg)); |
| 245 | break; |
| 246 | case 'M': |
| 247 | ndnTlvPeek.setMaxSuffixComponents(atoi(optarg)); |
| 248 | break; |
| 249 | case 'l': |
| 250 | ndnTlvPeek.setInterestLifetime(atoi(optarg)); |
| 251 | break; |
| 252 | case 'p': |
| 253 | ndnTlvPeek.setPayloadOnly(); |
| 254 | break; |
| 255 | case 'w': |
| 256 | ndnTlvPeek.setTimeout(atoi(optarg)); |
| 257 | break; |
| 258 | case 'V': |
| 259 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 260 | return 0; |
| 261 | default: |
| 262 | ndnTlvPeek.usage(); |
| 263 | break; |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 264 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 265 | } |
jeraldabraham | 20b2dc5 | 2014-03-15 22:17:57 -0700 | [diff] [blame] | 266 | |
| 267 | argc -= optind; |
| 268 | argv += optind; |
| 269 | |
| 270 | if (argv[0] == 0) |
| 271 | ndnTlvPeek.usage(); |
| 272 | |
| 273 | ndnTlvPeek.setPrefixName(argv[0]); |
| 274 | ndnTlvPeek.run(); |
| 275 | |
| 276 | if (ndnTlvPeek.isDataReceived()) |
| 277 | return 0; |
| 278 | else |
| 279 | return 1; |
| 280 | } |