-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataServer.cpp
More file actions
138 lines (129 loc) · 3.49 KB
/
Copy pathDataServer.cpp
File metadata and controls
138 lines (129 loc) · 3.49 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
# include <iostream>
#include <fstream>
# include "DataServer.h"
// Set the chunk size to 2MB
int chunk_size = 2 * 1024 * 1024;
/**
* @brief: Constructor
* @param: name, name of the DataServer
* @return: void
*/
DataServer::DataServer(const std::string &name): ds_name_(name), buffer_(nullptr), is_active_(false)
{
std::string cmd = "mkdir -p " + ds_name_;
system(cmd.c_str());
}
/**
* @brief: Overload the operator () to run the DataServer
* @return: void
*/
void DataServer::operator()()
{
while (true)
{
// First lock the mutex for the DataServer, continue if the DataServer is active
std::unique_lock<std::mutex> lck(mtx);
condition.wait(lck, [this] { return this->is_active_; });
if (cmd == "upload")
{
size_ += buffer_size_ / 1024.0 / 1024.0; // Update the size of the DataServer (in MB)
upload();
}
else if (cmd == "read")
{
read();
}
else if (cmd == "download")
{
download();
}
else if (cmd == "find")
{
find();
}
this->is_active_ = false; // Set the DataServer to inactive
lck.unlock(); // Unlock the mutex
condition.notify_all(); // Notify all the threads
}
}
/**
* @brief: Upload the data to the DataServer
* @return: void
*/
void DataServer::upload()
{
int start = 0;
std::ofstream os;
// Divide the buffer into chunks and store them in different data servers
while(start < buffer_size_)
{
int offset = start / chunk_size; // Which chunk the data belongs to
std::string filePath = ds_name_ + "/" + std::to_string(id) + " " + std::to_string(offset);
os.open(filePath);
if(!os)
std::cerr << "Create file error in dataserver: (file name) " << filePath << std::endl;
os.write(&buffer_[start], std::min(chunk_size, buffer_size_ - start)); // Write the data to the file
start += chunk_size;
os.close();
}
}
/**
* @brief: Read the data from the DataServer
* @return: void
*/
void DataServer::read()
{
int start = 0;
buffer_ = new char[buffer_size_];
while(start < buffer_size_)
{
int offset = start / chunk_size;
std::string filePath = ds_name_ + "/" + std::to_string(id) + " " + std::to_string(offset);
std::ifstream is(filePath);
if(!is)
{
delete []buffer_;
buffer_size_ = 0;
break;
}
is.read(&buffer_[start], std::min(chunk_size, buffer_size_ - start));
start += chunk_size;
}
}
/**
* @brief: Download the data from the DataServer
* @return: void
*/
void DataServer::download()
{
buffer_ = new char[chunk_size];
std::string filePath = ds_name_ + "/" + std::to_string(id) + " " + std::to_string(offset);
std::ifstream is(filePath);
if(!is)
{
delete []buffer_;
buffer_size_ = 0;
}
else
{
is.read(buffer_, std::min(chunk_size, buffer_size_ - chunk_size * offset));
buffer_size_ = is.tellg();
}
}
/**
*@brief: Find the data in the DataServer
*@return: void
*/
void DataServer::find()
{
std::string filePath = ds_name_ + "/" + std::to_string(id) + " " + std::to_string(offset);
// std::cout << "file path in " << filePath << std::endl;
std::ifstream is(filePath);
if(is)
{
// std::cout << "file found in " << ds_name_ << std::endl;
buffer_size_ = 1;
}
else
buffer_size_ = 0;
}