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