read-echo: functions to read data from repo

Change-Id: I61a0b8bf254cd84c49def0f7ddda5ab026c689f4
diff --git a/ndn-handle/base-handle.hpp b/ndn-handle/base-handle.hpp
new file mode 100644
index 0000000..97df137
--- /dev/null
+++ b/ndn-handle/base-handle.hpp
@@ -0,0 +1,46 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef REPO_NDN_HANDLE_BASE_HANDLE_HPP
+#define REPO_NDN_HANDLE_BASE_HANDLE_HPP
+
+#include "ndn-handle-common.hpp"
+
+namespace repo {
+
+class BaseHandle : noncopyable
+{
+
+public:
+  BaseHandle(Face* face, StorageHandle* storageHandle)
+    : m_face(face)
+    , m_storageHandle(storageHandle)
+  {
+  }
+
+  virtual void
+  listen(const Name& prefix) = 0;
+
+  inline Face*
+  getFace()
+  {
+    return m_face;
+  }
+
+  inline StorageHandle*
+  getStorageHandle()
+  {
+    return m_storageHandle;
+  }
+
+private:
+  Face* m_face;
+  StorageHandle* m_storageHandle;
+};
+
+} //namespace repo
+
+#endif // REPO_NDN_HANDLE_BASE_HANDLE_HPP
diff --git a/ndn-handle/ndn-handle-common.hpp b/ndn-handle/ndn-handle-common.hpp
new file mode 100644
index 0000000..f178d53
--- /dev/null
+++ b/ndn-handle/ndn-handle-common.hpp
@@ -0,0 +1,35 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+#ifndef REPO_NDN_HANDLE_NDN_HANDLE_COMMON_HPP
+#define REPO_NDN_HANDLE_NDN_HANDLE_COMMON_HPP
+
+
+#include <cstdlib>
+#include <sstream>
+#include <iostream>
+#include <time.h>
+#include <unistd.h>
+#include <ndn-cpp-dev/face.hpp>
+#include <ndn-cpp-dev/security/key-chain.hpp>
+#include "../storage/storage-handle.hpp"
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/uniform_int_distribution.hpp>
+#include <map>
+
+#define RETRY_TIMEOUT 3
+#define DEFAULT_CREDIT 12
+#define NOEND_TIMEOUT 10000
+
+namespace repo {
+using ndn::Face;
+using ndn::Name;
+using ndn::Interest;
+using ndn::KeyChain;
+using ndn::Selectors;
+using ndn::bind;
+}
+
+#endif
diff --git a/ndn-handle/read-handle.cpp b/ndn-handle/read-handle.cpp
new file mode 100644
index 0000000..9ab7822
--- /dev/null
+++ b/ndn-handle/read-handle.cpp
@@ -0,0 +1,34 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+#include "read-handle.hpp"
+
+namespace repo {
+
+void
+ReadHandle::onInterest(const Name& prefix, const Interest& interest)
+{
+  Data data;
+  if (getStorageHandle()->readData(interest, data)) {
+    getFace()->put(data);
+  }
+}
+
+void
+ReadHandle::onRegisterFailed(const Name& prefix, const std::string& reason)
+{
+  std::cerr << "ERROR: Failed to register prefix in local hub's daemon" << std::endl;
+  getFace()->shutdown();
+}
+
+void
+ReadHandle::listen(const Name& prefix)
+{
+  getFace()->setInterestFilter(prefix,
+                            bind(&ReadHandle::onInterest, this, _1, _2),
+                            bind(&ReadHandle::onRegisterFailed, this, _1, _2));
+}
+
+} //namespace repo
diff --git a/ndn-handle/read-handle.hpp b/ndn-handle/read-handle.hpp
new file mode 100644
index 0000000..5af1fb3
--- /dev/null
+++ b/ndn-handle/read-handle.hpp
@@ -0,0 +1,39 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef REPO_NDN_HANDLE_READ_HANDLE_HPP
+#define REPO_NDN_HANDLE_READ_HANDLE_HPP
+
+#include "base-handle.hpp"
+
+namespace repo {
+
+class ReadHandle : BaseHandle
+{
+
+public:
+  ReadHandle(Face* face, StorageHandle* storageHandle)
+  : BaseHandle(face, storageHandle)
+  {
+  }
+
+  virtual void
+  listen(const Name& prefix);
+
+private:
+  /**
+   * @brief Read the name from backend storage
+   */
+  void
+  onInterest(const Name& prefix, const Interest& interest);
+
+  void
+  onRegisterFailed(const Name& prefix, const std::string& reason);
+};
+
+} //namespace repo
+
+#endif // REPO_NDN_HANDLE_READ_HANDLE_HPP
diff --git a/server/server.cpp b/server/server.cpp
index fb240f7..6e4ee85 100644
--- a/server/server.cpp
+++ b/server/server.cpp
@@ -4,8 +4,51 @@
  * See COPYING for copyright and distribution information.
  */
 
+#include <string>
+#include <iostream>
+#include <ndn-cpp-dev/face.hpp>
+
+#include "../storage/storage-handle.hpp"
+#include "../storage/sqlite/sqlite-handle.hpp"
+#include "../ndn-handle/read-handle.hpp"
+
+using namespace repo;
+
+static const string ndnRepoUsageMessage =
+  "ndn-repo - NDNx Repository Daemon\n"
+  "-d: set database path\n"
+  "-h: show help message\n"
+  "-c: set config file path\n"
+  ;
+
 int
-main(int argc, char** argv)
-{
+main(int argc, char** argv) {
+  int opt;
+  string dbPath;
+  string confPath;
+  while ((opt = getopt(argc, argv, "d:hc:")) != -1) {
+    switch (opt) {
+    case 'd':
+      dbPath = string(optarg);
+      break;
+    case 'h':
+      std::cout << ndnRepoUsageMessage << std::endl;
+      return 1;
+    case 'c':
+      confPath = string(optarg);
+      break;
+    default:
+      break;
+    }
+  }
+  SqliteHandle sqliteHandle(dbPath);
+  StorageHandle* handle = &sqliteHandle;
+
+  Face face;
+  ReadHandle readHandle(&face, handle);
+  if (confPath.empty()) {
+    confPath = "./repo.conf";
+  }
+  face.processEvents();
   return 0;
 }