dispatcher

Change-Id: I5ff25afcba3f03249ae32cf9c656f69ecc38d424
diff --git a/src/dispatcher.hpp b/src/dispatcher.hpp
index 526a633..dcc73e4 100644
--- a/src/dispatcher.hpp
+++ b/src/dispatcher.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2016, Regents of the University of California.
+ * Copyright (c) 2013-2017, Regents of the University of California.
  *
  * This file is part of ChronoShare, a decentralized file sharing application over NDN.
  *
@@ -18,41 +18,60 @@
  * See AUTHORS.md for complete list of ChronoShare authors and contributors.
  */
 
-#ifndef DISPATCHER_H
-#define DISPATCHER_H
+#ifndef CHRONOSHARE_SRC_DISPATCHER_HPP
+#define CHRONOSHARE_SRC_DISPATCHER_HPP
+
+#include "core/chronoshare-common.hpp"
 
 #include "action-log.hpp"
-#include "ccnx-wrapper.hpp"
 #include "content-server.hpp"
-#include "executor.hpp"
 #include "fetch-manager.hpp"
 #include "object-db.hpp"
 #include "object-manager.hpp"
-#include "state-server.hpp"
 #include "sync-core.hpp"
 
 #include <boost/filesystem.hpp>
-#include <boost/function.hpp>
-#include <boost/shared_ptr.hpp>
+#include <boost/filesystem/fstream.hpp>
 #include <map>
 
-typedef boost::shared_ptr<ActionItem> ActionItemPtr;
+namespace ndn {
+namespace chronoshare {
+
+typedef shared_ptr<ActionItem> ActionItemPtr;
 
 // TODO:
 // This class lacks a permanent table to store the files in fetching process
 // and fetch the missing pieces for those in the table after the application launches
+
+/**
+  * @brief Class synchroize different components
+  *
+  * - from SyncLog: when state changes -> to fetch missing actions
+  *
+  * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
+  *                                                     -> to add action to the action log
+  *
+  * - from ActionLog/Delete: when action applied(file state changed, file deleted) -> to delete local file
+  *
+  * - from ActionLog/AddOrUpdate: when action applied(file state changes, file added or modified) ->
+  *                               to assemble the file if file is available in the ObjectDb, otherwise, do nothing
+  *
+  * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
+  *                            when file fetch is completed   -> if file belongs to FileState, then assemble
+  *                            it to filesystem. Don't do anything otherwise
+  */
+
 class Dispatcher
 {
 public:
   // sharedFolder is the name to be used in NDN name;
   // rootDir is the shared folder dir in local file system;
   Dispatcher(const std::string& localUserName, const std::string& sharedFolder,
-             const boost::filesystem::path& rootDir, Ccnx::CcnxWrapperPtr ccnx,
-             bool enablePrefixDiscovery = true);
+             const boost::filesystem::path& rootDir, Face& face, bool enablePrefixDiscovery = true);
   ~Dispatcher();
 
-  // ----- Callbacks, they only submit the job to executor and immediately return so that event processing thread won't be blocked for too long -------
-
+  // ----- Callbacks, they only submit the job to executor and immediately return so that event
+  // processing thread won't be blocked for too long -------
 
   // callback to process local file change
   void
@@ -68,7 +87,7 @@
   Restore_LocalFile(FileItemPtr file);
 
   // for test
-  HashPtr
+  ConstBufferPtr
   SyncRoot()
   {
     return m_core->root();
@@ -83,12 +102,17 @@
 
 private:
   void
-  Did_LocalFile_AddOrModify_Execute(
-    boost::filesystem::path relativeFilepath); // cannot be const & for Execute event!!! otherwise there will be segfault
+  Did_LocalFile_AddOrModify_Execute(boost::filesystem::path relativeFilepath); // cannot be const &
+                                                                               // for Execute
+                                                                               // event!!! otherwise
+                                                                               // there will be
+                                                                               // segfault
 
   void
-  Did_LocalFile_Delete_Execute(
-    boost::filesystem::path relativeFilepath); // cannot be const & for Execute event!!! otherwise there will be segfault
+  Did_LocalFile_Delete_Execute(boost::filesystem::path relativeFilepath); // cannot be const & for
+                                                                          // Execute event!!!
+                                                                          // otherwise there will be
+                                                                          // segfault
 
   void
   Restore_LocalFile_Execute(FileItemPtr file);
@@ -97,17 +121,19 @@
   /**
    * Callbacks:
    *
- x * - from SyncLog: when state changes -> to fetch missing actions
+   * - from SyncLog: when state changes -> to fetch missing actions
    *
- x * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
+   * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
    *                                                     -> to add action to the action log
    *
-   * - from ActionLog/Delete:      when action applied (file state changed, file deleted)           -> to delete local file
+   * - from ActionLog/Delete: when action applied(file state changed, file deleted) -> to delete local file
    *
-   * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> to assemble the file if file is available in the ObjectDb, otherwise, do nothing
+   * - from ActionLog/AddOrUpdate: when action applied(file state changes, file added or modified) ->
+   *                               to assemble the file if file is available in the ObjectDb, otherwise, do nothing
    *
- x * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
-   *                            when file fetch is completed   -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
+   * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
+   *                            when file fetch is completed   -> if file belongs to FileState, then assemble
+   *                            it to filesystem. Don't do anything otherwise
    */
 
   // callback to process remote sync state change
@@ -118,8 +144,8 @@
   Did_SyncLog_StateChange_Execute(SyncStateMsgPtr stateMsg);
 
   void
-  Did_FetchManager_ActionFetch(const Ccnx::Name& deviceName, const Ccnx::Name& actionName,
-                               uint32_t seqno, Ccnx::PcoPtr actionPco);
+  Did_FetchManager_ActionFetch(const Name& deviceName, const Name& actionName, uint32_t seqno,
+                               shared_ptr<Data> actionData);
 
   void
   Did_ActionLog_ActionApply_Delete(const std::string& filename);
@@ -128,29 +154,31 @@
   Did_ActionLog_ActionApply_Delete_Execute(std::string filename);
 
   // void
-  // Did_ActionLog_ActionApply_AddOrModify (const std::string &filename, Ndnx::Name device_name, sqlite3_int64 seq_no,
-  //                                        HashPtr hash, time_t m_time, int mode, int seg_num);
+  // Did_ActionLog_ActionApply_AddOrModify(const std::string &filename, Name device_name,
+  // sqlite3_int64 seq_no,
+  //                                        ConstBufferPtr hash, time_t m_time, int mode, int
+  //                                        seg_num);
 
   void
-  Did_FetchManager_FileSegmentFetch(const Ccnx::Name& deviceName, const Ccnx::Name& fileSegmentName,
-                                    uint32_t segment, Ccnx::PcoPtr fileSegmentPco);
+  Did_FetchManager_FileSegmentFetch(const Name& deviceName, const Name& fileSegmentName,
+                                    uint32_t segment, shared_ptr<Data> fileSegmentData);
 
   void
-  Did_FetchManager_FileSegmentFetch_Execute(Ccnx::Name deviceName, Ccnx::Name fileSegmentName,
-                                            uint32_t segment, Ccnx::PcoPtr fileSegmentPco);
+  Did_FetchManager_FileSegmentFetch_Execute(Name deviceName, Name fileSegmentName, uint32_t segment,
+                                            shared_ptr<Data> fileSegmentData);
 
   void
-  Did_FetchManager_FileFetchComplete(const Ccnx::Name& deviceName, const Ccnx::Name& fileBaseName);
+  Did_FetchManager_FileFetchComplete(const Name& deviceName, const Name& fileBaseName);
 
   void
-  Did_FetchManager_FileFetchComplete_Execute(Ccnx::Name deviceName, Ccnx::Name fileBaseName);
+  Did_FetchManager_FileFetchComplete_Execute(Name deviceName, Name fileBaseName);
 
   void
-  Did_LocalPrefix_Updated(const Ccnx::Name& prefix);
+  Did_LocalPrefix_Updated(const Name& prefix);
 
 private:
   void
-  AssembleFile_Execute(const Ccnx::Name& deviceName, const Hash& filehash,
+  AssembleFile_Execute(const Name& deviceName, const Buffer& filehash,
                        const boost::filesystem::path& relativeFilepath);
 
   // void
@@ -163,42 +191,48 @@
   // actionReceived(const ActionItemPtr &actionItem);
 
   // void
-  // fileSegmentReceived(const Ndnx::Name &name, const Ndnx::Bytes &content);
+  // fileSegmentReceived(const Name &name, const Ccnx::Bytes &content);
 
   // void
-  // fileReady(const Ndnx::Name &fileNamePrefix);
+  // fileReady(const Name &fileNamePrefix);
 
 private:
-  Ccnx::CcnxWrapperPtr m_ccnx;
-  SyncCore* m_core;
+  Face& m_face;
+  unique_ptr<SyncCore> m_core;
   SyncLogPtr m_syncLog;
   ActionLogPtr m_actionLog;
   FileStatePtr m_fileState;
   FileStatePtr m_fileStateCow;
 
   boost::filesystem::path m_rootDir;
-  Executor m_executor;
+  boost::asio::io_service& m_ioService;
+
   ObjectManager m_objectManager;
-  Ndnx::Name m_localUserName;
+  Name m_localUserName;
   // maintain object db ptrs so that we don't need to create them
   // for every fetched segment of a file
 
-  std::map<Hash, ObjectDbPtr> m_objectDbMap;
+  std::map<Buffer, shared_ptr<ObjectDb>> m_objectDbMap;
 
   std::string m_sharedFolder;
-  ContentServer* m_server;
-  StateServer* m_stateServer;
+  unique_ptr<ContentServer> m_server;
+  //unique_ptr<StateServer> m_stateServer;
   bool m_enablePrefixDiscovery;
 
   FetchManagerPtr m_actionFetcher;
   FetchManagerPtr m_fileFetcher;
+
+  KeyChain m_keyChain;
 };
 
-namespace Error {
+namespace error {
 struct Dispatcher : virtual boost::exception, virtual std::exception
 {
 };
 typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
-}
+} // namespace error
 
-#endif // DISPATCHER_H
+} // namespace chronoshare
+} // namespace ndn
+
+#endif // CHRONOSHARE_SRC_DISPATCHER_HPP