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 | */ |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 25 | |
| 26 | #ifndef NFD_TOOLS_NFDC_HPP |
| 27 | #define NFD_TOOLS_NFDC_HPP |
| 28 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/face.hpp> |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/security/key-chain.hpp> |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 31 | #include <ndn-cxx/util/time.hpp> |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 32 | #include <ndn-cxx/management/nfd-controller.hpp> |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 33 | #include <ndn-cxx/util/face-uri.hpp> |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 34 | #include <memory> |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 35 | |
| 36 | namespace nfdc { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 37 | |
| 38 | using namespace ndn::nfd; |
| 39 | |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 40 | class Nfdc : boost::noncopyable |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 41 | { |
| 42 | public: |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 43 | |
| 44 | static const ndn::time::milliseconds DEFAULT_EXPIRATION_PERIOD; |
| 45 | static const uint64_t DEFAULT_COST; |
| 46 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 47 | class Error : public std::runtime_error |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 48 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 49 | public: |
| 50 | explicit |
| 51 | Error(const std::string& what) |
| 52 | : std::runtime_error(what) |
| 53 | { |
| 54 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 55 | }; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 56 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 57 | class FaceIdFetcher |
| 58 | { |
| 59 | public: |
| 60 | typedef std::function<void(uint32_t)> SuccessCallback; |
| 61 | typedef std::function<void(const std::string&)> FailureCallback; |
| 62 | |
| 63 | /** \brief obtain FaceId from input |
| 64 | * \param face Reference to the Face that should be used to fetch data |
| 65 | * \param controller Reference to the controller that should be used to sign the Interest |
| 66 | * \param input User input, either FaceId or FaceUri |
| 67 | * \param allowCreate Whether creating face is allowed |
| 68 | * \param onSucceed Callback to be fired when faceId is obtained |
| 69 | * \param onFail Callback to be fired when an error occurs |
| 70 | */ |
| 71 | static void |
| 72 | start(ndn::Face& face, |
| 73 | Controller& controller, |
| 74 | const std::string& input, |
| 75 | bool allowCreate, |
| 76 | const SuccessCallback& onSucceed, |
| 77 | const FailureCallback& onFail); |
| 78 | |
| 79 | private: |
| 80 | FaceIdFetcher(ndn::Face& face, |
| 81 | Controller& controller, |
| 82 | bool allowCreate, |
| 83 | const SuccessCallback& onSucceed, |
| 84 | const FailureCallback& onFail); |
| 85 | |
| 86 | void |
| 87 | onQuerySuccess(const ndn::ConstBufferPtr& data, |
| 88 | const ndn::util::FaceUri& canonicalUri); |
| 89 | |
| 90 | void |
| 91 | onQueryFailure(uint32_t errorCode, |
| 92 | const ndn::util::FaceUri& canonicalUri); |
| 93 | |
| 94 | void |
| 95 | onCanonizeSuccess(const ndn::util::FaceUri& canonicalUri); |
| 96 | |
| 97 | void |
| 98 | onCanonizeFailure(const std::string& reason); |
| 99 | |
| 100 | void |
| 101 | startGetFaceId(const ndn::util::FaceUri& faceUri); |
| 102 | |
| 103 | void |
| 104 | startFaceCreate(const ndn::util::FaceUri& canonicalUri); |
| 105 | |
| 106 | void |
| 107 | onFaceCreateError(uint32_t code, |
| 108 | const std::string& error, |
| 109 | const std::string& message); |
| 110 | |
| 111 | void |
| 112 | succeed(uint32_t faceId); |
| 113 | |
| 114 | void |
| 115 | fail(const std::string& reason); |
| 116 | |
| 117 | private: |
| 118 | ndn::Face& m_face; |
| 119 | Controller& m_controller; |
| 120 | bool m_allowCreate; |
| 121 | SuccessCallback m_onSucceed; |
| 122 | FailureCallback m_onFail; |
| 123 | }; |
| 124 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 125 | explicit |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 126 | Nfdc(ndn::Face& face); |
| 127 | |
| 128 | ~Nfdc(); |
| 129 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 130 | bool |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 131 | dispatch(const std::string& cmd); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 132 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 133 | /** |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 134 | * \brief Adds a nexthop to a FIB entry |
hilata | dc947ec | 2014-03-10 12:48:31 -0500 | [diff] [blame] | 135 | * |
| 136 | * If the FIB entry does not exist, it is inserted automatically |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 137 | * |
| 138 | * cmd format: |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 139 | * [-c cost] name faceId|faceUri |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 140 | * |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 141 | */ |
| 142 | void |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 143 | fibAddNextHop(); |
| 144 | |
| 145 | /** |
hilata | dc947ec | 2014-03-10 12:48:31 -0500 | [diff] [blame] | 146 | * \brief Removes a nexthop from an existing FIB entry |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 147 | * |
hilata | dc947ec | 2014-03-10 12:48:31 -0500 | [diff] [blame] | 148 | * If the last nexthop record in a FIB entry is removed, the FIB entry is also deleted |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 149 | * |
| 150 | * cmd format: |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 151 | * name faceId |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 152 | * |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 153 | */ |
| 154 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 155 | fibRemoveNextHop(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 156 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 157 | /** |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 158 | * \brief Registers name to the given faceId or faceUri |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 159 | * |
| 160 | * cmd format: |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 161 | * [-I] [-C] [-c cost] name faceId|faceUri |
| 162 | */ |
| 163 | void |
| 164 | ribRegisterPrefix(); |
| 165 | |
| 166 | /** |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 167 | * \brief Unregisters name from the given faceId/faceUri |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 168 | * |
| 169 | * cmd format: |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 170 | * name faceId/faceUri |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 171 | */ |
| 172 | void |
| 173 | ribUnregisterPrefix(); |
| 174 | |
| 175 | /** |
| 176 | * \brief Creates new face |
| 177 | * |
| 178 | * This command allows creation of UDP unicast and TCP faces only |
| 179 | * |
| 180 | * cmd format: |
| 181 | * uri |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 182 | * |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 183 | */ |
| 184 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 185 | faceCreate(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 186 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 187 | /** |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 188 | * \brief Destroys face |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 189 | * |
| 190 | * cmd format: |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 191 | * faceId|faceUri |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 192 | * |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 193 | */ |
| 194 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 195 | faceDestroy(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 196 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 197 | /** |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 198 | * \brief Sets the strategy for a namespace |
| 199 | * |
| 200 | * cmd format: |
| 201 | * name strategy |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 202 | * |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 203 | */ |
| 204 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 205 | strategyChoiceSet(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 206 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 207 | /** |
| 208 | * \brief Unset the strategy for a namespace |
| 209 | * |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 210 | * cmd format: |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 211 | * name strategy |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 212 | * |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 213 | */ |
| 214 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 215 | strategyChoiceUnset(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 216 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 217 | private: |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 218 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 219 | void |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 220 | onSuccess(const ControlParameters& commandSuccessResult, |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 221 | const std::string& message); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 222 | |
| 223 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 224 | onError(uint32_t code, const std::string& error, const std::string& message); |
| 225 | |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 226 | void |
| 227 | onCanonizeFailure(const std::string& reason); |
| 228 | |
| 229 | void |
| 230 | startFaceCreate(const ndn::util::FaceUri& canonicalUri); |
| 231 | |
| 232 | void |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 233 | onObtainFaceIdFailure(const std::string& message); |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 234 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 235 | public: |
| 236 | const char* m_programName; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 237 | |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 238 | // command parameters without leading 'cmd' component |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 239 | const char* const* m_commandLineArguments; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 240 | int m_nOptions; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 241 | uint64_t m_flags; |
| 242 | uint64_t m_cost; |
| 243 | uint64_t m_faceId; |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 244 | uint64_t m_origin; |
| 245 | ndn::time::milliseconds m_expires; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 246 | std::string m_name; |
Joao Pereira | c6184cd | 2015-09-03 15:27:29 -0400 | [diff] [blame] | 247 | FacePersistency m_facePersistency; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 248 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 249 | private: |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 250 | ndn::KeyChain m_keyChain; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 251 | ndn::Face& m_face; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 252 | Controller m_controller; |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 253 | boost::asio::io_service& m_ioService; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 254 | }; |
| 255 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 256 | } // namespace nfdc |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 257 | |
| 258 | #endif // NFD_TOOLS_NFDC_HPP |