Debugging NameInfo, FullState, and DiffState
Now NameInfo is thread-safe and has automatic garbage collector.
Internal container is managed by weak_ptr instead of shared_ptr as it
was before.
diff --git a/model/sync-full-state.cc b/model/sync-full-state.cc
index 801bfbf..b30d2d4 100644
--- a/model/sync-full-state.cc
+++ b/model/sync-full-state.cc
@@ -64,14 +64,18 @@
{
if (m_digest == 0)
{
+ // std::cout << "getDigest: ";
m_digest = make_shared<Digest> ();
BOOST_FOREACH (LeafConstPtr leaf, m_leaves.get<ordered> ())
{
FullLeafConstPtr fullLeaf = dynamic_pointer_cast<const FullLeaf> (leaf);
BOOST_ASSERT (fullLeaf != 0);
*m_digest << fullLeaf->getDigest ();
+ // std::cout << *leaf << "[" << fullLeaf->getDigest () << "] ";
}
+ // std::cout << "\n";
}
+ m_digest->finalize ();
return m_digest;
}
diff --git a/model/sync-leaf.h b/model/sync-leaf.h
index 037e4a5..00ae151 100644
--- a/model/sync-leaf.h
+++ b/model/sync-leaf.h
@@ -72,6 +72,13 @@
typedef boost::shared_ptr<Leaf> LeafPtr;
typedef boost::shared_ptr<const Leaf> LeafConstPtr;
+inline std::ostream &
+operator << (std::ostream &os, const Leaf &leaf)
+{
+ os << leaf.getInfo () << "(" << leaf.getSeq () << ")";
+ return os;
+}
+
} // Sync
#endif // SYNC_LEAF_H
diff --git a/model/sync-name-info.cc b/model/sync-name-info.cc
index 8f730e3..270e6fc 100644
--- a/model/sync-name-info.cc
+++ b/model/sync-name-info.cc
@@ -22,18 +22,12 @@
#include "sync-name-info.h"
-#include <boost/lexical_cast.hpp>
+// #include <boost/lexical_cast.hpp>
namespace Sync {
NameInfo::NameMap NameInfo::m_names;
size_t NameInfo::m_ids = 0;
-
-/**
- * @brief Calculates digest of the name
- */
-// tempalte<>
-// Digest
-// NameInfo::getDigest<PrefixInfo> () const;
+boost::mutex NameInfo::m_namesMutex;
} // Sync
diff --git a/model/sync-name-info.h b/model/sync-name-info.h
index ebe946b..a4d7e7d 100644
--- a/model/sync-name-info.h
+++ b/model/sync-name-info.h
@@ -24,6 +24,8 @@
#define SYNC_NAME_INFO_H
#include <boost/shared_ptr.hpp>
+#include <boost/weak_ptr.hpp>
+#include <boost/thread/mutex.hpp>
#include <map>
#include <string>
#include "sync-digest.h"
@@ -37,7 +39,7 @@
class NameInfo
{
private:
- typedef boost::shared_ptr<const NameInfo> const_ptr;
+ typedef boost::weak_ptr<const NameInfo> const_weak_ptr;
public:
virtual ~NameInfo () { };
@@ -74,21 +76,28 @@
*/
virtual std::string
toString () const = 0;
-
+
protected:
// actual stuff
size_t m_id; ///< @brief Identifies NameInfo throughout the library (for hash container, doesn't need to be strictly unique)
Digest m_digest;
// static stuff
- typedef std::map<std::string, const_ptr> NameMap;
+ typedef std::map<std::string, const_weak_ptr> NameMap;
static size_t m_ids;
static NameMap m_names;
+ static boost::mutex m_namesMutex;
};
typedef boost::shared_ptr<NameInfo> NameInfoPtr;
typedef boost::shared_ptr<const NameInfo> NameInfoConstPtr;
+inline std::ostream &
+operator << (std::ostream &os, const NameInfo &info)
+{
+ os << info.toString ();
+ return os;
+}
} // Sync
diff --git a/model/sync-ns3-name-info.cc b/model/sync-ns3-name-info.cc
index d1c8e31..9e0ef78 100644
--- a/model/sync-ns3-name-info.cc
+++ b/model/sync-ns3-name-info.cc
@@ -27,6 +27,7 @@
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
+#include <boost/make_shared.hpp>
#include <utility>
using namespace std;
@@ -40,11 +41,17 @@
{
string key = lexical_cast<string> (*name);
- NameInfoPtr value = NameInfoPtr (new Ns3NameInfo (name));
- pair<NameMap::iterator,bool> item =
- m_names.insert (make_pair (key, value));
+ NameMap::iterator item = m_names.find (key);
+ if (item == m_names.end ())
+ {
+ NameInfoPtr value = NameInfoPtr (new Ns3NameInfo (name));
+ pair<NameMap::iterator,bool> inserted =
+ m_names.insert (make_pair (key, value));
+ BOOST_ASSERT (inserted.second); // previous call has to insert value
+ item = inserted.first;
+ }
- return item.first->second;
+ return item->second;
}
Ns3NameInfo::Ns3NameInfo (ns3::Ptr<const ns3::CcnxNameComponents> name)
diff --git a/model/sync-seq-no.h b/model/sync-seq-no.h
index 354a1b6..ae7f355 100644
--- a/model/sync-seq-no.h
+++ b/model/sync-seq-no.h
@@ -139,6 +139,13 @@
uint32_t m_seq;
};
+inline std::ostream &
+operator << (std::ostream &os, const SeqNo &seqno)
+{
+ os << seqno.getSession () << ":" << seqno.getSeq ();
+ return os;
+}
+
} // Sync
#endif // SYNC_SEQ_NO_H
diff --git a/model/sync-state.cc b/model/sync-state.cc
index 2680859..268a45f 100644
--- a/model/sync-state.cc
+++ b/model/sync-state.cc
@@ -19,3 +19,10 @@
* 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
* Alexander Afanasyev <alexander.afanasyev@ucla.edu>
*/
+
+#include "sync-state.h"
+#include <boost/assert.hpp>
+
+namespace Sync {
+
+}
diff --git a/model/sync-state.h b/model/sync-state.h
index 0576af2..03d4ec4 100644
--- a/model/sync-state.h
+++ b/model/sync-state.h
@@ -52,7 +52,6 @@
/**
* @brief Remove leaf from the state tree
- *
* @param info name of the leaf
*/
virtual void
diff --git a/model/sync-std-name-info.cc b/model/sync-std-name-info.cc
index 53fcdac..762ecfd 100644
--- a/model/sync-std-name-info.cc
+++ b/model/sync-std-name-info.cc
@@ -21,8 +21,10 @@
*/
#include "sync-std-name-info.h"
+#include "boost/thread/locks.hpp"
using namespace std;
+using namespace boost;
namespace Sync {
@@ -30,11 +32,30 @@
NameInfoConstPtr
StdNameInfo::FindOrCreate (const std::string &key)
{
- NameInfoPtr value = NameInfoPtr (new StdNameInfo (key));
- pair<NameMap::iterator,bool> item =
- m_names.insert (make_pair (key, value));
+ mutex::scoped_lock namesLock (m_namesMutex);
+
+ // std::cout << "FindOrCreate: " << m_names.size () << "\n";
+
+ NameInfoConstPtr ret;
+
+ NameMap::iterator item = m_names.find (key);
+ if (item != m_names.end ())
+ {
+ ret = item->second.lock ();
+ BOOST_ASSERT (ret != 0);
+ }
+ else
+ {
+ ret = NameInfoPtr (new StdNameInfo (key));
+ weak_ptr<const NameInfo> value (ret);
+ pair<NameMap::iterator,bool> inserted =
+ m_names.insert (make_pair (key, value));
+
+ BOOST_ASSERT (inserted.second); // previous call has to insert value
+ item = inserted.first;
+ }
- return item.first->second;
+ return ret;
}
StdNameInfo::StdNameInfo (const std::string &name)
@@ -43,6 +64,16 @@
m_id = m_ids ++; // set ID for a newly inserted element
m_digest << name;
m_digest.getHash (); // finalize digest
+
+ // std::cout << "StdNameInfo: " << name << " = " << m_id << "\n";
+}
+
+StdNameInfo::~StdNameInfo ()
+{
+ mutex::scoped_lock namesLock (m_namesMutex);
+
+ // cout << "Destructor for " << m_name << "\n";
+ m_names.erase (toString ());
}
string
diff --git a/model/sync-std-name-info.h b/model/sync-std-name-info.h
index 1821fb4..a70af38 100644
--- a/model/sync-std-name-info.h
+++ b/model/sync-std-name-info.h
@@ -38,7 +38,10 @@
static NameInfoConstPtr
FindOrCreate (const std::string &name);
- virtual ~StdNameInfo () { };
+ /**
+ * @brief Destructor which will clean up m_names structure
+ */
+ virtual ~StdNameInfo ();
// from NameInfo
virtual bool