Introducing a separate Scheduler class
SyncLogic almost works...
diff --git a/model/sync-logic.cc b/model/sync-logic.cc
index e39b86b..9491a5c 100644
--- a/model/sync-logic.cc
+++ b/model/sync-logic.cc
@@ -35,9 +35,6 @@
namespace Sync
{
-const boost::posix_time::time_duration SyncLogic::m_delayedCheckTime = boost::posix_time::seconds (4.0);
-
-
SyncLogic::SyncLogic (const std::string &syncPrefix,
LogicUpdateCallback onUpdate,
LogicRemoveCallback onRemove,
@@ -46,66 +43,17 @@
, m_onUpdate (onUpdate)
, m_onRemove (onRemove)
, m_ccnxHandle (ccnxHandle)
- , m_delayedCheckThreadRunning (true)
+ , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
+ , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (20,100))
{
- srandom(time(NULL));
m_ccnxHandle->setInterestFilter (syncPrefix,
bind (&SyncLogic::respondSyncInterest, this, _1));
-
- m_delayedCheckThread = thread (&SyncLogic::delayedChecksLoop, this);
}
SyncLogic::~SyncLogic ()
{
- m_delayedCheckThreadRunning = false;
- // cout << "Requested stop" << this_thread::get_id () << endl;
- m_delayedCheckThread.interrupt ();
- m_delayedCheckThread.join ();
}
-void
-SyncLogic::delayedChecksLoop ()
-{
- while (m_delayedCheckThreadRunning)
- {
- try
- {
- DelayedChecksList::value_type tuple;
-
- {
- unique_lock<mutex> lock (m_listChecksMutex);
- while (m_delayedCheckThreadRunning && m_listChecks.size () == 0)
- {
- m_listChecksCondition.wait (lock);
- // cout << "Got something" << endl;
- }
-
- if (m_listChecks.size () == 0) continue;
-
- tuple = m_listChecks.front ();
- m_listChecks.pop_front ();
- // cout << "pop" << endl;
- // release the mutex
- }
-
- // waiting and calling
-
- // cout << "Duration: " << tuple.get<0> () - get_system_time () << endl;
- this_thread::sleep (tuple.get<0> ());
-
- if (!m_delayedCheckThreadRunning) continue;
- tuple.get<1> () (); // call the scheduled function
- }
- catch (thread_interrupted e)
- {
- // cout << "interrupted: " << this_thread::get_id () << endl;
- // do nothing
- }
- }
- // cout << "Exited...\n";
-}
-
-
void
SyncLogic::respondSyncInterest (const string &interest)
@@ -150,19 +98,9 @@
if (!timedProcessing)
{
- {
- // Alex: Should we ignore interests if interest with the same digest is already in the wait queue?
-
- lock_guard<mutex> lock (m_listChecksMutex);
- system_time delay = get_system_time () + m_delayedCheckTime;
- // do we need randomization??
- // delay += boost::ptime::milliseconds (rand() % 80 + 20);
+ m_delayedChecksScheduler.schedule (posix_time::milliseconds (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
+ bind (&SyncLogic::processSyncInterest, this, digest, interestName, true));
- m_listChecks.push_back (make_tuple (delay,
- bind (&SyncLogic::processSyncInterest, this, digest, interestName, true))
- );
- }
- m_listChecksCondition.notify_one ();
}
else
{