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