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