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