hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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/>. |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 25 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 26 | #include "nfdc.hpp" |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 27 | #include "version.hpp" |
| 28 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 29 | #include <boost/lexical_cast.hpp> |
| 30 | #include <boost/algorithm/string.hpp> |
| 31 | #include <boost/algorithm/string/regex_find_format.hpp> |
| 32 | #include <boost/regex.hpp> |
| 33 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 34 | #include <ndn-cxx/management/nfd-face-query-filter.hpp> |
| 35 | #include <ndn-cxx/management/nfd-face-status.hpp> |
| 36 | #include <ndn-cxx/util/segment-fetcher.hpp> |
| 37 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 38 | void |
| 39 | usage(const char* programName) |
| 40 | { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 41 | std::cout << "Usage:\n" << programName << " [-h] [-V] COMMAND [<Command Options>]\n" |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 42 | " -h print usage and exit\n" |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 43 | " -V print version and exit\n" |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 44 | "\n" |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 45 | " COMMAND can be one of the following:\n" |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 46 | " register [-I] [-C] [-c cost] [-e expiration time] [-o origin] name <faceId | faceUri>\n" |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 47 | " register name to the given faceId or faceUri\n" |
| 48 | " -I: unset CHILD_INHERIT flag\n" |
| 49 | " -C: set CAPTURE flag\n" |
Syed Obaid | 818e8a6 | 2014-07-02 18:24:11 -0500 | [diff] [blame] | 50 | " -c: specify cost (default 0)\n" |
| 51 | " -e: specify expiration time in ms\n" |
| 52 | " (by default the entry remains in FIB for the lifetime of the associated face)\n" |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 53 | " -o: specify origin\n" |
Syed Obaid | 818e8a6 | 2014-07-02 18:24:11 -0500 | [diff] [blame] | 54 | " 0 for Local producer applications, 128 for NLSR, 255(default) for static routes\n" |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 55 | " unregister [-o origin] name <faceId | faceUri>\n" |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 56 | " unregister name from the given faceId\n" |
| 57 | " create <faceUri> \n" |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 58 | " Create a face in one of the following formats:\n" |
| 59 | " UDP unicast: udp[4|6]://<remote-IP-or-host>[:<remote-port>]\n" |
| 60 | " TCP: tcp[4|6]://<remote-IP-or-host>[:<remote-port>] \n" |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 61 | " destroy <faceId | faceUri> \n" |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 62 | " Destroy a face\n" |
| 63 | " set-strategy <name> <strategy> \n" |
| 64 | " Set the strategy for a namespace \n" |
| 65 | " unset-strategy <name> \n" |
| 66 | " Unset the strategy for a namespace \n" |
hilata | 6ee6e07 | 2014-04-20 17:10:18 -0500 | [diff] [blame] | 67 | " add-nexthop [-c <cost>] <name> <faceId | faceUri>\n" |
| 68 | " Add a nexthop to a FIB entry\n" |
Syed Obaid | 818e8a6 | 2014-07-02 18:24:11 -0500 | [diff] [blame] | 69 | " -c: specify cost (default 0)\n" |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 70 | " remove-nexthop <name> <faceId | faceUri> \n" |
hilata | 6ee6e07 | 2014-04-20 17:10:18 -0500 | [diff] [blame] | 71 | " Remove a nexthop from a FIB entry\n" |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 72 | << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | namespace nfdc { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 76 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 77 | using std::bind; |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 78 | |
Syed Obaid | 818e8a6 | 2014-07-02 18:24:11 -0500 | [diff] [blame] | 79 | const ndn::time::milliseconds Nfdc::DEFAULT_EXPIRATION_PERIOD = ndn::time::milliseconds::max(); |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 80 | const uint64_t Nfdc::DEFAULT_COST = 0; |
| 81 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 82 | Nfdc::FaceIdFetcher::FaceIdFetcher(ndn::Face& face, |
| 83 | Controller& controller, |
| 84 | bool allowCreate, |
| 85 | const SuccessCallback& onSucceed, |
| 86 | const FailureCallback& onFail) |
| 87 | : m_face(face) |
| 88 | , m_controller(controller) |
| 89 | , m_allowCreate(allowCreate) |
| 90 | , m_onSucceed(onSucceed) |
| 91 | , m_onFail(onFail) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | Nfdc::FaceIdFetcher::start(ndn::Face& face, |
| 97 | Controller& controller, |
| 98 | const std::string& input, |
| 99 | bool allowCreate, |
| 100 | const SuccessCallback& onSucceed, |
| 101 | const FailureCallback& onFail) |
| 102 | { |
| 103 | // 1. Try parse input as FaceId, if input is FaceId, succeed with parsed FaceId |
| 104 | // 2. Try parse input as FaceUri, if input is not FaceUri, fail |
| 105 | // 3. Canonize faceUri |
| 106 | // 4. If canonization fails, fail |
| 107 | // 5. Query for face |
| 108 | // 6. If query succeeds and finds a face, succeed with found FaceId |
| 109 | // 7. Create face |
| 110 | // 8. If face creation succeeds, succeed with created FaceId |
| 111 | // 9. Fail |
| 112 | |
| 113 | boost::regex e("^[a-z0-9]+\\:.*"); |
| 114 | if (!boost::regex_match(input, e)) { |
| 115 | try |
| 116 | { |
| 117 | u_int32_t faceId = boost::lexical_cast<uint32_t>(input); |
| 118 | onSucceed(faceId); |
| 119 | return; |
| 120 | } |
| 121 | catch (boost::bad_lexical_cast&) |
| 122 | { |
| 123 | onFail("No valid faceId or faceUri is provided"); |
| 124 | return; |
| 125 | } |
| 126 | } |
| 127 | else { |
| 128 | ndn::util::FaceUri faceUri; |
| 129 | if (!faceUri.parse(input)) { |
| 130 | onFail("FaceUri parse failed"); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | auto fetcher = new FaceIdFetcher(std::ref(face), std::ref(controller), |
| 135 | allowCreate, onSucceed, onFail); |
| 136 | |
| 137 | fetcher->startGetFaceId(faceUri); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | Nfdc::FaceIdFetcher::startGetFaceId(const ndn::util::FaceUri& faceUri) |
| 143 | { |
| 144 | faceUri.canonize(bind(&FaceIdFetcher::onCanonizeSuccess, this, _1), |
| 145 | bind(&FaceIdFetcher::onCanonizeFailure, this, _1), |
| 146 | m_face.getIoService(), ndn::time::seconds(4)); |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | Nfdc::FaceIdFetcher::onCanonizeSuccess(const ndn::util::FaceUri& canonicalUri) |
| 151 | { |
| 152 | ndn::Name queryName("/localhost/nfd/faces/query"); |
| 153 | ndn::nfd::FaceQueryFilter queryFilter; |
| 154 | queryFilter.setRemoteUri(canonicalUri.toString()); |
| 155 | queryName.append(queryFilter.wireEncode()); |
| 156 | |
| 157 | ndn::Interest interestPacket(queryName); |
| 158 | interestPacket.setMustBeFresh(true); |
| 159 | interestPacket.setInterestLifetime(ndn::time::milliseconds(4000)); |
| 160 | auto interest = std::make_shared<ndn::Interest>(interestPacket); |
| 161 | |
| 162 | ndn::util::SegmentFetcher::fetch(m_face, *interest, |
| 163 | ndn::util::DontVerifySegment(), |
| 164 | bind(&FaceIdFetcher::onQuerySuccess, |
| 165 | this, _1, canonicalUri), |
| 166 | bind(&FaceIdFetcher::onQueryFailure, |
| 167 | this, _1, canonicalUri)); |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | Nfdc::FaceIdFetcher::onCanonizeFailure(const std::string& reason) |
| 172 | { |
| 173 | fail("Canonize faceUri failed : " + reason); |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | Nfdc::FaceIdFetcher::onQuerySuccess(const ndn::ConstBufferPtr& data, |
| 178 | const ndn::util::FaceUri& canonicalUri) |
| 179 | { |
| 180 | size_t offset = 0; |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 181 | bool isOk = false; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 182 | ndn::Block block; |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 183 | std::tie(isOk, block) = ndn::Block::fromBuffer(data, offset); |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 184 | |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 185 | if (!isOk) { |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 186 | if (m_allowCreate) { |
| 187 | startFaceCreate(canonicalUri); |
| 188 | } |
| 189 | else { |
| 190 | fail("Fail to find faceId"); |
| 191 | } |
| 192 | } |
| 193 | else { |
| 194 | try { |
| 195 | FaceStatus status(block); |
| 196 | succeed(status.getFaceId()); |
| 197 | } |
| 198 | catch (const ndn::tlv::Error& e) { |
| 199 | std::string errorMessage(e.what()); |
| 200 | fail("ERROR: " + errorMessage); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | Nfdc::FaceIdFetcher::onQueryFailure(uint32_t errorCode, |
| 207 | const ndn::util::FaceUri& canonicalUri) |
| 208 | { |
| 209 | std::stringstream ss; |
| 210 | ss << "Cannot fetch data (code " << errorCode << ")"; |
| 211 | fail(ss.str()); |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | Nfdc::FaceIdFetcher::onFaceCreateError(uint32_t code, |
| 216 | const std::string& error, |
| 217 | const std::string& message) |
| 218 | { |
| 219 | std::stringstream ss; |
| 220 | ss << message << " : " << error << " (code " << code << ")"; |
| 221 | fail(ss.str()); |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | Nfdc::FaceIdFetcher::startFaceCreate(const ndn::util::FaceUri& canonicalUri) |
| 226 | { |
| 227 | ControlParameters parameters; |
| 228 | parameters.setUri(canonicalUri.toString()); |
| 229 | |
| 230 | m_controller.start<FaceCreateCommand>(parameters, |
| 231 | [this] (const ControlParameters& result) { |
| 232 | succeed(result.getFaceId()); |
| 233 | }, |
| 234 | bind(&FaceIdFetcher::onFaceCreateError, this, _1, _2, |
| 235 | "Face creation failed")); |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | Nfdc::FaceIdFetcher::succeed(uint32_t faceId) |
| 240 | { |
| 241 | m_onSucceed(faceId); |
| 242 | delete this; |
| 243 | } |
| 244 | |
| 245 | void |
| 246 | Nfdc::FaceIdFetcher::fail(const std::string& reason) |
| 247 | { |
| 248 | m_onFail(reason); |
| 249 | delete this; |
| 250 | } |
| 251 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 252 | Nfdc::Nfdc(ndn::Face& face) |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 253 | : m_flags(ROUTE_FLAG_CHILD_INHERIT) |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 254 | , m_cost(DEFAULT_COST) |
| 255 | , m_origin(ROUTE_ORIGIN_STATIC) |
| 256 | , m_expires(DEFAULT_EXPIRATION_PERIOD) |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 257 | , m_face(face) |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 258 | , m_controller(face, m_keyChain) |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 259 | , m_ioService(face.getIoService()) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 260 | { |
| 261 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 262 | |
| 263 | Nfdc::~Nfdc() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 264 | { |
| 265 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 266 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 267 | bool |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 268 | Nfdc::dispatch(const std::string& command) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 269 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 270 | if (command == "add-nexthop") { |
| 271 | if (m_nOptions != 2) |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 272 | return false; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 273 | fibAddNextHop(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 274 | } |
| 275 | else if (command == "remove-nexthop") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 276 | if (m_nOptions != 2) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 277 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 278 | fibRemoveNextHop(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 279 | } |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 280 | else if (command == "register") { |
| 281 | if (m_nOptions != 2) |
| 282 | return false; |
| 283 | ribRegisterPrefix(); |
| 284 | } |
| 285 | else if (command == "unregister") { |
| 286 | if (m_nOptions != 2) |
| 287 | return false; |
| 288 | ribUnregisterPrefix(); |
| 289 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 290 | else if (command == "create") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 291 | if (m_nOptions != 1) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 292 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 293 | faceCreate(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 294 | } |
| 295 | else if (command == "destroy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 296 | if (m_nOptions != 1) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 297 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 298 | faceDestroy(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 299 | } |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 300 | else if (command == "set-strategy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 301 | if (m_nOptions != 2) |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 302 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 303 | strategyChoiceSet(); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 304 | } |
| 305 | else if (command == "unset-strategy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 306 | if (m_nOptions != 1) |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 307 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 308 | strategyChoiceUnset(); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 309 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 310 | else |
multicast proxy | d647464 | 2015-03-21 16:09:15 +0100 | [diff] [blame] | 311 | return false; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 312 | |
| 313 | return true; |
| 314 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 315 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 316 | void |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 317 | Nfdc::fibAddNextHop() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 318 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 319 | m_name = m_commandLineArguments[0]; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 320 | const std::string& faceName = m_commandLineArguments[1]; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 321 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 322 | FaceIdFetcher::start(m_face, m_controller, faceName, true, |
| 323 | [this] (const uint32_t faceId) { |
| 324 | ControlParameters parameters; |
| 325 | parameters |
| 326 | .setName(m_name) |
| 327 | .setCost(m_cost) |
| 328 | .setFaceId(faceId); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 329 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 330 | m_controller |
| 331 | .start<FibAddNextHopCommand>(parameters, |
| 332 | bind(&Nfdc::onSuccess, this, _1, |
| 333 | "Nexthop insertion succeeded"), |
| 334 | bind(&Nfdc::onError, this, _1, _2, |
| 335 | "Nexthop insertion failed")); |
| 336 | }, |
| 337 | bind(&Nfdc::onObtainFaceIdFailure, this, _1)); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void |
| 341 | Nfdc::fibRemoveNextHop() |
| 342 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 343 | m_name = m_commandLineArguments[0]; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 344 | const std::string& faceName = m_commandLineArguments[1]; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 345 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 346 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 347 | [this] (const uint32_t faceId) { |
| 348 | ControlParameters parameters; |
| 349 | parameters |
| 350 | .setName(m_name) |
| 351 | .setFaceId(faceId); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 352 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 353 | m_controller |
| 354 | .start<FibRemoveNextHopCommand>(parameters, |
| 355 | bind(&Nfdc::onSuccess, this, _1, |
| 356 | "Nexthop removal succeeded"), |
| 357 | bind(&Nfdc::onError, this, _1, _2, |
| 358 | "Nexthop removal failed")); |
| 359 | }, |
| 360 | bind(&Nfdc::onObtainFaceIdFailure, this, _1)); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void |
| 364 | Nfdc::ribRegisterPrefix() |
| 365 | { |
| 366 | m_name = m_commandLineArguments[0]; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 367 | const std::string& faceName = m_commandLineArguments[1]; |
Alexander Afanasyev | f67cf08 | 2014-07-18 16:47:29 -0700 | [diff] [blame] | 368 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 369 | FaceIdFetcher::start(m_face, m_controller, faceName, true, |
| 370 | [this] (const uint32_t faceId) { |
| 371 | ControlParameters parameters; |
| 372 | parameters |
| 373 | .setName(m_name) |
| 374 | .setCost(m_cost) |
| 375 | .setFlags(m_flags) |
| 376 | .setOrigin(m_origin) |
| 377 | .setFaceId(faceId); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 378 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 379 | if (m_expires != DEFAULT_EXPIRATION_PERIOD) |
| 380 | parameters.setExpirationPeriod(m_expires); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 381 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 382 | m_controller |
| 383 | .start<RibRegisterCommand>(parameters, |
| 384 | bind(&Nfdc::onSuccess, this, _1, |
| 385 | "Successful in name registration"), |
| 386 | bind(&Nfdc::onError, this, _1, _2, |
| 387 | "Failed in name registration")); |
| 388 | }, |
| 389 | bind(&Nfdc::onObtainFaceIdFailure, this, _1)); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | void |
| 393 | Nfdc::ribUnregisterPrefix() |
| 394 | { |
| 395 | m_name = m_commandLineArguments[0]; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 396 | const std::string& faceName = m_commandLineArguments[1]; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 397 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 398 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 399 | [this] (const uint32_t faceId) { |
| 400 | ControlParameters parameters; |
| 401 | parameters |
| 402 | .setName(m_name) |
| 403 | .setFaceId(faceId) |
| 404 | .setOrigin(m_origin); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 405 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 406 | m_controller |
| 407 | .start<RibUnregisterCommand>(parameters, |
| 408 | bind(&Nfdc::onSuccess, this, _1, |
| 409 | "Successful in unregistering name"), |
| 410 | bind(&Nfdc::onError, this, _1, _2, |
| 411 | "Failed in unregistering name")); |
| 412 | }, |
| 413 | bind(&Nfdc::onObtainFaceIdFailure, this, _1)); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 417 | Nfdc::onCanonizeFailure(const std::string& reason) |
| 418 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 419 | BOOST_THROW_EXCEPTION(Error(reason)); |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | void |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 423 | Nfdc::onObtainFaceIdFailure(const std::string& message) |
| 424 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 425 | BOOST_THROW_EXCEPTION(Error(message)); |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 429 | Nfdc::faceCreate() |
| 430 | { |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 431 | boost::regex e("^[a-z0-9]+\\:.*"); |
| 432 | if (!boost::regex_match(m_commandLineArguments[0], e)) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 433 | BOOST_THROW_EXCEPTION(Error("invalid uri format")); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 434 | |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 435 | ndn::util::FaceUri faceUri; |
| 436 | faceUri.parse(m_commandLineArguments[0]); |
| 437 | |
| 438 | faceUri.canonize(bind(&Nfdc::startFaceCreate, this, _1), |
| 439 | bind(&Nfdc::onCanonizeFailure, this, _1), |
| 440 | m_ioService, ndn::time::seconds(4)); |
| 441 | } |
| 442 | |
| 443 | void |
| 444 | Nfdc::startFaceCreate(const ndn::util::FaceUri& canonicalUri) |
| 445 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 446 | ControlParameters parameters; |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 447 | parameters.setUri(canonicalUri.toString()); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 448 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 449 | m_controller.start<FaceCreateCommand>(parameters, |
| 450 | bind(&Nfdc::onSuccess, this, _1, |
| 451 | "Face creation succeeded"), |
| 452 | bind(&Nfdc::onError, this, _1, _2, |
| 453 | "Face creation failed")); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 454 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 455 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 456 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 457 | Nfdc::faceDestroy() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 458 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 459 | ControlParameters parameters; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 460 | const std::string& faceName = m_commandLineArguments[0]; |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 461 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 462 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 463 | [this] (const uint32_t faceId) { |
| 464 | ControlParameters faceParameters; |
| 465 | faceParameters.setFaceId(faceId); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 466 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 467 | m_controller.start<FaceDestroyCommand>(faceParameters, |
| 468 | bind(&Nfdc::onSuccess, this, _1, |
| 469 | "Face destroy succeeded"), |
| 470 | bind(&Nfdc::onError, this, _1, _2, |
| 471 | "Face destroy failed")); |
| 472 | }, |
| 473 | bind(&Nfdc::onObtainFaceIdFailure, this, _1)); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 474 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 475 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 476 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 477 | Nfdc::strategyChoiceSet() |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 478 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 479 | const std::string& name = m_commandLineArguments[0]; |
| 480 | const std::string& strategy = m_commandLineArguments[1]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 481 | |
| 482 | ControlParameters parameters; |
| 483 | parameters |
| 484 | .setName(name) |
| 485 | .setStrategy(strategy); |
| 486 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 487 | m_controller.start<StrategyChoiceSetCommand>(parameters, |
| 488 | bind(&Nfdc::onSuccess, this, _1, |
| 489 | "Successfully set strategy choice"), |
| 490 | bind(&Nfdc::onError, this, _1, _2, |
| 491 | "Failed to set strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 492 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 493 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 494 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 495 | Nfdc::strategyChoiceUnset() |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 496 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 497 | const std::string& name = m_commandLineArguments[0]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 498 | |
| 499 | ControlParameters parameters; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 500 | parameters.setName(name); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 501 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 502 | m_controller.start<StrategyChoiceUnsetCommand>(parameters, |
| 503 | bind(&Nfdc::onSuccess, this, _1, |
| 504 | "Successfully unset strategy choice"), |
| 505 | bind(&Nfdc::onError, this, _1, _2, |
| 506 | "Failed to unset strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 507 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 508 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 509 | void |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 510 | Nfdc::onSuccess(const ControlParameters& commandSuccessResult, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 511 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 512 | std::cout << message << ": " << commandSuccessResult << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 513 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 514 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 515 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 516 | Nfdc::onError(uint32_t code, const std::string& error, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 517 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 518 | std::ostringstream os; |
| 519 | os << message << ": " << error << " (code: " << code << ")"; |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 520 | BOOST_THROW_EXCEPTION(Error(os.str())); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 521 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 522 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 523 | } // namespace nfdc |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 524 | |
| 525 | int |
| 526 | main(int argc, char** argv) |
| 527 | { |
| 528 | ndn::Face face; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 529 | nfdc::Nfdc p(face); |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 530 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 531 | p.m_programName = argv[0]; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 532 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 533 | if (argc < 2) { |
| 534 | usage(p.m_programName); |
| 535 | return 0; |
| 536 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 537 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 538 | if (!strcmp(argv[1], "-h")) { |
| 539 | usage(p.m_programName); |
| 540 | return 0; |
| 541 | } |
| 542 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 543 | if (!strcmp(argv[1], "-V")) { |
| 544 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 545 | return 0; |
| 546 | } |
| 547 | |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 548 | ::optind = 2; //start reading options from 2nd argument i.e. Command |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 549 | int opt; |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 550 | while ((opt = ::getopt(argc, argv, "ICc:e:o:")) != -1) { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 551 | switch (opt) { |
| 552 | case 'I': |
| 553 | p.m_flags = p.m_flags & ~(nfdc::ROUTE_FLAG_CHILD_INHERIT); |
| 554 | break; |
| 555 | |
| 556 | case 'C': |
| 557 | p.m_flags = p.m_flags | nfdc::ROUTE_FLAG_CAPTURE; |
| 558 | break; |
| 559 | |
| 560 | case 'c': |
| 561 | try { |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 562 | p.m_cost = boost::lexical_cast<uint64_t>(::optarg); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 563 | } |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 564 | catch (boost::bad_lexical_cast&) { |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 565 | std::cerr << "Error: cost must be in unsigned integer format" << std::endl; |
| 566 | return 1; |
| 567 | } |
| 568 | break; |
| 569 | |
| 570 | case 'e': |
| 571 | uint64_t expires; |
| 572 | try { |
| 573 | expires = boost::lexical_cast<uint64_t>(::optarg); |
| 574 | } |
| 575 | catch (boost::bad_lexical_cast&) { |
| 576 | std::cerr << "Error: expiration time must be in unsigned integer format" << std::endl; |
| 577 | return 1; |
| 578 | } |
| 579 | p.m_expires = ndn::time::milliseconds(expires); |
| 580 | break; |
| 581 | |
| 582 | case 'o': |
| 583 | try { |
| 584 | p.m_origin = boost::lexical_cast<uint64_t>(::optarg); |
| 585 | } |
| 586 | catch (boost::bad_lexical_cast&) { |
| 587 | std::cerr << "Error: origin must be in unsigned integer format" << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 588 | return 1; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 589 | } |
| 590 | break; |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 591 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 592 | default: |
| 593 | usage(p.m_programName); |
| 594 | return 1; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 595 | } |
| 596 | } |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 597 | |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 598 | if (argc == ::optind) { |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 599 | usage(p.m_programName); |
| 600 | return 1; |
| 601 | } |
| 602 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 603 | try { |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 604 | p.m_commandLineArguments = argv + ::optind; |
| 605 | p.m_nOptions = argc - ::optind; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 606 | |
| 607 | //argv[1] points to the command, so pass it to the dispatch |
| 608 | bool isOk = p.dispatch(argv[1]); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 609 | if (!isOk) { |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 610 | usage(p.m_programName); |
| 611 | return 1; |
| 612 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 613 | face.processEvents(); |
| 614 | } |
| 615 | catch (const std::exception& e) { |
| 616 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 617 | return 2; |
| 618 | } |
| 619 | return 0; |
| 620 | } |