Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 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 |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_RIB_REMOTE_REGISTRATOR_HPP |
| 27 | #define NFD_RIB_REMOTE_REGISTRATOR_HPP |
| 28 | |
| 29 | #include "rib.hpp" |
| 30 | #include "core/config-file.hpp" |
| 31 | #include "rib-status-publisher.hpp" |
| 32 | |
| 33 | #include <unordered_map> |
| 34 | #include <ndn-cxx/security/key-chain.hpp> |
| 35 | #include <ndn-cxx/management/nfd-controller.hpp> |
| 36 | #include <ndn-cxx/management/nfd-control-command.hpp> |
| 37 | #include <ndn-cxx/management/nfd-control-parameters.hpp> |
| 38 | #include <ndn-cxx/management/nfd-command-options.hpp> |
| 39 | |
| 40 | namespace nfd { |
| 41 | namespace rib { |
| 42 | |
| 43 | /** |
| 44 | * @brief define the RemoteRegistrator class, which handles |
| 45 | * the registration/unregistration to remote hub(s). |
| 46 | */ |
| 47 | class RemoteRegistrator : noncopyable |
| 48 | { |
| 49 | public: |
| 50 | class Error : public std::runtime_error |
| 51 | { |
| 52 | public: |
| 53 | explicit |
| 54 | Error(const std::string& what) |
| 55 | : std::runtime_error(what) |
| 56 | { |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | RemoteRegistrator(ndn::nfd::Controller& controller, |
| 61 | ndn::KeyChain& keyChain, |
| 62 | Rib& rib); |
| 63 | |
| 64 | ~RemoteRegistrator(); |
| 65 | |
| 66 | /** |
| 67 | * @brief load the "remote_register" section from config file |
| 68 | * |
| 69 | * @param configSection the sub section in "rib" section. |
| 70 | */ |
| 71 | void |
| 72 | loadConfig(const ConfigSection& configSection); |
| 73 | |
| 74 | /** |
| 75 | * @brief register a prefix to remote hub(s). |
| 76 | * |
| 77 | * For the input prefix, we find the longest identity |
| 78 | * in the key-chain that can sign it, and then |
| 79 | * register this identity to remote hub(s). |
| 80 | * |
| 81 | * @param prefix the prefix being registered in local RIB. |
| 82 | */ |
| 83 | void |
| 84 | registerPrefix(const Name& prefix); |
| 85 | |
| 86 | /** |
| 87 | * @brief unregister a prefix from remote hub(s). |
| 88 | * |
| 89 | * For the input prefix, if the longest identity can sign it |
| 90 | * is already registered remotely, that identity should be |
| 91 | * unregistered from remote hub(s). |
| 92 | * |
| 93 | * @param prefix the prefix being unregistered in local RIB. |
| 94 | */ |
| 95 | void |
| 96 | unregisterPrefix(const Name& prefix); |
| 97 | |
| 98 | private: |
| 99 | /** |
| 100 | * @brief find the most proper identity that can sign the |
| 101 | * registration/unregistration command for the input prefix. |
| 102 | * |
| 103 | * @return the identity and the length of the longest match to the |
| 104 | * input prefix. |
| 105 | * |
| 106 | * @retval { ignored, 0 } no matching identity |
| 107 | */ |
| 108 | std::pair<Name, size_t> |
| 109 | findIdentityForRegistration(const Name& prefix); |
| 110 | |
| 111 | /** |
| 112 | * @brief make and send the remote registration command. |
| 113 | * |
| 114 | * @param nRetries remaining number of retries. |
| 115 | */ |
| 116 | void |
| 117 | startRegistration(const ndn::nfd::ControlParameters& parameters, |
| 118 | const ndn::nfd::CommandOptions& options, |
| 119 | int nRetries); |
| 120 | |
| 121 | /** |
| 122 | * @brief make and send the remote unregistration command. |
| 123 | * |
| 124 | * @param nRetries remaining number of retries. |
| 125 | */ |
| 126 | void |
| 127 | startUnregistration(const ndn::nfd::ControlParameters& parameters, |
| 128 | const ndn::nfd::CommandOptions& options, |
| 129 | int nRetries); |
| 130 | /** |
| 131 | * @brief refresh the remotely registered entry if registration |
| 132 | * successes by re-sending the registration command. |
| 133 | * |
| 134 | * The interval of sending refresh command is defined in the |
| 135 | * "remote_register" section of the config file. |
| 136 | * |
| 137 | * @param parameters the same paremeters from startRegistration. |
| 138 | * @param options the same options as in startRegistration. |
| 139 | */ |
| 140 | void |
| 141 | onRegSuccess(const ndn::nfd::ControlParameters& parameters, |
| 142 | const ndn::nfd::CommandOptions& options); |
| 143 | |
| 144 | /** |
| 145 | * @brief retry to send registration command if registration fails. |
| 146 | * |
| 147 | * the number of retries is defined in the "remote_register" |
| 148 | * section of the config file. |
| 149 | * |
| 150 | * @param code error code. |
| 151 | * @param reason error reason in string. |
| 152 | * @param parameters the same paremeters from startRegistration. |
| 153 | * @param options the same options from startRegistration. |
| 154 | * @param nRetries remaining number of retries. |
| 155 | */ |
| 156 | void |
| 157 | onRegFailure(uint32_t code, const std::string& reason, |
| 158 | const ndn::nfd::ControlParameters& parameters, |
| 159 | const ndn::nfd::CommandOptions& options, |
| 160 | int nRetries); |
| 161 | |
| 162 | void |
| 163 | onUnregSuccess(const ndn::nfd::ControlParameters& parameters, |
| 164 | const ndn::nfd::CommandOptions& options); |
| 165 | |
| 166 | /** |
| 167 | * @brief retry to send unregistration command if registration fails. |
| 168 | * |
| 169 | * the number of retries is defined in the "remote_register" |
| 170 | * section of the config file. |
| 171 | * |
| 172 | * @param code error code. |
| 173 | * @param reason error reason in string. |
| 174 | * @param parameters the same paremeters as in startRegistration. |
| 175 | * @param options the same options as in startRegistration. |
| 176 | * @param nRetries remaining number of retries. |
| 177 | */ |
| 178 | void |
| 179 | onUnregFailure(uint32_t code, const std::string& reason, |
| 180 | const ndn::nfd::ControlParameters& parameters, |
| 181 | const ndn::nfd::CommandOptions& options, |
| 182 | int nRetries); |
| 183 | |
| 184 | /** |
| 185 | * @brief re-register all prefixes |
| 186 | * |
| 187 | * This is called when a HUB connection is established. |
| 188 | */ |
| 189 | void |
| 190 | redoRegistration(); |
| 191 | |
| 192 | /** |
| 193 | * @brief clear all refresh events |
| 194 | * |
| 195 | * This is called when all HUB connections are lost. |
| 196 | */ |
| 197 | void |
| 198 | clearRefreshEvents(); |
| 199 | |
| 200 | |
| 201 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 202 | /** |
| 203 | * When a locally registered prefix triggles remote |
| 204 | * registration, we actually register the longest |
| 205 | * identity that can sign this prefix to remote hub(s). |
| 206 | * |
| 207 | * Thus, the remotely reigstered prefix does not equal |
| 208 | * to Route Name. So it needs seperate sotrage instead |
| 209 | * of storing within the RIB. |
| 210 | */ |
| 211 | typedef std::unordered_map<Name, EventId> RegisteredList; |
| 212 | typedef RegisteredList::iterator RegisteredEntryIt; |
| 213 | typedef RegisteredList::value_type RegisteredEntry; |
| 214 | RegisteredList m_regEntries; |
| 215 | |
| 216 | private: |
| 217 | ndn::nfd::Controller& m_nfdController; |
| 218 | ndn::KeyChain& m_keyChain; |
| 219 | Rib& m_rib; |
| 220 | |
| 221 | ndn::nfd::ControlParameters m_controlParameters; |
| 222 | ndn::nfd::CommandOptions m_commandOptions; |
| 223 | time::seconds m_refreshInterval; |
| 224 | bool m_hasConnectedHub; |
| 225 | int m_nRetries; |
| 226 | |
| 227 | static const Name RM_LOCAL_PREFIX; // /localhost |
| 228 | static const Name RM_HUB_PREFIX; // /localhop/nfd |
| 229 | static const name::Component RM_IGNORE_COMMPONENT; // rib |
| 230 | }; |
| 231 | |
| 232 | } // namespace rib |
| 233 | } // namespace nfd |
| 234 | |
| 235 | #endif // NFD_RIB_REMOTE_REGISTRATOR_HPP |