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 | 9bcbc7c | 2014-04-06 19:37:37 -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 |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 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/>. |
| 24 | **/ |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 25 | #include "nfdc.hpp" |
| 26 | #include <boost/lexical_cast.hpp> |
| 27 | #include <boost/algorithm/string.hpp> |
| 28 | #include <boost/algorithm/string/regex_find_format.hpp> |
| 29 | #include <boost/regex.hpp> |
| 30 | |
| 31 | void |
| 32 | usage(const char* programName) |
| 33 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 34 | std::cout << "Usage:\n" << programName << " [-h] COMMAND [<Command Options>]\n" |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 35 | " -h print usage and exit\n" |
| 36 | "\n" |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 37 | " COMMAND can be one of the following:\n" |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 38 | " register [-I] [-C] [-c cost] name <faceId | faceUri>\n" |
| 39 | " register name to the given faceId or faceUri\n" |
| 40 | " -I: unset CHILD_INHERIT flag\n" |
| 41 | " -C: set CAPTURE flag\n" |
| 42 | " -c: specify cost\n" |
| 43 | " unregister name <faceId>\n" |
| 44 | " unregister name from the given faceId\n" |
| 45 | " create <faceUri> \n" |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 46 | " Create a face in one of the following formats:\n" |
| 47 | " UDP unicast: udp[4|6]://<remote-IP-or-host>[:<remote-port>]\n" |
| 48 | " TCP: tcp[4|6]://<remote-IP-or-host>[:<remote-port>] \n" |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 49 | " destroy <faceId | faceUri> \n" |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 50 | " Destroy a face\n" |
| 51 | " set-strategy <name> <strategy> \n" |
| 52 | " Set the strategy for a namespace \n" |
| 53 | " unset-strategy <name> \n" |
| 54 | " Unset the strategy for a namespace \n" |
hilata | 6ee6e07 | 2014-04-20 17:10:18 -0500 | [diff] [blame^] | 55 | " add-nexthop [-c <cost>] <name> <faceId | faceUri>\n" |
| 56 | " Add a nexthop to a FIB entry\n" |
| 57 | " -c: specify cost\n" |
| 58 | " remove-nexthop <name> <faceId> \n" |
| 59 | " Remove a nexthop from a FIB entry\n" |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 60 | << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | namespace nfdc { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 64 | |
| 65 | Nfdc::Nfdc(ndn::Face& face) |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 66 | : m_flags(ROUTE_FLAG_CHILD_INHERIT) |
| 67 | , m_cost(0) |
| 68 | , m_controller(face) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 69 | { |
| 70 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 71 | |
| 72 | Nfdc::~Nfdc() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 73 | { |
| 74 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 75 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 76 | bool |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 77 | Nfdc::dispatch(const std::string& command) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 78 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 79 | if (command == "add-nexthop") { |
| 80 | if (m_nOptions != 2) |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 81 | return false; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 82 | fibAddNextHop(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 83 | } |
| 84 | else if (command == "remove-nexthop") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 85 | if (m_nOptions != 2) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 86 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 87 | fibRemoveNextHop(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 88 | } |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 89 | else if (command == "register") { |
| 90 | if (m_nOptions != 2) |
| 91 | return false; |
| 92 | ribRegisterPrefix(); |
| 93 | } |
| 94 | else if (command == "unregister") { |
| 95 | if (m_nOptions != 2) |
| 96 | return false; |
| 97 | ribUnregisterPrefix(); |
| 98 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 99 | else if (command == "create") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 100 | if (m_nOptions != 1) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 101 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 102 | faceCreate(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 103 | } |
| 104 | else if (command == "destroy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 105 | if (m_nOptions != 1) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 106 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 107 | faceDestroy(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 108 | } |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 109 | else if (command == "set-strategy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 110 | if (m_nOptions != 2) |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 111 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 112 | strategyChoiceSet(); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 113 | } |
| 114 | else if (command == "unset-strategy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 115 | if (m_nOptions != 1) |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 116 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 117 | strategyChoiceUnset(); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 118 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 119 | else |
| 120 | usage(m_programName); |
| 121 | |
| 122 | return true; |
| 123 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 124 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 125 | namespace { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 126 | |
| 127 | inline bool |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 128 | isValidUri(const std::string& input) |
| 129 | { |
| 130 | // an extended regex to support the validation of uri structure |
| 131 | // boost::regex e("^[a-z0-9]+-?+[a-z0-9]+\\:\\/\\/.*"); |
| 132 | boost::regex e("^[a-z0-9]+\\:.*"); |
| 133 | return boost::regex_match(input, e); |
| 134 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 135 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 136 | } // anonymous namespace |
| 137 | |
| 138 | void |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 139 | Nfdc::fibAddNextHop() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 140 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 141 | m_name = m_commandLineArguments[0]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 142 | ControlParameters parameters; |
| 143 | parameters |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 144 | .setName(m_name) |
| 145 | .setCost(m_cost); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 146 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 147 | if (!isValidUri(m_commandLineArguments[1])) { |
| 148 | try { //So the uri is not valid, may be a faceId is provided. |
| 149 | m_faceId = boost::lexical_cast<int>(m_commandLineArguments[1]); |
| 150 | } |
| 151 | catch (const std::exception& e) { |
| 152 | std::cerr << "No valid faceUri or faceId is provided"<< std::endl; |
| 153 | return; |
| 154 | } |
| 155 | parameters.setFaceId(m_faceId); |
| 156 | fibAddNextHop(parameters); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 157 | } |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 158 | else { |
| 159 | ControlParameters faceParameters; |
| 160 | faceParameters.setUri(m_commandLineArguments[1]); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 161 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 162 | m_controller.start<FaceCreateCommand>(faceParameters, |
| 163 | bind(&Nfdc::fibAddNextHop, this, _1), |
| 164 | bind(&Nfdc::onError, this, _1, _2, |
| 165 | "Face creation failed")); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | Nfdc::fibAddNextHop(const ControlParameters& faceCreateResult) |
| 171 | { |
| 172 | ControlParameters ribParameters; |
| 173 | ribParameters |
| 174 | .setName(m_name) |
| 175 | .setCost(m_cost) |
| 176 | .setFaceId(faceCreateResult.getFaceId()); |
| 177 | |
| 178 | m_controller.start<FibAddNextHopCommand>(ribParameters, |
| 179 | bind(&Nfdc::onSuccess, this, _1, |
| 180 | "Nexthop insertion succeeded"), |
| 181 | bind(&Nfdc::onError, this, _1, _2, |
| 182 | "Nexthop insertion failed")); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void |
| 186 | Nfdc::fibRemoveNextHop() |
| 187 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 188 | m_name = m_commandLineArguments[0]; |
| 189 | try { |
| 190 | m_faceId = boost::lexical_cast<int>(m_commandLineArguments[1]); |
| 191 | } |
| 192 | catch (const std::exception& e) { |
| 193 | std::cerr << "No valid faceUri or faceId is provided"<< std::endl; |
| 194 | return; |
| 195 | } |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 196 | |
| 197 | ControlParameters parameters; |
| 198 | parameters |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 199 | .setName(m_name) |
| 200 | .setFaceId(m_faceId); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 201 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 202 | m_controller.start<FibRemoveNextHopCommand>(parameters, |
| 203 | bind(&Nfdc::onSuccess, this, _1, |
| 204 | "Nexthop removal succeeded"), |
| 205 | bind(&Nfdc::onError, this, _1, _2, |
| 206 | "Nexthop removal failed")); |
| 207 | } |
| 208 | |
| 209 | void |
| 210 | Nfdc::ribRegisterPrefix() |
| 211 | { |
| 212 | m_name = m_commandLineArguments[0]; |
| 213 | ControlParameters parameters; |
| 214 | parameters |
| 215 | .setName(m_name) |
| 216 | .setCost(m_cost) |
| 217 | .setFlags(m_flags); |
| 218 | |
| 219 | if (!isValidUri(m_commandLineArguments[1])) { |
| 220 | try { //So the uri is not valid, may be a faceId is provided. |
| 221 | m_faceId = boost::lexical_cast<int>(m_commandLineArguments[1]); |
| 222 | } |
| 223 | catch (const std::exception& e) { |
| 224 | std::cerr << "No valid faceUri or faceId is provided"<< std::endl; |
| 225 | return; |
| 226 | } |
| 227 | parameters.setFaceId(m_faceId); |
| 228 | ribRegisterPrefix(parameters); |
| 229 | } |
| 230 | else { |
| 231 | ControlParameters faceParameters; |
| 232 | faceParameters.setUri(m_commandLineArguments[1]); |
| 233 | |
| 234 | m_controller.start<FaceCreateCommand>(faceParameters, |
| 235 | bind(&Nfdc::ribRegisterPrefix, this, _1), |
| 236 | bind(&Nfdc::onError, this, _1, _2, |
| 237 | "Face creation failed")); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void |
| 242 | Nfdc::ribRegisterPrefix(const ControlParameters& faceCreateResult) |
| 243 | { |
| 244 | ControlParameters ribParameters; |
| 245 | ribParameters |
| 246 | .setName(m_name) |
| 247 | .setCost(m_cost) |
| 248 | .setFlags(m_flags) |
| 249 | .setFaceId(faceCreateResult.getFaceId()); |
| 250 | |
| 251 | m_controller.start<RibRegisterCommand>(ribParameters, |
| 252 | bind(&Nfdc::onSuccess, this, _1, |
| 253 | "Successful in name registration"), |
| 254 | bind(&Nfdc::onError, this, _1, _2, |
| 255 | "Failed in name registration")); |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | Nfdc::ribUnregisterPrefix() |
| 260 | { |
| 261 | m_name = m_commandLineArguments[0]; |
| 262 | try { |
| 263 | m_faceId = boost::lexical_cast<int>(m_commandLineArguments[1]); |
| 264 | } |
| 265 | catch (const std::exception& e) { |
| 266 | std::cerr << "No valid faceUri or faceId is provided"<< std::endl; |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | ControlParameters parameters; |
| 271 | parameters |
| 272 | .setName(m_name) |
| 273 | .setFaceId(m_faceId); |
| 274 | |
| 275 | m_controller.start<RibUnregisterCommand>(parameters, |
| 276 | bind(&Nfdc::onSuccess, this, _1, |
| 277 | "Successful in unregistering name"), |
| 278 | bind(&Nfdc::onError, this, _1, _2, |
| 279 | "Failed in unregistering name")); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void |
| 283 | Nfdc::faceCreate() |
| 284 | { |
| 285 | if (!isValidUri(m_commandLineArguments[0])) |
| 286 | throw Error("invalid uri format"); |
| 287 | |
| 288 | ControlParameters parameters; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 289 | parameters.setUri(m_commandLineArguments[0]); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 290 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 291 | m_controller.start<FaceCreateCommand>(parameters, |
| 292 | bind(&Nfdc::onSuccess, this, _1, |
| 293 | "Face creation succeeded"), |
| 294 | bind(&Nfdc::onError, this, _1, _2, |
| 295 | "Face creation failed")); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 296 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 297 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 298 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 299 | Nfdc::faceDestroy() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 300 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 301 | ControlParameters parameters; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 302 | if (!isValidUri(m_commandLineArguments[0])) { |
| 303 | try { //So the uri is not valid, may be a faceId is provided. |
| 304 | m_faceId = boost::lexical_cast<int>(m_commandLineArguments[0]); |
| 305 | } |
| 306 | catch (const std::exception& e) { |
| 307 | std::cerr << "No valid faceUri or faceId is provided" << std::endl; |
| 308 | return; |
| 309 | } |
| 310 | parameters.setFaceId(m_faceId); |
| 311 | faceDestroy(parameters); |
| 312 | } |
| 313 | else{ |
| 314 | ControlParameters faceParameters; |
| 315 | faceParameters.setUri(m_commandLineArguments[0]); |
| 316 | m_controller.start<FaceCreateCommand>(faceParameters, |
| 317 | bind(&Nfdc::faceDestroy, this, _1), |
| 318 | bind(&Nfdc::onError, this, _1, _2, |
| 319 | "Face destroy failed")); |
| 320 | } |
| 321 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 322 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 323 | void |
| 324 | Nfdc::faceDestroy(const ControlParameters& faceCreateResult) |
| 325 | { |
| 326 | ControlParameters faceParameters; |
| 327 | faceParameters.setFaceId(faceCreateResult.getFaceId()); |
| 328 | |
| 329 | m_controller.start<FaceDestroyCommand>(faceParameters, |
| 330 | bind(&Nfdc::onSuccess, this, _1, |
| 331 | "Face destroy succeeded"), |
| 332 | bind(&Nfdc::onError, this, _1, _2, |
| 333 | "Face destroy failed")); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 334 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 335 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 336 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 337 | Nfdc::strategyChoiceSet() |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 338 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 339 | const std::string& name = m_commandLineArguments[0]; |
| 340 | const std::string& strategy = m_commandLineArguments[1]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 341 | |
| 342 | ControlParameters parameters; |
| 343 | parameters |
| 344 | .setName(name) |
| 345 | .setStrategy(strategy); |
| 346 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 347 | m_controller.start<StrategyChoiceSetCommand>(parameters, |
| 348 | bind(&Nfdc::onSuccess, this, _1, |
| 349 | "Successfully set strategy choice"), |
| 350 | bind(&Nfdc::onError, this, _1, _2, |
| 351 | "Failed to set strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 352 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 353 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 354 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 355 | Nfdc::strategyChoiceUnset() |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 356 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 357 | const std::string& name = m_commandLineArguments[0]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 358 | |
| 359 | ControlParameters parameters; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 360 | parameters.setName(name); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 361 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 362 | m_controller.start<StrategyChoiceUnsetCommand>(parameters, |
| 363 | bind(&Nfdc::onSuccess, this, _1, |
| 364 | "Successfully unset strategy choice"), |
| 365 | bind(&Nfdc::onError, this, _1, _2, |
| 366 | "Failed to unset strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 367 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 368 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 369 | void |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 370 | Nfdc::onSuccess(const ControlParameters& commandSuccessResult, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 371 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 372 | std::cout << message << ": " << commandSuccessResult << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 373 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 374 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 375 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 376 | Nfdc::onError(uint32_t code, const std::string& error, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 377 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 378 | std::ostringstream os; |
| 379 | os << message << ": " << error << " (code: " << code << ")"; |
| 380 | throw Error(os.str()); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 381 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 382 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 383 | |
| 384 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 385 | } // namespace nfdc |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 386 | |
| 387 | int |
| 388 | main(int argc, char** argv) |
| 389 | { |
| 390 | ndn::Face face; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 391 | nfdc::Nfdc p(face); |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 392 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 393 | p.m_programName = argv[0]; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 394 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 395 | if (argc < 2) { |
| 396 | usage(p.m_programName); |
| 397 | return 0; |
| 398 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 399 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 400 | if (!strcmp(argv[1], "-h")) { |
| 401 | usage(p.m_programName); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | int opt; |
| 407 | //start reading options from 2nd argument i.e. Command |
| 408 | optind=2; |
| 409 | while ((opt = getopt(argc, argv, "ICc:")) != -1) { |
| 410 | switch (opt) { |
| 411 | case 'I': |
| 412 | p.m_flags = p.m_flags & ~(nfdc::ROUTE_FLAG_CHILD_INHERIT); |
| 413 | break; |
| 414 | |
| 415 | case 'C': |
| 416 | p.m_flags = p.m_flags | nfdc::ROUTE_FLAG_CAPTURE; |
| 417 | break; |
| 418 | |
| 419 | case 'c': |
| 420 | try { |
| 421 | p.m_cost = boost::lexical_cast<int>(optarg); |
| 422 | } |
| 423 | catch (const std::exception& e) { |
| 424 | std::cerr << "Error: cost must be in integer format" << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 425 | return 1; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 426 | } |
| 427 | break; |
| 428 | default: |
| 429 | usage(p.m_programName); |
| 430 | return 1; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 431 | } |
| 432 | } |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 433 | |
| 434 | if (argc == optind) { |
| 435 | usage(p.m_programName); |
| 436 | return 1; |
| 437 | } |
| 438 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 439 | try { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 440 | p.m_commandLineArguments = const_cast<const char**>(argv + optind); |
| 441 | p.m_nOptions = argc - optind; |
| 442 | |
| 443 | //argv[1] points to the command, so pass it to the dispatch |
| 444 | bool isOk = p.dispatch(argv[1]); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 445 | if (!isOk) { |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 446 | usage(p.m_programName); |
| 447 | return 1; |
| 448 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 449 | face.processEvents(); |
| 450 | } |
| 451 | catch (const std::exception& e) { |
| 452 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 453 | return 2; |
| 454 | } |
| 455 | return 0; |
| 456 | } |