blob: 7c3220e03e439c83f49fc64df24d112c5f97a8f5 [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"
31#include <boost/function.hpp>
32#include <boost/filesystem.hpp>
33#include <boost/shared_ptr.hpp>
34#include <map>
35
36typedef 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
41class Dispatcher
42{
43public:
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 Afanasyevcbda9922013-01-22 11:21:12 -080052 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 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
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
79private:
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
95private:
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080096 Ccnx::CcnxWrapperPtr m_ccnx;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080097 SyncCore *m_core;
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080098 SyncLogPtr m_syncLog;
99 ActionLogPtr m_actionLog;
100
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800101 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
111namespace 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