hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 24 | #include "nfdc.hpp" |
| 25 | #include <boost/lexical_cast.hpp> |
| 26 | #include <boost/algorithm/string.hpp> |
| 27 | #include <boost/algorithm/string/regex_find_format.hpp> |
| 28 | #include <boost/regex.hpp> |
| 29 | |
| 30 | void |
| 31 | usage(const char* programName) |
| 32 | { |
| 33 | std::cout << "Usage:\n" << programName << " [-h] COMMAND\n" |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 34 | " -h print usage and exit\n" |
| 35 | "\n" |
| 36 | " COMMAND can be one of following:\n" |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 37 | " add <name> <faceUri> [<cost>]\n" |
| 38 | " Create a face, and add a nexthop for this face to a FIB entry\n" |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 39 | " add-nexthop <name> <faceId> [<cost>]\n" |
| 40 | " Add a nexthop to a FIB entry\n" |
| 41 | " remove-nexthop <name> <faceId> \n" |
| 42 | " Remove a nexthop from a FIB entry\n" |
| 43 | " create <uri> \n" |
| 44 | " Create a face in one of the following formats:\n" |
| 45 | " UDP unicast: udp[4|6]://<remote-IP-or-host>[:<remote-port>]\n" |
| 46 | " TCP: tcp[4|6]://<remote-IP-or-host>[:<remote-port>] \n" |
| 47 | " destroy <faceId> \n" |
| 48 | " Destroy a face\n" |
| 49 | " set-strategy <name> <strategy> \n" |
| 50 | " Set the strategy for a namespace \n" |
| 51 | " unset-strategy <name> \n" |
| 52 | " Unset the strategy for a namespace \n" |
| 53 | << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | namespace nfdc { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 57 | |
| 58 | Nfdc::Nfdc(ndn::Face& face) |
| 59 | : m_controller(face) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 60 | { |
| 61 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 62 | |
| 63 | Nfdc::~Nfdc() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 64 | { |
| 65 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 66 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 67 | bool |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 68 | Nfdc::dispatch(const std::string& command) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 69 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 70 | if (command == "add") { |
| 71 | if (m_nOptions == 2) |
| 72 | addName(); |
| 73 | else if (m_nOptions == 3) |
| 74 | addName(); |
| 75 | else |
| 76 | return false; |
| 77 | } |
| 78 | else if (command == "add-nexthop") { |
| 79 | if (m_nOptions == 2) |
| 80 | fibAddNextHop(false); |
| 81 | else if (m_nOptions == 3) |
| 82 | fibAddNextHop(true); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 83 | else |
| 84 | return false; |
| 85 | } |
| 86 | else if (command == "remove-nexthop") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 87 | if (m_nOptions != 2) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 88 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 89 | fibRemoveNextHop(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 90 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 91 | else if (command == "create") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 92 | if (m_nOptions != 1) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 93 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 94 | faceCreate(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 95 | } |
| 96 | else if (command == "destroy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 97 | if (m_nOptions != 1) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 98 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 99 | faceDestroy(); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 100 | } |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 101 | else if (command == "set-strategy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 102 | if (m_nOptions != 2) |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 103 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 104 | strategyChoiceSet(); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 105 | } |
| 106 | else if (command == "unset-strategy") { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 107 | if (m_nOptions != 1) |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 108 | return false; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 109 | strategyChoiceUnset(); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 110 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 111 | else |
| 112 | usage(m_programName); |
| 113 | |
| 114 | return true; |
| 115 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 116 | |
hilata | 9b27e69 | 2014-02-25 15:43:19 -0600 | [diff] [blame] | 117 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 118 | namespace { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 119 | |
| 120 | inline bool |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 121 | isValidUri(const std::string& input) |
| 122 | { |
| 123 | // an extended regex to support the validation of uri structure |
| 124 | // boost::regex e("^[a-z0-9]+-?+[a-z0-9]+\\:\\/\\/.*"); |
| 125 | boost::regex e("^[a-z0-9]+\\:.*"); |
| 126 | return boost::regex_match(input, e); |
| 127 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 128 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 129 | } // anonymous namespace |
| 130 | |
| 131 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 132 | Nfdc::addName() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 133 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 134 | if (!isValidUri(m_commandLineArguments[1])) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 135 | throw Error("invalid uri format"); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 136 | |
| 137 | ControlParameters parameters; |
| 138 | parameters |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 139 | .setUri(m_commandLineArguments[1]); |
| 140 | |
| 141 | m_controller.start<FaceCreateCommand>( |
| 142 | parameters, |
| 143 | bind(&Nfdc::onAddSuccess, this, _1, "nfdc add:Face creation succeeded"), |
| 144 | bind(&Nfdc::onError, this, _1, _2, "nfdc add: Face creation failed")); |
| 145 | |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | Nfdc::fibAddNextHop(bool hasCost) |
| 150 | { |
| 151 | const std::string& name = m_commandLineArguments[0]; |
| 152 | const int faceId = boost::lexical_cast<int>(m_commandLineArguments[1]); |
| 153 | |
| 154 | fibAddNextHop(name, faceId, hasCost); |
| 155 | |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | Nfdc::fibAddNextHop(const std::string& name, const uint64_t faceId, bool hasCost) |
| 160 | { |
| 161 | ControlParameters parameters; |
| 162 | parameters |
| 163 | .setName(name) |
| 164 | .setFaceId(faceId); |
| 165 | |
| 166 | if (hasCost) |
| 167 | { |
| 168 | const uint64_t cost = boost::lexical_cast<uint64_t>(m_commandLineArguments[2]); |
| 169 | parameters.setCost(cost); |
| 170 | } |
| 171 | |
| 172 | m_controller.start<FibAddNextHopCommand>( |
| 173 | parameters, |
| 174 | bind(&Nfdc::onSuccess, this, _1, "Nexthop insertion succeeded"), |
| 175 | bind(&Nfdc::onError, this, _1, _2, "Nexthop insertion failed")); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | Nfdc::fibRemoveNextHop() |
| 180 | { |
| 181 | const std::string& name = m_commandLineArguments[0]; |
| 182 | const int faceId = boost::lexical_cast<int>(m_commandLineArguments[1]); |
| 183 | |
| 184 | ControlParameters parameters; |
| 185 | parameters |
| 186 | .setName(name) |
| 187 | .setFaceId(faceId); |
| 188 | |
| 189 | m_controller.start<FibRemoveNextHopCommand>( |
| 190 | parameters, |
| 191 | bind(&Nfdc::onSuccess, this, _1, "Nexthop removal succeeded"), |
| 192 | bind(&Nfdc::onError, this, _1, _2, "Nexthop removal failed")); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | Nfdc::faceCreate() |
| 197 | { |
| 198 | if (!isValidUri(m_commandLineArguments[0])) |
| 199 | throw Error("invalid uri format"); |
| 200 | |
| 201 | ControlParameters parameters; |
| 202 | parameters |
| 203 | .setUri(m_commandLineArguments[0]); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 204 | |
| 205 | m_controller.start<FaceCreateCommand>( |
| 206 | parameters, |
| 207 | bind(&Nfdc::onSuccess, this, _1, "Face creation succeeded"), |
| 208 | bind(&Nfdc::onError, this, _1, _2, "Face creation failed")); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 209 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 210 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 211 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 212 | Nfdc::faceDestroy() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 213 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 214 | const int faceId = boost::lexical_cast<int>(m_commandLineArguments[0]); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 215 | |
| 216 | ControlParameters parameters; |
| 217 | parameters |
| 218 | .setFaceId(faceId); |
| 219 | |
| 220 | m_controller.start<FaceDestroyCommand>( |
| 221 | parameters, |
| 222 | bind(&Nfdc::onSuccess, this, _1, "Face destroy succeeded"), |
| 223 | bind(&Nfdc::onError, this, _1, _2, "Face destroy failed")); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 224 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 225 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 226 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 227 | Nfdc::strategyChoiceSet() |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 228 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 229 | const std::string& name = m_commandLineArguments[0]; |
| 230 | const std::string& strategy = m_commandLineArguments[1]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 231 | |
| 232 | ControlParameters parameters; |
| 233 | parameters |
| 234 | .setName(name) |
| 235 | .setStrategy(strategy); |
| 236 | |
| 237 | m_controller.start<StrategyChoiceSetCommand>( |
| 238 | parameters, |
| 239 | bind(&Nfdc::onSuccess, this, _1, "Successfully set strategy choice"), |
| 240 | bind(&Nfdc::onError, this, _1, _2, "Failed to set strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 241 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 242 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 243 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 244 | Nfdc::strategyChoiceUnset() |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 245 | { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 246 | const std::string& name = m_commandLineArguments[0]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 247 | |
| 248 | ControlParameters parameters; |
| 249 | parameters |
| 250 | .setName(name); |
| 251 | |
| 252 | m_controller.start<StrategyChoiceUnsetCommand>( |
| 253 | parameters, |
| 254 | bind(&Nfdc::onSuccess, this, _1, "Successfully unset strategy choice"), |
| 255 | bind(&Nfdc::onError, this, _1, _2, "Failed to unset strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 256 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 257 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 258 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 259 | Nfdc::onSuccess(const ControlParameters& parameters, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 260 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 261 | std::cout << message << ": " << parameters << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 262 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 263 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 264 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 265 | Nfdc::onAddSuccess(const ControlParameters& parameters, const std::string& message) |
| 266 | { |
| 267 | uint64_t faceId = parameters.getFaceId(); |
| 268 | |
| 269 | if (m_nOptions == 2) |
| 270 | fibAddNextHop(m_commandLineArguments[0], faceId, false); |
| 271 | else if (m_nOptions == 3) |
| 272 | fibAddNextHop(m_commandLineArguments[0], faceId, true); |
| 273 | else |
| 274 | throw Error("invalid number of arguments"); |
| 275 | } |
| 276 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 277 | Nfdc::onError(uint32_t code, const std::string& error, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 278 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 279 | std::ostringstream os; |
| 280 | os << message << ": " << error << " (code: " << code << ")"; |
| 281 | throw Error(os.str()); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 282 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 283 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 284 | } // namespace nfdc |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 285 | |
| 286 | int |
| 287 | main(int argc, char** argv) |
| 288 | { |
| 289 | ndn::Face face; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 290 | nfdc::Nfdc p(face); |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 291 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 292 | p.m_programName = argv[0]; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 293 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 294 | int opt; |
| 295 | while ((opt = getopt(argc, argv, "h")) != -1) { |
| 296 | switch (opt) { |
| 297 | case 'h': |
| 298 | usage(p.m_programName); |
| 299 | return 0; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 300 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 301 | default: |
| 302 | usage(p.m_programName); |
| 303 | return 1; |
| 304 | } |
| 305 | } |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 306 | |
| 307 | if (argc == optind) { |
| 308 | usage(p.m_programName); |
| 309 | return 1; |
| 310 | } |
| 311 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 312 | try { |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 313 | p.m_commandLineArguments = const_cast<const char**>(argv + optind + 1); |
| 314 | p.m_nOptions = argc - optind - 1; |
| 315 | bool isOk = p.dispatch(argv[optind]); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 316 | if (!isOk) { |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 317 | usage(p.m_programName); |
| 318 | return 1; |
| 319 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 320 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 321 | face.processEvents(); |
| 322 | } |
| 323 | catch (const std::exception& e) { |
| 324 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 325 | return 2; |
| 326 | } |
| 327 | return 0; |
| 328 | } |