blob: 47fa1467e7314a50400b30ea633264d6ae1e4ae8 [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";
Yanbiao Lide88b462015-01-30 11:20:35 -080039const Name RemoteRegistrator::REMOTE_HUB_PREFIX = "/localhop/nfd";
Yanbiao Lib9d439d2014-12-11 16:12:32 -080040const 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
102 m_commandOptions
Yanbiao Lide88b462015-01-30 11:20:35 -0800103 .setPrefix(REMOTE_HUB_PREFIX)
Yanbiao Lic17de832014-11-21 17:51:45 -0800104 .setTimeout(time::milliseconds(timeout));
105
106 m_nRetries = retry;
107
108 if (interval == 0)
109 {
110 interval = intervalDef;
111 }
112
113 interval = std::min(interval, intervalMax);
114
115 m_refreshInterval = time::seconds(interval);
116}
117
118void
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800119RemoteRegistrator::enable()
120{
121 // do remote registration after an entry is inserted into the RIB.
122 m_afterInsertConnection =
123 m_rib.afterInsertEntry.connect([this] (const Name& prefix) {
124 registerPrefix(prefix);
125 });
126
127 // do remote unregistration after an entry is erased from the RIB.
128 m_afterEraseConnection =
129 m_rib.afterEraseEntry.connect([this] (const Name& prefix) {
130 unregisterPrefix(prefix);
131 });
132}
133
134void
135RemoteRegistrator::disable()
136{
137 m_afterInsertConnection.disconnect();
138 m_afterEraseConnection.disconnect();
139}
140
141void
Yanbiao Lic17de832014-11-21 17:51:45 -0800142RemoteRegistrator::registerPrefix(const Name& prefix)
143{
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800144 if (LOCAL_REGISTRATION_PREFIX.isPrefixOf(prefix))
Yanbiao Lic17de832014-11-21 17:51:45 -0800145 {
146 NFD_LOG_INFO("local registration only for " << prefix);
147 return;
148 }
149
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800150 bool isHubPrefix = prefix == REMOTE_HUB_PREFIX;
Yanbiao Lic17de832014-11-21 17:51:45 -0800151
152 if (isHubPrefix)
153 {
154 NFD_LOG_INFO("this is a prefix registered by some hub: " << prefix);
155
156 m_hasConnectedHub = true;
157
158 redoRegistration();
159 return;
160 }
161
162 if (!m_hasConnectedHub)
163 {
164 NFD_LOG_INFO("no hub connected when registering " << prefix);
165 return;
166 }
167
168 std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);
169
170 if (0 == identity.second)
171 {
172 NFD_LOG_INFO("no proper identity found for registering " << prefix);
173 return;
174 }
175
176 Name prefixForRegistration;
177 if (identity.first.size() == identity.second)
178 {
179 prefixForRegistration = identity.first;
180 }
181 else
182 {
183 prefixForRegistration = identity.first.getPrefix(-1);
184 }
185
186 if (m_regEntries.find(prefixForRegistration) != m_regEntries.end())
187 {
188 NFD_LOG_INFO("registration already in process for " << prefix);
189 return;
190 }
191
192 // make copies of m_controlParameters and m_commandOptions to
193 // avoid unreasonable overwriting during concurrent registration
194 // and unregistration.
195 ControlParameters parameters = m_controlParameters;
196 CommandOptions options = m_commandOptions;
197
198 startRegistration(parameters.setName(prefixForRegistration),
199 options.setSigningIdentity(identity.first),
200 m_nRetries);
201}
202
203void
204RemoteRegistrator::unregisterPrefix(const Name& prefix)
205{
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800206 if (prefix == REMOTE_HUB_PREFIX)
Yanbiao Lic17de832014-11-21 17:51:45 -0800207 {
208 NFD_LOG_INFO("disconnected to hub with prefix: " << prefix);
209
210 // for phase 1: suppose there is at most one hub connected.
211 // if the hub prefix has been unregistered locally, there may
212 // be no connected hub.
213 m_hasConnectedHub = false;
214
215 clearRefreshEvents();
216 return;
217 }
218
219 if (!m_hasConnectedHub)
220 {
221 NFD_LOG_INFO("no hub connected when unregistering " << prefix);
222 return;
223 }
224
225 std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);
226
227 if (0 == identity.second)
228 {
229 NFD_LOG_INFO("no proper identity found for unregistering " << prefix);
230 return;
231 }
232
233 Name prefixForRegistration;
234 if (identity.first.size() == identity.second)
235 {
236 prefixForRegistration = identity.first;
237 }
238 else
239 {
240 prefixForRegistration = identity.first.getPrefix(-1);
241 }
242
243 RegisteredEntryIt iRegEntry = m_regEntries.find(prefixForRegistration);
244 if (m_regEntries.end() == iRegEntry)
245 {
246 NFD_LOG_INFO("no existing entry found when unregistering " << prefix);
247 return;
248 }
249
250 for (auto&& entry : m_rib)
251 {
252 if (prefixForRegistration.isPrefixOf(entry.first) &&
253 findIdentityForRegistration(entry.first) == identity)
254 {
255 NFD_LOG_INFO("this identity should be kept for other rib entry: "
256 << entry.first);
257 return;
258 }
259 }
260
261 scheduler::cancel(iRegEntry->second);
262 m_regEntries.erase(iRegEntry);
263
264 // make copies of m_controlParameters and m_commandOptions to
265 // avoid unreasonable overwriting during concurrent registration
266 // and unregistration.
267 ControlParameters parameters = m_controlParameters;
268 CommandOptions options = m_commandOptions;
269
270 startUnregistration(parameters.setName(prefixForRegistration).unsetCost(),
271 options.setSigningIdentity(identity.first),
272 m_nRetries);
273}
274
275std::pair<Name, size_t>
276RemoteRegistrator::findIdentityForRegistration(const Name& prefix)
277{
278 std::pair<Name, size_t> candidateIdentity;
279 std::vector<Name> identities;
280 bool isPrefix = false;
281 size_t maxLength = 0, curLength = 0;
282
283 // get all identies from the key-cahin except the default one.
284 m_keyChain.getAllIdentities(identities, false);
285
286 // get the default identity.
287 identities.push_back(m_keyChain.getDefaultIdentity());
288
289 // longest prefix matching to all indenties.
290 for (auto&& i : identities)
291 {
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800292 if (!i.empty() && IGNORE_COMMPONENT == i.at(-1))
Yanbiao Lic17de832014-11-21 17:51:45 -0800293 {
294 isPrefix = i.getPrefix(-1).isPrefixOf(prefix);
295 curLength = i.size() - 1;
296 }
297 else
298 {
299 isPrefix = i.isPrefixOf(prefix);
300 curLength = i.size();
301 }
302
303 if (isPrefix && curLength > maxLength)
304 {
305 candidateIdentity.first = i;
306 maxLength = curLength;
307 }
308 }
309
310 candidateIdentity.second = maxLength;
311
312 return candidateIdentity;
313}
314
315void
316RemoteRegistrator::startRegistration(const ControlParameters& parameters,
317 const CommandOptions& options,
318 int nRetries)
319{
320 NFD_LOG_INFO("start register " << parameters.getName());
321
322 m_nfdController.start<ndn::nfd::RibRegisterCommand>(
323 parameters,
324 bind(&RemoteRegistrator::onRegSuccess,
325 this, parameters, options),
326 bind(&RemoteRegistrator::onRegFailure,
327 this, _1, _2, parameters, options, nRetries),
328 options);
329}
330
331void
332RemoteRegistrator::startUnregistration(const ControlParameters& parameters,
333 const CommandOptions& options,
334 int nRetries)
335{
336 NFD_LOG_INFO("start unregister " << parameters.getName());
337
338 m_nfdController.start<ndn::nfd::RibUnregisterCommand>(
339 parameters,
340 bind(&RemoteRegistrator::onUnregSuccess,
341 this, parameters, options),
342 bind(&RemoteRegistrator::onUnregFailure,
343 this, _1, _2, parameters, options, nRetries),
344 options);
345}
346
347void
348RemoteRegistrator::onRegSuccess(const ControlParameters& parameters,
349 const CommandOptions& options)
350{
351 NFD_LOG_INFO("success to register " << parameters.getName());
352
353 RegisteredEntryIt iRegEntry = m_regEntries.find(parameters.getName());
354
355 if (m_regEntries.end() != iRegEntry)
356 {
357 NFD_LOG_DEBUG("Existing Entry: (" << iRegEntry->first
358 << ", " << iRegEntry->second
359 << ")");
360
361 scheduler::cancel(iRegEntry->second);
362 iRegEntry->second = scheduler::schedule(
363 m_refreshInterval,
364 bind(&RemoteRegistrator::startRegistration,
365 this, parameters, options, m_nRetries));
366 }
367 else
368 {
369 NFD_LOG_DEBUG("New Entry");
370 m_regEntries.insert(RegisteredEntry(
371 parameters.getName(),
372 scheduler::schedule(
373 m_refreshInterval,
374 bind(&RemoteRegistrator::startRegistration,
375 this, parameters, options, m_nRetries))));
376 }
377}
378
379void
380RemoteRegistrator::onRegFailure(uint32_t code, const std::string& reason,
381 const ControlParameters& parameters,
382 const CommandOptions& options,
383 int nRetries)
384{
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800385 NFD_LOG_INFO("fail to register " << parameters.getName()
386 << "\n\t reason:" << reason
387 << "\n\t remain retries:" << nRetries);
Yanbiao Lic17de832014-11-21 17:51:45 -0800388
389 if (nRetries > 0)
390 {
391 startRegistration(parameters, options, nRetries - 1);
392 }
393}
394
395void
396RemoteRegistrator::onUnregSuccess(const ControlParameters& parameters,
397 const CommandOptions& options)
398{
399 NFD_LOG_INFO("success to unregister " << parameters.getName());
400}
401
402void
403RemoteRegistrator::onUnregFailure(uint32_t code, const std::string& reason,
404 const ControlParameters& parameters,
405 const CommandOptions& options,
406 int nRetries)
407{
408 NFD_LOG_INFO("fail to unregister " << parameters.getName()
409 << "\n\t reason:" << reason
410 << "\n\t remain retries:" << nRetries);
411
412 if (nRetries > 0)
413 {
414 startUnregistration(parameters, options, nRetries - 1);
415 }
416}
417
418void
419RemoteRegistrator::redoRegistration()
420{
421 NFD_LOG_INFO("redo " << m_regEntries.size()
422 << " registration when new Hub connection is built.");
423
424 for (auto&& entry : m_regEntries)
425 {
426 // make copies to avoid unreasonable overwrite.
427 ControlParameters parameters = m_controlParameters;
428 CommandOptions options = m_commandOptions;
429 startRegistration(parameters.setName(entry.first),
430 options.setSigningIdentity(entry.first),
431 m_nRetries);
432 }
433}
434
435void
436RemoteRegistrator::clearRefreshEvents()
437{
438 for (auto&& entry : m_regEntries)
439 {
440 scheduler::cancel(entry.second);
441 }
442}
443
444} // namespace rib
445} // namespace nfd