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) |
Alexander Afanasyev | f67cf08 | 2014-07-18 16:47:29 -0700 | [diff] [blame^] | 231 | .setOrigin(m_origin); |
| 232 | |
| 233 | if (m_expires != DEFAULT_EXPIRATION_PERIOD) |
| 234 | parameters.setExpirationPeriod(m_expires); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 235 | |
| 236 | if (!isValidUri(m_commandLineArguments[1])) { |
| 237 | try { //So the uri is not valid, may be a faceId is provided. |
| 238 | m_faceId = boost::lexical_cast<int>(m_commandLineArguments[1]); |
| 239 | } |
| 240 | catch (const std::exception& e) { |
| 241 | std::cerr << "No valid faceUri or faceId is provided"<< std::endl; |
| 242 | return; |
| 243 | } |
| 244 | parameters.setFaceId(m_faceId); |
| 245 | ribRegisterPrefix(parameters); |
| 246 | } |
| 247 | else { |
| 248 | ControlParameters faceParameters; |
| 249 | faceParameters.setUri(m_commandLineArguments[1]); |
| 250 | |
| 251 | m_controller.start<FaceCreateCommand>(faceParameters, |
| 252 | bind(&Nfdc::ribRegisterPrefix, this, _1), |
| 253 | bind(&Nfdc::onError, this, _1, _2, |
| 254 | "Face creation failed")); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | Nfdc::ribRegisterPrefix(const ControlParameters& faceCreateResult) |
| 260 | { |
| 261 | ControlParameters ribParameters; |
| 262 | ribParameters |
| 263 | .setName(m_name) |
| 264 | .setCost(m_cost) |
| 265 | .setFlags(m_flags) |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 266 | .setOrigin(m_origin) |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 267 | .setFaceId(faceCreateResult.getFaceId()); |
| 268 | |
Alexander Afanasyev | f67cf08 | 2014-07-18 16:47:29 -0700 | [diff] [blame^] | 269 | if (m_expires != DEFAULT_EXPIRATION_PERIOD) |
| 270 | ribParameters.setExpirationPeriod(m_expires); |
| 271 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 272 | m_controller.start<RibRegisterCommand>(ribParameters, |
| 273 | bind(&Nfdc::onSuccess, this, _1, |
| 274 | "Successful in name registration"), |
| 275 | bind(&Nfdc::onError, this, _1, _2, |
| 276 | "Failed in name registration")); |
| 277 | } |
| 278 | |
| 279 | void |
| 280 | Nfdc::ribUnregisterPrefix() |
| 281 | { |
| 282 | m_name = m_commandLineArguments[0]; |
| 283 | try { |
| 284 | m_faceId = boost::lexical_cast<int>(m_commandLineArguments[1]); |
| 285 | } |
| 286 | catch (const std::exception& e) { |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 287 | std::cerr << "No valid faceId is provided" << std::endl; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
| 291 | ControlParameters parameters; |
| 292 | parameters |
| 293 | .setName(m_name) |
Syed Obaid | f93f9d9 | 2014-06-22 13:50:48 -0500 | [diff] [blame] | 294 | .setFaceId(m_faceId) |
| 295 | .setOrigin(m_origin); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 296 | |
| 297 | m_controller.start<RibUnregisterCommand>(parameters, |
| 298 | bind(&Nfdc::onSuccess, this, _1, |
| 299 | "Successful in unregistering name"), |
| 300 | bind(&Nfdc::onError, this, _1, _2, |
| 301 | "Failed in unregistering name")); |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void |
| 305 | Nfdc::faceCreate() |
| 306 | { |
| 307 | if (!isValidUri(m_commandLineArguments[0])) |
| 308 | throw Error("invalid uri format"); |
| 309 | |
| 310 | ControlParameters parameters; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 311 | parameters.setUri(m_commandLineArguments[0]); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 312 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 313 | m_controller.start<FaceCreateCommand>(parameters, |
| 314 | bind(&Nfdc::onSuccess, this, _1, |
| 315 | "Face creation succeeded"), |
| 316 | bind(&Nfdc::onError, this, _1, _2, |
| 317 | "Face creation failed")); |
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 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 320 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 321 | Nfdc::faceDestroy() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 322 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 323 | ControlParameters parameters; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 324 | if (!isValidUri(m_commandLineArguments[0])) { |
| 325 | try { //So the uri is not valid, may be a faceId is provided. |
| 326 | m_faceId = boost::lexical_cast<int>(m_commandLineArguments[0]); |
| 327 | } |
| 328 | catch (const std::exception& e) { |
| 329 | std::cerr << "No valid faceUri or faceId is provided" << std::endl; |
| 330 | return; |
| 331 | } |
| 332 | parameters.setFaceId(m_faceId); |
| 333 | faceDestroy(parameters); |
| 334 | } |
| 335 | else{ |
| 336 | ControlParameters faceParameters; |
| 337 | faceParameters.setUri(m_commandLineArguments[0]); |
| 338 | m_controller.start<FaceCreateCommand>(faceParameters, |
| 339 | bind(&Nfdc::faceDestroy, this, _1), |
| 340 | bind(&Nfdc::onError, this, _1, _2, |
| 341 | "Face destroy failed")); |
| 342 | } |
| 343 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 344 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 345 | void |
| 346 | Nfdc::faceDestroy(const ControlParameters& faceCreateResult) |
| 347 | { |
| 348 | ControlParameters faceParameters; |
| 349 | faceParameters.setFaceId(faceCreateResult.getFaceId()); |
| 350 | |
| 351 | m_controller.start<FaceDestroyCommand>(faceParameters, |
| 352 | bind(&Nfdc::onSuccess, this, _1, |
| 353 | "Face destroy succeeded"), |
| 354 | bind(&Nfdc::onError, this, _1, _2, |
| 355 | "Face destroy failed")); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [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::strategyChoiceSet() |
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]; |
| 362 | const std::string& strategy = m_commandLineArguments[1]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 363 | |
| 364 | ControlParameters parameters; |
| 365 | parameters |
| 366 | .setName(name) |
| 367 | .setStrategy(strategy); |
| 368 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 369 | m_controller.start<StrategyChoiceSetCommand>(parameters, |
| 370 | bind(&Nfdc::onSuccess, this, _1, |
| 371 | "Successfully set strategy choice"), |
| 372 | bind(&Nfdc::onError, this, _1, _2, |
| 373 | "Failed to set strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 374 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 375 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 376 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 377 | Nfdc::strategyChoiceUnset() |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 378 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 379 | const std::string& name = m_commandLineArguments[0]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 380 | |
| 381 | ControlParameters parameters; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 382 | parameters.setName(name); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 383 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 384 | m_controller.start<StrategyChoiceUnsetCommand>(parameters, |
| 385 | bind(&Nfdc::onSuccess, this, _1, |
| 386 | "Successfully unset strategy choice"), |
| 387 | bind(&Nfdc::onError, this, _1, _2, |
| 388 | "Failed to unset strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 389 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 390 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 391 | void |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 392 | Nfdc::onSuccess(const ControlParameters& commandSuccessResult, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 393 | { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 394 | std::cout << message << ": " << commandSuccessResult << std::endl; |
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 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 397 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 398 | Nfdc::onError(uint32_t code, const std::string& error, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 399 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 400 | std::ostringstream os; |
| 401 | os << message << ": " << error << " (code: " << code << ")"; |
| 402 | throw Error(os.str()); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 403 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 404 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 405 | |
| 406 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 407 | } // namespace nfdc |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 408 | |
| 409 | int |
| 410 | main(int argc, char** argv) |
| 411 | { |
| 412 | ndn::Face face; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 413 | nfdc::Nfdc p(face); |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 414 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 415 | p.m_programName = argv[0]; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 416 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 417 | if (argc < 2) { |
| 418 | usage(p.m_programName); |
| 419 | return 0; |
| 420 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 421 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 422 | if (!strcmp(argv[1], "-h")) { |
| 423 | usage(p.m_programName); |
| 424 | return 0; |
| 425 | } |
| 426 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 427 | if (!strcmp(argv[1], "-V")) { |
| 428 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 429 | return 0; |
| 430 | } |
| 431 | |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 432 | ::optind = 2; //start reading options from 2nd argument i.e. Command |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 433 | int opt; |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 434 | while ((opt = ::getopt(argc, argv, "ICc:e:o:")) != -1) { |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 435 | switch (opt) { |
| 436 | case 'I': |
| 437 | p.m_flags = p.m_flags & ~(nfdc::ROUTE_FLAG_CHILD_INHERIT); |
| 438 | break; |
| 439 | |
| 440 | case 'C': |
| 441 | p.m_flags = p.m_flags | nfdc::ROUTE_FLAG_CAPTURE; |
| 442 | break; |
| 443 | |
| 444 | case 'c': |
| 445 | try { |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 446 | p.m_cost = boost::lexical_cast<uint64_t>(::optarg); |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 447 | } |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 448 | catch (boost::bad_lexical_cast&) { |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 449 | std::cerr << "Error: cost must be in unsigned integer format" << std::endl; |
| 450 | return 1; |
| 451 | } |
| 452 | break; |
| 453 | |
| 454 | case 'e': |
| 455 | uint64_t expires; |
| 456 | try { |
| 457 | expires = boost::lexical_cast<uint64_t>(::optarg); |
| 458 | } |
| 459 | catch (boost::bad_lexical_cast&) { |
| 460 | std::cerr << "Error: expiration time must be in unsigned integer format" << std::endl; |
| 461 | return 1; |
| 462 | } |
| 463 | p.m_expires = ndn::time::milliseconds(expires); |
| 464 | break; |
| 465 | |
| 466 | case 'o': |
| 467 | try { |
| 468 | p.m_origin = boost::lexical_cast<uint64_t>(::optarg); |
| 469 | } |
| 470 | catch (boost::bad_lexical_cast&) { |
| 471 | std::cerr << "Error: origin must be in unsigned integer format" << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 472 | return 1; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 473 | } |
| 474 | break; |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 475 | |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 476 | default: |
| 477 | usage(p.m_programName); |
| 478 | return 1; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 479 | } |
| 480 | } |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 481 | |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 482 | if (argc == ::optind) { |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 483 | usage(p.m_programName); |
| 484 | return 1; |
| 485 | } |
| 486 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 487 | try { |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 488 | p.m_commandLineArguments = argv + ::optind; |
| 489 | p.m_nOptions = argc - ::optind; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 490 | |
| 491 | //argv[1] points to the command, so pass it to the dispatch |
| 492 | bool isOk = p.dispatch(argv[1]); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 493 | if (!isOk) { |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 494 | usage(p.m_programName); |
| 495 | return 1; |
| 496 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 497 | face.processEvents(); |
| 498 | } |
| 499 | catch (const std::exception& e) { |
| 500 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 501 | return 2; |
| 502 | } |
| 503 | return 0; |
| 504 | } |