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