Fix InterestTable
Change-Id: I203e9a28ec2e6c7a699df25fb00919172e20e1d0
diff --git a/tests/unit-tests/test-interest-table.cpp.outdated b/src/interest-container.cpp
similarity index 61%
rename from tests/unit-tests/test-interest-table.cpp.outdated
rename to src/interest-container.cpp
index 2db3eed..4d42321 100644
--- a/tests/unit-tests/test-interest-table.cpp.outdated
+++ b/src/interest-container.cpp
@@ -15,28 +15,24 @@
*
* 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 <boost/test/unit_test.hpp>
-#include <boost/test/output_test_stream.hpp>
-#include <map>
-using boost::test_tools::output_test_stream;
+#include "interest-container.hpp"
-#include <boost/make_shared.hpp>
+namespace chronosync {
-#include "sync-interest-table.h"
-
-using namespace Sync;
-using namespace std;
-using namespace boost;
-
-BOOST_AUTO_TEST_CASE (InterestTableTest)
+UnsatisfiedInterest::UnsatisfiedInterest(shared_ptr<const Interest> interest,
+ ndn::ConstBufferPtr digest,
+ bool isUnknown)
+ : interest(interest)
+ , digest(digest)
+ , isUnknown(isUnknown)
{
- // Alex: test is broken due to changes in SyncInterestTable
- // cerr << "InterestTableTest is broken" << endl;
-
- // SyncInterestTable *table = 0;
- // BOOST_CHECK_NO_THROW (table = new SyncInterestTable ());
-
- // BOOST_CHECK_NO_THROW (delete table);
}
+
+} // 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
diff --git a/tests/unit-tests/test-interest-table.cpp b/tests/unit-tests/test-interest-table.cpp
new file mode 100644
index 0000000..3a312d4
--- /dev/null
+++ b/tests/unit-tests/test-interest-table.cpp
@@ -0,0 +1,177 @@
+/* -*- 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/>.
+ */
+
+#include "interest-table.hpp"
+
+#include <ndn-cxx/util/scheduler.hpp>
+#include <unistd.h>
+
+#include "boost-test.hpp"
+
+namespace chronosync {
+namespace test {
+
+class InterestTableFixture
+{
+public:
+ InterestTableFixture()
+ : scheduler(io)
+ {
+ uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
+ Name prefix("/test/prefix");
+
+ Name interestName1;
+ digest1 = ndn::crypto::sha256(origin, 1);
+ interestName1.append(prefix).append(name::Component(digest1));
+ interest1 = make_shared<Interest>(interestName1);
+ interest1->setInterestLifetime(time::milliseconds(100));
+
+ Name interestName2;
+ digest2 = ndn::crypto::sha256(origin, 2);
+ interestName2.append(prefix).append(name::Component(digest2));
+ interest2 = make_shared<Interest>(interestName2);
+ interest2->setInterestLifetime(time::milliseconds(100));
+
+ Name interestName3;
+ digest3 = ndn::crypto::sha256(origin, 3);
+ interestName3.append(prefix).append(name::Component(digest3));
+ interest3 = make_shared<Interest>(interestName3);
+ interest3->setInterestLifetime(time::milliseconds(100));
+ }
+
+ void
+ insert(InterestTable& table,
+ shared_ptr<Interest> interest,
+ ndn::ConstBufferPtr digest)
+ {
+ table.insert(interest, digest);
+ }
+
+ void
+ check(InterestTable& table, size_t size)
+ {
+ BOOST_CHECK_EQUAL(table.size(), size);
+ }
+
+ void
+ terminate()
+ {
+ io.stop();
+ }
+
+ shared_ptr<Interest> interest1;
+ ndn::ConstBufferPtr digest1;
+
+ shared_ptr<Interest> interest2;
+ ndn::ConstBufferPtr digest2;
+
+ shared_ptr<Interest> interest3;
+ ndn::ConstBufferPtr digest3;
+
+ boost::asio::io_service io;
+ ndn::Scheduler scheduler;
+};
+
+BOOST_FIXTURE_TEST_SUITE(InterestTableTest, InterestTableFixture)
+
+BOOST_AUTO_TEST_CASE(Container)
+{
+ InterestContainer container;
+
+ container.insert(boost::make_shared<UnsatisfiedInterest>(interest1, digest1));
+ container.insert(boost::make_shared<UnsatisfiedInterest>(interest2, digest2));
+ container.insert(boost::make_shared<UnsatisfiedInterest>(interest3, digest3));
+
+ BOOST_CHECK_EQUAL(container.size(), 3);
+ BOOST_CHECK(container.find(digest3) != container.end());
+ BOOST_CHECK(container.find(digest2) != container.end());
+ BOOST_CHECK(container.find(digest1) != container.end());
+}
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+ InterestTable table(io);
+
+ table.insert(interest1, digest1);
+ table.insert(interest2, digest2);
+ table.insert(interest3, digest3);
+
+ BOOST_CHECK_EQUAL(table.size(), 3);
+ InterestTable::const_iterator it = table.begin();
+ BOOST_CHECK(it != table.end());
+ it++;
+ BOOST_CHECK(it != table.end());
+ it++;
+ BOOST_CHECK(it != table.end());
+ it++;
+ BOOST_CHECK(it == table.end());
+
+ BOOST_CHECK_EQUAL(table.size(), 3);
+ table.erase(digest1);
+ BOOST_CHECK_EQUAL(table.size(), 2);
+ table.erase(digest2);
+ BOOST_CHECK_EQUAL(table.size(), 1);
+ ConstUnsatisfiedInterestPtr pendingInterest = *table.begin();
+ table.clear();
+ BOOST_CHECK_EQUAL(table.size(), 0);
+ BOOST_CHECK(*pendingInterest->digest == *digest3);
+}
+
+BOOST_AUTO_TEST_CASE(Expire)
+{
+ InterestTable table(io);
+
+ scheduler.scheduleEvent(ndn::time::milliseconds(50),
+ ndn::bind(&InterestTableFixture::insert, this,
+ boost::ref(table), interest1, digest1));
+
+ scheduler.scheduleEvent(ndn::time::milliseconds(150),
+ ndn::bind(&InterestTableFixture::insert, this,
+ boost::ref(table), interest2, digest2));
+
+ scheduler.scheduleEvent(ndn::time::milliseconds(150),
+ ndn::bind(&InterestTableFixture::insert, this,
+ boost::ref(table), interest3, digest3));
+
+ scheduler.scheduleEvent(ndn::time::milliseconds(200),
+ ndn::bind(&InterestTableFixture::insert, this,
+ boost::ref(table), interest2, digest2));
+
+ scheduler.scheduleEvent(ndn::time::milliseconds(220),
+ ndn::bind(&InterestTableFixture::check, this,
+ boost::ref(table), 2));
+
+ scheduler.scheduleEvent(ndn::time::milliseconds(270),
+ ndn::bind(&InterestTableFixture::check, this,
+ boost::ref(table), 1));
+
+ scheduler.scheduleEvent(ndn::time::milliseconds(420),
+ ndn::bind(&InterestTableFixture::check, this,
+ boost::ref(table), 0));
+
+ scheduler.scheduleEvent(ndn::time::milliseconds(500),
+ ndn::bind(&InterestTableFixture::terminate, this));
+
+ io.run();
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace test
+} // namespace chronosync
diff --git a/tests/unit-tests/test-pit.cpp.outdated b/tests/unit-tests/test-pit.cpp.outdated
deleted file mode 100644
index 39b36b9..0000000
--- a/tests/unit-tests/test-pit.cpp.outdated
+++ /dev/null
@@ -1,72 +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/>.
- */
-
-#include <boost/test/unit_test.hpp>
-#include <boost/test/output_test_stream.hpp>
-using boost::test_tools::output_test_stream;
-#include <vector>
-
-#include <boost/make_shared.hpp>
-
-#include "sync-interest-table.h"
-#include "sync-logging.h"
-
-using namespace Sync;
-using namespace std;
-using namespace boost;
-
-// string sitToString(SyncInterestTable *sit) {
-// vector<string> ent = sit->fetchAll();
-// sort(ent.begin(), ent.end());
-// string str = "";
-// while(!ent.empty()){
-// str += ent.back();
-// ent.pop_back();
-// }
-// return str;
-// }
-
-BOOST_AUTO_TEST_CASE (SyncInterestTableTest)
-{
- cerr << "SyncInterestTableTest is broken" << endl;
-
- // INIT_LOGGERS ();
- // INIT_LOGGER ("Test.Pit");
-
- // SyncInterestTable sit;
- // sit.insert("/ucla.edu/0");
- // sit.insert("/ucla.edu/1");
- // string str = sitToString(&sit);
- // BOOST_CHECK_EQUAL(str, "/ucla.edu/1/ucla.edu/0");
-
- // str = sitToString(&sit);
- // BOOST_CHECK_EQUAL(str, "");
-
- // _LOG_DEBUG ("Adding 0 and 1");
- // sit.insert("/ucla.edu/0");
- // sit.insert("/ucla.edu/1");
- // sleep(2);
- // _LOG_DEBUG ("Adding 0 and 2");
- // sit.insert("/ucla.edu/0");
- // sit.insert("/ucla.edu/2");
- // sleep(3);
- // _LOG_DEBUG ("Checking");
- // str = sitToString(&sit);
- // BOOST_CHECK_EQUAL(str, "/ucla.edu/2/ucla.edu/0");
-}