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