in process of writing sync-core
diff --git a/src/sync-core.cc b/src/sync-core.cc
index d0312d0..21201aa 100644
--- a/src/sync-core.cc
+++ b/src/sync-core.cc
@@ -22,7 +22,113 @@
#include "sync-core.h"
SyncCore::SyncCore
- : m_log(path, localName)
+ : m_log(path, userName)
+ , m_localPrefix(localPrefix)
+ , m_syncPrefix(syncPrefix)
+ , m_stateMsgCallback(callback)
, m_handle(handle)
+ , m_scheduler(scheduler)
{
+ m_rootHash = m_log.RememberStateInStateLog();
+ m_interestClosure = new Closure(0, boost::bind(&SyncCore::handleSyncData, this, _1, _2), boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1));
+ m_scheduler->start();
+ sendSyncInterest();
+}
+
+SyncCore::~SyncCore()
+{
+ m_scheduler->stop();
+ delete m_interestClosure;
+ m_interestClosure = 0;
+}
+
+void
+SyncCore::updateLocalPrefix(const Name &localPrefix)
+{
+ m_localPrefix = localPrefix;
+ // optionally, we can have a sync action to announce the new prefix
+ // we are not doing this for now
+}
+
+void
+SyncCore::updateLocalState(seqno_t seqno)
+{
+ m_log.UpdateDeviceSeqNo(seqno);
+ HashPtr oldHash = m_rootHash;
+ m_rootHash = m_log.RememberStateInStateLog();
+
+ SyncStateMsgPtr msg = m_log.FindStateDifferences(oldHash, m_rootHash);
+
+ // reply sync Interest with oldHash as last component
+ Name syncName = constructSyncName(oldHash);
+ Bytes syncData;
+ msgToBytes(msg, syncData);
+ m_handle->publishData(syncName, syncData, FRESHNESS);
+
+ // no hurry in sending out new Sync Interest; if others send the new Sync Interest first, no problem, we know the new root hash already;
+ // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers
+ ostringstream ss;
+ ss << m_rootHash;
+ TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.05));
+ m_scheduler->addTask(task);
+}
+
+void
+SyncCore::handleSyncInterest(const Name &name)
+{
+}
+
+Closure::TimeoutCallbackReturnValue
+SyncCore::handleSyncInterestTimeout(const Name &name)
+{
+ // sendInterestInterest with the current root hash;
+ sendSyncInterest();
+ return Closure::OK;
+}
+
+void
+SyncCore::handleSyncData(const Name &name, const Bytes &content)
+{
+ SyncStateMsgPtr msg(new SyncStateMsg);
+ bool success = msg->ParseFromArray(head(content), content.size());
+ if(!success)
+ {
+ // ignore misformed SyncData
+ cerr << "Misformed SyncData with name: " << name << endl;
+ return;
+ }
+
+ int size = msg->state_size();
+ int index = 0;
+ while (index < size)
+ {
+ SyncState state = msg->state(index);
+ index++;
+ }
+
+}
+
+void
+SyncCore::sendSyncInterest()
+{
+ Name syncInterest = constructSyncName(m_rootHash);
+ sendInterest(syncInterest, m_interestClosure);
+}
+
+Name
+SyncCore::constructSyncName(const HashPtr &hash)
+{
+ Bytes bytes;
+ readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes);
+ Name syncName = m_syncPrefix;
+ syncName.append(bytes);
+ return syncName;
+}
+
+void
+SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
+{
+ int size = msg.ByteSize();
+ bytes.resize(size);
+ msg.SerializeToArray(head(bytes), size);
}