repo: Watching prefix

Change-Id: Ia315d6e529fa4402ad9ca4706b70db1e75eb054c
Refs: #1784
diff --git a/src/handles/watch-handle.hpp b/src/handles/watch-handle.hpp
new file mode 100644
index 0000000..10cddda
--- /dev/null
+++ b/src/handles/watch-handle.hpp
@@ -0,0 +1,170 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California.
+ *
+ * This file is part of NDN repo-ng (Next generation of NDN repository).
+ * See AUTHORS.md for complete list of repo-ng authors and contributors.
+ *
+ * repo-ng 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.
+ *
+ * repo-ng 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
+ * repo-ng, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef REPO_HANDLES_WATCH_HANDLE_HPP
+#define REPO_HANDLES_WATCH_HANDLE_HPP
+
+#include "base-handle.hpp"
+
+#include <queue>
+
+namespace repo {
+
+using std::map;
+using std::pair;
+using std::queue;
+using namespace ndn::time;
+/**
+ * @brief WatchHandle provides a different way for repo to insert data.
+ *
+ * Repo keeps sending interest to request the data with same prefix,
+ *
+ * but with different exclude selectors(updated every time). Repo will stop
+ *
+ * watching the prefix until a command interest tell it to stop, the total
+ *
+ * amount of sent interests reaches a specific number or time out.
+ */
+class WatchHandle : public BaseHandle
+{
+
+public:
+  class Error : public BaseHandle::Error
+  {
+  public:
+    explicit
+    Error(const std::string& what)
+      : BaseHandle::Error(what)
+    {
+    }
+  };
+
+
+public:
+  WatchHandle(Face& face, RepoStorage& storageHandle, KeyChain& keyChain,
+              Scheduler& scheduler, ValidatorConfig& validator);
+
+  virtual void
+  listen(const Name& prefix);
+
+private: // watch-insert command
+  /**
+   * @brief handle watch commands
+   */
+  void
+  onInterest(const Name& prefix, const Interest& interest);
+
+  void
+  onValidated(const shared_ptr<const Interest>& interest, const Name& prefix);
+
+  void
+  onValidationFailed(const shared_ptr<const Interest>& interest, const string& reason);
+
+  void
+  onRegistered(const Name& prefix);
+
+  void
+  onRegisterFailed(const Name& prefix, const std::string& reason);
+
+private: // data fetching
+  /**
+   * @brief fetch data and send next interest
+   */
+  void
+  onData(const Interest& interest, Data& data, const Name& name);
+
+  /**
+   * @brief handle when fetching one data timeout
+   */
+  void
+  onTimeout(const Interest& interest, const Name& name);
+
+  void
+  onDataValidated(const Interest& interest, const shared_ptr<const Data>& data,
+                  const Name& name);
+
+  /**
+   * @brief failure of validation
+   */
+  void
+  onDataValidationFailed(const Interest& interest, const shared_ptr<const Data>& data,
+                         const std::string& reason, const Name& name);
+
+
+  void
+  processWatchCommand(const Interest& interest, RepoCommandParameter& parameter);
+
+  void
+  watchStop(const Name& name);
+
+private: // watch state check command
+  /**
+   * @brief handle watch check command
+   */
+  void
+  onCheckInterest(const Name& prefix, const Interest& interest);
+
+  void
+  onCheckValidated(const shared_ptr<const Interest>& interest, const Name& prefix);
+
+  void
+  onCheckValidationFailed(const shared_ptr<const Interest>& interest, const std::string& reason);
+
+private: // watch stop command
+  /**
+   * @brief handle watch stop command
+   */
+  void
+  onStopInterest(const Name& prefix, const Interest& interest);
+
+  void
+  onStopValidated(const shared_ptr<const Interest>& interest, const Name& prefix);
+
+  void
+  onStopValidationFailed(const shared_ptr<const Interest>& interest, const std::string& reason);
+
+private:
+  void
+  negativeReply(const Interest& interest, int statusCode);
+
+  void
+  deferredDeleteProcess(const Name& name);
+
+  void
+  deleteProcess(const Name& name);
+
+  bool
+  onRunning(const Name& name);
+
+private:
+
+  ValidatorConfig& m_validator;
+
+  map<Name, std::pair<RepoCommandResponse, bool> > m_processes;
+  int64_t m_interestNum;
+  int64_t m_maxInterestNum;
+  milliseconds m_interestLifetime;
+  milliseconds m_watchTimeout;
+  ndn::time::steady_clock::TimePoint m_startTime;
+  int64_t m_size;
+};
+
+} // namespace repo
+
+#endif // REPO_HANDLES_Watch_HANDLE_HPP