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