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