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