Merge branch 'master' of git.irl.cs.ucla.edu:ndn/sync
diff --git a/model/sync-interest-table.cc b/model/sync-interest-table.cc
index fcb93c8..7a7a394 100644
--- a/model/sync-interest-table.cc
+++ b/model/sync-interest-table.cc
@@ -30,12 +30,47 @@
 
 unordered_set<string> SyncInterestTable::fetchAll()
 {
-  return m_table;
+	expireInterest();
+
+	recursive_mutex::scoped_lock lock(m_mutex);
+	unordered_set<string> entries;
+	for (unordered_map<string, time_t>::iterator it = m_table.begin(); it !=
+		m_table.end(); ++it) {
+		entries.insert(it->first);
+	}
+
+	return entries;
 }
 
 bool SyncInterestTable::insert(string interest)
 {
-  m_table.insert(interest);
+	recursive_mutex::scoped_lock lock(m_mutex);
+	m_table.erase(m_table.find(interest));
+	time_t currentTime = time(0);
+	m_table.insert(make_pair(interest, currentTime));
 }
 
-}
\ No newline at end of file
+void SyncInterestTable::SyncInterestTable() {
+	m_thread = thread(&SyncInterestTable::periodicCheck, this);
+}
+
+void SyncInterestTable::expireInterests() {
+	recursive_mutex::scoped_lock lock(m_mutex);
+	time_t currentTime = time(0);
+	unordered_map<string, time_t>::iterator it = m_table.begin(); 
+	while(it != m_table.end()) {
+		time_t timestamp = it->second;
+		if (currentTime - timestamp > m_checkPeriod) {
+			it = m_table.erase(it);
+		}
+		else
+			++it;
+	}
+}
+
+void SyncInterestTable::periodicCheck() {
+	sleep(4);
+	expireInterest();
+}
+
+}
diff --git a/model/sync-interest-table.h b/model/sync-interest-table.h
index 1acac15..c489909 100644
--- a/model/sync-interest-table.h
+++ b/model/sync-interest-table.h
@@ -23,7 +23,11 @@
 #ifndef SYNC_INTEREST_TABLE_H
 #define SYNC_INTEREST_TABLE_H
 #include <string>
+#include <boost/unordered_map.hpp>
 #include <boost/unordered_set.hpp>
+#include <boost/thread/recursive_mutex.hpp>
+#include <boost/thread/thread.hpp>
+#include <ctime>
 
 /**
  * \defgroup sync SYNC protocol
@@ -35,14 +39,17 @@
 /**
  * \ingroup sync
  * @brief A table to keep unanswered Sync Interest
+ * all access operation to the table should grab the 
+ * mutex first
  */
 class SyncInterestTable
 {
-private:
-	boost::unordered_set<std::string> m_table;
 public:
+	SyncInterestTable();
+
 	/**
-	 * @brief Insert an interest
+	 * @brief Insert an interest, if interest already exists, update the
+	 * timestamp
 	 */
 	bool insert(std::string interest);
 
@@ -51,6 +58,20 @@
 	 */
 	boost::unordered_set<std::string> fetchAll();
 
+private:
+	/**
+	 * @brief periodically called to expire Interest
+	 */
+	void expireInterests();
+
+	void periodicCheck();
+
+private:
+	static int m_checkPeriod = 4;
+	boost::unordered_map<std::string, time_t> m_table; // pit entries
+	boost::thread m_thread; // thread to check every 4 sec
+	boost::recursive_mutex m_mutex;
+
 };
 
 } // Sync