-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.cpp
311 lines (281 loc) · 7.94 KB
/
server.cpp
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
#include<cstdlib>
#include<iostream>
#include "network.h"
#include "server.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <map>
//#define DEBUG
using namespace std;
Server M;
void sendTestPacket(Server *M)
{
cout << "sending packet "<<M->theSocket() <<endl;
RFPacket pack;
pack.type = 0;
memset(pack.body, 0, sizeof(pack.body));
strcpy((char *)&pack.body, "Hello World\n");
sendPacket(M, &pack);
}
void handleReqServerInit(struct IdPacket * id)
{
unsigned int clientId = ntohl(id->Id);
#ifdef DEBUG
cout << "Handle Server Init Req from " << ntohl(id->Id) << endl;
#endif
if(M.clientId() != clientId) {
M.clientIdIs(clientId);
if(M.fd() >= 0)
close(M.fd());
M.fdIs(-1);
M.openFile_ = false;
M.serverBufferReset();
}
RFPacket pack;
pack.type = ACK_INIT;
struct IdPacket * sid = (struct IdPacket *) & pack.body;
sid->Id = htonl(M.theId());
sendPacket(&M, &pack);
}
void handleOpenFileReq(struct ReqOpenFilePacket * req)
{
#ifdef DEBUG
cout << "Handle Server Open File Req\n";
#endif
RFPacket pack;
pack.type = ACK_OPEN_FILE;
struct AckOpenFilePacket * ack = (struct AckOpenFilePacket *) & pack.body;
ack->serverId = htonl(M.theId());
unsigned short nameLen = ntohs(req->nameLen);
const char * fileName = req->name;
char nameBuf[128];
if(nameLen > 127) {
nameLen = 127;
strncpy(nameBuf, fileName, 127);
} else
strncpy(nameBuf, fileName, nameLen);
nameBuf[nameLen] = 0;
if(M.file() != string(nameBuf) && M.fd() > 0){
close(M.fd());
M.fdIs(-1);
}
M.fileIs(string(nameBuf));
M.openFile_ = true;
ack->success = true;
sendPacket(&M, &pack);
}
void handleReqWriteFile(struct ReqWritePacket * reqWrite)
{
size_t stagedSlot = ntohl(reqWrite->stagedSlot);
struct StagedWrite & stagedWrite = M.stagedWrites[stagedSlot];
stagedWrite.writeSeq = ntohl(reqWrite->writeSeq),
stagedWrite.byteOffset = ntohl(reqWrite->byteOffset),
stagedWrite.blockSize = ntohl(reqWrite->blockSize) ;
memcpy(stagedWrite.buffer, reqWrite->buffer, stagedWrite.blockSize);
}
void handleCheckCommitStatus(struct CheckCommitStatusPacket *check)
{
#ifdef DEBUG
cout << "Handle Check Commit "<<endl;
#endif
int commitId = ntohl(check->commitId);
int curCommitId = M.lastCommitId_ + 1;
if(curCommitId < 0)
curCommitId = 0;
if(commitId < curCommitId) {
cout << "Don't handle\n";
cout << "Cur commit Id "<< curCommitId << endl;
cout << "req Commit Id "<<commitId << endl;
return;
}
size_t totalWriteCount = ntohl(check->totalWriteCount);
int writeSeqFirst = ntohl(check->writeSeqFirst);
M.totalWriteCount_ = totalWriteCount;
int writeSeq = writeSeqFirst;
vector<size_t>missing;
for(size_t i=0; i < totalWriteCount; ++i){
if(M.stagedWrites[i].writeSeq != writeSeq) {
#ifdef DEBUG
cout << i <<" Slot writeSeq is "<<writeSeq<<endl;
#endif
missing.push_back(i);
if(missing.size() >= MAX_MISSING_WRITE)
break;
}
writeSeq++;
if(writeSeq < 0)
writeSeq = 0;
}
if(missing.size() == 0) {
M.readyToCommit_ = true;
#ifdef DEBUG
cout << "++++ Ready to commit \n";
#endif
} else
M.readyToCommit_ = false;
RFPacket pack;
pack.type = ACK_COMMIT_STATUS;
struct AckCommitStatusPacket *ack =(struct AckCommitStatusPacket*)&pack.body;
ack->serverId = htonl(M.theId());
ack->commitId = htonl(commitId);
ack->totalMissingWrite = htons((unsigned short)( missing.size() ) );
for(size_t i=0; i < missing.size(); ++i ) {
ack->missingWriteStages[i] = htonl( missing[i] );
}
sendPacket(&M, &pack);
}
void handleReqCommit(struct CommitIdPacket *req)
{
int reqCommitId = ntohl(req->commitId);
int currentCommitId = M.lastCommitId_ + 1;
if(currentCommitId < 0)
currentCommitId = 0;
if(M.readyToCommit_ && currentCommitId <= reqCommitId) {
int fd = M.fd();
if(fd < 0 && M.openFile_ == true && M.totalWriteCount_) {
fd = open((M.dir() + '/' + M.file()).c_str(),
O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR );
if(fd >= 0)
M.fdIs(fd);
else
cerr << "**** Open file failed in the server *******\n";
}
for(size_t i = 0; i < M.totalWriteCount_; ++i) {
struct StagedWrite & stagedWrite = M.stagedWrites[i];
if ( lseek( M.fd(), stagedWrite.byteOffset, SEEK_SET ) < 0 ) {
cerr<< "WriteBlock Seek" << endl;
}
if ( write( M.fd(), stagedWrite.buffer, stagedWrite.blockSize ) < 0 ) {
cerr<< "WriteBlock write" << endl;
}
}
M.lastCommitId_ = reqCommitId;
M.readyToCommit_ = false;
}
if(M.lastCommitId_ == reqCommitId) {
M.sendServerIdPacket(ACK_COMMIT);
}
}
void Server::sendServerIdPacket(const int type)
{
RFPacket pack;
pack.type = type;
struct ServerIdPacket *ack = (struct ServerIdPacket *) &pack.body;
ack->serverId = htonl(this->theId());
ack->success = true;
sendPacket(this, &pack);
}
void handleReqAbort(struct CommitIdPacket *req)
{
int reqAbortId = ntohl(req->commitId);
int currentCommitId = M.lastCommitId_ + 1;
if(currentCommitId < 0)
currentCommitId = 0;
if(currentCommitId <= reqAbortId) {
for(size_t i=0; i < MAX_WRITE_STAGE; ++i) {
M.stagedWrites[i].writeSeq = -1;
}
M.lastCommitId_ = reqAbortId;
M.readyToCommit_ = false;
M.totalWriteCount_ = 0;
}
if(M.lastCommitId_ == reqAbortId) {
M.sendServerIdPacket(ACK_ABORT);
}
}
void Server::serverBufferReset()
{
for(size_t i=0; i < MAX_WRITE_STAGE; ++i)
M.stagedWrites[i].writeSeq = -1;
M.readyToCommit_ = false;
M.lastCommitId_ = -1;
M.totalWriteCount_ = 0;
}
void handleReqClose(struct CommitIdPacket *req)
{
if(M.fd() >= 0) {
if (close( M.fd()) < 0 )
cerr << "Close error" << endl;
M.fdIs(-1);
M.openFile_ = false;
M.serverBufferReset();
}
M.sendServerIdPacket(ACK_CLOSE);
}
void processPacket(RFEvent *eventPacket)
{
RFPacket * pack = eventPacket->eventDetail;
switch(pack->type) {
case 0: cout << (char *)&pack->body << endl; break;
case REQ_SERVER_INIT:
handleReqServerInit((struct IdPacket *) &pack->body); break;
case REQ_OPEN_FILE:
handleOpenFileReq((struct ReqOpenFilePacket *) &pack->body); break;
case REQ_WRITE:
handleReqWriteFile((struct ReqWritePacket *) &pack->body); break;
case CHECK_COMMIT_STATUS:
handleCheckCommitStatus((struct CheckCommitStatusPacket*)&pack->body);
break;
case REQ_COMMIT:
handleReqCommit((struct CommitIdPacket *) &pack->body); break;
case REQ_ABORT:
handleReqAbort((struct CommitIdPacket *) &pack->body); break;
case REQ_CLOSE:
handleReqClose((struct CommitIdPacket *) &pack->body); break;
}
}
void serve(Server *M)
{
RFEvent event;
RFPacket incoming;
event.eventDetail = &incoming;
while(1) {
NextEvent(&event, M->theSocket());
switch(event.eventType) {
case EVENT_TIMEOUT: break;
case EVENT_NETWORK:
if(rand() % 100 < M->packetLoss() )
break;
processPacket(&event); break;
}
}
}
int main (int argc, char *argv[])
{
map<string, string> param;
if(argc<3)
exit(0);
for(int i=1; i+1<argc; i += 2)
param[string(argv[i])] = string(argv[i+1]);
if(param.find(string("-port")) == param.end() ||
param.find(string("-mount")) == param.end() ||
param.find(string("-drop")) == param.end() ) {
cout << "Usage: replFsServer -port ... -mount ... -drop ...\n";
exit(0);
}
if(mkdir( param[string("-mount")].c_str(), 0777 )) {
if(errno == EEXIST) {
cout << "machine alredy in use\n";
#ifndef DEBUG
exit(1);
#endif
}
}
M.dirIs(param[string("-mount")]);
char buf[128];
gethostname(buf, sizeof(buf));
int seed = 0;
for(int i=0; i<128 && buf[i]; ++i)
seed += buf[i];
srand(seed);
M.theIdIs(rand());
#ifdef DEBUG
cout << "The server ID is " << M.theId() << endl;
#endif
int portNum = atoi(param[string("-port")].c_str());
M.portIs(portNum);
M.setMaxRetry( atoi(param[string("-drop")].c_str()) );
netInit(&M);
serve(&M);
return 0;
}