Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, 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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "legacy-nfdc.hpp" |
| 27 | #include "face-id-fetcher.hpp" |
| 28 | |
| 29 | #include <boost/regex.hpp> |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace tools { |
| 33 | namespace nfdc { |
| 34 | |
| 35 | using ndn::nfd::ControlParameters; |
| 36 | |
| 37 | const time::milliseconds LegacyNfdc::DEFAULT_EXPIRATION_PERIOD = time::milliseconds::max(); |
| 38 | const uint64_t LegacyNfdc::DEFAULT_COST = 0; |
| 39 | |
| 40 | LegacyNfdc::LegacyNfdc(ndn::Face& face) |
| 41 | : m_flags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT) |
| 42 | , m_cost(DEFAULT_COST) |
| 43 | , m_origin(ndn::nfd::ROUTE_ORIGIN_STATIC) |
| 44 | , m_expires(DEFAULT_EXPIRATION_PERIOD) |
| 45 | , m_facePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT) |
| 46 | , m_face(face) |
| 47 | , m_controller(face, m_keyChain) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | bool |
| 52 | LegacyNfdc::dispatch(const std::string& command) |
| 53 | { |
| 54 | if (command == "add-nexthop") { |
| 55 | if (m_nOptions != 2) |
| 56 | return false; |
| 57 | fibAddNextHop(); |
| 58 | } |
| 59 | else if (command == "remove-nexthop") { |
| 60 | if (m_nOptions != 2) |
| 61 | return false; |
| 62 | fibRemoveNextHop(); |
| 63 | } |
| 64 | else if (command == "register") { |
| 65 | if (m_nOptions != 2) |
| 66 | return false; |
| 67 | ribRegisterPrefix(); |
| 68 | } |
| 69 | else if (command == "unregister") { |
| 70 | if (m_nOptions != 2) |
| 71 | return false; |
| 72 | ribUnregisterPrefix(); |
| 73 | } |
| 74 | else if (command == "create") { |
| 75 | if (m_nOptions != 1) |
| 76 | return false; |
| 77 | faceCreate(); |
| 78 | } |
| 79 | else if (command == "destroy") { |
| 80 | if (m_nOptions != 1) |
| 81 | return false; |
| 82 | faceDestroy(); |
| 83 | } |
| 84 | else if (command == "set-strategy") { |
| 85 | if (m_nOptions != 2) |
| 86 | return false; |
| 87 | strategyChoiceSet(); |
| 88 | } |
| 89 | else if (command == "unset-strategy") { |
| 90 | if (m_nOptions != 1) |
| 91 | return false; |
| 92 | strategyChoiceUnset(); |
| 93 | } |
| 94 | else |
| 95 | return false; |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | LegacyNfdc::fibAddNextHop() |
| 102 | { |
| 103 | m_name = m_commandLineArguments[0]; |
| 104 | const std::string& faceName = m_commandLineArguments[1]; |
| 105 | |
| 106 | FaceIdFetcher::start(m_face, m_controller, faceName, true, |
| 107 | [this] (const uint32_t faceId) { |
| 108 | ControlParameters parameters; |
| 109 | parameters |
| 110 | .setName(m_name) |
| 111 | .setCost(m_cost) |
| 112 | .setFaceId(faceId); |
| 113 | |
| 114 | m_controller.start<ndn::nfd::FibAddNextHopCommand>(parameters, |
| 115 | bind(&LegacyNfdc::onSuccess, this, _1, "Nexthop insertion succeeded"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 116 | bind(&LegacyNfdc::onError, this, _1, "Nexthop insertion failed")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 117 | }, |
| 118 | bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1)); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | LegacyNfdc::fibRemoveNextHop() |
| 123 | { |
| 124 | m_name = m_commandLineArguments[0]; |
| 125 | const std::string& faceName = m_commandLineArguments[1]; |
| 126 | |
| 127 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 128 | [this] (const uint32_t faceId) { |
| 129 | ControlParameters parameters; |
| 130 | parameters |
| 131 | .setName(m_name) |
| 132 | .setFaceId(faceId); |
| 133 | |
| 134 | m_controller.start<ndn::nfd::FibRemoveNextHopCommand>(parameters, |
| 135 | bind(&LegacyNfdc::onSuccess, this, _1, "Nexthop removal succeeded"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 136 | bind(&LegacyNfdc::onError, this, _1, "Nexthop removal failed")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 137 | }, |
| 138 | bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1)); |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | LegacyNfdc::ribRegisterPrefix() |
| 143 | { |
| 144 | m_name = m_commandLineArguments[0]; |
| 145 | const std::string& faceName = m_commandLineArguments[1]; |
| 146 | |
| 147 | FaceIdFetcher::start(m_face, m_controller, faceName, true, |
| 148 | [this] (const uint32_t faceId) { |
| 149 | ControlParameters parameters; |
| 150 | parameters |
| 151 | .setName(m_name) |
| 152 | .setCost(m_cost) |
| 153 | .setFlags(m_flags) |
| 154 | .setOrigin(m_origin) |
| 155 | .setFaceId(faceId); |
| 156 | |
| 157 | if (m_expires != DEFAULT_EXPIRATION_PERIOD) |
| 158 | parameters.setExpirationPeriod(m_expires); |
| 159 | |
| 160 | m_controller.start<ndn::nfd::RibRegisterCommand>(parameters, |
| 161 | bind(&LegacyNfdc::onSuccess, this, _1, "Successful in name registration"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 162 | bind(&LegacyNfdc::onError, this, _1, "Failed in name registration")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 163 | }, |
| 164 | bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1)); |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | LegacyNfdc::ribUnregisterPrefix() |
| 169 | { |
| 170 | m_name = m_commandLineArguments[0]; |
| 171 | const std::string& faceName = m_commandLineArguments[1]; |
| 172 | |
| 173 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 174 | [this] (const uint32_t faceId) { |
| 175 | ControlParameters parameters; |
| 176 | parameters |
| 177 | .setName(m_name) |
| 178 | .setFaceId(faceId) |
| 179 | .setOrigin(m_origin); |
| 180 | |
| 181 | m_controller.start<ndn::nfd::RibUnregisterCommand>(parameters, |
| 182 | bind(&LegacyNfdc::onSuccess, this, _1, "Successful in unregistering name"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 183 | bind(&LegacyNfdc::onError, this, _1, "Failed in unregistering name")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 184 | }, |
| 185 | bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1)); |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | LegacyNfdc::onCanonizeFailure(const std::string& reason) |
| 190 | { |
| 191 | BOOST_THROW_EXCEPTION(Error(reason)); |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | LegacyNfdc::onObtainFaceIdFailure(const std::string& message) |
| 196 | { |
| 197 | BOOST_THROW_EXCEPTION(Error(message)); |
| 198 | } |
| 199 | |
| 200 | void |
| 201 | LegacyNfdc::faceCreate() |
| 202 | { |
| 203 | boost::regex e("^[a-z0-9]+\\:.*"); |
| 204 | if (!boost::regex_match(m_commandLineArguments[0], e)) |
| 205 | BOOST_THROW_EXCEPTION(Error("invalid uri format")); |
| 206 | |
| 207 | FaceUri faceUri; |
| 208 | faceUri.parse(m_commandLineArguments[0]); |
| 209 | |
| 210 | faceUri.canonize(bind(&LegacyNfdc::startFaceCreate, this, _1), |
| 211 | bind(&LegacyNfdc::onCanonizeFailure, this, _1), |
| 212 | m_face.getIoService(), time::seconds(4)); |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | LegacyNfdc::startFaceCreate(const FaceUri& canonicalUri) |
| 217 | { |
| 218 | ControlParameters parameters; |
| 219 | parameters.setUri(canonicalUri.toString()); |
| 220 | parameters.setFacePersistency(m_facePersistency); |
| 221 | |
| 222 | m_controller.start<ndn::nfd::FaceCreateCommand>(parameters, |
| 223 | bind(&LegacyNfdc::onSuccess, this, _1, "Face creation succeeded"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 224 | bind(&LegacyNfdc::onError, this, _1, "Face creation failed")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void |
| 228 | LegacyNfdc::faceDestroy() |
| 229 | { |
| 230 | ControlParameters parameters; |
| 231 | const std::string& faceName = m_commandLineArguments[0]; |
| 232 | |
| 233 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 234 | [this] (const uint32_t faceId) { |
| 235 | ControlParameters faceParameters; |
| 236 | faceParameters.setFaceId(faceId); |
| 237 | |
| 238 | m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters, |
| 239 | bind(&LegacyNfdc::onSuccess, this, _1, "Face destroy succeeded"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 240 | bind(&LegacyNfdc::onError, this, _1, "Face destroy failed")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 241 | }, |
| 242 | bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1)); |
| 243 | } |
| 244 | |
| 245 | void |
| 246 | LegacyNfdc::strategyChoiceSet() |
| 247 | { |
| 248 | const std::string& name = m_commandLineArguments[0]; |
| 249 | const std::string& strategy = m_commandLineArguments[1]; |
| 250 | |
| 251 | ControlParameters parameters; |
| 252 | parameters |
| 253 | .setName(name) |
| 254 | .setStrategy(strategy); |
| 255 | |
| 256 | m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters, |
| 257 | bind(&LegacyNfdc::onSuccess, this, _1, "Successfully set strategy choice"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 258 | bind(&LegacyNfdc::onError, this, _1, "Failed to set strategy choice")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void |
| 262 | LegacyNfdc::strategyChoiceUnset() |
| 263 | { |
| 264 | const std::string& name = m_commandLineArguments[0]; |
| 265 | |
| 266 | ControlParameters parameters; |
| 267 | parameters.setName(name); |
| 268 | |
| 269 | m_controller.start<ndn::nfd::StrategyChoiceUnsetCommand>(parameters, |
| 270 | bind(&LegacyNfdc::onSuccess, this, _1, "Successfully unset strategy choice"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 271 | bind(&LegacyNfdc::onError, this, _1, "Failed to unset strategy choice")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void |
| 275 | LegacyNfdc::onSuccess(const ControlParameters& commandSuccessResult, const std::string& message) |
| 276 | { |
| 277 | std::cout << message << ": " << commandSuccessResult << std::endl; |
| 278 | } |
| 279 | |
| 280 | void |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 281 | LegacyNfdc::onError(const ndn::nfd::ControlResponse& response, const std::string& message) |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 282 | { |
| 283 | std::ostringstream os; |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 284 | os << message << ": " << response.getText() << " (code: " << response.getCode() << ")"; |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 285 | BOOST_THROW_EXCEPTION(Error(os.str())); |
| 286 | } |
| 287 | |
| 288 | } // namespace nfdc |
| 289 | } // namespace tools |
| 290 | } // namespace nfd |