-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
241 lines (201 loc) · 7.04 KB
/
client.c
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
/************************/
/* Your Name: Sining Ma */
/* Date: 05/10/2014 */
/* CS 244B */
/* Spring 2013 */
/************************/
#define DEBUG
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <client.h>
#include "clientInstance.h"
ClientInstance *client;
/* ------------------------------------------------------------------ */
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. */
/****************************************************/
timeval ct;
gettimeofday(&ct, NULL);
srand(ct.tv_usec);
uint32_t nodeId = (uint32_t)rand();
client = new ClientInstance(packetLoss, nodeId, numServers);
printf("Client port: %u, packetLoss: %d, numServers: %d, nodeId: %010u\n", portNum, packetLoss, numServers, nodeId);
client->rfs_NetInit(portNum);
printf("Client Init phase...\n");
if (client->execute(INIT_OP, SHORT_TIMEOUT, NULL, 0, NULL) == ErrorReturn)
return ErrorReturn;
printf("Client Init phase is done\n");
return (NormalReturn);
}
/* ------------------------------------------------------------------ */
int
OpenFile( char * fileName ) {
if (fileName == NULL || strlen(fileName) >= MAXFILENAMESIZE)
return ErrorReturn;
if ((int)client->serverIds.size() < client->numServers) {
printf("Client error: Not enough servers are available\n");
return ErrorReturn;
}
#ifdef DEBUG
printf( "OpenFile: Opening File '%s'\n", fileName );
#endif
int fd = client->nextFd;
client->nextFd = getNextNum(client->nextFd);
printf("Client OpenFile phase...\n");
std::set<uint32_t> recvServerId;
if (client->execute(OPEN_OP, LONG_TIMEOUT, &recvServerId, fd, fileName) == ErrorReturn)
return ErrorReturn;
client->isFileOpen = true;
printf("Client OpenFile phase is done\n");
return fd;
}
/* ------------------------------------------------------------------ */
int
WriteBlock( int fd, char * buffer, int byteOffset, int blockSize ) {
if( fd < 0 )
return ErrorReturn;
if (fd != (client->nextFd - 1)) // check if passing open file fd
return ErrorReturn;
if ( byteOffset < 0 || byteOffset > MAXFILESIZE)
return ErrorReturn;
if ( buffer == NULL)
return ErrorReturn;
if ( blockSize < 0 || blockSize > MaxBlockLength )
return ErrorReturn;
if (!client->isFileOpen)
return ErrorReturn;
if ((int)client->serverIds.size() < client->numServers) {
printf("Client error: Not enough servers are available\n");
return ErrorReturn;
}
#ifdef DEBUG
printf( "WriteBlock: Writing FD=%d, Offset=%d, Length=%d\n", fd, byteOffset, blockSize );
#endif
client->sendWriteBlockMessage(fd, client->updateId, byteOffset, blockSize, buffer, 1);
client->updateId = getNextNum(client->updateId);
return( fd );
}
/* ------------------------------------------------------------------ */
int
Commit( int fd ) {
if (fd < 0)
return ErrorReturn;
if (fd != (client->nextFd - 1)) // check if passing open file fd
return ErrorReturn;
if (!client->isFileOpen)
return ErrorReturn;
if ((int)client->serverIds.size() < client->numServers) {
printf("Client error: Not enough servers are available\n");
return ErrorReturn;
}
#ifdef DEBUG
printf( "Commit: FD=%d\n", fd );
#endif
/****************************************************/
/* Prepare to Commit Phase */
/* - Check that all writes made it to the server(s) */
/****************************************************/
printf("Client Vote phase...\n");
std::set<uint32_t> recvVoteServerId;
while(1) {
int ret = client->execute(VOTE_OP, LONG_TIMEOUT, &recvVoteServerId, fd, NULL);
if (ret == ErrorReturn) {
// error happens on servers, abort all file updates
Abort(fd);
return (ErrorReturn);
} else if (ret == 0)
break;
}
printf("Client Vote phase is done\n");
/****************/
/* Commit Phase */
/****************/
printf("Client Commit phase...\n");
std::set<uint32_t> recvCommitServerId;
if (client->execute(COMMIT_OP, LONG_TIMEOUT, &recvCommitServerId, fd, NULL) == ErrorReturn) {
printf("File commit error, rollback all updates in fileId: %d\n", fd);
Abort(fd);
return ErrorReturn;
}
printf("Client Commit phase is done\n");
client->reset();
return (NormalReturn);
}
/* ------------------------------------------------------------------ */
int
Abort( int fd )
{
if (fd < 0)
return ErrorReturn;
if (fd != (client->nextFd - 1)) // check if passing open file fd
return ErrorReturn;
if (!client->isFileOpen)
return ErrorReturn;
if ((int)client->serverIds.size() < client->numServers) {
printf("Client error: Not enough servers are available\n");
return ErrorReturn;
}
#ifdef DEBUG
printf( "Abort: FD=%d\n", fd );
#endif
/*************************/
/* Abort the transaction */
/*************************/
printf("Client Abort phase...\n");
std::set<uint32_t> recvServerId;
if (client->execute(ABORT_OP, LONG_TIMEOUT, &recvServerId, fd, NULL) == ErrorReturn) {
client->reset();
return ErrorReturn;
}
printf("Client Abort phase is done\n");
client->reset();
return (NormalReturn);
}
/* ------------------------------------------------------------------ */
int
CloseFile( int fd ) {
if (fd < 0)
return ErrorReturn;
if (fd != (client->nextFd - 1)) // check if passing open file fd
return ErrorReturn;
if (!client->isFileOpen)
return ErrorReturn;
if ((int)client->serverIds.size() < client->numServers) {
printf("Client error: Not enough servers are available\n");
return ErrorReturn;
}
#ifdef DEBUG
printf( "Close: FD=%d\n", fd );
#endif
/*****************************/
/* Check for Commit or Abort */
/*****************************/
printf("Client Close phase...\n");
std::set<uint32_t> recvServerId;
int ret = client->execute(CLOSE_OP, SHORT_TIMEOUT, &recvServerId, fd, NULL);
if (ret == ErrorReturn)
return ErrorReturn;
else if (ret == 1) {
// server has uncommitted updates, do commit
printf("Servers have uncommitted file updates\n");
if (Commit(fd) == ErrorReturn)
return ErrorReturn;
// try to close file again
if (client->execute(CLOSE_OP, SHORT_TIMEOUT, &recvServerId, fd, NULL) == ErrorReturn)
return ErrorReturn;
}
client->isFileOpen = false;
printf("Client CloseFile phase is done\n\n");
return (NormalReturn);
}
/* ------------------------------------------------------------------ */