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" |
| 37 | " add-nexthop <name> <faceId> [<cost>]\n" |
| 38 | " Add a nexthop to a FIB entry\n" |
| 39 | " remove-nexthop <name> <faceId> \n" |
| 40 | " Remove a nexthop from a FIB entry\n" |
| 41 | " create <uri> \n" |
| 42 | " Create a face in one of the following formats:\n" |
| 43 | " UDP unicast: udp[4|6]://<remote-IP-or-host>[:<remote-port>]\n" |
| 44 | " TCP: tcp[4|6]://<remote-IP-or-host>[:<remote-port>] \n" |
| 45 | " destroy <faceId> \n" |
| 46 | " Destroy a face\n" |
| 47 | " set-strategy <name> <strategy> \n" |
| 48 | " Set the strategy for a namespace \n" |
| 49 | " unset-strategy <name> \n" |
| 50 | " Unset the strategy for a namespace \n" |
| 51 | << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | namespace nfdc { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 55 | |
| 56 | Nfdc::Nfdc(ndn::Face& face) |
| 57 | : m_controller(face) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 58 | { |
| 59 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 60 | |
| 61 | Nfdc::~Nfdc() |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 62 | { |
| 63 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 64 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 65 | bool |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 66 | Nfdc::dispatch(const std::string& command, const char* commandOptions[], int nOptions) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 67 | { |
hilata | dc947ec | 2014-03-10 12:48:31 -0500 | [diff] [blame] | 68 | if (command == "add-nexthop") { |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 69 | if (nOptions == 2) |
| 70 | fibAddNextHop(commandOptions, false); |
| 71 | else if (nOptions == 3) |
| 72 | fibAddNextHop(commandOptions, true); |
| 73 | else |
| 74 | return false; |
| 75 | } |
| 76 | else if (command == "remove-nexthop") { |
| 77 | if (nOptions != 2) |
| 78 | return false; |
| 79 | fibRemoveNextHop(commandOptions); |
| 80 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 81 | else if (command == "create") { |
| 82 | if (nOptions != 1) |
| 83 | return false; |
| 84 | faceCreate(commandOptions); |
| 85 | } |
| 86 | else if (command == "destroy") { |
| 87 | if (nOptions != 1) |
| 88 | return false; |
| 89 | faceDestroy(commandOptions); |
| 90 | } |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 91 | else if (command == "set-strategy") { |
| 92 | if (nOptions != 2) |
| 93 | return false; |
| 94 | strategyChoiceSet(commandOptions); |
| 95 | } |
| 96 | else if (command == "unset-strategy") { |
| 97 | if (nOptions != 1) |
| 98 | return false; |
| 99 | strategyChoiceUnset(commandOptions); |
| 100 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 101 | else |
| 102 | usage(m_programName); |
| 103 | |
| 104 | return true; |
| 105 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 106 | |
| 107 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 108 | Nfdc::fibAddNextHop(const char* commandOptions[], bool hasCost) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 109 | { |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 110 | const std::string& name = commandOptions[0]; |
| 111 | const int faceId = boost::lexical_cast<int>(commandOptions[1]); |
| 112 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 113 | ControlParameters parameters; |
| 114 | parameters |
| 115 | .setName(name) |
| 116 | .setFaceId(faceId); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 117 | |
| 118 | if (hasCost) |
| 119 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 120 | const uint64_t cost = boost::lexical_cast<uint64_t>(commandOptions[2]); |
| 121 | parameters.setCost(cost); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 122 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 123 | |
| 124 | m_controller.start<FibAddNextHopCommand>( |
| 125 | parameters, |
| 126 | bind(&Nfdc::onSuccess, this, _1, "Nexthop insertion succeeded"), |
| 127 | bind(&Nfdc::onError, this, _1, _2, "Nexthop insertion failed")); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 131 | Nfdc::fibRemoveNextHop(const char* commandOptions[]) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 132 | { |
| 133 | const std::string& name = commandOptions[0]; |
| 134 | const int faceId = boost::lexical_cast<int>(commandOptions[1]); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 135 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 136 | ControlParameters parameters; |
| 137 | parameters |
| 138 | .setName(name) |
| 139 | .setFaceId(faceId); |
| 140 | |
| 141 | m_controller.start<FibRemoveNextHopCommand>( |
| 142 | parameters, |
| 143 | bind(&Nfdc::onSuccess, this, _1, "Nexthop removal succeeded"), |
| 144 | bind(&Nfdc::onError, this, _1, _2, "Nexthop removal failed")); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 145 | } |
hilata | 9b27e69 | 2014-02-25 15:43:19 -0600 | [diff] [blame] | 146 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 147 | namespace { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 148 | |
| 149 | inline bool |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 150 | isValidUri(const std::string& input) |
| 151 | { |
| 152 | // an extended regex to support the validation of uri structure |
| 153 | // boost::regex e("^[a-z0-9]+-?+[a-z0-9]+\\:\\/\\/.*"); |
| 154 | boost::regex e("^[a-z0-9]+\\:.*"); |
| 155 | return boost::regex_match(input, e); |
| 156 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 157 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 158 | } // anonymous namespace |
| 159 | |
| 160 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 161 | Nfdc::faceCreate(const char* commandOptions[]) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 162 | { |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 163 | const std::string& uri = commandOptions[0]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 164 | if (!isValidUri(uri)) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 165 | throw Error("invalid uri format"); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 166 | |
| 167 | ControlParameters parameters; |
| 168 | parameters |
| 169 | .setUri(uri); |
| 170 | |
| 171 | m_controller.start<FaceCreateCommand>( |
| 172 | parameters, |
| 173 | bind(&Nfdc::onSuccess, this, _1, "Face creation succeeded"), |
| 174 | bind(&Nfdc::onError, this, _1, _2, "Face creation failed")); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 175 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 176 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 177 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 178 | Nfdc::faceDestroy(const char* commandOptions[]) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 179 | { |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 180 | const int faceId = boost::lexical_cast<int>(commandOptions[0]); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 181 | |
| 182 | ControlParameters parameters; |
| 183 | parameters |
| 184 | .setFaceId(faceId); |
| 185 | |
| 186 | m_controller.start<FaceDestroyCommand>( |
| 187 | parameters, |
| 188 | bind(&Nfdc::onSuccess, this, _1, "Face destroy succeeded"), |
| 189 | bind(&Nfdc::onError, this, _1, _2, "Face destroy failed")); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 190 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 191 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 192 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 193 | Nfdc::strategyChoiceSet(const char* commandOptions[]) |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 194 | { |
| 195 | const std::string& name = commandOptions[0]; |
| 196 | const std::string& strategy = commandOptions[1]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 197 | |
| 198 | ControlParameters parameters; |
| 199 | parameters |
| 200 | .setName(name) |
| 201 | .setStrategy(strategy); |
| 202 | |
| 203 | m_controller.start<StrategyChoiceSetCommand>( |
| 204 | parameters, |
| 205 | bind(&Nfdc::onSuccess, this, _1, "Successfully set strategy choice"), |
| 206 | bind(&Nfdc::onError, this, _1, _2, "Failed to set strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 207 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 208 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 209 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 210 | Nfdc::strategyChoiceUnset(const char* commandOptions[]) |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 211 | { |
| 212 | const std::string& name = commandOptions[0]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 213 | |
| 214 | ControlParameters parameters; |
| 215 | parameters |
| 216 | .setName(name); |
| 217 | |
| 218 | m_controller.start<StrategyChoiceUnsetCommand>( |
| 219 | parameters, |
| 220 | bind(&Nfdc::onSuccess, this, _1, "Successfully unset strategy choice"), |
| 221 | bind(&Nfdc::onError, this, _1, _2, "Failed to unset strategy choice")); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 222 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 223 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 224 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 225 | Nfdc::onSuccess(const ControlParameters& parameters, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 226 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 227 | std::cout << message << ": " << parameters << std::endl; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 228 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 229 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 230 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 231 | Nfdc::onError(uint32_t code, const std::string& error, const std::string& message) |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 232 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 233 | std::ostringstream os; |
| 234 | os << message << ": " << error << " (code: " << code << ")"; |
| 235 | throw Error(os.str()); |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 236 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 237 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 238 | } // namespace nfdc |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 239 | |
| 240 | int |
| 241 | main(int argc, char** argv) |
| 242 | { |
| 243 | ndn::Face face; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 244 | nfdc::Nfdc p(face); |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 245 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 246 | p.m_programName = argv[0]; |
| 247 | int opt; |
| 248 | while ((opt = getopt(argc, argv, "h")) != -1) { |
| 249 | switch (opt) { |
| 250 | case 'h': |
| 251 | usage(p.m_programName); |
| 252 | return 0; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 253 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 254 | default: |
| 255 | usage(p.m_programName); |
| 256 | return 1; |
| 257 | } |
| 258 | } |
Alexander Afanasyev | 9f935b8 | 2014-02-24 15:14:22 -0800 | [diff] [blame] | 259 | |
| 260 | if (argc == optind) { |
| 261 | usage(p.m_programName); |
| 262 | return 1; |
| 263 | } |
| 264 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 265 | try { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 266 | bool isOk = p.dispatch(argv[optind], |
| 267 | const_cast<const char**>(argv + optind + 1), |
| 268 | argc - optind - 1); |
| 269 | if (!isOk) { |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 270 | usage(p.m_programName); |
| 271 | return 1; |
| 272 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 273 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 274 | face.processEvents(); |
| 275 | } |
| 276 | catch (const std::exception& e) { |
| 277 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 278 | return 2; |
| 279 | } |
| 280 | return 0; |
| 281 | } |