forked from matheuscscp/fd8-torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystem.hpp
146 lines (120 loc) · 4.67 KB
/
FileSystem.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* FileSystem.hpp
*
* Created on: Jun 20, 2014
* Author: Pimenta
*/
#ifndef FILESYSTEM_HPP_
#define FILESYSTEM_HPP_
// standard
#include <map>
#include <set>
// local
#include "Helpers.hpp"
class FileSystem {
public:
class File {
public:
uint32_t id;
uint32_t size;
uint32_t peer1;
uint32_t peer2;
std::string author;
File();
void serialize(helpers::ByteQueue& data);
void deserialize(helpers::ByteQueue& data);
void erase();
};
class Folder {
public:
std::map<std::string, Folder> subfolders;
std::map<std::string, File> files;
uint32_t getTotalFolders();
uint32_t getTotalFiles();
uint64_t getTotalSize();
void getPeersFiles(std::map<uint32_t, std::set<uint32_t>>& peersFiles);
void getSingleFiles(std::set<uint32_t>& singleFiles);
void removeOfflinePeers(const std::set<uint32_t>& peers);
Folder* findFolder(const std::string& subPath, Folder** parent = nullptr);
File* findFile(const std::string& subPath, Folder** parent = nullptr);
Folder* findFirstBottomUp(const std::string& subPath, std::string& foundPath);
File* findFile(uint32_t fileID);
void serialize(helpers::ByteQueue& data);
void deserialize(helpers::ByteQueue& data);
void eraseFiles();
private:
Folder* findFolder_(const std::string& subPath, Folder** parent);
File* findFile_(const std::string& subPath, Folder** parent);
Folder* findFirstBottomUp_(const std::string& subPath, std::string& foundPath);
};
class Command {
public:
enum {
DUPLICATION = 0,
BALANCING
};
static helpers::ByteQueue serialize(const std::list<Command*>& cmds);
static std::list<Command*> deserialize(helpers::ByteQueue& data);
virtual ~Command();
void serialize(helpers::ByteQueue& data);
protected:
virtual void serialize_(helpers::ByteQueue& data) = 0;
public:
virtual char type() = 0;
};
class DuplicationCommand : public Command {
public:
uint32_t fileID;
uint32_t srcPeer;
uint32_t dstPeer;
DuplicationCommand(helpers::ByteQueue& data);
DuplicationCommand(uint32_t fileID, uint32_t srcPeer, uint32_t dstPeer);
private:
void serialize_(helpers::ByteQueue& data);
public:
char type();
};
class BalancingCommand : public Command {
public:
uint32_t fileID;
uint32_t srcPeer;
uint32_t peer1;
uint32_t peer2;
BalancingCommand(helpers::ByteQueue& data);
BalancingCommand(uint32_t fileID, uint32_t srcPeer, uint32_t peer1, uint32_t peer2);
private:
void serialize_(helpers::ByteQueue& data);
public:
char type();
};
private:
static Folder rootFolder;
static Folder tmpRootFolder;
static uint32_t nextID;
static uint32_t localIP;
static std::set<uint32_t> storedFiles;
public:
static void init(uint32_t localIP);
static helpers::ByteQueue serialize();
static void deserialize(helpers::ByteQueue& data);
static bool parseName(const std::string& name);
static bool parsePath(const std::string& path);
static Folder* createFolder(const std::string& fullPath);
static Folder* retrieveFolder(const std::string& fullPath, std::string& foundPath);
static Folder* updateFolder(const std::string& fullPath, const std::string& newName);
static bool deleteFolder(const std::string& fullPath);
static File* createFile(const std::string& fullPath, const std::string& author);
static File* createFile(const std::string& fullPath, helpers::ByteQueue& info);
static File* retrieveFile(const std::string& fullPath);
static File* updateFile(const std::string& fullPath, const std::string& newName);
static bool deleteFile(const std::string& fullPath);
static uint32_t getTotalFolders();
static uint32_t getTotalFiles();
static uint64_t getTotalSize();
static std::list<Command*> calculateDuplications(const std::set<uint32_t>& peers);
static std::list<Command*> calculateBalancing(const std::set<uint32_t>& peers);
static void eliminateIntersections(std::list<Command*>& cmds);
static void processCommands(const std::list<Command*>& cmds);
static void initTmpFileSystem();
};
#endif /* FILESYSTEM_HPP_ */