Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 1688ded | 2015-03-29 10:09:26 -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. |
| 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" |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 53 | namespace ndn { |
| 54 | namespace peek { |
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 | class NdnPeek : boost::noncopyable |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 57 | { |
| 58 | public: |
| 59 | explicit |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 60 | NdnPeek(char* programName) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 61 | : m_programName(programName) |
| 62 | , m_mustBeFresh(false) |
| 63 | , m_isChildSelectorRightmost(false) |
| 64 | , m_minSuffixComponents(-1) |
| 65 | , m_maxSuffixComponents(-1) |
| 66 | , m_interestLifetime(-1) |
| 67 | , m_isPayloadOnlySet(false) |
| 68 | , m_timeout(-1) |
| 69 | , m_prefixName("") |
| 70 | , m_isDataReceived(false) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | usage() |
| 76 | { |
| 77 | std::cout << "\n Usage:\n " << m_programName << " " |
| 78 | "[-f] [-r] [-m min] [-M max] [-l lifetime] [-p] [-w timeout] ndn:/name\n" |
| 79 | " Get one data item matching the name prefix and write it to stdout\n" |
| 80 | " [-f] - set MustBeFresh\n" |
| 81 | " [-r] - set ChildSelector to select rightmost child\n" |
| 82 | " [-m min] - set MinSuffixComponents\n" |
| 83 | " [-M max] - set MaxSuffixComponents\n" |
| 84 | " [-l lifetime] - set InterestLifetime in time::milliseconds\n" |
| 85 | " [-p] - print payload only, not full packet\n" |
| 86 | " [-w timeout] - set Timeout in time::milliseconds\n" |
| 87 | " [-h] - print help and exit\n" |
| 88 | " [-V] - print version and exit\n" |
| 89 | "\n"; |
| 90 | exit(1); |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | setMustBeFresh() |
| 95 | { |
| 96 | m_mustBeFresh = true; |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | setRightmostChildSelector() |
| 101 | { |
| 102 | m_isChildSelectorRightmost = true; |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | setMinSuffixComponents(int minSuffixComponents) |
| 107 | { |
| 108 | if (minSuffixComponents < 0) |
| 109 | usage(); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 110 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 111 | m_minSuffixComponents = minSuffixComponents; |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | setMaxSuffixComponents(int maxSuffixComponents) |
| 116 | { |
| 117 | if (maxSuffixComponents < 0) |
| 118 | usage(); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 119 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 120 | m_maxSuffixComponents = maxSuffixComponents; |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | setInterestLifetime(int interestLifetime) |
| 125 | { |
| 126 | if (interestLifetime < 0) |
| 127 | usage(); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 128 | |
| 129 | m_interestLifetime = time::milliseconds(interestLifetime); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void |
| 133 | setPayloadOnly() |
| 134 | { |
| 135 | m_isPayloadOnlySet = true; |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | setTimeout(int timeout) |
| 140 | { |
| 141 | if (timeout < 0) |
| 142 | usage(); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 143 | |
| 144 | m_timeout = time::milliseconds(timeout); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
| 148 | setPrefixName(char* prefixName) |
| 149 | { |
| 150 | m_prefixName = prefixName; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 151 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 152 | if (m_prefixName.length() == 0) |
| 153 | usage(); |
| 154 | } |
| 155 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 156 | time::milliseconds |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 157 | getDefaultInterestLifetime() |
| 158 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 159 | return time::seconds(4); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 162 | Interest |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 163 | createInterestPacket() |
| 164 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 165 | Name interestName(m_prefixName); |
| 166 | Interest interestPacket(interestName); |
| 167 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 168 | if (m_mustBeFresh) |
| 169 | interestPacket.setMustBeFresh(true); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 170 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 171 | if (m_isChildSelectorRightmost) |
| 172 | interestPacket.setChildSelector(1); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 173 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 174 | if (m_minSuffixComponents >= 0) |
| 175 | interestPacket.setMinSuffixComponents(m_minSuffixComponents); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 176 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 177 | if (m_maxSuffixComponents >= 0) |
| 178 | interestPacket.setMaxSuffixComponents(m_maxSuffixComponents); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 179 | |
| 180 | if (m_interestLifetime < time::milliseconds::zero()) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 181 | interestPacket.setInterestLifetime(getDefaultInterestLifetime()); |
| 182 | else |
| 183 | interestPacket.setInterestLifetime(m_interestLifetime); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 184 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 185 | return interestPacket; |
| 186 | } |
| 187 | |
| 188 | void |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 189 | onData(const Interest& interest, Data& data) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 190 | { |
| 191 | m_isDataReceived = true; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 192 | if (m_isPayloadOnlySet) { |
| 193 | const Block& block = data.getContent(); |
| 194 | std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size()); |
| 195 | } |
| 196 | else { |
| 197 | const Block& block = data.wireEncode(); |
| 198 | std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size()); |
| 199 | } |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 203 | onTimeout(const Interest& interest) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 204 | { |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | run() |
| 209 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 210 | try { |
| 211 | m_face.expressInterest(createInterestPacket(), |
| 212 | bind(&NdnPeek::onData, this, _1, _2), |
| 213 | bind(&NdnPeek::onTimeout, this, _1)); |
| 214 | if (m_timeout < time::milliseconds::zero()) { |
| 215 | if (m_interestLifetime < time::milliseconds::zero()) |
| 216 | m_face.processEvents(getDefaultInterestLifetime()); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 217 | else |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 218 | m_face.processEvents(m_interestLifetime); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 219 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 220 | else |
| 221 | m_face.processEvents(m_timeout); |
| 222 | } |
| 223 | catch (std::exception& e) { |
| 224 | std::cerr << "ERROR: " << e.what() << "\n" << std::endl; |
| 225 | exit(1); |
| 226 | } |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | bool |
| 230 | isDataReceived() const |
| 231 | { |
| 232 | return m_isDataReceived; |
| 233 | } |
| 234 | |
| 235 | private: |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 236 | std::string m_programName; |
| 237 | bool m_mustBeFresh; |
| 238 | bool m_isChildSelectorRightmost; |
| 239 | int m_minSuffixComponents; |
| 240 | int m_maxSuffixComponents; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 241 | time::milliseconds m_interestLifetime; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 242 | bool m_isPayloadOnlySet; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 243 | time::milliseconds m_timeout; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 244 | std::string m_prefixName; |
| 245 | bool m_isDataReceived; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 246 | Face m_face; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 249 | int |
| 250 | main(int argc, char* argv[]) |
| 251 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 252 | NdnPeek program(argv[0]); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 253 | int option; |
| 254 | while ((option = getopt(argc, argv, "hfrm:M:l:pw:V")) != -1) { |
| 255 | switch (option) { |
| 256 | case 'h': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 257 | program.usage(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 258 | break; |
| 259 | case 'f': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 260 | program.setMustBeFresh(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 261 | break; |
| 262 | case 'r': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 263 | program.setRightmostChildSelector(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 264 | break; |
| 265 | case 'm': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 266 | program.setMinSuffixComponents(atoi(optarg)); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 267 | break; |
| 268 | case 'M': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 269 | program.setMaxSuffixComponents(atoi(optarg)); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 270 | break; |
| 271 | case 'l': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 272 | program.setInterestLifetime(atoi(optarg)); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 273 | break; |
| 274 | case 'p': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 275 | program.setPayloadOnly(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 276 | break; |
| 277 | case 'w': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 278 | program.setTimeout(atoi(optarg)); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 279 | break; |
| 280 | case 'V': |
Junxiao Shi | 1753afb | 2015-04-17 20:59:50 -0700 | [diff] [blame^] | 281 | std::cout << "ndnpeek " << tools::VERSION << std::endl; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 282 | return 0; |
| 283 | default: |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 284 | program.usage(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | argc -= optind; |
| 290 | argv += optind; |
| 291 | |
| 292 | if (argv[0] == 0) |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 293 | program.usage(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 294 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 295 | program.setPrefixName(argv[0]); |
| 296 | program.run(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 297 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 298 | if (program.isDataReceived()) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 299 | return 0; |
| 300 | else |
| 301 | return 1; |
| 302 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 303 | |
| 304 | } // namespace peek |
| 305 | } // namespace ndn |
| 306 | |
| 307 | int |
| 308 | main(int argc, char** argv) |
| 309 | { |
| 310 | return ndn::peek::main(argc, argv); |
| 311 | } |