29 #include <ndn-cxx/security/signing-helpers.hpp> 39 const Name RemoteRegistrator::LOCAL_REGISTRATION_PREFIX =
"/localhost";
40 const Name RemoteRegistrator::REMOTE_HUB_PREFIX =
"/localhop/nfd";
44 ndn::KeyChain& keyChain,
46 : m_nfdController(controller)
47 , m_keyChain(keyChain)
49 , m_refreshInterval(
time::seconds(25))
50 , m_hasConnectedHub(false)
58 for (
auto&& entry : m_regEntries)
67 size_t cost = 15, timeout = 10000;
70 const size_t intervalDef = 25, intervalMax = 600;
72 NFD_LOG_INFO(
"Load remote_register section in rib section");
73 for (
auto&& i : configSection)
75 if (i.first ==
"cost")
77 cost = i.second.get_value<
size_t>();
79 else if (i.first ==
"timeout")
81 timeout = i.second.get_value<
size_t>();
83 else if (i.first ==
"retry")
85 retry = i.second.get_value<
size_t>();
87 else if (i.first ==
"refresh_interval")
89 interval = i.second.get_value<
size_t>();
94 "\" in \"remote-registrator\" section"));
111 interval = intervalDef;
114 interval = std::min(interval, intervalMax);
116 m_refreshInterval = time::seconds(interval);
123 m_afterInsertConnection =
129 m_afterEraseConnection =
145 if (LOCAL_REGISTRATION_PREFIX.
isPrefixOf(prefix))
151 bool isHubPrefix = prefix == REMOTE_HUB_PREFIX;
155 NFD_LOG_INFO(
"this is a prefix registered by some hub: " << prefix);
157 m_hasConnectedHub =
true;
163 if (!m_hasConnectedHub)
165 NFD_LOG_INFO(
"no hub connected when registering " << prefix);
169 std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);
171 if (0 == identity.second)
173 NFD_LOG_INFO(
"no proper identity found for registering " << prefix);
177 Name prefixForRegistration;
178 if (identity.first.size() == identity.second)
180 prefixForRegistration = identity.first;
184 prefixForRegistration = identity.first.
getPrefix(-1);
187 if (m_regEntries.find(prefixForRegistration) != m_regEntries.end())
189 NFD_LOG_INFO(
"registration already in process for " << prefix);
199 startRegistration(parameters.
setName(prefixForRegistration),
207 if (prefix == REMOTE_HUB_PREFIX)
209 NFD_LOG_INFO(
"disconnected to hub with prefix: " << prefix);
214 m_hasConnectedHub =
false;
216 clearRefreshEvents();
220 if (!m_hasConnectedHub)
222 NFD_LOG_INFO(
"no hub connected when unregistering " << prefix);
226 std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);
228 if (0 == identity.second)
230 NFD_LOG_INFO(
"no proper identity found for unregistering " << prefix);
234 Name prefixForRegistration;
235 if (identity.first.size() == identity.second)
237 prefixForRegistration = identity.first;
241 prefixForRegistration = identity.first.
getPrefix(-1);
244 RegisteredEntryIt iRegEntry = m_regEntries.find(prefixForRegistration);
245 if (m_regEntries.end() == iRegEntry)
247 NFD_LOG_INFO(
"no existing entry found when unregistering " << prefix);
251 for (
auto&& entry : m_rib)
253 if (prefixForRegistration.
isPrefixOf(entry.first) &&
254 findIdentityForRegistration(entry.first) == identity)
256 NFD_LOG_INFO(
"this identity should be kept for other rib entry: " 263 m_regEntries.erase(iRegEntry);
276 std::pair<Name, size_t>
277 RemoteRegistrator::findIdentityForRegistration(
const Name& prefix)
279 std::pair<Name, size_t> candidateIdentity;
280 std::vector<Name> identities;
281 bool isPrefix =
false;
282 size_t maxLength = 0, curLength = 0;
285 m_keyChain.getAllIdentities(identities,
false);
288 identities.push_back(m_keyChain.getDefaultIdentity());
291 for (
auto&& i : identities)
293 if (!i.empty() && IGNORE_COMMPONENT == i.at(-1))
295 isPrefix = i.getPrefix(-1).isPrefixOf(prefix);
296 curLength = i.size() - 1;
300 isPrefix = i.isPrefixOf(prefix);
301 curLength = i.size();
304 if (isPrefix && curLength > maxLength)
306 candidateIdentity.first = i;
307 maxLength = curLength;
311 candidateIdentity.second = maxLength;
313 return candidateIdentity;
325 bind(&RemoteRegistrator::onRegSuccess,
326 this, parameters, options),
327 bind(&RemoteRegistrator::onRegFailure,
328 this, _1, _2, parameters, options, nRetries),
341 bind(&RemoteRegistrator::onUnregSuccess,
342 this, parameters, options),
343 bind(&RemoteRegistrator::onUnregFailure,
344 this, _1, _2, parameters, options, nRetries),
354 RegisteredEntryIt iRegEntry = m_regEntries.find(parameters.
getName());
356 if (m_regEntries.end() != iRegEntry)
359 <<
", " << iRegEntry->second
365 bind(&RemoteRegistrator::startRegistration,
366 this, parameters, options, m_nRetries));
371 m_regEntries.insert(RegisteredEntry(
375 bind(&RemoteRegistrator::startRegistration,
376 this, parameters, options, m_nRetries))));
381 RemoteRegistrator::onRegFailure(uint32_t code,
const std::string& reason,
387 <<
"\n\t reason:" << reason
388 <<
"\n\t remain retries:" << nRetries);
392 startRegistration(parameters, options, nRetries - 1);
404 RemoteRegistrator::onUnregFailure(uint32_t code,
const std::string& reason,
410 <<
"\n\t reason:" << reason
411 <<
"\n\t remain retries:" << nRetries);
415 startUnregistration(parameters, options, nRetries - 1);
420 RemoteRegistrator::redoRegistration()
423 <<
" registration when new Hub connection is built.");
425 for (
auto&& entry : m_regEntries)
430 startRegistration(parameters.
setName(entry.first),
437 RemoteRegistrator::clearRefreshEvents()
439 for (
auto&& entry : m_regEntries)
void start(const ControlParameters ¶meters, const CommandSucceedCallback &onSuccess, const CommandFailCallback &onFailure, const CommandOptions &options=CommandOptions())
start command execution
ControlParameters & setFaceId(uint64_t faceId)
#define NFD_LOG_DEBUG(expression)
represents parameters in a ControlCommand request or response
void cancel(const EventId &eventId)
cancel a scheduled event
void registerPrefix(const Name &prefix)
register a prefix to remote hub(s).
void unregisterPrefix(const Name &prefix)
unregister a prefix from remote hub(s).
void disable()
disable remote registration/unregistration.
void loadConfig(const ConfigSection &configSection)
load the "remote_register" section from config file
ControlParameters & unsetCost()
CommandOptions & setTimeout(const time::milliseconds &timeout)
sets command timeout
#define NFD_LOG_INFO(expression)
contains options for ControlCommand execution
Copyright (c) 2011-2015 Regents of the University of California.
SigningInfo signingByIdentity(const Name &identity)
const Name & getName() const
NFD Management protocol - ControlCommand client.
boost::property_tree::ptree ConfigSection
EventId schedule(const time::nanoseconds &after, const std::function< void()> &event)
schedule an event
Name abstraction to represent an absolute name.
represents a rib/unregister command
CommandOptions & setSigningInfo(const security::SigningInfo &signingInfo)
sets signing parameters
represents a rib/register command
ndn::util::signal::Signal< Rib, Name > afterInsertEntry
ControlParameters & setCost(uint64_t cost)
void disconnect()
disconnects the connection manually
Component holds a read-only name component value.
RemoteRegistrator(ndn::nfd::Controller &controller, ndn::KeyChain &keyChain, Rib &rib)
ndn::util::signal::Signal< Rib, Name > afterEraseEntry
void enable()
enable remote registration/unregistration.
#define NFD_LOG_INIT(name)
ControlParameters & setOrigin(uint64_t origin)
ControlParameters & setName(const Name &name)
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
CommandOptions & setPrefix(const Name &prefix)
sets command prefix