-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
334 lines (228 loc) · 7.37 KB
/
client.cpp
File metadata and controls
334 lines (228 loc) · 7.37 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
/****************/
/* Oleksandr Diachenko */
/* 05/22/2016 */
/* CS 244B */
/* Spring 2016 */
/****************/
#define DEBUG
#include <stdio.h>
#include <assert.h>
#include <sstream>
#include <map>
#include <client.h>
#include <network.h>
class Client {
public:
Client(Network *network, int numServers) {
this->numServers = numServers;
this->network = network;
}
;
string getClientId() {
char hostname[1024];
hostname[1023] = '\0';
gethostname(hostname, 1023);
std::ostringstream ss;
ss << ::getppid()<<hostname;
string clientId = ss.str();
return clientId;
}
;
int beginTransaction(string fileName) {
BaseEvent * beginTransactionRequestEvent =
new BeginTransactionRequestEvent(this->getClientId(),
string(fileName));
list<DfsPacket*> packets = this->network->sendPacketRetry(
this->getClientId(), this->getNumServers(),
beginTransactionRequestEvent,
EVENT_TYPE_BEGIN_TRANSACTION_RESPONSE);
list<DfsPacket*>::const_iterator iterator;
for (iterator = packets.begin(); iterator != packets.end();
++iterator) {
BeginTransactionResponseEvent *event =
(BeginTransactionResponseEvent*) this->network->packetToEvent(
*iterator);
transactionsPerServersPerFd[fileDescriptorCounter][event->getSenderNodeId()] =
event->getTransactionId();
printf("Server %s opened transaction %d, file descriptor %d\n",
event->getSenderNodeId().c_str(), event->getTransactionId(),
fileDescriptorCounter);
}
return fileDescriptorCounter++;
}
;
int writeBlock(int fd, char * buffer, int byteOffset, int blockSize) {
map<string, uint8_t>::iterator it;
for (it = transactionsPerServersPerFd[fd].begin();
it != transactionsPerServersPerFd[fd].end(); it++) {
WriteBlockEvent *event = new WriteBlockEvent(this->getClientId(),
it->first, it->second, buffer, byteOffset, blockSize);
this->network->sendPacket(event, true, this->numServers);
}
return blockSize;
}
int getNumServers() {
return this->numServers;
}
int commit(int fd, bool closeFile) {
int commitRequestStatus = this->prepareCommit(fd);
if (commitRequestStatus == NormalReturn) {
printf("Vote OK\n");
int finishCommitStatus = this->finishCommit(fd, closeFile);
return finishCommitStatus;
} else {
printf("Vote wasn't OK, rolling transaction back\n");
this->rollback(fd);
return ErrorReturn;
}
}
int prepareCommit(int fd) {
map<string, uint8_t>::iterator it;
list<DfsPacket*> packets;
int positiveVotesNumber = 0;
for (it = transactionsPerServersPerFd[fd].begin();
it != transactionsPerServersPerFd[fd].end(); it++) {
CommitRequestEvent *commitRequestEvent = new CommitRequestEvent(
this->getClientId(), it->first, it->second);
list<DfsPacket*> packetsPerServer = this->network->sendPacketRetry(
this->getClientId(), 1, commitRequestEvent,
EVENT_TYPE_COMMIT_VOTE);
packets.merge(packetsPerServer);
}
CommitVoteEvent *event;
list<DfsPacket*>::const_iterator it_votes;
for (it_votes = packets.begin(); it_votes != packets.end();
++it_votes) {
event = (CommitVoteEvent *) this->network->packetToEvent(*it_votes);
if (event->getVote() == POSITIVE_VOTE)
positiveVotesNumber++;
}
printf("Received commit votes, number of total votes: %d, positive votes number: %d\n",
packets.size(), positiveVotesNumber);
if (positiveVotesNumber < this->getNumServers()) {
printf("Number of votes is not sufficient for commit\n");
return ErrorReturn;
}
return NormalReturn;
}
int finishCommit(int fd, bool closeFile) {
map<string, uint8_t>::iterator it;
list<DfsPacket*> packets;
for (it = transactionsPerServersPerFd[fd].begin();
it != transactionsPerServersPerFd[fd].end(); it++) {
CommitEvent *commitEvent = new CommitEvent(this->getClientId(),
it->first, it->second, closeFile);
list<DfsPacket*> packetsPerServer = this->network->sendPacketRetry(
this->getClientId(), 1, commitEvent,
EVENT_TYPE_COMMIT_ROLLBACK_ACK);
packets.merge(packetsPerServer);
}
if (packets.size() == this->getNumServers())
return NormalReturn;
else
return ErrorReturn;
}
int rollback(int fd) {
map<string, uint8_t>::iterator it;
list<DfsPacket*> packets;
int positiveVotesNumber = 0;
for (it = transactionsPerServersPerFd[fd].begin();
it != transactionsPerServersPerFd[fd].end(); it++) {
RollbackEvent *rollbackEvent = new RollbackEvent(
this->getClientId(), it->first, it->second);
list<DfsPacket*> packetsPerServer = this->network->sendPacketRetry(
this->getClientId(), 1, rollbackEvent,
EVENT_TYPE_COMMIT_ROLLBACK_ACK);
packets.merge(packetsPerServer);
}
return NormalReturn;
}
private:
int fileDescriptorCounter;
int numServers;
Network *network;
map<int, map<string, uint8_t> > transactionsPerServersPerFd;
};
static Network *n;
static Client *c;
/* ------------------------------------------------------------------ */
int InitReplFs(unsigned short portNum, int packetLoss, int numServers) {
#ifdef DEBUG
printf("INITREPLFS: Port number %d, packet loss %d percent, %d servers\n",
portNum, packetLoss, numServers);
#endif
/****************************************************/
/* Initialize network access, local state, etc. */
/****************************************************/
n = new Network(portNum, packetLoss);
n->netInit();
c = new Client(n, numServers);
return (NormalReturn);
}
/* ------------------------------------------------------------------ */
int OpenFile(char * fileName) {
ASSERT(fileName);
#ifdef DEBUG
printf("OPENFILE: Opening File '%s'\n", fileName);
#endif
int fd = c->beginTransaction(fileName);
if (fd < 0) {
printf("Unable to open file\n");
return ErrorReturn;
}
return fd;
}
/* ------------------------------------------------------------------ */
int WriteBlock(int fd, char * buffer, int byteOffset, int blockSize) {
int bytesWritten;
ASSERT( fd >= 0);
ASSERT( byteOffset >= 0);
ASSERT( buffer);
ASSERT( blockSize >= 0 && blockSize < MaxBlockLength);
#ifdef DEBUG
printf("WRITEBLOCK: Writing FD=%d, Offset=%d, Length=%d\n", fd, byteOffset,blockSize);
#endif
bytesWritten = c->writeBlock(fd, buffer, byteOffset, blockSize);
return (bytesWritten);
}
/* ------------------------------------------------------------------ */
int Commit(int fd) {
ASSERT( fd >= 0);
#ifdef DEBUG
printf("COMMIT: FD=%d\n", fd);
#endif
/****************************************************/
/* Prepare to Commit Phase */
/* - Check that all writes made it to the server(s) */
/****************************************************/
/****************/
/* Commit Phase */
/****************/
int commitStatus = c->commit(fd, false);
return commitStatus;
}
/* ------------------------------------------------------------------ */
int Abort(int fd) {
ASSERT( fd >= 0);
#ifdef DEBUG
printf("ABORT: FD=%d\n", fd);
#endif
/*************************/
/* Abort the transaction */
/*************************/
int abortStatus = c->rollback(fd);
return abortStatus;
}
/* ------------------------------------------------------------------ */
int CloseFile(int fd) {
ASSERT( fd >= 0);
#ifdef DEBUG
printf("CLOSE: FD=%d\n", fd);
#endif
/*****************************/
/* Check for Commit or Abort */
/*****************************/
int commitStatus = c->commit(fd, true);
return commitStatus;
}
/* ------------------------------------------------------------------ */