content server

Change-Id: I08eb9da3d7fcdb337b02c73695dd648bcfe82c81
diff --git a/src/content-server.cpp b/src/content-server.cpp
index b795afe..888baea 100644
--- a/src/content-server.cpp
+++ b/src/content-server.cpp
@@ -19,149 +19,135 @@
  */
 
 #include "content-server.hpp"
-#include "logging.hpp"
-#include "periodic-task.hpp"
-#include "simple-interval-generator.hpp"
-#include "task.hpp"
+#include "core/logging.hpp"
+
+#include <ndn-cxx/util/string-helper.hpp>
+#include <ndn-cxx/security/signing-helpers.hpp>
+
 #include <boost/lexical_cast.hpp>
-#include <boost/make_shared.hpp>
-#include <utility>
+
+namespace ndn {
+namespace chronoshare {
 
 _LOG_INIT(ContentServer);
 
-using namespace Ndnx;
-using namespace std;
-using namespace boost;
-
 static const int DB_CACHE_LIFETIME = 60;
 
-ContentServer::ContentServer(CcnxWrapperPtr ccnx, ActionLogPtr actionLog,
-                             const boost::filesystem::path& rootDir, const Ccnx::Name& userName,
+ContentServer::ContentServer(Face& face, ActionLogPtr actionLog,
+                             const boost::filesystem::path& rootDir, const Name& userName,
                              const std::string& sharedFolderName, const std::string& appName,
-                             int freshness)
-  : m_ndnx(ndnx)
+                             KeyChain& keyChain, int freshness)
+  : m_face(face)
   , m_actionLog(actionLog)
   , m_dbFolder(rootDir / ".chronoshare")
   , m_freshness(freshness)
-  , m_scheduler(new Scheduler())
+  , m_scheduler(face.getIoService())
+  , m_flushStateDbCacheEvent(m_scheduler)
   , m_userName(userName)
   , m_sharedFolderName(sharedFolderName)
   , m_appName(appName)
+  , m_keyChain(keyChain)
 {
-  m_scheduler->start();
-  TaskPtr flushStaleDbCacheTask =
-    boost::make_shared<PeriodicTask>(boost::bind(&ContentServer::flushStaleDbCache, this),
-                                     "flush-state-db-cache", m_scheduler,
-                                     boost::make_shared<SimpleIntervalGenerator>(DB_CACHE_LIFETIME));
-  m_scheduler->addTask(flushStaleDbCacheTask);
+  m_flushStateDbCacheEvent = m_scheduler.scheduleEvent(time::seconds(DB_CACHE_LIFETIME),
+                                                       bind(&ContentServer::flushStaleDbCache, this));
 }
 
 ContentServer::~ContentServer()
 {
-  m_scheduler->shutdown();
-
   ScopedLock lock(m_mutex);
-  for (PrefixIt forwardingHint = m_prefixes.begin(); forwardingHint != m_prefixes.end();
-       ++forwardingHint) {
-    m_ccnx->clearInterestFilter(*forwardingHint);
+  for (FilterIdIt it = m_interestFilterIds.begin(); it != m_interestFilterIds.end(); ++it) {
+    m_face.unsetInterestFilter(it->second);
   }
 
-  m_prefixes.clear();
+  m_interestFilterIds.clear();
 }
 
 void
 ContentServer::registerPrefix(const Name& forwardingHint)
 {
   // Format for files:   /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment>
-  // Format for actions: /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq>
+  // Format for actions:
+  // /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq>
 
   _LOG_DEBUG(">> content server: register " << forwardingHint);
 
-  m_ccnx->setInterestFilter(forwardingHint,
-                            bind(&ContentServer::filterAndServe, this, forwardingHint, _1));
-
   ScopedLock lock(m_mutex);
-  m_prefixes.insert(forwardingHint);
+  m_interestFilterIds[forwardingHint] =
+    m_face.setInterestFilter(InterestFilter(forwardingHint),
+                             bind(&ContentServer::filterAndServe, this, _1, _2),
+                             RegisterPrefixSuccessCallback(), RegisterPrefixFailureCallback());
 }
 
 void
 ContentServer::deregisterPrefix(const Name& forwardingHint)
 {
   _LOG_DEBUG("<< content server: deregister " << forwardingHint);
-  m_ccnx->clearInterestFilter(forwardingHint);
+  m_face.unsetInterestFilter(m_interestFilterIds[forwardingHint]);
 
   ScopedLock lock(m_mutex);
-  m_prefixes.erase(forwardingHint);
+  m_interestFilterIds.erase(forwardingHint);
 }
 
-
 void
 ContentServer::filterAndServeImpl(const Name& forwardingHint, const Name& name, const Name& interest)
 {
   // interest for files:   /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment>
-  // interest for actions: /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq>
+  // interest for actions:
+  // /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq>
 
   // name for files:   /<device_name>/<appname>/file/<hash>/<segment>
   // name for actions: /<device_name>/<appname>/action/<shared-folder>/<action-seq>
 
-  try {
-    if (name.size() >= 4 && name.getCompFromBackAsString(3) == m_appName) {
-      string type = name.getCompFromBackAsString(2);
-      if (type == "file") {
-        serve_File(forwardingHint, name, interest);
-      }
-      else if (type == "action") {
-        string folder = name.getCompFromBackAsString(1);
-        if (folder == m_sharedFolderName) {
-          serve_Action(forwardingHint, name, interest);
-        }
+  if (name.size() >= 4 && name.get(-4).toUri() == m_appName) {
+    std::string type = name.get(-3).toUri();
+    if (type == "file") {
+      serve_File(forwardingHint, name, interest);
+    }
+    else if (type == "action") {
+      std::string folder = name.get(-2).toUri();
+      if (folder == m_sharedFolderName) {
+        serve_Action(forwardingHint, name, interest);
       }
     }
   }
-  catch (Ccnx::NameException& ne) {
-    // ignore any unexpected interests and errors
-    _LOG_ERROR(boost::get_error_info<Ccnx::error_info_str>(ne));
-  }
 }
 
 void
-ContentServer::filterAndServe(Name forwardingHint, const Name& interest)
+ContentServer::filterAndServe(const InterestFilter& interestFilter, const Interest& interestTrue)
 {
-  try {
-    if (forwardingHint.size() > 0 && m_userName.size() >= forwardingHint.size() &&
-        m_userName.getPartialName(0, forwardingHint.size()) == forwardingHint) {
-      filterAndServeImpl(Name("/"), interest, interest); // try without forwarding hints
-    }
+  Name forwardingHint = Name(interestFilter);
+  Name interest = interestTrue.getName();
+  _LOG_DEBUG("I'm serving ForwardingHint: " << forwardingHint << " Interest: " << interest);
+  if (forwardingHint.size() > 0 && m_userName.size() >= forwardingHint.size() &&
+      m_userName.getSubName(0, forwardingHint.size()) == forwardingHint) {
+    _LOG_DEBUG("Triggered without Forwardinghint!");
+    filterAndServeImpl(Name("/"), interest, interest); // try without forwarding hints
+  }
 
-    filterAndServeImpl(forwardingHint, interest.getPartialName(forwardingHint.size()),
-                       interest); // always try with hint... :( have to
-  }
-  catch (Ccnx::NameException& ne) {
-    // ignore any unexpected interests and errors
-    _LOG_ERROR(boost::get_error_info<Ccnx::error_info_str>(ne));
-  }
+  _LOG_DEBUG("Triggered with Forwardinghint~!");
+  filterAndServeImpl(forwardingHint, interest.getSubName(forwardingHint.size()),
+                     interest); // always try with hint... :( have to
 }
 
 void
 ContentServer::serve_Action(const Name& forwardingHint, const Name& name, const Name& interest)
 {
-  _LOG_DEBUG(">> content server serving ACTION, hint: " << forwardingHint
-                                                        << ", interest: " << interest);
-  m_scheduler->scheduleOneTimeTask(m_scheduler, 0, bind(&ContentServer::serve_Action_Execute, this,
-                                                        forwardingHint, name, interest),
-                                   boost::lexical_cast<string>(name));
+  _LOG_DEBUG(
+    ">> content server serving ACTION, hint: " << forwardingHint << ", interest: " << interest);
+  m_scheduler.scheduleEvent(time::seconds(0),
+                            bind(&ContentServer::serve_Action_Execute, this, forwardingHint, name,
+                                 interest));
   // need to unlock ccnx mutex... or at least don't lock it
 }
 
 void
 ContentServer::serve_File(const Name& forwardingHint, const Name& name, const Name& interest)
 {
-  _LOG_DEBUG(">> content server serving FILE, hint: " << forwardingHint
-                                                      << ", interest: " << interest);
+  _LOG_DEBUG(">> content server serving FILE, hint: " << forwardingHint << ", interest: " << interest);
 
-  m_scheduler->scheduleOneTimeTask(m_scheduler, 0, bind(&ContentServer::serve_File_Execute, this,
-                                                        forwardingHint, name, interest),
-                                   boost::lexical_cast<string>(name));
+  m_scheduler.scheduleEvent(time::seconds(0),
+                            bind(&ContentServer::serve_File_Execute, this, forwardingHint, name,
+                                 interest));
   // need to unlock ccnx mutex... or at least don't lock it
 }
 
@@ -172,17 +158,16 @@
   // interest:       /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment>
   // name:           /<device_name>/<appname>/file/<hash>/<segment>
 
-  int64_t segment = name.getCompFromBackAsInt(0);
-  Name deviceName = name.getPartialName(0, name.size() - 4);
-  Hash hash(head(name.getCompFromBack(1)), name.getCompFromBack(1).size());
+  int64_t segment = name.get(-1).toNumber();
+  Name deviceName = name.getSubName(0, name.size() - 4);
+  Buffer hash(name.get(-2).value(), name.get(-2).value_size());
 
-  _LOG_DEBUG(" server FILE for device: " << deviceName << ", file_hash: " << hash.shortHash()
-                                         << " segment: "
+  _LOG_DEBUG(" server FILE for device: " << deviceName << ", file_hash: " << toHex(hash) << " segment: "
                                          << segment);
 
-  string hashStr = lexical_cast<string>(hash);
+  std::string hashStr = toHex(hash);
 
-  ObjectDbPtr db;
+  shared_ptr<ObjectDb> db;
 
   ScopedLock(m_dbCacheMutex);
   {
@@ -191,39 +176,41 @@
       db = it->second;
     }
     else {
-      if (ObjectDb::DoesExist(m_dbFolder, deviceName,
+      if (ObjectDb::doesExist(m_dbFolder, deviceName,
                               hashStr)) // this is kind of overkill, as it counts available segments
       {
-        db = boost::make_shared<ObjectDb>(m_dbFolder, hashStr);
+        db = make_shared<ObjectDb>(m_dbFolder, hashStr);
         m_dbCache.insert(make_pair(hash, db));
       }
       else {
-        _LOG_ERROR("ObjectDd doesn't exist for device: " << deviceName << ", file_hash: "
-                                                         << hash.shortHash());
+        _LOG_ERROR(
+          "ObjectDd doesn't exist for device: " << deviceName << ", file_hash: " << toHex(hash));
       }
     }
   }
 
   if (db) {
-    BytesPtr co = db->fetchSegment(deviceName, segment);
-    if (co) {
+    shared_ptr<Data> data = db->fetchSegment(deviceName, segment);
+    if (data) {
       if (forwardingHint.size() == 0) {
-        _LOG_DEBUG(ParsedContentObject(*co).name());
-        m_ccnx->putToCcnd(*co);
+        m_face.put(*data);
       }
       else {
-        if (m_freshness > 0) {
-          m_ccnx->publishData(interest, *co, m_freshness);
-        }
-        else {
-          m_ccnx->publishData(interest, *co);
-        }
+        shared_ptr<Data> outerData = make_shared<Data>();
+
+        outerData->setContent(data->wireEncode());
+        outerData->setFreshnessPeriod(time::seconds(m_freshness));
+        outerData->setName(interest);
+
+        m_keyChain.sign(*outerData, signingWithSha256());;
+        m_face.put(*outerData);
       }
+      _LOG_DEBUG("Send File Data Done!");
     }
     else {
       _LOG_ERROR("ObjectDd exists, but no segment " << segment << " for device: " << deviceName
                                                     << ", file_hash: "
-                                                    << hash.shortHash());
+                                                    << toHex(hash));
     }
   }
 }
@@ -235,24 +222,24 @@
   // interest:       /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq>
   // name for actions: /<device_name>/<appname>/action/<shared-folder>/<action-seq>
 
-  int64_t seqno = name.getCompFromBackAsInt(0);
-  Name deviceName = name.getPartialName(0, name.size() - 4);
+  int64_t seqno = name.get(-1).toNumber();
+  Name deviceName = name.getSubName(0, name.size() - 4);
 
   _LOG_DEBUG(" server ACTION for device: " << deviceName << " and seqno: " << seqno);
 
-  PcoPtr pco = m_actionLog->LookupActionPco(deviceName, seqno);
-  if (pco) {
+  shared_ptr<Data> data = m_actionLog->LookupActionData(deviceName, seqno);
+  if (data) {
     if (forwardingHint.size() == 0) {
-      m_ccnx->putToCcnd(pco->buf());
+      m_keyChain.sign(*data);
+      m_face.put(*data);
     }
     else {
-      const Bytes& content = pco->buf();
+      data->setName(interest);
       if (m_freshness > 0) {
-        m_ccnx->publishData(interest, content, m_freshness);
+        data->setFreshnessPeriod(time::seconds(m_freshness));
       }
-      else {
-        m_ccnx->publishData(interest, content);
-      }
+      m_keyChain.sign(*data);
+      m_face.put(*data);
     }
   }
   else {
@@ -266,12 +253,18 @@
   ScopedLock(m_dbCacheMutex);
   DbCache::iterator it = m_dbCache.begin();
   while (it != m_dbCache.end()) {
-    ObjectDbPtr db = it->second;
-    if (db->secondsSinceLastUse() >= DB_CACHE_LIFETIME) {
+    shared_ptr<ObjectDb> db = it->second;
+    if (time::steady_clock::now() >= time::seconds(DB_CACHE_LIFETIME) + db->getLastUsed()) {
       m_dbCache.erase(it++);
     }
     else {
       ++it;
     }
   }
+
+  m_flushStateDbCacheEvent = m_scheduler.scheduleEvent(time::seconds(DB_CACHE_LIFETIME),
+                                                       bind(&ContentServer::flushStaleDbCache, this));
 }
+
+} // namespace chronoshare
+} // namespace ndn