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