blob: b2b1e5cdebcc009a7f0f6f425b4fd7c87ece2c24 [file] [log] [blame]
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -08001/* -*- 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"
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080031#include "content-server.h"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080032#include "fetch-manager.h"
33
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080034#include <boost/function.hpp>
35#include <boost/filesystem.hpp>
36#include <boost/shared_ptr.hpp>
37#include <map>
38
39typedef boost::shared_ptr<ActionItem> ActionItemPtr;
40
41// TODO:
42// This class lacks a permanent table to store the files in fetching process
43// and fetch the missing pieces for those in the table after the application launches
44class Dispatcher
45{
46public:
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080047 // sharedFolder is the name to be used in NDN name;
48 // rootDir is the shared folder dir in local file system;
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080049 Dispatcher(const std::string &localUserName
50 , const std::string &sharedFolder
51 , const boost::filesystem::path &rootDir
52 , Ccnx::CcnxWrapperPtr ccnx
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080053 , bool enablePrefixDiscovery = true
54 );
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080055 ~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
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080059
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080060 // callback to process local file change
61 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080062 Did_LocalFile_AddOrModify (const boost::filesystem::path &relativeFilepath);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080063
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080064 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080065 Did_LocalFile_Delete (const boost::filesystem::path &relativeFilepath);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080066
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080067 // for test
68 HashPtr
69 SyncRoot() { return m_core->root(); }
70
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080071private:
72 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080073 Did_LocalFile_AddOrModify_Execute (boost::filesystem::path relativeFilepath); // cannot be const & for Execute event!!! otherwise there will be segfault
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080074
75 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080076 Did_LocalFile_Delete_Execute (boost::filesystem::path relativeFilepath); // cannot be const & for Execute event!!! otherwise there will be segfault
77
78
79private:
80 /**
81 * Callbacks:
82 *
83 x * - from SyncLog: when state changes -> to fetch missing actions
84 *
85 x * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
86 * -> to add action to the action log
87 *
88 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
89 *
90 * - 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
91 *
92 x * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
93 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
94 */
95
96 // callback to process remote sync state change
97 void
Alexander Afanasyevfc720362013-01-24 21:49:48 -080098 Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg);
99
100 void
101 Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800102
103 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800104 Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionName, uint32_t seqno, Ccnx::PcoPtr actionPco);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800105
106 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800107 Did_ActionLog_ActionApply_Delete (const std::string &filename);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800108
109 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800110 Did_ActionLog_ActionApply_Delete_Execute (std::string filename);
111
112 // void
113 // Did_ActionLog_ActionApply_AddOrModify (const std::string &filename, Ccnx::Name device_name, sqlite3_int64 seq_no,
114 // HashPtr hash, time_t m_time, int mode, int seg_num);
115
116 void
117 Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco);
118
119 void
120 Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco);
121
122 void
123 Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName);
124
125 void
126 Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName);
127
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800128 void
129 Did_LocalPrefix_Updated (const Ccnx::Name &prefix);
130
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800131private:
132 void
133 AssembleFile_Execute (const Ccnx::Name &deviceName, const Hash &filehash, const boost::filesystem::path &relativeFilepath);
134
135 // void
136 // fileChanged(const boost::filesystem::path &relativeFilepath, ActionType type);
137
138 // void
139 // syncStateChanged(const SyncStateMsgPtr &stateMsg);
140
141 // void
142 // actionReceived(const ActionItemPtr &actionItem);
143
144 // void
145 // fileSegmentReceived(const Ccnx::Name &name, const Ccnx::Bytes &content);
146
147 // void
148 // fileReady(const Ccnx::Name &fileNamePrefix);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800149
150private:
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800151 Ccnx::CcnxWrapperPtr m_ccnx;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800152 SyncCore *m_core;
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800153 SyncLogPtr m_syncLog;
154 ActionLogPtr m_actionLog;
155
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800156 boost::filesystem::path m_rootDir;
157 Executor m_executor;
158 ObjectManager m_objectManager;
159 Ccnx::Name m_localUserName;
160 // maintain object db ptrs so that we don't need to create them
161 // for every fetched segment of a file
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800162
163 std::map<Hash, ObjectDbPtr> m_objectDbMap;
164
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800165 std::string m_sharedFolder;
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800166 ContentServer *m_server;
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800167 bool m_enablePrefixDiscovery;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800168
169 FetchManagerPtr m_actionFetcher;
170 FetchManagerPtr m_fileFetcher;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800171};
172
173namespace Error
174{
175 struct Dispatcher : virtual boost::exception, virtual std::exception {};
176 typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
177}
178
179#endif // DISPATCHER_H
180