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