hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [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 |
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; |
| 181 | ndn::Block block; |
| 182 | bool ok = ndn::Block::fromBuffer(data, offset, block); |
| 183 | |
| 184 | if (!ok) { |
| 185 | if (m_allowCreate) { |
| 186 | startFaceCreate(canonicalUri); |
| 187 | } |
| 188 | else { |
| 189 | fail("Fail to find faceId"); |
| 190 | } |
| 191 | } |
| 192 | else { |
| 193 | try { |
| 194 | FaceStatus status(block); |
| 195 | succeed(status.getFaceId()); |
| 196 | } |
| 197 | catch (const ndn::tlv::Error& e) { |
| 198 | std::string errorMessage(e.what()); |
| 199 | fail("ERROR: " + errorMessage); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | Nfdc::FaceIdFetcher::onQueryFailure(uint32_t errorCode, |
| 206 | const ndn::util::FaceUri& canonicalUri) |
| 207 | { |
| 208 | std::stringstream ss; |
| 209 | ss << "Cannot fetch data (code " << errorCode << ")"; |
| 210 | fail(ss.str()); |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | Nfdc::FaceIdFetcher::onFaceCreateError(uint32_t code, |
| 215 | const std::string& error, |
| 216 | const std::string& message) |
| 217 | { |
| 218 | std::stringstream ss; |
| 219 | ss << message << " : " << error << " (code " << code << ")"; |
| 220 | fail(ss.str()); |
| 221 | } |
| 222 | |
| 223 | void |
| 224 | Nfdc::FaceIdFetcher::startFaceCreate(const ndn::util::FaceUri& canonicalUri) |
| 225 | { |
| 226 | ControlParameters parameters; |
| 227 | parameters.setUri(canonicalUri.toString()); |
| 228 | |
| 229 | m_controller.start<FaceCreateCommand>(parameters, |
| 230 | [this] (const ControlParameters& result) { |
| 231 | succeed(result.getFaceId()); |
| 232 | }, |
| 233 | bind(&FaceIdFetcher::onFaceCreateError, this, _1, _2, |
| 234 | "Face creation failed")); |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | Nfdc::FaceIdFetcher::succeed(uint32_t faceId) |
| 239 | { |
| 240 | m_onSucceed(faceId); |
| 241 | delete this; |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | Nfdc::FaceIdFetcher::fail(const std::string& reason) |
| 246 | { |
| 247 | m_onFail(reason); |
| 248 | delete this; |
| 249 | } |
| 250 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 251 | Nfdc::Nfdc(ndn::Face& face) |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 252 | : m_flags(ROUTE_FLAG_CHILD_INHERIT) |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 253 | , m_cost(DEFAULT_COST) |
| 254 | , m_origin(ROUTE_ORIGIN_STATIC) |
| 255 | , m_expires(DEFAULT_EXPIRATION_PERIOD) |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 256 | , m_face(face) |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 257 | , m_controller(face, m_keyChain) |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 258 | , m_ioService(face.getIoService()) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 259 | { |
| 260 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 261 | |
| 262 | Nfdc::~Nfdc() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 263 | { |
| 264 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 265 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 266 | bool |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 267 | Nfdc::dispatch(const std::string& command) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 268 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 269 | if (command == "add-nexthop") { |
| 270 | if (m_nOptions != 2) |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 271 | return false; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 272 | fibAddNextHop(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 273 | } |
| 274 | else if (command == "remove-nexthop") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 275 | if (m_nOptions != 2) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 276 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 277 | fibRemoveNextHop(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 278 | } |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 279 | else if (command == "register") { |
| 280 | if (m_nOptions != 2) |
| 281 | return false; |
| 282 | ribRegisterPrefix(); |
| 283 | } |
| 284 | else if (command == "unregister") { |
| 285 | if (m_nOptions != 2) |
| 286 | return false; |
| 287 | ribUnregisterPrefix(); |
| 288 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 289 | else if (command == "create") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 290 | if (m_nOptions != 1) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 291 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 292 | faceCreate(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 293 | } |
| 294 | else if (command == "destroy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 295 | if (m_nOptions != 1) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 296 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 297 | faceDestroy(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 298 | } |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 299 | else if (command == "set-strategy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 300 | if (m_nOptions != 2) |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 301 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 302 | strategyChoiceSet(); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 303 | } |
| 304 | else if (command == "unset-strategy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 305 | if (m_nOptions != 1) |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 306 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 307 | strategyChoiceUnset(); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 308 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 309 | else |
| 310 | usage(m_programName); |
| 311 | |
| 312 | return true; |
| 313 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 314 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 315 | void |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 316 | Nfdc::fibAddNextHop() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 317 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 318 | m_name = m_commandLineArguments[0]; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 319 | const std::string& faceName = m_commandLineArguments[1]; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 320 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 321 | FaceIdFetcher::start(m_face, m_controller, faceName, true, |
| 322 | [this] (const uint32_t faceId) { |
| 323 | ControlParameters parameters; |
| 324 | parameters |
| 325 | .setName(m_name) |
| 326 | .setCost(m_cost) |
| 327 | .setFaceId(faceId); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 328 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 329 | m_controller |
| 330 | .start<FibAddNextHopCommand>(parameters, |
| 331 | bind(&Nfdc::onSuccess, this, _1, |
| 332 | "Nexthop insertion succeeded"), |
| 333 | bind(&Nfdc::onError, this, _1, _2, |
| 334 | "Nexthop insertion failed")); |
| 335 | }, |
| 336 | bind(&Nfdc::onObtainFaceIdFailure, this, _1)); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void |
| 340 | Nfdc::fibRemoveNextHop() |
| 341 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 342 | m_name = m_commandLineArguments[0]; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 343 | const std::string& faceName = m_commandLineArguments[1]; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 344 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 345 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 346 | [this] (const uint32_t faceId) { |
| 347 | ControlParameters parameters; |
| 348 | parameters |
| 349 | .setName(m_name) |
| 350 | .setFaceId(faceId); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 351 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 352 | m_controller |
| 353 | .start<FibRemoveNextHopCommand>(parameters, |
| 354 | bind(&Nfdc::onSuccess, this, _1, |
| 355 | "Nexthop removal succeeded"), |
| 356 | bind(&Nfdc::onError, this, _1, _2, |
| 357 | "Nexthop removal failed")); |
| 358 | }, |
| 359 | bind(&Nfdc::onObtainFaceIdFailure, this, _1)); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void |
| 363 | Nfdc::ribRegisterPrefix() |
| 364 | { |
| 365 | m_name = m_commandLineArguments[0]; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 366 | const std::string& faceName = m_commandLineArguments[1]; |
Alexander Afanasyev | f67cf08 | 2014-07-18 16:47:29 -0700 | [diff] [blame] | 367 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 368 | FaceIdFetcher::start(m_face, m_controller, faceName, true, |
| 369 | [this] (const uint32_t faceId) { |
| 370 | ControlParameters parameters; |
| 371 | parameters |
| 372 | .setName(m_name) |
| 373 | .setCost(m_cost) |
| 374 | .setFlags(m_flags) |
| 375 | .setOrigin(m_origin) |
| 376 | .setFaceId(faceId); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 377 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 378 | if (m_expires != DEFAULT_EXPIRATION_PERIOD) |
| 379 | parameters.setExpirationPeriod(m_expires); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 380 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 381 | m_controller |
| 382 | .start<RibRegisterCommand>(parameters, |
| 383 | bind(&Nfdc::onSuccess, this, _1, |
| 384 | "Successful in name registration"), |
| 385 | bind(&Nfdc::onError, this, _1, _2, |
| 386 | "Failed in name registration")); |
| 387 | }, |
| 388 | bind(&Nfdc::onObtainFaceIdFailure, this, _1)); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void |
| 392 | Nfdc::ribUnregisterPrefix() |
| 393 | { |
| 394 | m_name = m_commandLineArguments[0]; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 395 | const std::string& faceName = m_commandLineArguments[1]; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 396 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 397 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 398 | [this] (const uint32_t faceId) { |
| 399 | ControlParameters parameters; |
| 400 | parameters |
| 401 | .setName(m_name) |
| 402 | .setFaceId(faceId) |
| 403 | .setOrigin(m_origin); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 404 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 405 | m_controller |
| 406 | .start<RibUnregisterCommand>(parameters, |
| 407 | bind(&Nfdc::onSuccess, this, _1, |
| 408 | "Successful in unregistering name"), |
| 409 | bind(&Nfdc::onError, this, _1, _2, |
| 410 | "Failed in unregistering name")); |
| 411 | }, |
| 412 | bind(&Nfdc::onObtainFaceIdFailure, this, _1)); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 416 | Nfdc::onCanonizeFailure(const std::string& reason) |
| 417 | { |
Chengyu Fan | 5010123 | 2015-01-20 14:55:59 -0700 | [diff] [blame] | 418 | throw Error(reason); |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | void |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 422 | Nfdc::onObtainFaceIdFailure(const std::string& message) |
| 423 | { |
Chengyu Fan | 5010123 | 2015-01-20 14:55:59 -0700 | [diff] [blame] | 424 | throw Error(message); |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 428 | Nfdc::faceCreate() |
| 429 | { |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 430 | boost::regex e("^[a-z0-9]+\\:.*"); |
| 431 | if (!boost::regex_match(m_commandLineArguments[0], e)) |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 432 | throw Error("invalid uri format"); |
| 433 | |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 434 | ndn::util::FaceUri faceUri; |
| 435 | faceUri.parse(m_commandLineArguments[0]); |
| 436 | |
| 437 | faceUri.canonize(bind(&Nfdc::startFaceCreate, this, _1), |
| 438 | bind(&Nfdc::onCanonizeFailure, this, _1), |
| 439 | m_ioService, ndn::time::seconds(4)); |
| 440 | } |
| 441 | |
| 442 | void |
| 443 | Nfdc::startFaceCreate(const ndn::util::FaceUri& canonicalUri) |
| 444 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 445 | ControlParameters parameters; |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 446 | parameters.setUri(canonicalUri.toString()); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 447 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 448 | m_controller.start<FaceCreateCommand>(parameters, |
| 449 | bind(&Nfdc::onSuccess, this, _1, |
| 450 | "Face creation succeeded"), |
| 451 | bind(&Nfdc::onError, this, _1, _2, |
| 452 | "Face creation failed")); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 453 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 454 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 455 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 456 | Nfdc::faceDestroy() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 457 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 458 | ControlParameters parameters; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 459 | const std::string& faceName = m_commandLineArguments[0]; |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 460 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 461 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 462 | [this] (const uint32_t faceId) { |
| 463 | ControlParameters faceParameters; |
| 464 | faceParameters.setFaceId(faceId); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 465 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 466 | m_controller.start<FaceDestroyCommand>(faceParameters, |
| 467 | bind(&Nfdc::onSuccess, this, _1, |
| 468 | "Face destroy succeeded"), |
| 469 | bind(&Nfdc::onError, this, _1, _2, |
| 470 | "Face destroy failed")); |
| 471 | }, |
| 472 | bind(&Nfdc::onObtainFaceIdFailure, this, _1)); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 473 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 474 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 475 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 476 | Nfdc::strategyChoiceSet() |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 477 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 478 | const std::string& name = m_commandLineArguments[0]; |
| 479 | const std::string& strategy = m_commandLineArguments[1]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 480 | |
| 481 | ControlParameters parameters; |
| 482 | parameters |
| 483 | .setName(name) |
| 484 | .setStrategy(strategy); |
| 485 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 486 | m_controller.start<StrategyChoiceSetCommand>(parameters, |
| 487 | bind(&Nfdc::onSuccess, this, _1, |
| 488 | "Successfully set strategy choice"), |
| 489 | bind(&Nfdc::onError, this, _1, _2, |
| 490 | "Failed to set strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 491 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 492 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 493 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 494 | Nfdc::strategyChoiceUnset() |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 495 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 496 | const std::string& name = m_commandLineArguments[0]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 497 | |
| 498 | ControlParameters parameters; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 499 | parameters.setName(name); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 500 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 501 | m_controller.start<StrategyChoiceUnsetCommand>(parameters, |
| 502 | bind(&Nfdc::onSuccess, this, _1, |
| 503 | "Successfully unset strategy choice"), |
| 504 | bind(&Nfdc::onError, this, _1, _2, |
| 505 | "Failed to unset strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 506 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 507 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 508 | void |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 509 | Nfdc::onSuccess(const ControlParameters& commandSuccessResult, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 510 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 511 | std::cout << message << ": " << commandSuccessResult << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 512 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 513 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 514 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 515 | Nfdc::onError(uint32_t code, const std::string& error, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 516 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 517 | std::ostringstream os; |
| 518 | os << message << ": " << error << " (code: " << code << ")"; |
| 519 | throw Error(os.str()); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 520 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 521 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 522 | } // namespace nfdc |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 523 | |
| 524 | int |
| 525 | main(int argc, char** argv) |
| 526 | { |
| 527 | ndn::Face face; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 528 | nfdc::Nfdc p(face); |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 529 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 530 | p.m_programName = argv[0]; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 531 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 532 | if (argc < 2) { |
| 533 | usage(p.m_programName); |
| 534 | return 0; |
| 535 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 536 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 537 | if (!strcmp(argv[1], "-h")) { |
| 538 | usage(p.m_programName); |
| 539 | return 0; |
| 540 | } |
| 541 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 542 | if (!strcmp(argv[1], "-V")) { |
| 543 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 544 | return 0; |
| 545 | } |
| 546 | |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 547 | ::optind = 2; //start reading options from 2nd argument i.e. Command |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 548 | int opt; |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 549 | while ((opt = ::getopt(argc, argv, "ICc:e:o:")) != -1) { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 550 | switch (opt) { |
| 551 | case 'I': |
| 552 | p.m_flags = p.m_flags & ~(nfdc::ROUTE_FLAG_CHILD_INHERIT); |
| 553 | break; |
| 554 | |
| 555 | case 'C': |
| 556 | p.m_flags = p.m_flags | nfdc::ROUTE_FLAG_CAPTURE; |
| 557 | break; |
| 558 | |
| 559 | case 'c': |
| 560 | try { |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 561 | p.m_cost = boost::lexical_cast<uint64_t>(::optarg); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 562 | } |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 563 | catch (boost::bad_lexical_cast&) { |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 564 | std::cerr << "Error: cost must be in unsigned integer format" << std::endl; |
| 565 | return 1; |
| 566 | } |
| 567 | break; |
| 568 | |
| 569 | case 'e': |
| 570 | uint64_t expires; |
| 571 | try { |
| 572 | expires = boost::lexical_cast<uint64_t>(::optarg); |
| 573 | } |
| 574 | catch (boost::bad_lexical_cast&) { |
| 575 | std::cerr << "Error: expiration time must be in unsigned integer format" << std::endl; |
| 576 | return 1; |
| 577 | } |
| 578 | p.m_expires = ndn::time::milliseconds(expires); |
| 579 | break; |
| 580 | |
| 581 | case 'o': |
| 582 | try { |
| 583 | p.m_origin = boost::lexical_cast<uint64_t>(::optarg); |
| 584 | } |
| 585 | catch (boost::bad_lexical_cast&) { |
| 586 | std::cerr << "Error: origin must be in unsigned integer format" << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 587 | return 1; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 588 | } |
| 589 | break; |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 590 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 591 | default: |
| 592 | usage(p.m_programName); |
| 593 | return 1; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 594 | } |
| 595 | } |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 596 | |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 597 | if (argc == ::optind) { |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 598 | usage(p.m_programName); |
| 599 | return 1; |
| 600 | } |
| 601 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 602 | try { |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 603 | p.m_commandLineArguments = argv + ::optind; |
| 604 | p.m_nOptions = argc - ::optind; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 605 | |
| 606 | //argv[1] points to the command, so pass it to the dispatch |
| 607 | bool isOk = p.dispatch(argv[1]); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 608 | if (!isOk) { |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 609 | usage(p.m_programName); |
| 610 | return 1; |
| 611 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 612 | face.processEvents(); |
| 613 | } |
| 614 | catch (const std::exception& e) { |
| 615 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 616 | return 2; |
| 617 | } |
| 618 | return 0; |
| 619 | } |