Fix InterestTable

Change-Id: I203e9a28ec2e6c7a699df25fb00919172e20e1d0
diff --git a/src/interest-container.cpp b/src/interest-container.cpp
new file mode 100644
index 0000000..4d42321
--- /dev/null
+++ b/src/interest-container.cpp
@@ -0,0 +1,38 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2014 University of California, Los Angeles
+ *
+ * This file is part of ChronoSync, synchronization library for distributed realtime
+ * applications for NDN.
+ *
+ * ChronoSync is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ChronoSync 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
+ * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
+ * @author Chaoyi Bian <bcy@pku.edu.cn>
+ * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
+ * @author Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#include "interest-container.hpp"
+
+namespace chronosync {
+
+UnsatisfiedInterest::UnsatisfiedInterest(shared_ptr<const Interest> interest,
+                                         ndn::ConstBufferPtr digest,
+                                         bool isUnknown)
+  : interest(interest)
+  , digest(digest)
+  , isUnknown(isUnknown)
+{
+}
+
+} // namespace chronosync
diff --git a/src/interest-container.hpp b/src/interest-container.hpp
new file mode 100644
index 0000000..9b30b19
--- /dev/null
+++ b/src/interest-container.hpp
@@ -0,0 +1,82 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2014 University of California, Los Angeles
+ *
+ * This file is part of ChronoSync, synchronization library for distributed realtime
+ * applications for NDN.
+ *
+ * ChronoSync is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ChronoSync 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
+ * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
+ * @author Chaoyi Bian <bcy@pku.edu.cn>
+ * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
+ * @author Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#ifndef CHRONOSYNC_INTEREST_CONTAINER_HPP
+#define CHRONOSYNC_INTEREST_CONTAINER_HPP
+
+#include "common.hpp"
+#include "mi-tag.hpp"
+#include "diff-state-container.hpp"
+
+#include <ndn-cxx/util/time.hpp>
+#include <ndn-cxx/util/scheduler.hpp>
+
+#include <boost/multi_index_container.hpp>
+#include <boost/multi_index/tag.hpp>
+#include <boost/multi_index/hashed_index.hpp>
+#include <boost/multi_index/ordered_index.hpp>
+#include <boost/multi_index/member.hpp>
+
+namespace chronosync {
+
+namespace mi = boost::multi_index;
+
+/// @brief Entry to record unsatisfied Sync Interest
+class UnsatisfiedInterest : noncopyable
+{
+public:
+  UnsatisfiedInterest(shared_ptr<const Interest> interest,
+                      ndn::ConstBufferPtr digest,
+                      bool isUnknown = false);
+
+public:
+  shared_ptr<const Interest> interest;
+  ndn::ConstBufferPtr        digest;
+  bool                       isUnknown;
+  ndn::EventId               expirationEvent;
+};
+
+typedef boost::shared_ptr<UnsatisfiedInterest> UnsatisfiedInterestPtr;
+typedef boost::shared_ptr<const UnsatisfiedInterest> ConstUnsatisfiedInterestPtr;
+
+/**
+ * @brief Container for unsatisfied Sync Interests
+ */
+struct InterestContainer : public mi::multi_index_container<
+  UnsatisfiedInterestPtr,
+  mi::indexed_by<
+    mi::hashed_unique<
+      mi::tag<hashed>,
+      mi::member<UnsatisfiedInterest, ndn::ConstBufferPtr, &UnsatisfiedInterest::digest>,
+      DigestPtrHash,
+      DigestPtrEqual
+      >
+    >
+  >
+{
+};
+
+} // namespace chronosync
+
+#endif // CHRONOSYNC_INTEREST_CONTAINER_HPP
diff --git a/src/interest-table.cpp b/src/interest-table.cpp
new file mode 100644
index 0000000..ef7193a
--- /dev/null
+++ b/src/interest-table.cpp
@@ -0,0 +1,90 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2014 University of California, Los Angeles
+ *
+ * This file is part of ChronoSync, synchronization library for distributed realtime
+ * applications for NDN.
+ *
+ * ChronoSync is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ChronoSync 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
+ * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
+ * @author Chaoyi Bian <bcy@pku.edu.cn>
+ * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
+ * @author Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#include "interest-table.hpp"
+
+namespace chronosync {
+
+InterestTable::InterestTable(boost::asio::io_service& io)
+  : m_scheduler(io)
+{
+}
+
+InterestTable::~InterestTable()
+{
+  clear();
+}
+
+bool
+InterestTable::insert(shared_ptr<const Interest> interest,
+                      ndn::ConstBufferPtr digest,
+                      bool isKnown/*=false*/)
+{
+  bool doesExist = erase(digest);
+
+  UnsatisfiedInterestPtr request =
+    boost::make_shared<UnsatisfiedInterest>(interest, digest, isKnown);
+
+  time::milliseconds entryLifetime = interest->getInterestLifetime();
+  if (entryLifetime < time::milliseconds::zero())
+    entryLifetime = ndn::DEFAULT_INTEREST_LIFETIME;
+
+  request->expirationEvent =
+    m_scheduler.scheduleEvent(entryLifetime, ndn::bind(&InterestTable::erase, this, digest));
+
+  m_table.insert(request);
+
+  return doesExist;
+}
+
+bool
+InterestTable::erase(ndn::ConstBufferPtr digest)
+{
+  InterestContainer::index<hashed>::type::iterator it = m_table.get<hashed>().find(digest);
+  if (it != m_table.get<hashed>().end()) {
+    m_scheduler.cancelEvent((*it)->expirationEvent);
+    m_table.erase(it);
+    return true;
+  }
+  return false;
+}
+
+size_t
+InterestTable::size() const
+{
+  return m_table.size();
+}
+
+void
+InterestTable::clear()
+{
+  for (InterestContainer::iterator it = m_table.begin();
+       it != m_table.end(); it++) {
+    m_scheduler.cancelEvent((*it)->expirationEvent);
+  }
+
+  m_table.clear();
+}
+
+}
diff --git a/src/interest-table.hpp b/src/interest-table.hpp
new file mode 100644
index 0000000..288ed9b
--- /dev/null
+++ b/src/interest-table.hpp
@@ -0,0 +1,125 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2014 University of California, Los Angeles
+ *
+ * This file is part of ChronoSync, synchronization library for distributed realtime
+ * applications for NDN.
+ *
+ * ChronoSync is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ChronoSync 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
+ * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
+ * @author Chaoyi Bian <bcy@pku.edu.cn>
+ * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
+ * @author Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#ifndef CHRONOSYNC_INTEREST_TABLE_HPP
+#define CHRONOSYNC_INTEREST_TABLE_HPP
+
+#include "interest-container.hpp"
+
+#include <ndn-cxx/util/scheduler.hpp>
+
+namespace chronosync {
+
+/**
+ * @brief A table to keep unsatisfied Sync Interest
+ */
+class InterestTable : noncopyable
+{
+public:
+  class Error : public std::runtime_error
+  {
+  public:
+    explicit
+    Error(const std::string& what)
+      : std::runtime_error(what)
+    {
+    }
+  };
+
+  typedef InterestContainer::iterator iterator;
+  typedef InterestContainer::const_iterator const_iterator;
+
+  explicit
+  InterestTable(boost::asio::io_service& io);
+
+  ~InterestTable();
+
+  /**
+   * @brief Insert an interest
+   *
+   * If the interest already exists in the table, the old interest will be replaced,
+   * and expire timer will be reset.
+   * Interests with the same name are counted as the same.
+   * This method assumes that the sync prefix of all interests are the same
+   * thus it only compares the digest part.
+   *
+   * @param interest Interest to insert.
+   * @param digest   The value of the last digest component.
+   * @param isKnown  false if the digest is an unknown digest.
+   * @return true if the same interest exists in the table before insertion.
+   */
+  bool
+  insert(shared_ptr<const Interest> interest,
+         ndn::ConstBufferPtr digest,
+         bool isKnown = false);
+
+  /**
+   * @brief Delete interest by digest (e.g., when it was satisfied)
+   *
+   * @return true if an interest with the digest exists in the table before deletion
+   */
+  bool
+  erase(ndn::ConstBufferPtr digest);
+
+  const_iterator
+  begin() const
+  {
+    return m_table.begin();
+  }
+
+  iterator
+  begin()
+  {
+    return m_table.begin();
+  }
+
+  const_iterator
+  end() const
+  {
+    return m_table.end();
+  }
+
+  iterator
+  end()
+  {
+    return m_table.end();
+  }
+
+  size_t
+  size() const;
+
+  void
+  clear();
+
+private:
+  ndn::Scheduler m_scheduler;
+  ndn::time::steady_clock::Duration m_entryLifetime;
+  ndn::time::steady_clock::Duration m_cleanPeriod;
+
+  InterestContainer m_table;
+};
+
+} // namespace chronosync
+
+#endif // CHRONOSYNC_INTEREST_TABLE_HPP
diff --git a/src/sync-interest-container.h b/src/sync-interest-container.h
deleted file mode 100644
index 86b7460..0000000
--- a/src/sync-interest-container.h
+++ /dev/null
@@ -1,100 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2014 University of California, Los Angeles
- *
- * This file is part of ChronoSync, synchronization library for distributed realtime
- * applications for NDN.
- *
- * ChronoSync is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation, either
- * version 3 of the License, or (at your option) any later version.
- *
- * ChronoSync 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
- * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- *
- * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
- * @author Chaoyi Bian <bcy@pku.edu.cn>
- * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
- */
-
-#ifndef SYNC_INTEREST_CONTAINER_H
-#define SYNC_INTEREST_CONTAINER_H
-
-#include <ndn-cxx/util/time.hpp>
-
-#include "sync-digest.h"
-
-#include <boost/multi_index_container.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>
-#include <boost/multi_index/sequenced_index.hpp>
-#include <boost/multi_index/ordered_index.hpp>
-// #include <boost/multi_index/random_access_index.hpp>
-#include <boost/multi_index/member.hpp>
-#include <boost/multi_index/mem_fun.hpp>
-
-namespace mi = boost::multi_index;
-
-namespace Sync {
-
-struct Interest
-{
-  Interest (DigestConstPtr digest, const std::string &name, bool unknown=false)
-  : m_digest (digest)
-  , m_name (name)
-  , m_time (ndn::time::system_clock::now())
-  , m_unknown (unknown)
-  {
-  }
-
-  DigestConstPtr   m_digest;
-  std::string      m_name;
-  ndn::time::system_clock::TimePoint m_time;
-  bool             m_unknown;
-};
-
-/// @cond include_hidden
-struct named { };
-struct hashed;
-struct timed;
-/// @endcond
-
-/**
- * \ingroup sync
- * @brief Container for interests (application PIT)
- */
-struct InterestContainer : public mi::multi_index_container<
-  Interest,
-  mi::indexed_by<
-    mi::hashed_unique<
-      mi::tag<named>,
-      BOOST_MULTI_INDEX_MEMBER(Interest, std::string, m_name)
-    >
-    ,
-
-    mi::hashed_non_unique<
-      mi::tag<hashed>,
-      BOOST_MULTI_INDEX_MEMBER(Interest, DigestConstPtr, m_digest),
-      DigestPtrHash,
-      DigestPtrEqual
-      >
-    ,
-
-    mi::ordered_non_unique<
-      mi::tag<timed>,
-      BOOST_MULTI_INDEX_MEMBER(Interest, ndn::time::system_clock::TimePoint, m_time)
-      >
-    >
-  >
-{
-};
-
-} // Sync
-
-#endif // SYNC_INTEREST_CONTAINER_H
diff --git a/src/sync-interest-table.cc b/src/sync-interest-table.cc
deleted file mode 100644
index 548ebe2..0000000
--- a/src/sync-interest-table.cc
+++ /dev/null
@@ -1,126 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2014 University of California, Los Angeles
- *
- * This file is part of ChronoSync, synchronization library for distributed realtime
- * applications for NDN.
- *
- * ChronoSync is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation, either
- * version 3 of the License, or (at your option) any later version.
- *
- * ChronoSync 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
- * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- *
- * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
- * @author Chaoyi Bian <bcy@pku.edu.cn>
- * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
- */
-
-#include "sync-interest-table.h"
-#include "sync-logging.h"
-
-INIT_LOGGER ("SyncInterestTable")
-
-namespace Sync
-{
-
-SyncInterestTable::SyncInterestTable (boost::asio::io_service& io, ndn::time::system_clock::Duration lifetime)
-  : m_entryLifetime (lifetime)
-  , m_scheduler(io)
-{
-  m_scheduler.scheduleEvent(ndn::time::seconds (4),
-                            ndn::bind(&SyncInterestTable::expireInterests, this));
-}
-
-SyncInterestTable::~SyncInterestTable ()
-{
-}
-
-Interest
-SyncInterestTable::pop ()
-{
-  if (m_table.size () == 0)
-    BOOST_THROW_EXCEPTION (Error::InterestTableIsEmpty ());
-
-  Interest ret = *m_table.begin ();
-  m_table.erase (m_table.begin ());
-
-  return ret;
-}
-
-bool
-SyncInterestTable::insert (DigestConstPtr digest, const std::string& name, bool unknownState/*=false*/)
-{
-  bool existent = false;
-
-  InterestContainer::index<named>::type::iterator it = m_table.get<named> ().find (name);
-  if (it != m_table.end())
-    {
-      existent = true;
-      m_table.erase (it);
-    }
-  m_table.insert (Interest (digest, name, unknownState));
-
-  return existent;
-}
-
-uint32_t
-SyncInterestTable::size () const
-{
-  return m_table.size ();
-}
-
-bool
-SyncInterestTable::remove (const std::string& name)
-{
-  InterestContainer::index<named>::type::iterator item = m_table.get<named> ().find (name);
-  if (item != m_table.get<named> ().end ())
-    {
-      m_table.get<named> ().erase (name);
-      return true;
-    }
-
-  return false;
-}
-
-bool
-SyncInterestTable::remove (DigestConstPtr digest)
-{
-  InterestContainer::index<hashed>::type::iterator item = m_table.get<hashed> ().find (digest);
-  if (item != m_table.get<hashed> ().end ())
-    {
-      m_table.get<hashed> ().erase (digest); // erase all records associated with the digest
-      return true;
-    }
-  return false;
-}
-
-void SyncInterestTable::expireInterests ()
-{
-  uint32_t count = 0;
-  ndn::time::system_clock::TimePoint expireTime = ndn::time::system_clock::now() - m_entryLifetime;
-
-  while (m_table.size () > 0)
-    {
-      InterestContainer::index<timed>::type::iterator item = m_table.get<timed> ().begin ();
-
-      if (item->m_time <= expireTime)
-        {
-          m_table.get<timed> ().erase (item);
-          count ++;
-        }
-      else
-        break;
-  }
-
-  m_scheduler.scheduleEvent(ndn::time::seconds (4),
-                            ndn::bind(&SyncInterestTable::expireInterests, this));
-}
-
-
-}
diff --git a/src/sync-interest-table.h b/src/sync-interest-table.h
deleted file mode 100644
index fad339f..0000000
--- a/src/sync-interest-table.h
+++ /dev/null
@@ -1,97 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2014 University of California, Los Angeles
- *
- * This file is part of ChronoSync, synchronization library for distributed realtime
- * applications for NDN.
- *
- * ChronoSync is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation, either
- * version 3 of the License, or (at your option) any later version.
- *
- * ChronoSync 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
- * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- *
- * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
- * @author Chaoyi Bian <bcy@pku.edu.cn>
- * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
- */
-
-#ifndef SYNC_INTEREST_TABLE_H
-#define SYNC_INTEREST_TABLE_H
-
-#include <ndn-cxx/util/scheduler.hpp>
-
-#include <string>
-#include <vector>
-
-#include "sync-digest.h"
-#include "sync-interest-container.h"
-
-namespace Sync {
-
-/**
- * \ingroup sync
- * @brief A table to keep unanswered Sync Interest
- * all access operation to the table should grab the
- * mutex first
- */
-class SyncInterestTable
-{
-public:
-  SyncInterestTable (boost::asio::io_service& io, ndn::time::system_clock::Duration lifetime);
-  ~SyncInterestTable ();
-
-  /**
-   * @brief Insert an interest, if interest already exists, update the
-   * timestamp
-   */
-  bool
-  insert (DigestConstPtr interest, const std::string &name, bool unknownState=false);
-
-  /**
-   * @brief Remove interest by digest (e.g., when it was satisfied)
-   */
-  bool
-  remove (DigestConstPtr interest);
-
-  /**
-   * @brief Remove interest by name (e.g., when it was satisfied)
-   */
-  bool
-  remove (const std::string &name);
-
-  /**
-   * @brief pop a non-expired Interest from PIT
-   */
-  Interest
-  pop ();
-
-  uint32_t
-  size () const;
-
-private:
-  /**
-   * @brief periodically called to expire Interest
-   */
-  void
-  expireInterests ();
-
-private:
-  ndn::time::system_clock::Duration m_entryLifetime;
-  InterestContainer m_table;
-
-  ndn::Scheduler m_scheduler;
-};
-
-namespace Error {
-struct InterestTableIsEmpty : virtual boost::exception, virtual std::exception { };
-}
-
-} // Sync
-
-#endif // SYNC_INTEREST_TABLE_H