Leaf::getInfo () now returns boost::shared_ptr<const NameInfo> instead of NameInfo reference as before.
SyncLogic is temporarily broken
diff --git a/model/sync-diff-state-container.h b/model/sync-diff-state-container.h
index 0f70437..9349e76 100644
--- a/model/sync-diff-state-container.h
+++ b/model/sync-diff-state-container.h
@@ -40,14 +40,8 @@
namespace Sync {
-struct DigestHash : public std::unary_function<Digest, std::size_t>
+struct DigestPtrHash : public std::unary_function<Digest, std::size_t>
{
- // std::size_t
- // operator() (const Digest &digest) const
- // {
- // return digest.getHash ();
- // }
-
std::size_t
operator() (DigestConstPtr digest) const
{
@@ -55,6 +49,16 @@
}
};
+struct DigestPtrEqual : public std::unary_function<Digest, std::size_t>
+{
+ bool
+ operator() (DigestConstPtr digest1, DigestConstPtr digest2) const
+ {
+ return *digest1 == *digest2;
+ }
+};
+
+
/// @cond include_hidden
struct sequenced { };
/// @endcond
@@ -70,7 +74,8 @@
mi::hashed_unique<
mi::tag<hashed>,
mi::const_mem_fun<DiffState, DigestConstPtr, &DiffState::getDigest>,
- DigestHash
+ DigestPtrHash,
+ DigestPtrEqual
>
,
// sequenced index to access older/newer element (like in list)
diff --git a/model/sync-diff-state.cc b/model/sync-diff-state.cc
index 75e0be5..65323d3 100644
--- a/model/sync-diff-state.cc
+++ b/model/sync-diff-state.cc
@@ -61,22 +61,26 @@
}
// from State
-void
+bool
DiffState::update (NameInfoConstPtr info, const SeqNo &seq)
{
- m_leaves.erase (*info);
+ m_leaves.erase (info);
DiffLeafPtr leaf = make_shared<DiffLeaf> (info, cref (seq));
m_leaves.insert (leaf);
+
+ return true;
}
-void
+bool
DiffState::remove (NameInfoConstPtr info)
{
- m_leaves.erase (*info);
+ m_leaves.erase (info);
DiffLeafPtr leaf = make_shared<DiffLeaf> (info);
m_leaves.insert (leaf);
+
+ return true;
}
} // ns3
diff --git a/model/sync-diff-state.h b/model/sync-diff-state.h
index 7a889c4..6a43aef 100644
--- a/model/sync-diff-state.h
+++ b/model/sync-diff-state.h
@@ -86,10 +86,10 @@
operator += (const DiffState &state);
// from State
- virtual void
+ virtual bool
update (NameInfoConstPtr info, const SeqNo &seq);
- virtual void
+ virtual bool
remove (NameInfoConstPtr info);
private:
diff --git a/model/sync-full-leaf.cc b/model/sync-full-leaf.cc
index ee1d3dc..3636860 100644
--- a/model/sync-full-leaf.cc
+++ b/model/sync-full-leaf.cc
@@ -37,7 +37,7 @@
FullLeaf::updateDigest ()
{
m_digest.reset ();
- m_digest << getInfo ().getDigest () << *getSeq ().getDigest ();
+ m_digest << getInfo ()->getDigest () << *getSeq ().getDigest ();
m_digest.finalize ();
}
diff --git a/model/sync-full-state.cc b/model/sync-full-state.cc
index e8a2149..ed3a3ee 100644
--- a/model/sync-full-state.cc
+++ b/model/sync-full-state.cc
@@ -81,7 +81,7 @@
}
// from State
-void
+bool
FullState::update (NameInfoConstPtr info, const SeqNo &seq)
{
#ifndef STANDALONE
@@ -92,18 +92,23 @@
m_digest.reset ();
- LeafContainer::iterator item = m_leaves.find (*info);
+ LeafContainer::iterator item = m_leaves.find (info);
if (item == m_leaves.end ())
{
m_leaves.insert (make_shared<FullLeaf> (info, cref (seq)));
}
else
{
- m_leaves.modify (item, ll::bind (&Leaf::setSeq, *ll::_1, seq));
+ if ((*item)->getSeq () == seq || seq < (*item)->getSeq ())
+ return false;
+
+ m_leaves.modify (item,
+ ll::bind (&Leaf::setSeq, *ll::_1, seq));
}
+ return true;
}
-void
+bool
FullState::remove (NameInfoConstPtr info)
{
#ifndef STANDALONE
@@ -114,7 +119,14 @@
m_digest.reset ();
- m_leaves.erase (*info);
+ LeafContainer::iterator item = m_leaves.find (info);
+ if (item != m_leaves.end ())
+ {
+ m_leaves.erase (info);
+ return true;
+ }
+ else
+ return false;
}
} // Sync
diff --git a/model/sync-full-state.h b/model/sync-full-state.h
index 9769878..2aac7b9 100644
--- a/model/sync-full-state.h
+++ b/model/sync-full-state.h
@@ -67,10 +67,10 @@
getDigest ();
// from State
- virtual void
+ virtual bool
update (NameInfoConstPtr info, const SeqNo &seq);
- virtual void
+ virtual bool
remove (NameInfoConstPtr info);
private:
diff --git a/model/sync-leaf.h b/model/sync-leaf.h
index 00ae151..5970709 100644
--- a/model/sync-leaf.h
+++ b/model/sync-leaf.h
@@ -46,8 +46,8 @@
/**
* @brief Get name of the leaf
*/
- const NameInfo &
- getInfo () const { return *m_info; }
+ NameInfoConstPtr
+ getInfo () const { return m_info; }
/**
* @brief Get sequence number of the leaf
diff --git a/model/sync-logic.cc b/model/sync-logic.cc
index a3fe621..6b11c9a 100644
--- a/model/sync-logic.cc
+++ b/model/sync-logic.cc
@@ -39,10 +39,10 @@
SyncLogic::SyncLogic (const string &syncPrefix,
- LogicCallback fetch,
+ LogicCallback fetchCallback,
CcnxWrapperPtr ccnxHandle)
: m_syncPrefix (syncPrefix)
- , m_fetch (fetch)
+ , m_fetchCallback (fetchCallback)
, m_ccnxHandle (ccnxHandle)
, m_delayedCheckThreadRunning (true)
{
@@ -173,9 +173,8 @@
SyncLogic::processSyncData (const string &name, const string &dataBuffer)
{
string last = name.substr(name.find_last_of("/") + 1);
- stringstream ss(dataBuffer);
+ istringstream ss (dataBuffer);
- const LeafContainer &fullLc = m_state.getLeaves();
DiffStatePtr diffLog = make_shared<DiffState>();
if (last == "state")
@@ -184,39 +183,38 @@
ss >> full;
BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves().get<ordered>())
{
- shared_ptr<const FullLeaf> fullLeaf = dynamic_pointer_cast<const FullLeaf>(leaf);
- const NameInfo &info = fullLeaf->getInfo();
- LeafContainer::iterator it = fullLc.find(info);
- NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
- SeqNo seq = fullLeaf->getSeq();
+ NameInfoConstPtr info = leaf->getInfo ();
+ LeafContainer::iterator it = m_state.getLeaves().find (info);
- if (it == fullLc.end())
- {
- string prefix = info.toString();
- prefix += "/";
- prefix += seq.getSession();
- m_fetch(prefix, 1, seq.getSeq());
- m_state.update(pInfo, seq);
- diffLog->update(pInfo, seq);
- }
- else
- {
- SeqNo currSeq = (*it)->getSeq();
- if (currSeq < seq)
- {
- string prefix = info.toString();
- prefix += "/";
- prefix += seq.getSession();
+ SeqNo seq = leaf->getSeq ();
- if (currSeq.getSession() == seq.getSession())
- m_fetch(prefix, currSeq.getSeq() + 1, seq.getSeq());
- else
- m_fetch(prefix, 1, seq.getSeq());
+ // if (it == m_state.getLeaves().end())
+ // {
+ // string prefix = info.toString();
+ // prefix += "/";
+ // prefix += seq.getSession();
+ // m_fetchCallback (prefix, 1, seq.getSeq());
+ // m_state.update(pInfo, seq);
+ // diffLog->update(pInfo, seq);
+ // }
+ // else
+ // {
+ // SeqNo currSeq = (*it)->getSeq();
+ // if (currSeq < seq)
+ // {
+ // string prefix = info.toString();
+ // prefix += "/";
+ // prefix += seq.getSession();
- m_state.update(pInfo, seq);
- diffLog->update(pInfo, seq);
- }
- }
+ // if (currSeq.getSession() == seq.getSession())
+ // m_fetchCallback(prefix, currSeq.getSeq() + 1, seq.getSeq());
+ // else
+ // m_fetchCallback(prefix, 1, seq.getSeq());
+
+ // m_state.update(pInfo, seq);
+ // diffLog->update(pInfo, seq);
+ // }
+ // }
}
}
else
@@ -225,63 +223,74 @@
ss >> diff;
BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
{
- shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf>(leaf);
- const NameInfo &info = diffLeaf->getInfo();
- LeafContainer::iterator it = fullLc.find(info);
+ shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
+ if (diffLeaf == 0)
+ {
+ return;
+ /// \todo Log the error
+ }
+ NameInfoConstPtr info = diffLeaf->getInfo();
+ LeafContainer::iterator it = m_state.getLeaves().find (info);
SeqNo seq = diffLeaf->getSeq();
- switch (diffLeaf->getOperation())
- {
- case UPDATE:
- if (it == fullLc.end())
- {
- string prefix = info.toString();
- prefix += "/";
- prefix += seq.getSession();
- m_fetch(prefix, 1, seq.getSeq());
+ // switch (diffLeaf->getOperation())
+ // {
+ // case UPDATE:
+ // if (it == m_state.getLeaves().end())
+ // {
+ // string prefix = info.toString();
+ // prefix += "/";
+ // prefix += seq.getSession();
+ // m_fetchCallback(prefix, 1, seq.getSeq());
- NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
- m_state.update(pInfo, seq);
- diffLog->update(pInfo, seq);
- }
- else
- {
- SeqNo currSeq = (*it)->getSeq();
- if (currSeq < seq)
- {
- string prefix = info.toString();
- prefix += "/";
- prefix += seq.getSession();
+ // NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
+ // m_state.update(pInfo, seq);
+ // diffLog->update(pInfo, seq);
+ // }
+ // else
+ // {
+ // SeqNo currSeq = (*it)->getSeq();
+ // if (currSeq < seq)
+ // {
+ // string prefix = info.toString();
+ // prefix += "/";
+ // prefix += seq.getSession();
- if (currSeq.getSession() == seq.getSession())
- m_fetch(prefix, currSeq.getSeq() + 1, seq.getSeq());
- else
- m_fetch(prefix, 1, seq.getSeq());
+ // if (currSeq.getSession() == seq.getSession())
+ // m_fetchCallback(prefix, currSeq.getSeq() + 1, seq.getSeq());
+ // else
+ // m_fetchCallback(prefix, 1, seq.getSeq());
- NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
- m_state.update(pInfo, seq);
- diffLog->update(pInfo, seq);
- }
- }
- break;
+ // NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
+ // m_state.update(pInfo, seq);
+ // diffLog->update(pInfo, seq);
+ // }
+ // }
+ // break;
- case REMOVE:
- if (it != fullLc.end())
- {
- NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
- m_state.remove(pInfo);
- diffLog->remove(pInfo);
- }
- break;
+ // case REMOVE:
+ // if (it != m_state.getLeaves().end())
+ // {
+ // NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
+ // m_state.remove(pInfo);
+ // diffLog->remove(pInfo);
+ // }
+ // break;
- default:
- break;
- }
+ // default:
+ // break;
+ // }
}
}
diffLog->setDigest(m_state.getDigest());
- m_log.insert(diffLog);
+ m_log.insert (diffLog);
+
+ // notify upper layer
+ BOOST_FOREACH (LeafConstPtr leaf, diffLog->getLeaves ())
+ {
+ }
+
sendSyncInterest();
}
@@ -301,7 +310,7 @@
ss << *diff;
for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
{
- m_ccnxHandle->publishData(*ii, ss.str(), m_syncResponseFreshness);
+ m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
}
}
diff --git a/model/sync-logic.h b/model/sync-logic.h
index b624ecb..42a2ae0 100644
--- a/model/sync-logic.h
+++ b/model/sync-logic.h
@@ -101,7 +101,7 @@
SyncInterestTable m_syncInterestTable;
std::string m_syncPrefix;
- LogicCallback m_fetch;
+ LogicCallback m_fetchCallback;
CcnxWrapperPtr m_ccnxHandle;
boost::thread m_delayedCheckThread;
diff --git a/model/sync-state-leaf-container.h b/model/sync-state-leaf-container.h
index 9ae98b0..7111ff6 100644
--- a/model/sync-state-leaf-container.h
+++ b/model/sync-state-leaf-container.h
@@ -42,9 +42,27 @@
struct NameInfoHash : public std::unary_function<NameInfo, std::size_t>
{
std::size_t
- operator() (const NameInfo &prefix) const
+ operator() (NameInfoConstPtr prefix) const
{
- return prefix.getHashId ();
+ return prefix->getHashId ();
+ }
+};
+
+struct NameInfoEqual : public std::unary_function<NameInfo, std::size_t>
+{
+ bool
+ operator() (NameInfoConstPtr prefix1, NameInfoConstPtr prefix2) const
+ {
+ return *prefix1 == *prefix2;
+ }
+};
+
+struct NameInfoCompare : public std::unary_function<NameInfo, std::size_t>
+{
+ bool
+ operator() (NameInfoConstPtr prefix1, NameInfoConstPtr prefix2) const
+ {
+ return *prefix1 < *prefix2;
}
};
@@ -63,13 +81,16 @@
// For fast access to elements using NameInfo
mi::hashed_unique<
mi::tag<hashed>,
- mi::const_mem_fun<Leaf, const NameInfo&, &Leaf::getInfo>,
- NameInfoHash
+ mi::const_mem_fun<Leaf, NameInfoConstPtr, &Leaf::getInfo>,
+ NameInfoHash,
+ NameInfoEqual
>,
- mi::ordered_unique<
- mi::tag<ordered>,
- mi::const_mem_fun<Leaf, const NameInfo&, &Leaf::getInfo>
- >
+
+ mi::ordered_unique<
+ mi::tag<ordered>,
+ mi::const_mem_fun<Leaf, NameInfoConstPtr, &Leaf::getInfo>,
+ NameInfoCompare
+ >
>
>
{
diff --git a/model/sync-state.cc b/model/sync-state.cc
index 4befe62..c021d0e 100644
--- a/model/sync-state.cc
+++ b/model/sync-state.cc
@@ -64,7 +64,7 @@
{
os << "<item>"; DEBUG_ENDL;
}
- os << "<name>" << leaf->getInfo () << "</name>"; DEBUG_ENDL;
+ os << "<name>" << *leaf->getInfo () << "</name>"; DEBUG_ENDL;
if (diffLeaf == 0 || (diffLeaf != 0 && diffLeaf->getOperation () == UPDATE))
{
os << "<seq>" << leaf->getSeq () << "</seq>"; DEBUG_ENDL;
diff --git a/model/sync-state.h b/model/sync-state.h
index bfe2517..2806fdb 100644
--- a/model/sync-state.h
+++ b/model/sync-state.h
@@ -48,14 +48,14 @@
* @param info name of the leaf
* @param seq sequence number of the leaf
*/
- virtual void
+ virtual bool
update (NameInfoConstPtr info, const SeqNo &seq) = 0;
/**
* @brief Remove leaf from the state tree
* @param info name of the leaf
*/
- virtual void
+ virtual bool
remove (NameInfoConstPtr info) = 0;
/**
diff --git a/test/test_state.cc b/test/test_state.cc
index 7732798..00ff467 100644
--- a/test/test_state.cc
+++ b/test/test_state.cc
@@ -156,6 +156,7 @@
ostringstream os;
os << state;
string s = os.str ();
+ // cout << s << endl;
erase_all (s, "\n");
BOOST_CHECK_EQUAL (s, xml1);
}