22 #include <ndn-cxx/util/logger.hpp> 23 #include <ndn-cxx/util/segment-fetcher.hpp> 24 #include <ndn-cxx/security/validator-null.hpp> 36 const ndn::Name& syncPrefix,
37 const ndn::Name& userPrefix,
39 ndn::time::milliseconds syncInterestLifetime,
40 ndn::time::milliseconds syncReplyFreshness,
43 :
ProducerBase(expectedNumEntries, face, syncPrefix, userPrefix, syncReplyFreshness,
44 ibltCompression, contentCompression)
45 , m_syncInterestLifetime(syncInterestLifetime)
46 , m_onUpdate(onUpdateCallBack)
49 m_registeredPrefix =
m_face.setInterestFilter(
51 std::bind(&FullProducer::onSyncInterest,
this, _1, _2),
70 NDN_LOG_WARN(
"Prefix not added: " << prefix);
74 uint64_t newSeq = seq.value_or(
m_prefixes[prefix] + 1);
76 NDN_LOG_INFO(
"Publish: "<< prefix <<
"/" << newSeq);
80 satisfyPendingInterests();
84 FullProducer::sendSyncInterest()
99 m_outstandingInterestName = syncInterestName;
101 m_scheduledSyncInterestId =
102 m_scheduler.schedule(m_syncInterestLifetime / 2 + ndn::time::milliseconds(m_jitter(
m_rng)),
103 [
this] { sendSyncInterest(); });
105 ndn::Interest syncInterest(syncInterestName);
107 using ndn::util::SegmentFetcher;
108 SegmentFetcher::Options options;
109 options.interestLifetime = m_syncInterestLifetime;
110 options.maxTimeout = m_syncInterestLifetime;
111 options.rttOptions.initialRto = m_syncInterestLifetime;
113 m_fetcher = SegmentFetcher::start(
m_face, syncInterest,
114 ndn::security::v2::getAcceptAllValidator(), options);
116 m_fetcher->onComplete.connect([
this, syncInterest] (
const ndn::ConstBufferPtr& bufferPtr) {
117 onSyncData(syncInterest, bufferPtr);
120 m_fetcher->onError.connect([
this] (uint32_t errorCode,
const std::string& msg) {
121 NDN_LOG_ERROR(
"Cannot fetch sync data, error: " << errorCode <<
" message: " << msg);
122 if (errorCode == SegmentFetcher::ErrorCode::NACK_ERROR) {
123 auto after = ndn::time::milliseconds(m_jitter(
m_rng));
124 NDN_LOG_DEBUG(
"Schedule sync interest after: " << after);
125 m_scheduledSyncInterestId =
m_scheduler.schedule(after, [
this] { sendSyncInterest(); });
129 NDN_LOG_DEBUG(
"sendFullSyncInterest, nonce: " << syncInterest.getNonce() <<
130 ", hash: " << std::hash<ndn::Name>{}(syncInterestName));
134 FullProducer::onSyncInterest(
const ndn::Name& prefixName,
const ndn::Interest& interest)
141 ndn::Name nameWithoutSyncPrefix = interest.getName().getSubName(prefixName.size());
142 ndn::Name interestName;
144 if (nameWithoutSyncPrefix.size() == 1) {
146 interestName = interest.getName();
148 else if (nameWithoutSyncPrefix.size() == 3) {
150 interestName = interest.getName().getPrefix(-2);
156 ndn::name::Component ibltName = interestName.get(interestName.size()-1);
158 NDN_LOG_DEBUG(
"Full Sync Interest Received, nonce: " << interest.getNonce() <<
159 ", hash: " << std::hash<ndn::Name>{}(interestName));
165 catch (
const std::exception& e) {
166 NDN_LOG_WARN(e.what());
172 std::set<uint32_t> positive;
173 std::set<uint32_t> negative;
176 NDN_LOG_TRACE(
"Cannot decode differences, positive: " << positive.size()
177 <<
" negative: " << negative.size() <<
" m_threshold: " 182 if (positive.size() + negative.size() >=
m_threshold ||
183 (positive.size() == 0 && negative.size() == 0)) {
186 if (content.second != 0) {
187 state.
addContent(ndn::Name(content.first).appendNumber(content.second));
192 sendSyncData(interest.getName(), state.
wireEncode());
200 for (
const auto& hash : positive) {
201 auto nameIt =
m_biMap.left.find(hash);
202 if (nameIt !=
m_biMap.left.end()) {
203 ndn::Name nameWithoutSeq = nameIt->second.getPrefix(-1);
206 !isFutureHash(nameWithoutSeq.toUri(), negative)) {
213 NDN_LOG_DEBUG(
"Sending sync content: " << state);
214 sendSyncData(interestName, state.
wireEncode());
218 auto& entry = m_pendingEntries.emplace(interestName,
PendingEntryInfoFull{iblt, {}}).first->second;
219 entry.expirationEvent =
m_scheduler.schedule(interest.getInterestLifetime(),
221 NDN_LOG_TRACE(
"Erase Pending Interest " << interest.getNonce());
222 m_pendingEntries.erase(interest.getName());
227 FullProducer::sendSyncData(
const ndn::Name& name,
const ndn::Block& block)
229 NDN_LOG_DEBUG(
"Checking if data will satisfy our own pending interest");
231 ndn::Name nameWithIblt;
235 ndn::Name dataName(ndn::Name(name).appendNumber(std::hash<ndn::Name>{}(nameWithIblt)));
240 if (m_outstandingInterestName == name) {
241 NDN_LOG_DEBUG(
"Satisfied our own pending interest");
244 NDN_LOG_DEBUG(
"Removing our pending interest from face (stop fetcher)");
246 m_outstandingInterestName = ndn::Name(
"");
249 NDN_LOG_DEBUG(
"Sending Sync Data");
254 NDN_LOG_TRACE(
"Renewing sync interest");
258 NDN_LOG_DEBUG(
"Sending Sync Data");
264 FullProducer::onSyncData(
const ndn::Interest& interest,
const ndn::ConstBufferPtr& bufferPtr)
266 deletePendingInterests(interest.getName());
271 state.wireDecode(ndn::Block(std::move(decompressed)));
273 catch (
const std::exception& e) {
274 NDN_LOG_ERROR(
"Cannot parse received Sync Data: " << e.what());
278 std::vector<MissingDataInfo> updates;
280 NDN_LOG_DEBUG(
"Sync Data Received: " << state);
282 for (
const auto& content : state.getContent()) {
283 ndn::Name prefix = content.getPrefix(-1);
284 uint64_t seq = content.get(content.size() - 1).toNumber();
297 if (!updates.empty()) {
299 NDN_LOG_TRACE(
"Renewing sync interest");
303 NDN_LOG_TRACE(
"No new update, interest nonce: " << interest.getNonce() <<
304 " , hash: " << std::hash<ndn::Name>{}(interest.getName()));
309 FullProducer::satisfyPendingInterests()
311 NDN_LOG_DEBUG(
"Satisfying full sync interest: " << m_pendingEntries.size());
313 for (
auto it = m_pendingEntries.begin(); it != m_pendingEntries.end();) {
316 std::set<uint32_t> positive;
317 std::set<uint32_t> negative;
320 NDN_LOG_TRACE(
"Decode failed for pending interest");
321 if (positive.size() + negative.size() >=
m_threshold ||
322 (positive.size() == 0 && negative.size() == 0)) {
323 NDN_LOG_TRACE(
"pos + neg > threshold or no diff can be found, erase pending interest");
324 it = m_pendingEntries.erase(it);
330 for (
const auto& hash : positive) {
331 auto nameIt =
m_biMap.left.find(hash);
332 if (nameIt !=
m_biMap.left.end()) {
333 if (
m_prefixes[nameIt->second.getPrefix(-1)] != 0) {
340 NDN_LOG_DEBUG(
"Satisfying sync content: " << state);
342 it = m_pendingEntries.erase(it);
351 FullProducer::isFutureHash(
const ndn::Name& prefix,
const std::set<uint32_t>& negative)
354 ndn::Name(prefix).appendNumber(
m_prefixes[prefix] + 1).toUri());
355 for (
const auto& nHash : negative) {
356 if (nHash == nextHash) {
365 FullProducer::deletePendingInterests(
const ndn::Name& interestName)
367 auto it = m_pendingEntries.find(interestName);
368 if (it != m_pendingEntries.end()) {
369 NDN_LOG_TRACE(
"Delete pending interest: " << interestName);
370 it = m_pendingEntries.erase(it);
void updateSeqNo(const ndn::Name &prefix, uint64_t seq)
Update m_prefixes and IBF with the given prefix and seq.
void initialize(const ndn::name::Component &ibltName)
Populate the hash table using the vector representation of IBLT.
void addContent(const ndn::Name &prefix)
const ndn::Block & wireEncode() const
NDN_LOG_INIT(psync.Consumer)
Full sync logic to synchronize with other nodes where all nodes wants to get all names prefixes synce...
uint32_t murmurHash3(uint32_t nHashSeed, const std::vector< unsigned char > &vDataToHash)
void publishName(const ndn::Name &prefix, ndn::optional< uint64_t > seq=ndn::nullopt)
Publish name to let others know.
Invertible Bloom Lookup Table (Invertible Bloom Filter)
void publish(const ndn::Name &interestName, const ndn::Name &dataName, const ndn::Block &block, ndn::time::milliseconds freshness, const ndn::security::SigningInfo &signingInfo=ndn::security::v2::KeyChain::getDefaultSigningInfo())
Put all the segments in memory.
std::shared_ptr< ndn::Buffer > decompress(CompressionScheme scheme, const uint8_t *buffer, size_t bufferSize)
const std::vector< ndn::Name > & getContent() const
uint32_t m_expectedNumEntries
bool replyFromStore(const ndn::Name &interestName)
Try to reply from memory, return false if we cannot find the segment.
std::shared_ptr< ndn::Buffer > compress(CompressionScheme scheme, const uint8_t *buffer, size_t bufferSize)
CompressionScheme m_contentCompression
bool listEntries(std::set< uint32_t > &positive, std::set< uint32_t > &negative) const
List all the entries in the IBLT.
FullProducer(size_t expectedNumEntries, ndn::Face &face, const ndn::Name &syncPrefix, const ndn::Name &userPrefix, const UpdateCallback &onUpdateCallBack, ndn::time::milliseconds syncInterestLifetime=SYNC_INTEREST_LIFTIME, ndn::time::milliseconds syncReplyFreshness=SYNC_REPLY_FRESHNESS, CompressionScheme ibltCompression=CompressionScheme::ZLIB, CompressionScheme contentCompression=CompressionScheme::ZLIB)
constructor
void appendToName(ndn::Name &name) const
Appends self to name.
ndn::random::RandomNumberEngine & m_rng
void onRegisterFailed(const ndn::Name &prefix, const std::string &msg) const
Logs a message if setting an interest filter fails.
ndn::Scheduler m_scheduler
std::function< void(const std::vector< MissingDataInfo > &)> UpdateCallback
ndn::time::milliseconds m_syncReplyFreshness
SegmentPublisher m_segmentPublisher
Base class for PartialProducer and FullProducer.
std::map< ndn::Name, uint64_t > m_prefixes