-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameServer.cpp
More file actions
259 lines (255 loc) · 8.89 KB
/
Copy pathNameServer.cpp
File metadata and controls
259 lines (255 loc) · 8.89 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include "NameServer.h"
#include "md5.h"
#include "utils.h"
/**
* @brief: Constructor
* @param: replicate_num, number of replicas
* @return: void
*/
NameServer::NameServer(int replicate_num):num_replicate_(replicate_num), cnt_(0){}
/**
* @brief: Add a DataServer to the NameServer
* @return: void
*/
void NameServer::add(DataServer *server)
{
dataServers_.push_back(server);
}
/**
* @brief: Get the cmd from the user
* @return: vector of strings
*/
std::vector<std::string> NameServer::read_cmd()
{
std::cout << "DFS command line > ";
std::string cmd, tmp;
std::getline(std::cin, cmd);
std::vector<std::string> params;
std::stringstream ss(cmd);
while(ss >> tmp)
params.push_back(tmp);
return params;
}
/**
* @brief: Overload the operator () to run the NameServer
* @return: void
*/
void NameServer::operator()()
{
while (true)
{
std::vector<std::string> cmd_lines = read_cmd();
std::vector<int> idx; // Index of the DataServers
std::ifstream is;
char* buffer = nullptr;
// NOTE: CHECK CMD
if (cmd_lines.empty())
{
std::cerr << "[Error] Input blank." << std::endl;
continue;
}
// NOTE: QUIT
if (cmd_lines[0] == "quit")
{
std::cout << "Exiting..." << std::endl;
exit(0);
}
// NOTE: LIST
else if (cmd_lines[0] == "ls")
{
if (cmd_lines.size() != 1)
{
std::cerr << "[Error] Usage: ls (list the file system)" << std::endl;
continue;
}
else
{
std::cout << "FileName\tFileID\tChunks(2MB)" << std::endl;
fileSys_.list(file_info);
}
continue;
}
// NOTE: UPLOAD
else if (cmd_lines[0] == "upload")
{
if (cmd_lines.size() != 3)
{
std::cerr << "[Error] Usage: upload <src_file_path> <dst_file_path>" << std::endl;
continue;
}
is.open(cmd_lines[1], std::ifstream::ate | std::ifstream::binary);
if(!is)
{
std::cerr << "[Error] Open file error." << std::endl;
continue;
}
else if (!fileSys_.insert_node(cmd_lines[2], true))
{
std::cerr << "[Error] File creation error. File may already exists." << std::endl;
continue;
}
else
{
int file_size = is.tellg();
buffer = new char[file_size];
is.seekg(0, is.beg);
is.read(buffer, file_size);
std::vector<double> ds_server_size;
for (auto &ds: dataServers_)
ds_server_size.push_back(ds->get_size());
idx = argsort<double>(ds_server_size);
++cnt_;
for (int i = 0; i < 3; ++i)
{
std::unique_lock<std::mutex> lck(dataServers_[idx[i]]->mtx);
file_info[cmd_lines[2]] = std::make_pair(cnt_, file_size); // Update the file info (file_name->(file_id, file_size))
DataServer *ds = dataServers_[idx[i]];
ds->cmd = "upload";
ds->id = cnt_;
ds->buffer_ = buffer;
ds->buffer_size_ = file_size;
ds->is_active_ = true;
lck.unlock();
ds->condition.notify_all();
}
}
}
// NOTE: READ / DOWNLOAD
else if (cmd_lines[0] == "read" || cmd_lines[0] == "download")
{
if (cmd_lines.size() != 3 && cmd_lines.size() != 4)
{
std::cerr << "[Error] Usage: " << " read <src_file_path> <dst_file_path>" << std::endl;
std::cerr << "[Error] Usage: " << " download <FileID> <Offset> <dst_file_path>" << std::endl;
continue;
}
else
{
if (cmd_lines[0] == "read" && file_info.find(cmd_lines[1]) == file_info.end())
{
std::cerr << "[Error] File not found." << std::endl;
continue;
}
for (int i = 0; i < 4; ++i)
{
DataServer *ds = dataServers_[i];
std::unique_lock<std::mutex> lck(ds->mtx);
ds->cmd = cmd_lines[0];
if (cmd_lines[0] == "read")
{
std::pair<int, int> fileInfo = file_info[cmd_lines[1]];
ds->id = fileInfo.first;
ds->buffer_size_ = fileInfo.second;
}
else // cmd_lines[0] == "download"
{
ds->id = std::stoi(cmd_lines[1]);
ds->offset = std::stoi(cmd_lines[2]);
}
ds->is_active_ = true;
lck.unlock();
ds->condition.notify_all();
}
}
}
// NOTE: FIND
else if (cmd_lines[0] == "find")
{
if (cmd_lines.size() != 3)
{
std::cerr << "[Error] Usage: find <FileID> <Offset>" << std::endl;
continue;
}
else
{
for(int i = 0; i < 4; ++i)
{
DataServer *ds = dataServers_[i];
std::unique_lock<std::mutex> lck(ds->mtx);
ds->cmd = "find";
ds->id = std::stoi(cmd_lines[1]);
ds->offset = std::stoi(cmd_lines[2]);
ds->is_active_ = true;
lck.unlock();
ds->condition.notify_all();
}
}
}
// NOTE: ERROR
else
{
std::cerr << "[Error] Command not found." << std::endl;
}
// Wait for the DataServers to finish the task
for (const auto &ds: dataServers_)
{
std::unique_lock<std::mutex> lck(ds->mtx);
(ds->condition).wait(lck, [&](){ return !ds->is_active_; });
lck.unlock();
(ds->condition).notify_all();
}
// Post moves for Read/Download the file
if (cmd_lines[0] == "read" || cmd_lines[0] == "download")
{
std::string md5_checksum, pre_checksum;
for (int i = 0; i < 4; ++i)
{
if (dataServers_[i]->buffer_size_ )
{
std::ofstream os;
if (cmd_lines[0] == "read") os.open(cmd_lines[2]);
else if (cmd_lines[0] == "download") os.open(cmd_lines[3]);
if (!os)
{
std::cerr << "[Error] Read/Download file error." << std::endl;
}
else
{
os.write(dataServers_[i]->buffer_, dataServers_[i]->buffer_size_);
os.close();
auto input_string = std::string(dataServers_[i]->buffer_, dataServers_[i]->buffer_size_);
MD5 md5(input_string);
md5_checksum = md5.toStr();
if(!pre_checksum.empty() && pre_checksum != md5_checksum)
{
std::cerr << "[Error] Checksum error for different dataServers. " << std::endl;
}
else if (!pre_checksum.empty() && pre_checksum == md5_checksum)
{
std::cout << "Checksum: " << md5_checksum << std::endl;
}
pre_checksum = md5_checksum;
}
delete []dataServers_[i]->buffer_;
}
}
}
// Post moves for Upload
else if (cmd_lines[0] == "upload")
{
std::cout << "Successfully uploaded. The fileID is " << cnt_ << std::endl;
}
// Post moves for Find and List
else if (cmd_lines[0] == "find" || cmd_lines[0] == "ls")
{
bool not_found = true;
for (int i = 0; i < 4; ++i)
{
if (dataServers_[i]->buffer_size_ != 0)
{
not_found = false;
std::cout << "File FOUND. FileID: " << dataServers_[i]->id << " Offset: " << dataServers_[i]->offset << " Place: " << dataServers_[i]->get_name() << std::endl;
}
}
if (not_found)
std::cout << "File NOT FOUND." << std::endl;
}
delete []buffer;
is.close();
}
}