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