-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
362 lines (256 loc) · 8.82 KB
/
server.cpp
File metadata and controls
362 lines (256 loc) · 8.82 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/****************/
/* Oleksandr Diachenko */
/* Date */
/* CS 244B */
/* Spring 2016 */
/****************/
#define DEBUG
#include <stdio.h>
#include <server.h>
#include <fcntl.h>
#include <fstream>
#include <map>
#include <sstream>
#include <sys/stat.h>
/* ------------------------------------------------------------------ */
class Server {
public:
Server() {
}
Server(string mountPoint, int packetLoss, Network *network) {
this->mountPoint = mountPoint;
this->packetLoss = packetLoss;
this->network = network;
}
void runServer() {
this->prepareLocalFileSystem();
while (1) {
char body[MAX_PACKET_SIZE] = { };
int bytes = this->network->readFromSocket(body, 0);
if (bytes > 0) {
DfsPacket *packet = new DfsPacket(body, bytes);
this->processPacket(packet);
}
}
}
void prepareLocalFileSystem() {
struct stat sb;
if (stat(this->mountPoint.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
exitError("machine already in use");
}
int createStatus = mkdir(this->mountPoint.c_str(), S_IRWXU | S_IRWXG);
}
string getMountPoint() {
return this->mountPoint;
}
private:
string mountPoint;
int packetLoss;
Network *network;
map<string, map<uint8_t, int> > transactionIdPerClient;
map<int, string> fileNamePerFileDescriptor;
uint8_t _transactionId;
void beginTransaction(string clientId, string fileName) {
string uncommittedFileName = this->getUncommittedFileName(
this->_transactionId, clientId);
string uncommittedFileNameAbsolute = this->getAbsolutePath(
uncommittedFileName);
int fd = open(uncommittedFileNameAbsolute.c_str(), O_WRONLY | O_CREAT,
S_IRWXU | S_IRWXG);
this->copyContent(uncommittedFileNameAbsolute,
this->getAbsolutePath(fileName));
transactionIdPerClient[clientId][this->_transactionId] = fd;
fileNamePerFileDescriptor[fd] = fileName;
BeginTransactionResponseEvent *beginTransactionEvent =
new BeginTransactionResponseEvent(clientId, this->getServerId(),
this->_transactionId);
this->network->sendPacket(beginTransactionEvent, false, 1);
printf("OPENFILE: %s, fd: %d, transactionId: %d\n", fileName.c_str(),
fd, this->_transactionId);
this->_transactionId++;
}
void writeBlock(uint8_t tranId, string clientId, string serverId,
int offset, int blockSize, char* bytes) {
if (this->transactionIdPerClient[clientId].count(tranId) == 0) {
printf("Invalid transaction, id: %d\n", tranId);
return;
}
int fd = this->transactionIdPerClient[clientId][tranId];
printf(
"WRITE BLOCK to file descriptor: %d, clientId: %s, transactionId: %d, size: %d, offset: %d\n",
fd, clientId.c_str(), tranId, blockSize, offset);
if (lseek(fd, offset, SEEK_SET) < 0) {
perror("WriteBlock Seek");
}
if ((write(fd, bytes, blockSize)) != blockSize) {
perror("WriteBlock write");
}
}
void processPacket(DfsPacket *packet) {
BaseEvent *event = this->network->packetToEvent(packet);
handleEvent(event);
}
string getServerId() {
char hostname[1024];
hostname[1023] = '\0';
gethostname(hostname, 1023);
std::ostringstream ss;
ss << ::getppid() << hostname;
string serverId = ss.str();
return serverId;
}
void handleEvent(BaseEvent *event) {
uint8_t eventType = event->getEventType();
if (event->getReceiverNodeId() == this->getServerId()
|| event->isBroadcast())
{
switch (eventType) {
case EVENT_TYPE_BEGIN_TRANSACTION_REQUEST:
beginTransaction(event->getSenderNodeId(),
((BeginTransactionRequestEvent *) event)->getFileName());
break;
case EVENT_TYPE_WRITE_BLOCK:
this->writeBlock(
((WriteBlockEvent *) event)->getTransactionId(),
((WriteBlockEvent *) event)->getSenderNodeId(),
((WriteBlockEvent *) event)->getReceiverNodeId(),
((WriteBlockEvent *) event)->getOffset(),
((WriteBlockEvent *) event)->getBlockSize(),
((WriteBlockEvent *) event)->getBytes());
break;
case EVENT_TYPE_COMMIT_REQUEST:
this->commitRequest(
((CommitRequestEvent *) event)->getTransactionId(),
((CommitRequestEvent *) event)->getSenderNodeId());
break;
case EVENT_TYPE_COMMIT:
this->finishCommit(((CommitEvent *) event)->getTransactionId(),
((CommitEvent *) event)->getSenderNodeId(),
((CommitEvent *) event)->getCloseFile());
break;
case EVENT_TYPE_ROLLBACK:
this->rollback(((RollbackEvent *) event)->getTransactionId(),
((RollbackEvent *) event)->getSenderNodeId());
break;
default:
break;
};
} else {
}
}
void commitRequest(uint8_t transactionId, string clientId) {
uint8_t readyToCommit = validateTransaction(transactionId, clientId);
CommitVoteEvent *event = new CommitVoteEvent(clientId,
this->getServerId(), transactionId, readyToCommit);
this->network->sendPacket(event, true, 1);
}
void finishCommit(uint8_t transactionId, string clientId, bool closeFile) {
int commitTransactionStatus = commitTransaction(transactionId, clientId,
closeFile);
if (commitTransactionStatus == NormalReturn) {
CommitRollbackAckEvent *event = new CommitRollbackAckEvent(clientId,
this->getServerId(), transactionId);
this->network->sendPacket(event, true, 1);
} else {
printf("Unable to finish commit for transactionId: %d\n",
transactionId);
}
}
void rollback(uint8_t transactionId, string clientId) {
printf("ROLLBACK transaction id : %d\n", transactionId);
int fd = this->transactionIdPerClient[clientId][transactionId];
string uncommittedFileName = this->getUncommittedFileName(transactionId,
clientId);
string uncommittedFileNameAbsolute = this->getAbsolutePath(
uncommittedFileName);
string fileName = fileNamePerFileDescriptor[fd];
this->copyContent(uncommittedFileNameAbsolute,
this->getAbsolutePath(fileName));
CommitRollbackAckEvent *event = new CommitRollbackAckEvent(clientId,
this->getServerId(), transactionId);
this->network->sendPacket(event, true, 1);
}
uint8_t validateTransaction(uint8_t transactionId, string clientId) {
return POSITIVE_VOTE;
}
uint8_t commitTransaction(uint8_t transactionId, string clientId,
bool closeFile) {
printf("COMMIT transaction id : %d\n", transactionId);
if (closeFile)
printf("CLOSEFILE\n");
int fd = this->transactionIdPerClient[clientId][transactionId];
if (closeFile) {
//Close file descriptor
int closeStatus = close(fd);
if (closeStatus != 0) {
printf("Unable to close file, probably already closed\n");
return ErrorReturn;
}
flushChanges(fd, transactionId, clientId, closeFile);
//Delete info about transaction
transactionIdPerClient[clientId].erase(transactionId);
fileNamePerFileDescriptor.erase(fd);
} else
flushChanges(fd, transactionId, clientId, closeFile);
return NormalReturn;
}
void flushChanges(int fd, uint8_t transactionId, string clientId,
bool closeFile) {
//Flush changes to filesystem
string absoluteFileName = this->getAbsolutePath(
fileNamePerFileDescriptor[fd]);
string absoluteUncommittedFileName = this->getAbsolutePath(
this->getUncommittedFileName(transactionId, clientId));
this->copyContent(absoluteFileName, absoluteUncommittedFileName);
chmod(absoluteFileName.c_str(), S_IRWXU | S_IRWXG | S_IROTH);
if (closeFile) {
int unlinkStatus = unlink(absoluteUncommittedFileName.c_str());
if (unlinkStatus != 0)
printf("Unable to unlink temp file: %s\n",
absoluteUncommittedFileName.c_str());
}
}
string getUncommittedFileName(uint8_t transactionId, string clientId) {
ostringstream ss;
ss << ".unstaged" << (int) transactionId << getServerId();
string uncommitedFileName = ss.str();
return uncommitedFileName;
}
string getAbsolutePath(string relativeFileName) {
return this->getMountPoint() + "//" + relativeFileName.c_str();
}
void copyContent(string destFileName, string sourceFileName) {
ifstream sourceFile(sourceFileName.c_str(), std::ios::binary);
ofstream destFile(destFileName.c_str(), std::ios::binary);
destFile << sourceFile.rdbuf();
destFile.close();
sourceFile.close();
}
};
int main(int argc, char* argv[]) {
string port_option, mount_option, drop_option;
unsigned short portNum;
int packetLoss;
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-port") == 0)
port_option = argv[i + 1];
if (strcmp(argv[i], "-mount") == 0)
mount_option = argv[i + 1];
if (strcmp(argv[i], "-drop") == 0)
drop_option = argv[i + 1];
}
if (port_option.empty())
exitError("Please provide port value");
if (mount_option.empty())
exitError("Please provide mount value");
if (drop_option.empty())
exitError("Please provide drop value");
portNum = atoi(port_option.c_str());
packetLoss = atoi(drop_option.c_str());
Network * n = new Network(portNum, packetLoss);
Server * s = new Server(mount_option, packetLoss, n);
n->netInit();
s->runServer();
return (NormalReturn);
}
/* ------------------------------------------------------------------ */