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