blob: 579ff79f207f3ceb0a288baead54d05322851bd8 [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;
52 Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix, const std::string &sharedFolder, const boost::filesystem::path &rootDir, const Ccnx::CcnxWrapperPtr &ccnx, const SchedulerPtr &scheduler, int poolSize = 2);
53 ~Dispatcher();
54
55 // ----- Callbacks, they only submit the job to executor and immediately return so that event processing thread won't be blocked for too long -------
56
57 // callback to process local file change
58 void
59 fileChangedCallback(const boost::filesystem::path &relativeFilepath, ActionType type);
60
61 // callback to process remote sync state change
62 void
63 syncStateChangedCallback(const SyncStateMsgPtr &stateMsg);
64
65 // callback to process remote action data
66 void
67 actionReceivedCallback(const ActionItemPtr &actionItem);
68
69 // callback to porcess file data
70 void
71 fileSegmentReceivedCallback(const Ccnx::Name &name, const Ccnx::Bytes &content);
72
73 // callback to assemble file
74 void
75 fileReadyCallback(const Ccnx::Name &fileNamePrefix);
76
77private:
78 void
79 fileChanged(const boost::filesystem::path &relativeFilepath, ActionType type);
80
81 void
82 syncStateChanged(const SyncStateMsgPtr &stateMsg);
83
84 void
85 actionReceived(const ActionItemPtr &actionItem);
86
87 void
88 fileSegmentReceived(const Ccnx::Name &name, const Ccnx::Bytes &content);
89
90 void
91 fileReady(const Ccnx::Name &fileNamePrefix);
92
93private:
94 SyncCore *m_core;
95 ActionLogPtr m_log;
96 CcnxWrapperPtr m_ccnx;
97 boost::filesystem::path m_rootDir;
98 Executor m_executor;
99 ObjectManager m_objectManager;
100 Ccnx::Name m_localUserName;
101 // maintain object db ptrs so that we don't need to create them
102 // for every fetched segment of a file
103 map<Ccnx::Name, ObjectDbPtr> m_objectDbMap;
104 std::string m_sharedFolder;
105};
106
107namespace Error
108{
109 struct Dispatcher : virtual boost::exception, virtual std::exception {};
110 typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
111}
112
113#endif // DISPATCHER_H
114