Implementing hash indexing of DiffStateContainer
diff --git a/model/sync-app.cc b/model/sync-app.cc
deleted file mode 100644
index 2680859..0000000
--- a/model/sync-app.cc
+++ /dev/null
@@ -1,21 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012 University of California, Los Angeles
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
- *         卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
- *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
- */
diff --git a/model/sync-app.h b/model/sync-app.h
deleted file mode 100644
index 9f130c3..0000000
--- a/model/sync-app.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012 University of California, Los Angeles
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
- *         卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
- *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
- */
-
-#ifndef SYNC_APP_H
-#define SYNC_APP_H
-
-namespace Sync
-{
-
-class App
-{
-public:
-private:
-};
-
-} // Sync
-
-#endif // SYNC_APP_H
diff --git a/model/sync-diff-state-container.h b/model/sync-diff-state-container.h
index 56cb95c..0f70437 100644
--- a/model/sync-diff-state-container.h
+++ b/model/sync-diff-state-container.h
@@ -24,9 +24,10 @@
 #define SYNC_DIFF_STATE_CONTAINER_H
 
 #include "sync-diff-state.h"
+#include "sync-digest.h"
 
 #include <boost/multi_index_container.hpp>
-// #include <boost/multi_index/tag.hpp>
+#include <boost/multi_index/tag.hpp>
 // #include <boost/multi_index/ordered_index.hpp>
 // #include <boost/multi_index/composite_key.hpp>
 #include <boost/multi_index/hashed_index.hpp>
@@ -39,14 +40,20 @@
 
 namespace Sync {
 
-// struct DigestHash : public std::unary_function<Digest, std::size_t>
-// {
-//   std::size_t
-//   operator() (const Digest &digest) const
-//   {
-//     return digest % std::limits<std::size_t>::max ();
-//   }
-// };
+struct DigestHash : 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
+  {
+    return digest->getHash ();
+  }
+};
 
 /// @cond include_hidden 
 struct sequenced { };
@@ -57,24 +64,22 @@
  * @brief Container for differential states
  */
 struct DiffStateContainer : public mi::multi_index_container<
-    DiffStatePtr,
-    // mi::indexed_by<
-    //   // For fast access to elements using DiffState hashes
-    //   mi::hashed_unique<
-    //     mi::tag<hashed>,
-    //     mi::const_mem_fun<Leaf, const Digest&, &DiffState::getDigest>,
-    //     DigestHash
-    //   >,
-
-  // sequenced index to access older/newer element (like in list)
-      mi::indexed_by<
-        mi::sequenced<mi::tag<sequenced> >
+  DiffStatePtr,
+  mi::indexed_by<
+    // For fast access to elements using DiffState hashes
+    mi::hashed_unique<
+      mi::tag<hashed>,
+      mi::const_mem_fun<DiffState, DigestConstPtr, &DiffState::getDigest>,
+      DigestHash
+      >
+    ,        
+    // sequenced index to access older/newer element (like in list)
+    mi::sequenced<mi::tag<sequenced> >
     >
-   >
+  >
 {
 };
 
-
 } // Sync
 
 #endif // SYNC_DIFF_STATE_CONTAINER_H
diff --git a/model/sync-diff-state.h b/model/sync-diff-state.h
index ac35625..7a889c4 100644
--- a/model/sync-diff-state.h
+++ b/model/sync-diff-state.h
@@ -67,7 +67,7 @@
    */
   DigestConstPtr
   getDigest () const { return m_digest; }
-
+  
   /**
    * @brief Accumulate differences from `this' state to the most current state
    * @returns Accumulated differences from `this' state to the most current state
diff --git a/model/sync-digest.cc b/model/sync-digest.cc
index 9b1c536..705a467 100644
--- a/model/sync-digest.cc
+++ b/model/sync-digest.cc
@@ -161,10 +161,12 @@
 }
   
 std::size_t
-Digest::getHash ()
+Digest::getHash () const
 {
   if (m_buffer == 0)
-    finalize ();
+    BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
+                           << errmsg_info_str ("Digest has not been yet finalized"));
+  // finalize ();
 
   if (sizeof (std::size_t) > m_hashLength)
     BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
diff --git a/model/sync-digest.h b/model/sync-digest.h
index 528180d..15c7278 100644
--- a/model/sync-digest.h
+++ b/model/sync-digest.h
@@ -64,7 +64,7 @@
    * Side effect: finalize() will be called on `this'
    */
   std::size_t
-  getHash ();
+  getHash () const;
 
   /**
    * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception
diff --git a/model/sync-interest-table.h b/model/sync-interest-table.h
index 6fc258e..f850708 100644
--- a/model/sync-interest-table.h
+++ b/model/sync-interest-table.h
@@ -23,6 +23,7 @@
 #ifndef SYNC_INTEREST_TABLE_H
 #define SYNC_INTEREST_TABLE_H
 #include <string>
+#include <vector>
 #include <boost/unordered_map.hpp>
 #include <boost/unordered_set.hpp>
 #include <boost/thread/recursive_mutex.hpp>
diff --git a/model/sync-logic.cc b/model/sync-logic.cc
index 6ba2c7a..754c995 100644
--- a/model/sync-logic.cc
+++ b/model/sync-logic.cc
@@ -179,19 +179,19 @@
 SyncLogic::respondSyncInterest (const string &interest)
 {
   string hash = interest.substr(interest.find_last_of("/") + 1);
-  Digest digest;
-  digest << hash;
+  DigestPtr digest = make_shared<Digest> ();
+  *digest << hash;
+  digest->finalize ();
 
-  if (*m_state.getDigest() == digest)
+  if (*m_state.getDigest() == *digest)
   {
-    m_syncInterestTable.insert(interest);
+    m_syncInterestTable.insert (interest);
     return;
   }
-/*
-  DiffStateContainer::index<hashed>::type& idx = m_log.get<hashed> ();
-  DiffStateContainer::iterator ii = idx.find(digest);
 
-  if (ii != idx.end())
+  DiffStateContainer::iterator ii = m_log.find (digest);
+  
+  if (ii != m_log.end())
   {
     stringstream ss;
     ss << *(*ii)->diff();
@@ -200,17 +200,17 @@
   else
   {
     int wait = rand() % 80 + 20;
-    sleep(wait/1000.0);
+    sleep(wait/1000.0); // ??? sleep in this thread???
   }
-
-  if (*m_state.getDigest() == digest)
+  
+  if (*m_state.getDigest() == *digest)
   {
     m_syncInterestTable.insert(interest);
     return;
   }
 
-  ii = idx.find(digest);
-  if (ii != idx.end())
+  ii = m_log.find (digest);
+  if (ii != m_log.end ())
   {
     stringstream ss;
     ss << *(*ii)->diff();
@@ -222,7 +222,6 @@
     ss << m_state;
     m_ccnxHandle->publishData(interest + "/state", ss.str(), m_syncResponseFreshness);
   }
-  */
 }
 
 void
diff --git a/model/sync-state-leaf-container.h b/model/sync-state-leaf-container.h
index 98adb1b..9ae98b0 100644
--- a/model/sync-state-leaf-container.h
+++ b/model/sync-state-leaf-container.h
@@ -58,20 +58,20 @@
  * @brief Container for SYNC leaves
  */
 struct LeafContainer : public mi::multi_index_container<
-    LeafPtr,
-    mi::indexed_by<
-      // For fast access to elements using NameInfo
-      mi::hashed_unique<
-        mi::tag<hashed>,
-        mi::const_mem_fun<Leaf, const NameInfo&, &Leaf::getInfo>,
-        NameInfoHash
-        >,
-      mi::ordered_unique<
-        mi::tag<ordered>,
-        mi::const_mem_fun<Leaf, const NameInfo&, &Leaf::getInfo>
-        >
+  LeafPtr,
+  mi::indexed_by<
+    // For fast access to elements using NameInfo
+    mi::hashed_unique<
+      mi::tag<hashed>,
+      mi::const_mem_fun<Leaf, const NameInfo&, &Leaf::getInfo>,
+      NameInfoHash
+      >,
+        mi::ordered_unique<
+          mi::tag<ordered>,
+          mi::const_mem_fun<Leaf, const NameInfo&, &Leaf::getInfo>
+          >
     >
-   >
+  >
 {
 };
 
diff --git a/model/sync-std-name-info.cc b/model/sync-std-name-info.cc
index 762ecfd..02e4336 100644
--- a/model/sync-std-name-info.cc
+++ b/model/sync-std-name-info.cc
@@ -63,7 +63,7 @@
 {
   m_id = m_ids ++; // set ID for a newly inserted element
   m_digest << name;
-  m_digest.getHash (); // finalize digest
+  m_digest.finalize ();
 
   // std::cout << "StdNameInfo: " << name << " = " << m_id << "\n";
 }