blob: 2a748c3f5a70b21487c1ba3b800992ae6b0219f0 [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Chaoyi Bian <bcy@pku.edu.cn>
akmhoque157b0a42014-05-13 00:26:37 -050020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
akmhoque66e66182014-02-21 17:56:03 -060021 * Yingdi Yu <yingdi@cs.ucla.edu>
22 */
23
24#include "sync-logic.h"
25#include "sync-diff-leaf.h"
26#include "sync-full-leaf.h"
27#include "sync-logging.h"
28#include "sync-state.h"
29
30#include <boost/foreach.hpp>
31#include <boost/lexical_cast.hpp>
32#include <vector>
33
34using namespace std;
35using namespace ndn;
36
37INIT_LOGGER ("SyncLogic");
38
39
40#ifdef _DEBUG
41#define _LOG_DEBUG_ID(v) _LOG_DEBUG(m_instanceId << " " << v)
42#else
43#define _LOG_DEBUG_ID(v) _LOG_DEBUG(v)
44#endif
45
46#define GET_RANDOM(var) var ()
47
48#define TIME_SECONDS_WITH_JITTER(sec) \
akmhoque157b0a42014-05-13 00:26:37 -050049 (ndn::time::seconds(sec) + ndn::time::milliseconds(GET_RANDOM (m_reexpressionJitter)))
akmhoque66e66182014-02-21 17:56:03 -060050
51#define TIME_MILLISECONDS_WITH_JITTER(ms) \
akmhoque157b0a42014-05-13 00:26:37 -050052 (ndn::time::seconds(ms) + ndn::time::milliseconds(GET_RANDOM (m_reexpressionJitter)))
akmhoque66e66182014-02-21 17:56:03 -060053
54namespace Sync {
55
56using ndn::shared_ptr;
57
58int SyncLogic::m_instanceCounter = 0;
59
Yingdi Yu40cd1c32014-04-17 15:02:17 -070060const int SyncLogic::m_syncResponseFreshness = 1000; // MUST BE dividable by 1000!!!
61const int SyncLogic::m_syncInterestReexpress = 4; // seconds
62
akmhoque66e66182014-02-21 17:56:03 -060063SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu40cd1c32014-04-17 15:02:17 -070064 shared_ptr<Validator> validator,
akmhoque66e66182014-02-21 17:56:03 -060065 shared_ptr<Face> face,
66 LogicUpdateCallback onUpdate,
67 LogicRemoveCallback onRemove)
68 : m_state (new FullState)
akmhoque157b0a42014-05-13 00:26:37 -050069 , m_syncInterestTable (face->getIoService(), ndn::time::seconds(m_syncInterestReexpress))
akmhoque66e66182014-02-21 17:56:03 -060070 , m_syncPrefix (syncPrefix)
71 , m_onUpdate (onUpdate)
72 , m_onRemove (onRemove)
73 , m_perBranch (false)
74 , m_validator(validator)
75 , m_keyChain(new KeyChain())
76 , m_face(face)
akmhoque157b0a42014-05-13 00:26:37 -050077 , m_scheduler(face->getIoService())
akmhoque66e66182014-02-21 17:56:03 -060078 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
79 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
80 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
81 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070082{
83 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
akmhoque157b0a42014-05-13 00:26:37 -050084 bind(&SyncLogic::onSyncInterest,
85 this, _1, _2),
86 bind(&SyncLogic::onSyncRegisterSucceed,
87 this, _1),
88 bind(&SyncLogic::onSyncRegisterFailed,
89 this, _1, _2));
Yingdi Yu40cd1c32014-04-17 15:02:17 -070090
akmhoque66e66182014-02-21 17:56:03 -060091
akmhoque157b0a42014-05-13 00:26:37 -050092 m_reexpressingInterestId = m_scheduler.scheduleEvent (ndn::time::seconds (0),
akmhoque66e66182014-02-21 17:56:03 -060093 bind (&SyncLogic::sendSyncInterest, this));
94
95 m_instanceId = string("Instance " + boost::lexical_cast<string>(m_instanceCounter++) + " ");
96}
97
98SyncLogic::SyncLogic (const Name& syncPrefix,
99 shared_ptr<Validator> validator,
100 shared_ptr<Face> face,
101 LogicPerBranchCallback onUpdateBranch)
102 : m_state (new FullState)
akmhoque157b0a42014-05-13 00:26:37 -0500103 , m_syncInterestTable (face->getIoService(), ndn::time::seconds (m_syncInterestReexpress))
akmhoque66e66182014-02-21 17:56:03 -0600104 , m_syncPrefix (syncPrefix)
105 , m_onUpdateBranch (onUpdateBranch)
106 , m_perBranch(true)
107 , m_validator(validator)
108 , m_keyChain(new KeyChain())
109 , m_face(face)
akmhoque157b0a42014-05-13 00:26:37 -0500110 , m_scheduler(face->getIoService())
akmhoque66e66182014-02-21 17:56:03 -0600111 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
112 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
113 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
114 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700115{
116 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
akmhoque157b0a42014-05-13 00:26:37 -0500117 bind(&SyncLogic::onSyncInterest,
118 this, _1, _2),
119 bind(&SyncLogic::onSyncRegisterSucceed,
120 this, _1),
121 bind(&SyncLogic::onSyncRegisterFailed,
122 this, _1, _2));
akmhoque66e66182014-02-21 17:56:03 -0600123
akmhoque157b0a42014-05-13 00:26:37 -0500124 m_reexpressingInterestId = m_scheduler.scheduleEvent (ndn::time::seconds (0),
akmhoque66e66182014-02-21 17:56:03 -0600125 bind (&SyncLogic::sendSyncInterest, this));
126}
127
128SyncLogic::~SyncLogic ()
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700129{
130 m_face->unsetInterestFilter(m_syncRegisteredPrefixId);
akmhoque66e66182014-02-21 17:56:03 -0600131 m_scheduler.cancelEvent (m_reexpressingInterestId);
132 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
133}
134
135/**
136 * Two types of intersts
137 *
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700138 * Normal name: .../<hash>
akmhoque66e66182014-02-21 17:56:03 -0600139 * Recovery name: .../recovery/<hash>
140 */
141boost::tuple<DigestConstPtr, std::string>
142SyncLogic::convertNameToDigestAndType (const Name &name)
143{
144 BOOST_ASSERT (m_syncPrefix.isPrefixOf(name));
145
146 int nameLengthDiff = name.size() - m_syncPrefix.size();
147 BOOST_ASSERT (nameLengthDiff > 0);
148 BOOST_ASSERT (nameLengthDiff < 3);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700149
akmhoque157b0a42014-05-13 00:26:37 -0500150 string hash = name.get(-1).toUri();
akmhoque66e66182014-02-21 17:56:03 -0600151 string interestType;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700152
akmhoque66e66182014-02-21 17:56:03 -0600153 if(nameLengthDiff == 1)
154 interestType = "normal";
155 else
akmhoque157b0a42014-05-13 00:26:37 -0500156 interestType = name.get(-2).toUri();
akmhoque66e66182014-02-21 17:56:03 -0600157
158 _LOG_DEBUG_ID (hash << ", " << interestType);
159
160 DigestPtr digest = boost::make_shared<Digest> ();
161 istringstream is (hash);
162 is >> *digest;
163
164 return make_tuple (digest, interestType);
165}
166
167void
168SyncLogic::onSyncInterest (const Name& prefix, const ndn::Interest& interest)
169{
170 Name name = interest.getName();
171
172 _LOG_DEBUG_ID("respondSyncInterest: " << name);
173
174 try
175 {
176 _LOG_DEBUG_ID ("<< I " << name);
177
178 DigestConstPtr digest;
179 string type;
180 tie (digest, type) = convertNameToDigestAndType (name);
181
182 if (type == "normal") // kind of ineffective...
183 {
184 processSyncInterest (name, digest);
185 }
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700186 else if (type == "recovery")
akmhoque66e66182014-02-21 17:56:03 -0600187 {
188 processSyncRecoveryInterest (name, digest);
189 }
190 }
191 catch (Error::DigestCalculationError &e)
192 {
193 _LOG_DEBUG_ID ("Something fishy happened...");
194 // log error. ignoring it for now, later we should log it
195 return ;
196 }
197}
198
199void
akmhoque157b0a42014-05-13 00:26:37 -0500200SyncLogic::onSyncRegisterSucceed(const Name& prefix)
201{
202 _LOG_DEBUG_ID("Sync prefix registration succeeded! " << prefix);
203}
204
205void
akmhoque66e66182014-02-21 17:56:03 -0600206SyncLogic::onSyncRegisterFailed(const Name& prefix, const string& msg)
207{
208 _LOG_DEBUG_ID("Sync prefix registration failed! " << msg);
209}
210
211void
212SyncLogic::onSyncData(const ndn::Interest& interest, Data& data)
213{
214 OnDataValidated onValidated = bind(&SyncLogic::onSyncDataValidated, this, _1);
215 OnDataValidationFailed onValidationFailed = bind(&SyncLogic::onSyncDataValidationFailed, this, _1);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700216 m_validator->validate(data, onValidated, onValidationFailed);
akmhoque66e66182014-02-21 17:56:03 -0600217}
218
219void
220SyncLogic::onSyncTimeout(const ndn::Interest& interest)
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700221{
222 // It is OK. Others will handle the time out situation.
akmhoque66e66182014-02-21 17:56:03 -0600223}
224
225void
226SyncLogic::onSyncDataValidationFailed(const shared_ptr<const Data>& data)
227{
228 _LOG_DEBUG_ID("Sync data cannot be verified!");
229}
230
231void
232SyncLogic::onSyncDataValidated(const shared_ptr<const Data>& data)
233{
234 Name name = data->getName();
235 const char* wireData = (const char*)data->getContent().value();
236 size_t len = data->getContent().value_size();
237
238 try
239 {
240 _LOG_DEBUG_ID ("<< D " << name);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700241
akmhoque66e66182014-02-21 17:56:03 -0600242 DigestConstPtr digest;
243 string type;
244 tie (digest, type) = convertNameToDigestAndType (name);
245
246 if (type == "normal")
247 {
248 processSyncData (name, digest, wireData, len);
249 }
250 else
251 {
252 // timer is always restarted when we schedule recovery
253 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
254 processSyncData (name, digest, wireData, len);
255 }
256 }
257 catch (Error::DigestCalculationError &e)
258 {
259 _LOG_DEBUG_ID ("Something fishy happened...");
260 // log error. ignoring it for now, later we should log it
261 return;
262 }
263}
264
265void
akmhoque157b0a42014-05-13 00:26:37 -0500266SyncLogic::processSyncInterest (const Name &name, DigestConstPtr digest,
267 bool timedProcessing/*=false*/)
akmhoque66e66182014-02-21 17:56:03 -0600268{
269 _LOG_DEBUG_ID("processSyncInterest");
270 DigestConstPtr rootDigest;
271 {
272 rootDigest = m_state->getDigest();
273 }
274
275 // Special case when state is not empty and we have received request with zero-root digest
276 if (digest->isZero () && !rootDigest->isZero ())
277 {
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700278
akmhoque66e66182014-02-21 17:56:03 -0600279 SyncStateMsg ssm;
280 {
281 ssm << (*m_state);
282 }
283 sendSyncData (name, digest, ssm);
284 return;
285 }
286
287 if (*rootDigest == *digest)
288 {
289 _LOG_DEBUG_ID ("processSyncInterest (): Same state. Adding to PIT");
290 m_syncInterestTable.insert (digest, name.toUri(), false);
291 return;
292 }
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700293
akmhoque66e66182014-02-21 17:56:03 -0600294 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
295
296 if (stateInDiffLog != m_log.end ())
297 {
298 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
299
300 sendSyncData (name, digest, stateDiff);
301 return;
302 }
303
304 if (!timedProcessing)
305 {
306 bool exists = m_syncInterestTable.insert (digest, name.toUri(), true);
307 if (exists) // somebody else replied, so restart random-game timer
308 {
akmhoque157b0a42014-05-13 00:26:37 -0500309 _LOG_DEBUG_ID("Unknown digest, but somebody may have already replied, " <<
310 "so restart our timer");
akmhoque66e66182014-02-21 17:56:03 -0600311 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
312 }
313
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700314 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
akmhoque157b0a42014-05-13 00:26:37 -0500315 _LOG_DEBUG_ID("Digest is not in the log. Schedule processing after small delay: " <<
316 ndn::time::milliseconds (waitDelay));
akmhoque66e66182014-02-21 17:56:03 -0600317
akmhoque157b0a42014-05-13 00:26:37 -0500318 m_delayedInterestProcessingId =
319 m_scheduler.scheduleEvent(ndn::time::milliseconds (waitDelay),
320 bind (&SyncLogic::processSyncInterest, this, name, digest, true));
akmhoque66e66182014-02-21 17:56:03 -0600321 }
322 else
323 {
akmhoque157b0a42014-05-13 00:26:37 -0500324 _LOG_DEBUG_ID(" (timed processing)");
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700325
akmhoque66e66182014-02-21 17:56:03 -0600326 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
327 sendSyncRecoveryInterests (digest);
328 }
329}
330
331void
akmhoque157b0a42014-05-13 00:26:37 -0500332SyncLogic::processSyncData (const Name &name, DigestConstPtr digest,
333 const char *wireData, size_t len)
akmhoque66e66182014-02-21 17:56:03 -0600334{
335 DiffStatePtr diffLog = boost::make_shared<DiffState> ();
336 bool ownInterestSatisfied = false;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700337
akmhoque66e66182014-02-21 17:56:03 -0600338 try
339 {
340
341 m_syncInterestTable.remove (name.toUri()); // Remove satisfied interest from PIT
342
343 ownInterestSatisfied = (name == m_outstandingInterestName);
344
345 DiffState diff;
346 SyncStateMsg msg;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700347 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
akmhoque66e66182014-02-21 17:56:03 -0600348 {
349 //Throw
350 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
351 }
352 msg >> diff;
353
354 vector<MissingDataInfo> v;
355 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
356 {
357 DiffLeafConstPtr diffLeaf = boost::dynamic_pointer_cast<const DiffLeaf> (leaf);
358 BOOST_ASSERT (diffLeaf != 0);
359
360 NameInfoConstPtr info = diffLeaf->getInfo();
361 if (diffLeaf->getOperation() == UPDATE)
362 {
363 SeqNo seq = diffLeaf->getSeq();
364
365 bool inserted = false;
366 bool updated = false;
367 SeqNo oldSeq;
368 {
369 boost::tie (inserted, updated, oldSeq) = m_state->update (info, seq);
370 }
371
372 if (inserted || updated)
373 {
374 diffLog->update (info, seq);
375 if (!oldSeq.isValid())
376 {
377 oldSeq = SeqNo(seq.getSession(), 0);
378 }
379 else
380 {
381 ++oldSeq;
382 }
383 // there is no need for application to process update on forwarder node
384 if (info->toString() != forwarderPrefix)
385 {
386 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
387 {
388 ostringstream interestName;
akmhoque157b0a42014-05-13 00:26:37 -0500389 interestName << mdi.prefix <<
390 "/" << mdi.high.getSession() <<
391 "/" << mdi.high.getSeq();
akmhoque66e66182014-02-21 17:56:03 -0600392 _LOG_DEBUG_ID("+++++++++++++++ " + interestName.str());
393 }
394 if (m_perBranch)
395 {
396 ostringstream interestName;
akmhoque157b0a42014-05-13 00:26:37 -0500397 interestName << mdi.prefix <<
398 "/" << mdi.high.getSession() <<
399 "/" << mdi.high.getSeq();
akmhoque66e66182014-02-21 17:56:03 -0600400 m_onUpdateBranch(interestName.str());
401 }
402 else
403 {
404 v.push_back(mdi);
405 }
406 }
407 }
408 }
409 else if (diffLeaf->getOperation() == REMOVE)
410 {
411 if (m_state->remove (info))
412 {
413 diffLog->remove (info);
414 if (!m_perBranch)
415 {
416 m_onRemove (info->toString ());
417 }
418 }
419 }
420 else
421 {
422 }
423 }
424
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700425 if (!v.empty())
akmhoque66e66182014-02-21 17:56:03 -0600426 {
427 if (!m_perBranch)
428 {
429 m_onUpdate(v);
430 }
431 }
432
433 insertToDiffLog (diffLog);
434 }
435 catch (Error::SyncStateMsgDecodingFailure &e)
436 {
437 _LOG_DEBUG_ID ("Something really fishy happened during state decoding " <<
438 diagnostic_information (e));
439 diffLog.reset ();
440 // don't do anything
441 }
442
443 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
444 ownInterestSatisfied)
445 {
446 _LOG_DEBUG_ID(" +++++++++++++++ state changed!!!");
447 // Do it only if everything went fine and state changed
448
449 // this is kind of wrong
akmhoque157b0a42014-05-13 00:26:37 -0500450 // satisfyPendingSyncInterests (diffLog); // if there are interests in PIT,
451 // // there is a point to satisfy them using new state
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700452
akmhoque66e66182014-02-21 17:56:03 -0600453 // if state has changed, then it is safe to express a new interest
akmhoque157b0a42014-05-13 00:26:37 -0500454 ndn::time::system_clock::Duration after =
455 ndn::time::milliseconds(GET_RANDOM (m_reexpressionJitter));
akmhoque66e66182014-02-21 17:56:03 -0600456 // cout << "------------ reexpress interest after: " << after << endl;
457 EventId eventId = m_scheduler.scheduleEvent (after,
458 bind (&SyncLogic::sendSyncInterest, this));
459
460 m_scheduler.cancelEvent (m_reexpressingInterestId);
461 m_reexpressingInterestId = eventId;
462 }
463}
464
465void
466SyncLogic::processSyncRecoveryInterest (const Name &name, DigestConstPtr digest)
467{
468 _LOG_DEBUG_ID("processSyncRecoveryInterest");
469 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
470
471 if (stateInDiffLog == m_log.end ())
472 {
473 _LOG_DEBUG_ID ("Could not find " << *digest << " in digest log");
474 return;
475 }
476
477 SyncStateMsg ssm;
478 {
479 ssm << (*m_state);
480 }
481 sendSyncData (name, digest, ssm);
482}
483
484void
485SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
486{
487 DiffStatePtr fullStateLog = boost::make_shared<DiffState> ();
488 {
489 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
490 {
491 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
492 /// @todo Impose limit on how many state info should be send out
493 }
494 }
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700495
akmhoque66e66182014-02-21 17:56:03 -0600496 try
497 {
498 uint32_t counter = 0;
499 while (m_syncInterestTable.size () > 0)
500 {
501 Sync::Interest interest = m_syncInterestTable.pop ();
502
503 if (!interest.m_unknown)
504 {
505 _LOG_DEBUG_ID (">> D " << interest.m_name);
506 sendSyncData (interest.m_name, interest.m_digest, diffLog);
507 }
508 else
509 {
510 _LOG_DEBUG_ID (">> D (unknown)" << interest.m_name);
511 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
512 }
513 counter ++;
514 }
515 _LOG_DEBUG_ID ("Satisfied " << counter << " pending interests");
516 }
517 catch (Error::InterestTableIsEmpty &e)
518 {
519 // ok. not really an error
520 }
521}
522
523void
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700524SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
akmhoque66e66182014-02-21 17:56:03 -0600525{
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700526 diffLog->setDigest (m_state->getDigest());
akmhoque66e66182014-02-21 17:56:03 -0600527 if (m_log.size () > 0)
528 {
529 m_log.get<sequenced> ().front ()->setNext (diffLog);
530 }
akmhoque157b0a42014-05-13 00:26:37 -0500531 m_log.erase (m_state->getDigest()); // remove diff state with the same digest.
532 // next pointers are still valid
akmhoque66e66182014-02-21 17:56:03 -0600533 /// @todo Optimization
534 m_log.get<sequenced> ().push_front (diffLog);
535}
536
537void
538SyncLogic::addLocalNames (const Name &prefix, uint64_t session, uint64_t seq)
539{
540 DiffStatePtr diff;
541 {
542 //cout << "Add local names" <<endl;
543 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
544
545 _LOG_DEBUG_ID ("addLocalNames (): old state " << *m_state->getDigest ());
546
547 SeqNo seqN (session, seq);
548 m_state->update(info, seqN);
549
550 _LOG_DEBUG_ID ("addLocalNames (): new state " << *m_state->getDigest ());
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700551
akmhoque66e66182014-02-21 17:56:03 -0600552 diff = boost::make_shared<DiffState>();
553 diff->update(info, seqN);
554 insertToDiffLog (diff);
555 }
556
557 // _LOG_DEBUG_ID ("PIT size: " << m_syncInterestTable.size ());
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700558 satisfyPendingSyncInterests (diff);
akmhoque66e66182014-02-21 17:56:03 -0600559}
560
561void
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700562SyncLogic::remove(const Name &prefix)
akmhoque66e66182014-02-21 17:56:03 -0600563{
564 DiffStatePtr diff;
565 {
566 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700567 m_state->remove(info);
akmhoque66e66182014-02-21 17:56:03 -0600568
569 // increment the sequence number for the forwarder node
570 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
571
572 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
573 SeqNo seqNo (0);
574 if (item != m_state->getLeaves ().end ())
575 {
576 seqNo = (*item)->getSeq ();
577 ++seqNo;
578 }
579 m_state->update (forwarderInfo, seqNo);
580
581 diff = boost::make_shared<DiffState>();
582 diff->remove(info);
583 diff->update(forwarderInfo, seqNo);
584
585 insertToDiffLog (diff);
586 }
587
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700588 satisfyPendingSyncInterests (diff);
akmhoque66e66182014-02-21 17:56:03 -0600589}
590
591void
592SyncLogic::sendSyncInterest ()
593{
594 _LOG_DEBUG_ID("sendSyncInterest");
595
596 {
597 m_outstandingInterestName = m_syncPrefix;
598 ostringstream os;
599 os << *m_state->getDigest();
600 m_outstandingInterestName.append(os.str());
601 _LOG_DEBUG_ID (">> I " << m_outstandingInterestName);
602 }
603
604 _LOG_DEBUG_ID("sendSyncInterest: " << m_outstandingInterestName);
605
akmhoque157b0a42014-05-13 00:26:37 -0500606 EventId eventId =
607 m_scheduler.scheduleEvent(ndn::time::seconds(m_syncInterestReexpress) +
608 ndn::time::milliseconds(GET_RANDOM(m_reexpressionJitter)),
609 bind (&SyncLogic::sendSyncInterest, this));
akmhoque66e66182014-02-21 17:56:03 -0600610 m_scheduler.cancelEvent (m_reexpressingInterestId);
611 m_reexpressingInterestId = eventId;
612
613 ndn::Interest interest(m_outstandingInterestName);
614 interest.setMustBeFresh(true);
615
616 m_face->expressInterest(interest,
617 bind(&SyncLogic::onSyncData, this, _1, _2),
618 bind(&SyncLogic::onSyncTimeout, this, _1));
619}
620
621void
622SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
623{
624 ostringstream os;
625 os << *digest;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700626
akmhoque66e66182014-02-21 17:56:03 -0600627 Name interestName = m_syncPrefix;
628 interestName.append("recovery").append(os.str());
629
akmhoque157b0a42014-05-13 00:26:37 -0500630 ndn::time::system_clock::Duration nextRetransmission =
631 ndn::time::milliseconds(m_recoveryRetransmissionInterval + GET_RANDOM (m_reexpressionJitter));
akmhoque66e66182014-02-21 17:56:03 -0600632
633 m_recoveryRetransmissionInterval <<= 1;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700634
akmhoque66e66182014-02-21 17:56:03 -0600635 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
636 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
akmhoque157b0a42014-05-13 00:26:37 -0500637 m_reexpressingRecoveryInterestId =
638 m_scheduler.scheduleEvent (nextRetransmission,
639 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest));
akmhoque66e66182014-02-21 17:56:03 -0600640
641 ndn::Interest interest(interestName);
642 interest.setMustBeFresh(true);
643
644 m_face->expressInterest(interest,
645 bind(&SyncLogic::onSyncData, this, _1, _2),
646 bind(&SyncLogic::onSyncTimeout, this, _1));
647}
648
649
650void
651SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, StateConstPtr state)
652{
653 SyncStateMsg msg;
654 msg << (*state);
655 sendSyncData(name, digest, msg);
656}
657
658// pass in state msg instead of state, so that there is no need to lock the state until
659// this function returns
660void
661SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, SyncStateMsg &ssm)
662{
663 _LOG_DEBUG_ID (">> D " << name);
664 int size = ssm.ByteSize();
665 char *wireData = new char[size];
666 ssm.SerializeToArray(wireData, size);
667
akmhoque69c9aa92014-07-23 15:15:05 -0500668 //Data syncData(name);
669 ndn::shared_ptr<ndn::Data> syncData = ndn::make_shared<ndn::Data>();
670 syncData->setName(name);
671 syncData->setContent(reinterpret_cast<const uint8_t*>(wireData), size);
672 syncData->setFreshnessPeriod(ndn::time::seconds(m_syncResponseFreshness));
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700673
akmhoque69c9aa92014-07-23 15:15:05 -0500674 m_keyChain->sign(*syncData);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700675
akmhoque69c9aa92014-07-23 15:15:05 -0500676 m_face->put(*syncData);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700677
akmhoque66e66182014-02-21 17:56:03 -0600678 delete []wireData;
679
680 // checking if our own interest got satisfied
681 bool satisfiedOwnInterest = false;
682 {
683 satisfiedOwnInterest = (m_outstandingInterestName == name);
684 }
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700685
akmhoque66e66182014-02-21 17:56:03 -0600686 if (satisfiedOwnInterest)
687 {
688 _LOG_DEBUG_ID ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700689
akmhoque157b0a42014-05-13 00:26:37 -0500690 ndn::time::system_clock::Duration after =
691 ndn::time::milliseconds(GET_RANDOM(m_reexpressionJitter));
akmhoque66e66182014-02-21 17:56:03 -0600692 // cout << "------------ reexpress interest after: " << after << endl;
693 EventId eventId = m_scheduler.scheduleEvent (after,
694 bind (&SyncLogic::sendSyncInterest, this));
695 m_scheduler.cancelEvent (m_reexpressingInterestId);
696 m_reexpressingInterestId = eventId;
697 }
698}
699
700string
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700701SyncLogic::getRootDigest()
akmhoque66e66182014-02-21 17:56:03 -0600702{
703 ostringstream os;
704 os << *m_state->getDigest();
705 return os.str();
706}
707
708size_t
709SyncLogic::getNumberOfBranches () const
710{
711 return m_state->getLeaves ().size ();
712}
713
714void
715SyncLogic::printState () const
716{
717 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
718 {
719 std::cout << *leaf << std::endl;
720 }
721}
722
723std::map<std::string, bool>
724SyncLogic::getBranchPrefixes() const
725{
726 std::map<std::string, bool> m;
727
728 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
729 {
730 std::string prefix = leaf->getInfo()->toString();
731 // do not return forwarder prefix
732 if (prefix != forwarderPrefix)
733 {
734 m.insert(pair<std::string, bool>(prefix, false));
735 }
736 }
737
738 return m;
739}
740
741}