Merge remote-tracking branch 'git.irl/master'

Conflicts:
	src/dispatcher.cc
diff --git a/src/content-server.cc b/src/content-server.cc
new file mode 100644
index 0000000..2432885
--- /dev/null
+++ b/src/content-server.cc
@@ -0,0 +1,113 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "content-server.h"
+
+using namespace Ccnx;
+using namespace std;
+
+ContentServer::ContentServer(CcnxWrapperPtr ccnx, ActionLogPtr actionLog, const boost::filesystem::path &rootDir)
+              : m_ccnx(ccnx)
+              , m_actionLog(actionLog)
+              , m_dbFolder(rootDir / ".chronoshare")
+{
+}
+
+ContentServer::~ContentServer()
+{
+  WriteLock lock(m_mutex);
+  int size = m_prefixes.size();
+  for (PrefixIt it = m_prefixes.begin(); it != m_prefixes.end(); ++it)
+  {
+    m_ccnx->clearInterestFilter(*it);
+  }
+}
+
+void
+ContentServer::registerPrefix(const Name &prefix)
+{
+  m_ccnx->setInterestFilter(prefix, bind(&ContentServer::serve, this, _1));
+  WriteLock lock(m_mutex);
+  m_prefixes.insert(prefix);
+}
+
+void
+ContentServer::deregisterPrefix(const Name &prefix)
+{
+  WriteLock lock(m_mutex);
+  PrefixIt it = m_prefixes.find(prefix);
+  if (it != m_prefixes.end())
+  {
+    m_ccnx->clearInterestFilter(*it);
+    m_prefixes.erase(it);
+  }
+}
+
+void
+ContentServer::serve(const Name &interest)
+{
+  ReadLock lock(m_mutex);
+  for (PrefixIt it = m_prefixes.begin(); it != m_prefixes.end(); ++it)
+  {
+    Name prefix = *it;
+    int prefixSize = prefix.size();
+    int interestSize = interest.size();
+    // this is the prefix of the interest
+    if (prefixSize <= interestSize && interest.getPartialName(0, prefixSize) == prefix)
+    {
+      // originalName should be either
+      // /device-name/file/file-hash/segment, or
+      // /device-name/action/shared-folder/seq
+      Name originalName = interest.getPartialName(prefixSize);
+      int nameSize = originalName.size();
+      if (nameSize > 3)
+      {
+        Name deviceName = originalName.getPartialName(0, nameSize - 3);
+        string type = originalName.getCompAsString(nameSize - 3);
+        BytesPtr co;
+        if (type == "action")
+        {
+          // TODO:
+          // get co from m_actionLog
+        }
+        else if (type == "file")
+        {
+          Bytes hashBytes = originalName.getComp(nameSize - 2);
+          Hash hash(head(hashBytes), hashBytes.size());
+          ostringstream oss;
+          oss << hash;
+          int64_t segment = originalName.getCompAsInt(nameSize - 1);
+          ObjectDb db(m_dbFolder, oss.str());
+          co = db.fetchSegment(deviceName, segment);
+        }
+        else
+        {
+          cerr << "Discard interest that ContentServer does not know how to handle: " << interest << ", prefix: " << prefix << endl;
+        }
+
+        if (co)
+        {
+          m_ccnx->publishData(interest, *co);
+        }
+      }
+    }
+  }
+}
diff --git a/src/content-server.h b/src/content-server.h
new file mode 100644
index 0000000..3eb2325
--- /dev/null
+++ b/src/content-server.h
@@ -0,0 +1,58 @@
+#ifndef CONTENT_SERVER_H
+#define CONTENT_SERVER_H
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "ccnx-wrapper.h"
+#include "object-db.h"
+#include "action-log.h"
+#include <set>
+#include <boost/thread/shared_mutex.hpp>
+#include <boost/thread/locks.hpp>
+
+class ContentServer
+{
+public:
+  ContentServer(Ccnx::CcnxWrapperPtr ccnx, ActionLogPtr actionLog, const boost::filesystem::path &rootDir);
+  ~ContentServer();
+
+  // the assumption is, when the interest comes in, interest is informs of
+  // /some-prefix/topology-independent-name
+  // currently /topology-independent-name must begin with /action or /file
+  // so that ContentServer knows where to look for the content object
+  void registerPrefix(const Ccnx::Name &prefix);
+  void deregisterPrefix(const Ccnx::Name &prefix);
+
+  void
+  serve(const Ccnx::Name &interest);
+
+private:
+  Ccnx::CcnxWrapperPtr m_ccnx;
+  ActionLogPtr m_actionLog;
+  typedef boost::shared_mutex Mutex;
+  typedef boost::unique_lock<Mutex> WriteLock;
+  typedef boost::shared_lock<Mutex> ReadLock;
+  typedef std::set<Ccnx::Name>::iterator PrefixIt;
+  std::set<Ccnx::Name> m_prefixes;
+  Mutex m_mutex;
+  boost::filesystem::path m_dbFolder;
+};
+#endif // CONTENT_SERVER_H
diff --git a/src/dispatcher.cc b/src/dispatcher.cc
index bdcc469..d7e570a 100644
--- a/src/dispatcher.cc
+++ b/src/dispatcher.cc
@@ -37,13 +37,19 @@
            , m_objectManager(ccnx, rootDir)
            , m_localUserName(localUserName)
            , m_sharedFolder(sharedFolder)
+           , m_server(NULL)
 {
   m_syncLog = make_shared<SyncLog>(path, localUserName);
   m_actionLog = make_shared<ActionLog>(m_ccnx, path, m_syncLog, sharedFolder);
+  Name syncPrefix = Name(BROADCAST_DOMAIN)(sharedFolder);
 
-  Name syncPrefix(BROADCAST_DOMAIN + sharedFolder);
+  m_server = new ContentServer(m_ccnx, m_actionLog, rootDir);
+  m_server->registerPrefix(localPrefix);
+  m_server->registerPrefix(syncPrefix);
+
   m_core = new SyncCore (m_syncLog, localUserName, localPrefix, syncPrefix,
                          bind(&Dispatcher::syncStateChanged, this, _1), ccnx, scheduler);
+
 }
 
 Dispatcher::~Dispatcher()
@@ -53,6 +59,12 @@
     delete m_core;
     m_core = NULL;
   }
+
+  if (m_server != NULL)
+  {
+    delete m_server;
+    m_server = NULL;
+  }
 }
 
 void
@@ -115,8 +127,8 @@
           int seg_num;
           tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
 
-          time_t wtime = filesystem::last_write_time (absolutePath);
-          filesystem::file_status stat = filesystem::status (absolutePath);
+          time_t wtime = filesystem::last_write_time(absolutePath);
+          filesystem::file_status stat = filesystem::status(absolutePath);
           int mode = stat.permissions();
 
           m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(), *hash, wtime, mode, seg_num);
diff --git a/src/dispatcher.h b/src/dispatcher.h
index 7c3220e..3ebf67e 100644
--- a/src/dispatcher.h
+++ b/src/dispatcher.h
@@ -28,6 +28,7 @@
 #include "executor.h"
 #include "object-db.h"
 #include "object-manager.h"
+#include "content-server.h"
 #include <boost/function.hpp>
 #include <boost/filesystem.hpp>
 #include <boost/shared_ptr.hpp>
@@ -106,6 +107,7 @@
   // for every fetched segment of a file
   map<Ccnx::Name, ObjectDbPtr> m_objectDbMap;
   std::string m_sharedFolder;
+  ContentServer *m_server;
 };
 
 namespace Error