blob: 3ebf67e15bc198ac48c717b3030f242f5eeeef4b [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"
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080032#include <boost/function.hpp>
33#include <boost/filesystem.hpp>
34#include <boost/shared_ptr.hpp>
35#include <map>
36
37typedef boost::shared_ptr<ActionItem> ActionItemPtr;
38
39// TODO:
40// This class lacks a permanent table to store the files in fetching process
41// and fetch the missing pieces for those in the table after the application launches
42class Dispatcher
43{
44public:
45 typedef enum
46 {
47 UPDATE = 0,
48 DELETE = 1
49 } ActionType;
50
51 // sharedFolder is the name to be used in NDN name;
52 // rootDir is the shared folder dir in local file system;
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080053 Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix,
54 const std::string &sharedFolder, const boost::filesystem::path &rootDir,
55 Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080056 ~Dispatcher();
57
58 // ----- Callbacks, they only submit the job to executor and immediately return so that event processing thread won't be blocked for too long -------
59
60 // callback to process local file change
61 void
62 fileChangedCallback(const boost::filesystem::path &relativeFilepath, ActionType type);
63
64 // callback to process remote sync state change
65 void
66 syncStateChangedCallback(const SyncStateMsgPtr &stateMsg);
67
68 // callback to process remote action data
69 void
70 actionReceivedCallback(const ActionItemPtr &actionItem);
71
72 // callback to porcess file data
73 void
74 fileSegmentReceivedCallback(const Ccnx::Name &name, const Ccnx::Bytes &content);
75
76 // callback to assemble file
77 void
78 fileReadyCallback(const Ccnx::Name &fileNamePrefix);
79
80private:
81 void
82 fileChanged(const boost::filesystem::path &relativeFilepath, ActionType type);
83
84 void
85 syncStateChanged(const SyncStateMsgPtr &stateMsg);
86
87 void
88 actionReceived(const ActionItemPtr &actionItem);
89
90 void
91 fileSegmentReceived(const Ccnx::Name &name, const Ccnx::Bytes &content);
92
93 void
94 fileReady(const Ccnx::Name &fileNamePrefix);
95
96private:
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080097 Ccnx::CcnxWrapperPtr m_ccnx;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080098 SyncCore *m_core;
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080099 SyncLogPtr m_syncLog;
100 ActionLogPtr m_actionLog;
101
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800102 boost::filesystem::path m_rootDir;
103 Executor m_executor;
104 ObjectManager m_objectManager;
105 Ccnx::Name m_localUserName;
106 // maintain object db ptrs so that we don't need to create them
107 // for every fetched segment of a file
108 map<Ccnx::Name, ObjectDbPtr> m_objectDbMap;
109 std::string m_sharedFolder;
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800110 ContentServer *m_server;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800111};
112
113namespace Error
114{
115 struct Dispatcher : virtual boost::exception, virtual std::exception {};
116 typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
117}
118
119#endif // DISPATCHER_H
120