PSync: initial commit

refs: #4641

Change-Id: Iabed3ad7632544d97559e6798547b7972b416784
diff --git a/src/partial-producer.hpp b/src/partial-producer.hpp
new file mode 100644
index 0000000..a2db7ac
--- /dev/null
+++ b/src/partial-producer.hpp
@@ -0,0 +1,128 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2018,  The University of Memphis
+ *
+ * This file is part of PSync.
+ * See AUTHORS.md for complete list of PSync authors and contributors.
+ *
+ * PSync 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.
+ *
+ * PSync 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
+ * PSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#ifndef PSYNC_PARTIAL_PRODUCER_HPP
+#define PSYNC_PARTIAL_PRODUCER_HPP
+
+#include "detail/bloom-filter.hpp"
+#include "producer-base.hpp"
+
+#include <map>
+#include <unordered_set>
+
+#include <ndn-cxx/face.hpp>
+#include <ndn-cxx/util/scheduler.hpp>
+#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
+#include <ndn-cxx/util/time.hpp>
+#include <ndn-cxx/security/key-chain.hpp>
+
+namespace psync {
+
+struct PendingEntryInfo
+{
+  BloomFilter bf;
+  IBLT iblt;
+  ndn::util::scheduler::ScopedEventId expirationEvent;
+};
+
+/**
+ * @brief Partial sync logic to publish data names
+ *
+ * Application should call publishName whenever it wants to
+ * let consumers know that new data is available.
+ * Additional userPrefix should be added via addUserNode before calling publishName
+ * Currently, publishing of data needs to be handled by the application.
+ */
+class PartialProducer : public ProducerBase
+{
+public:
+  /**
+   * @brief constructor
+   *
+   * Registers syncPrefix in NFD and sets internal filters for
+   * "sync" and "hello" under syncPrefix
+   *
+   * @param expectedNumEntries expected entries in IBF
+   * @param face application's face
+   * @param syncPrefix The prefix of the sync group
+   * @param userPrefix The prefix of the first user in the group
+   * @param syncReplyFreshness freshness of sync data
+   * @param helloReplyFreshness freshness of hello data
+   */
+  PartialProducer(size_t expectedNumEntries,
+                  ndn::Face& face,
+                  const ndn::Name& syncPrefix,
+                  const ndn::Name& userPrefix,
+                  ndn::time::milliseconds helloReplyFreshness = HELLO_REPLY_FRESHNESS,
+                  ndn::time::milliseconds syncReplyFreshness = SYNC_REPLY_FRESHNESS);
+
+  ~PartialProducer();
+
+  /**
+   * @brief Publish name to let subscribed consumers know
+   *
+   * If seq is null then the seq of prefix is incremented by 1 else
+   * the supplied sequence is set in the IBF.
+   * Upon updating the sequence in the IBF satisfyPendingSyncInterests
+   * is called to let subscribed consumers know.
+   *
+   * @param prefix the prefix to be updated
+   * @param seq the sequence number of the prefix
+   */
+  void
+  publishName(const ndn::Name& prefix, ndn::optional<uint64_t> seq = ndn::nullopt);
+
+private:
+  /**
+   * @brief Satisfy any pending interest that have subscription for prefix
+   *
+   * @param prefix the prefix that was updated in publishName
+   */
+  void
+  satisfyPendingSyncInterests(const ndn::Name& prefix);
+
+  /**
+   * @brief Receive hello interest from consumer and respond with hello data
+   *
+   * Hello data's name format is: /\<sync-prefix\>/hello/\<current-IBF\>
+   */
+  void
+  onHelloInterest(const ndn::Name& prefix, const ndn::Interest& interest);
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  /**
+   * @brief Receive sync interest from consumer
+   *
+   * Either respond with sync data if consumer is behind or
+   * store sync interest in m_pendingEntries
+   *
+   * Sync data's name format is: /\<sync-prefix\>/sync/\<old-IBF\>/\<current-IBF\>
+   */
+  void
+  onSyncInterest(const ndn::Name& prefix, const ndn::Interest& interest);
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  std::map <ndn::Name, PendingEntryInfo> m_pendingEntries;
+
+  const ndn::RegisteredPrefixId* m_registerPrefixId;
+};
+
+} // namespace psync
+
+#endif // PSYNC_PARTIAL_PRODUCER_HPP