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