Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #ifndef DISPATCHER_H |
| 23 | #define DISPATCHER_H |
| 24 | |
| 25 | #include "action-log.h" |
| 26 | #include "sync-core.h" |
| 27 | #include "ccnx-wrapper.h" |
| 28 | #include "executor.h" |
| 29 | #include "object-db.h" |
| 30 | #include "object-manager.h" |
| 31 | #include <boost/function.hpp> |
| 32 | #include <boost/filesystem.hpp> |
| 33 | #include <boost/shared_ptr.hpp> |
| 34 | #include <map> |
| 35 | |
| 36 | typedef boost::shared_ptr<ActionItem> ActionItemPtr; |
| 37 | |
| 38 | // TODO: |
| 39 | // This class lacks a permanent table to store the files in fetching process |
| 40 | // and fetch the missing pieces for those in the table after the application launches |
| 41 | class Dispatcher |
| 42 | { |
| 43 | public: |
| 44 | typedef enum |
| 45 | { |
| 46 | UPDATE = 0, |
| 47 | DELETE = 1 |
| 48 | } ActionType; |
| 49 | |
| 50 | // sharedFolder is the name to be used in NDN name; |
| 51 | // rootDir is the shared folder dir in local file system; |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame^] | 52 | Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix, |
| 53 | const std::string &sharedFolder, const boost::filesystem::path &rootDir, |
| 54 | Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 55 | ~Dispatcher(); |
| 56 | |
| 57 | // ----- Callbacks, they only submit the job to executor and immediately return so that event processing thread won't be blocked for too long ------- |
| 58 | |
| 59 | // callback to process local file change |
| 60 | void |
| 61 | fileChangedCallback(const boost::filesystem::path &relativeFilepath, ActionType type); |
| 62 | |
| 63 | // callback to process remote sync state change |
| 64 | void |
| 65 | syncStateChangedCallback(const SyncStateMsgPtr &stateMsg); |
| 66 | |
| 67 | // callback to process remote action data |
| 68 | void |
| 69 | actionReceivedCallback(const ActionItemPtr &actionItem); |
| 70 | |
| 71 | // callback to porcess file data |
| 72 | void |
| 73 | fileSegmentReceivedCallback(const Ccnx::Name &name, const Ccnx::Bytes &content); |
| 74 | |
| 75 | // callback to assemble file |
| 76 | void |
| 77 | fileReadyCallback(const Ccnx::Name &fileNamePrefix); |
| 78 | |
| 79 | private: |
| 80 | void |
| 81 | fileChanged(const boost::filesystem::path &relativeFilepath, ActionType type); |
| 82 | |
| 83 | void |
| 84 | syncStateChanged(const SyncStateMsgPtr &stateMsg); |
| 85 | |
| 86 | void |
| 87 | actionReceived(const ActionItemPtr &actionItem); |
| 88 | |
| 89 | void |
| 90 | fileSegmentReceived(const Ccnx::Name &name, const Ccnx::Bytes &content); |
| 91 | |
| 92 | void |
| 93 | fileReady(const Ccnx::Name &fileNamePrefix); |
| 94 | |
| 95 | private: |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame^] | 96 | Ccnx::CcnxWrapperPtr m_ccnx; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 97 | SyncCore *m_core; |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame^] | 98 | SyncLogPtr m_syncLog; |
| 99 | ActionLogPtr m_actionLog; |
| 100 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 101 | boost::filesystem::path m_rootDir; |
| 102 | Executor m_executor; |
| 103 | ObjectManager m_objectManager; |
| 104 | Ccnx::Name m_localUserName; |
| 105 | // maintain object db ptrs so that we don't need to create them |
| 106 | // for every fetched segment of a file |
| 107 | map<Ccnx::Name, ObjectDbPtr> m_objectDbMap; |
| 108 | std::string m_sharedFolder; |
| 109 | }; |
| 110 | |
| 111 | namespace Error |
| 112 | { |
| 113 | struct Dispatcher : virtual boost::exception, virtual std::exception {}; |
| 114 | typedef boost::error_info<struct tag_errmsg, std::string> error_info_str; |
| 115 | } |
| 116 | |
| 117 | #endif // DISPATCHER_H |
| 118 | |