blob: a587653e45c6a78610f4c39e445183c490fe339d [file] [log] [blame]
Yanbiao Lic17de832014-11-21 17:51:45 -08001/* -*- 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#include "remote-registrator.hpp"
27#include "core/logger.hpp"
28#include "core/scheduler.hpp"
29
30namespace nfd {
31namespace rib {
32
33NFD_LOG_INIT("RemoteRegistrator");
34
35using ndn::nfd::ControlParameters;
36using ndn::nfd::CommandOptions;
37
Yanbiao Lib9d439d2014-12-11 16:12:32 -080038const Name RemoteRegistrator::LOCAL_REGISTRATION_PREFIX = "/localhost";
39const Name RemoteRegistrator::REMOTE_HUB_PREFIX = "/localhop/nfd/rib";
40const name::Component RemoteRegistrator::IGNORE_COMMPONENT("rib");
Yanbiao Lic17de832014-11-21 17:51:45 -080041
42RemoteRegistrator::RemoteRegistrator(ndn::nfd::Controller& controller,
43 ndn::KeyChain& keyChain,
44 Rib& rib)
45 : m_nfdController(controller)
46 , m_keyChain(keyChain)
47 , m_rib(rib)
48 , m_refreshInterval(time::seconds(25))
49 , m_hasConnectedHub(false)
50 , m_nRetries(0)
51{
52}
53
54RemoteRegistrator::~RemoteRegistrator()
55{
56 // cancel all periodically refresh events.
57 for (auto&& entry : m_regEntries)
58 {
59 scheduler::cancel(entry.second);
60 }
61}
62
63void
64RemoteRegistrator::loadConfig(const ConfigSection& configSection)
65{
66 size_t cost = 15, timeout = 10000;
67 size_t retry = 0;
68 size_t interval = 0;
69 const size_t intervalDef = 25, intervalMax = 600;
70
71 NFD_LOG_INFO("Load remote_register section in rib section");
72 for (auto&& i : configSection)
73 {
74 if (i.first == "cost")
75 {
76 cost = i.second.get_value<size_t>();
77 }
78 else if (i.first == "timeout")
79 {
80 timeout = i.second.get_value<size_t>();
81 }
82 else if (i.first == "retry")
83 {
84 retry = i.second.get_value<size_t>();
85 }
86 else if (i.first == "refresh_interval")
87 {
88 interval = i.second.get_value<size_t>();
89 }
90 else
91 {
92 throw ConfigFile::Error("Unrecognized option \"" + i.first +
93 "\" in \"remote-registrator\" section");
94 }
95 }
96
97 m_controlParameters
98 .setCost(cost)
99 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)// set origin to client.
100 .setFaceId(0);// the remote hub will take the input face as the faceId.
101
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800102 Name commandPrefix = REMOTE_HUB_PREFIX;
103 if (IGNORE_COMMPONENT == commandPrefix.at(-1))
104 {
105 commandPrefix = commandPrefix.getPrefix(-1);
106 }
107
Yanbiao Lic17de832014-11-21 17:51:45 -0800108 m_commandOptions
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800109 .setPrefix(commandPrefix)
Yanbiao Lic17de832014-11-21 17:51:45 -0800110 .setTimeout(time::milliseconds(timeout));
111
112 m_nRetries = retry;
113
114 if (interval == 0)
115 {
116 interval = intervalDef;
117 }
118
119 interval = std::min(interval, intervalMax);
120
121 m_refreshInterval = time::seconds(interval);
122}
123
124void
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800125RemoteRegistrator::enable()
126{
127 // do remote registration after an entry is inserted into the RIB.
128 m_afterInsertConnection =
129 m_rib.afterInsertEntry.connect([this] (const Name& prefix) {
130 registerPrefix(prefix);
131 });
132
133 // do remote unregistration after an entry is erased from the RIB.
134 m_afterEraseConnection =
135 m_rib.afterEraseEntry.connect([this] (const Name& prefix) {
136 unregisterPrefix(prefix);
137 });
138}
139
140void
141RemoteRegistrator::disable()
142{
143 m_afterInsertConnection.disconnect();
144 m_afterEraseConnection.disconnect();
145}
146
147void
Yanbiao Lic17de832014-11-21 17:51:45 -0800148RemoteRegistrator::registerPrefix(const Name& prefix)
149{
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800150 if (LOCAL_REGISTRATION_PREFIX.isPrefixOf(prefix))
Yanbiao Lic17de832014-11-21 17:51:45 -0800151 {
152 NFD_LOG_INFO("local registration only for " << prefix);
153 return;
154 }
155
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800156 bool isHubPrefix = prefix == REMOTE_HUB_PREFIX;
Yanbiao Lic17de832014-11-21 17:51:45 -0800157
158 if (isHubPrefix)
159 {
160 NFD_LOG_INFO("this is a prefix registered by some hub: " << prefix);
161
162 m_hasConnectedHub = true;
163
164 redoRegistration();
165 return;
166 }
167
168 if (!m_hasConnectedHub)
169 {
170 NFD_LOG_INFO("no hub connected when registering " << prefix);
171 return;
172 }
173
174 std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);
175
176 if (0 == identity.second)
177 {
178 NFD_LOG_INFO("no proper identity found for registering " << prefix);
179 return;
180 }
181
182 Name prefixForRegistration;
183 if (identity.first.size() == identity.second)
184 {
185 prefixForRegistration = identity.first;
186 }
187 else
188 {
189 prefixForRegistration = identity.first.getPrefix(-1);
190 }
191
192 if (m_regEntries.find(prefixForRegistration) != m_regEntries.end())
193 {
194 NFD_LOG_INFO("registration already in process for " << prefix);
195 return;
196 }
197
198 // make copies of m_controlParameters and m_commandOptions to
199 // avoid unreasonable overwriting during concurrent registration
200 // and unregistration.
201 ControlParameters parameters = m_controlParameters;
202 CommandOptions options = m_commandOptions;
203
204 startRegistration(parameters.setName(prefixForRegistration),
205 options.setSigningIdentity(identity.first),
206 m_nRetries);
207}
208
209void
210RemoteRegistrator::unregisterPrefix(const Name& prefix)
211{
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800212 if (prefix == REMOTE_HUB_PREFIX)
Yanbiao Lic17de832014-11-21 17:51:45 -0800213 {
214 NFD_LOG_INFO("disconnected to hub with prefix: " << prefix);
215
216 // for phase 1: suppose there is at most one hub connected.
217 // if the hub prefix has been unregistered locally, there may
218 // be no connected hub.
219 m_hasConnectedHub = false;
220
221 clearRefreshEvents();
222 return;
223 }
224
225 if (!m_hasConnectedHub)
226 {
227 NFD_LOG_INFO("no hub connected when unregistering " << prefix);
228 return;
229 }
230
231 std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);
232
233 if (0 == identity.second)
234 {
235 NFD_LOG_INFO("no proper identity found for unregistering " << prefix);
236 return;
237 }
238
239 Name prefixForRegistration;
240 if (identity.first.size() == identity.second)
241 {
242 prefixForRegistration = identity.first;
243 }
244 else
245 {
246 prefixForRegistration = identity.first.getPrefix(-1);
247 }
248
249 RegisteredEntryIt iRegEntry = m_regEntries.find(prefixForRegistration);
250 if (m_regEntries.end() == iRegEntry)
251 {
252 NFD_LOG_INFO("no existing entry found when unregistering " << prefix);
253 return;
254 }
255
256 for (auto&& entry : m_rib)
257 {
258 if (prefixForRegistration.isPrefixOf(entry.first) &&
259 findIdentityForRegistration(entry.first) == identity)
260 {
261 NFD_LOG_INFO("this identity should be kept for other rib entry: "
262 << entry.first);
263 return;
264 }
265 }
266
267 scheduler::cancel(iRegEntry->second);
268 m_regEntries.erase(iRegEntry);
269
270 // make copies of m_controlParameters and m_commandOptions to
271 // avoid unreasonable overwriting during concurrent registration
272 // and unregistration.
273 ControlParameters parameters = m_controlParameters;
274 CommandOptions options = m_commandOptions;
275
276 startUnregistration(parameters.setName(prefixForRegistration).unsetCost(),
277 options.setSigningIdentity(identity.first),
278 m_nRetries);
279}
280
281std::pair<Name, size_t>
282RemoteRegistrator::findIdentityForRegistration(const Name& prefix)
283{
284 std::pair<Name, size_t> candidateIdentity;
285 std::vector<Name> identities;
286 bool isPrefix = false;
287 size_t maxLength = 0, curLength = 0;
288
289 // get all identies from the key-cahin except the default one.
290 m_keyChain.getAllIdentities(identities, false);
291
292 // get the default identity.
293 identities.push_back(m_keyChain.getDefaultIdentity());
294
295 // longest prefix matching to all indenties.
296 for (auto&& i : identities)
297 {
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800298 if (!i.empty() && IGNORE_COMMPONENT == i.at(-1))
Yanbiao Lic17de832014-11-21 17:51:45 -0800299 {
300 isPrefix = i.getPrefix(-1).isPrefixOf(prefix);
301 curLength = i.size() - 1;
302 }
303 else
304 {
305 isPrefix = i.isPrefixOf(prefix);
306 curLength = i.size();
307 }
308
309 if (isPrefix && curLength > maxLength)
310 {
311 candidateIdentity.first = i;
312 maxLength = curLength;
313 }
314 }
315
316 candidateIdentity.second = maxLength;
317
318 return candidateIdentity;
319}
320
321void
322RemoteRegistrator::startRegistration(const ControlParameters& parameters,
323 const CommandOptions& options,
324 int nRetries)
325{
326 NFD_LOG_INFO("start register " << parameters.getName());
327
328 m_nfdController.start<ndn::nfd::RibRegisterCommand>(
329 parameters,
330 bind(&RemoteRegistrator::onRegSuccess,
331 this, parameters, options),
332 bind(&RemoteRegistrator::onRegFailure,
333 this, _1, _2, parameters, options, nRetries),
334 options);
335}
336
337void
338RemoteRegistrator::startUnregistration(const ControlParameters& parameters,
339 const CommandOptions& options,
340 int nRetries)
341{
342 NFD_LOG_INFO("start unregister " << parameters.getName());
343
344 m_nfdController.start<ndn::nfd::RibUnregisterCommand>(
345 parameters,
346 bind(&RemoteRegistrator::onUnregSuccess,
347 this, parameters, options),
348 bind(&RemoteRegistrator::onUnregFailure,
349 this, _1, _2, parameters, options, nRetries),
350 options);
351}
352
353void
354RemoteRegistrator::onRegSuccess(const ControlParameters& parameters,
355 const CommandOptions& options)
356{
357 NFD_LOG_INFO("success to register " << parameters.getName());
358
359 RegisteredEntryIt iRegEntry = m_regEntries.find(parameters.getName());
360
361 if (m_regEntries.end() != iRegEntry)
362 {
363 NFD_LOG_DEBUG("Existing Entry: (" << iRegEntry->first
364 << ", " << iRegEntry->second
365 << ")");
366
367 scheduler::cancel(iRegEntry->second);
368 iRegEntry->second = scheduler::schedule(
369 m_refreshInterval,
370 bind(&RemoteRegistrator::startRegistration,
371 this, parameters, options, m_nRetries));
372 }
373 else
374 {
375 NFD_LOG_DEBUG("New Entry");
376 m_regEntries.insert(RegisteredEntry(
377 parameters.getName(),
378 scheduler::schedule(
379 m_refreshInterval,
380 bind(&RemoteRegistrator::startRegistration,
381 this, parameters, options, m_nRetries))));
382 }
383}
384
385void
386RemoteRegistrator::onRegFailure(uint32_t code, const std::string& reason,
387 const ControlParameters& parameters,
388 const CommandOptions& options,
389 int nRetries)
390{
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800391 NFD_LOG_INFO("fail to register " << parameters.getName()
392 << "\n\t reason:" << reason
393 << "\n\t remain retries:" << nRetries);
Yanbiao Lic17de832014-11-21 17:51:45 -0800394
395 if (nRetries > 0)
396 {
397 startRegistration(parameters, options, nRetries - 1);
398 }
399}
400
401void
402RemoteRegistrator::onUnregSuccess(const ControlParameters& parameters,
403 const CommandOptions& options)
404{
405 NFD_LOG_INFO("success to unregister " << parameters.getName());
406}
407
408void
409RemoteRegistrator::onUnregFailure(uint32_t code, const std::string& reason,
410 const ControlParameters& parameters,
411 const CommandOptions& options,
412 int nRetries)
413{
414 NFD_LOG_INFO("fail to unregister " << parameters.getName()
415 << "\n\t reason:" << reason
416 << "\n\t remain retries:" << nRetries);
417
418 if (nRetries > 0)
419 {
420 startUnregistration(parameters, options, nRetries - 1);
421 }
422}
423
424void
425RemoteRegistrator::redoRegistration()
426{
427 NFD_LOG_INFO("redo " << m_regEntries.size()
428 << " registration when new Hub connection is built.");
429
430 for (auto&& entry : m_regEntries)
431 {
432 // make copies to avoid unreasonable overwrite.
433 ControlParameters parameters = m_controlParameters;
434 CommandOptions options = m_commandOptions;
435 startRegistration(parameters.setName(entry.first),
436 options.setSigningIdentity(entry.first),
437 m_nRetries);
438 }
439}
440
441void
442RemoteRegistrator::clearRefreshEvents()
443{
444 for (auto&& entry : m_regEntries)
445 {
446 scheduler::cancel(entry.second);
447 }
448}
449
450} // namespace rib
451} // namespace nfd