-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.c
More file actions
196 lines (171 loc) · 4.45 KB
/
client.c
File metadata and controls
196 lines (171 loc) · 4.45 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
/****************/
/* Ethan Geller */
/* 5/21/16 */
/* CS 244B */
/* Spring 2016 */
/****************/
#define DEBUG
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <client.h>
#ifndef CLIENTSTATE
#define CLIENTSTATE 1
typedef struct _clientState {
unsigned int currentTransactionId;
unsigned int currentRevisionCount;
unsigned int nBlocks;
} ClientState;
ClientState* cState;
NetworkState* clientNetwork;
#endif
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
clientNetwork =(NetworkState*) malloc(sizeof(NetworkState));
cState = (ClientState*) malloc(sizeof(ClientState));
clientNetwork->my_ntype = NTYPE_CLIENT;
clientNetwork->droprate = ((float) packetLoss)/100.0f;
int err = startNetwork(GROUP,PORT,NTYPE_CLIENT,clientNetwork);
return( err );
}
int OpenFile( char * fileName ) {
int fd;
ASSERT( fileName );
#ifdef DEBUG
printf( "OpenFile: Opening File '%s'\n", fileName );
#endif
OpenFilePacket out;
out.ntype = NTYPE_CLIENT;
out.transactionid = rand();
cState->currentTransactionId = out.transactionid;
cState->nBlocks = 128;
#ifdef DEBUG
if ( fd < 0 )
perror( "OpenFile" );
#endif
sendPacket(PTYPE_OPENFILE,(void*)&out,clientNetwork);å
//wait a couple of attempts for a success packet
unsigned int numAttempts = ATTEMPT_COUNT;
PTYPE p_t;
void* packet;
while(numAttempts) {
packet = receivePacket(&p_t,clientNetwork);
if(p_t==PTYPE_OPENSUCCESS) {
OpenSuccessPacket* p = (OpenSuccessPacket*) packet;
if(p->transactionid == cState->currentTransactionId) {
if(p->success)fd = p->fd;
else fd=-1;
cState->currentRevisionCount = 0;
break;
}
}
numAttempts--;
}
free(packet);
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
cState->currentRevisionCount++;
WriteFilePacket out;
out.data = buffer;
out.sz = blockSize;
out.offset = byteOffset;
out.counter = cState->currentRevisionCount;
out.blockid = byteOffset/cState->nBlocks;
sendPacket(PTYPE_WRITEFILE,(void*)&out, clientNetwork);
return( bytesWritten );
}
/* ------------------------------------------------------------------ */
int Commit( int fd ) {
ASSERT( fd >= 0 );
#ifdef DEBUG
printf( "Commit: FD=%d\n", fd );
#endif
int err =0;
CommitPacket out;
out.ntype = NTYPE_CLIENT;
out.transactionid = cState->currentTransactionId;
err = sendPacket(PTYPE_COMMIT,(void*)&out,clientNetwork);
//wait a couple of attempts for a success packet
unsigned int numAttempts = ATTEMPT_COUNT;
PTYPE p_t;
void* packet;
while(numAttempts) {
packet = receivePacket(&p_t,clientNetwork);
if(p_t==PTYPE_COMMITSUCCESS) {
CommitSuccessPacket* p = (CommitSuccessPacket*) packet;
if(p->transactionid == cState->currentTransactionId) {
if(p->success){
cState->currentRevisionCount = 0;
}
else err=-1;
break;
}
}
numAttempts--;
}
free(packet);
return( err );
}
int Abort( int fd )
{
ASSERT( fd >= 0 );
#ifdef DEBUG
printf( "Abort: FD=%d\n", fd );
#endif
int err = 0;
AbortPacket out;
out.ntype = NTYPE_CLIENT;
out.transactionid = cState->currentTransactionId;
err = sendPacket(PTYPE_ABORT,(void*)&out, clientNetwork);
return(err);
}
int CloseFile( int fd ) {
ASSERT( fd >= 0 );
#ifdef DEBUG
printf( "Close: FD=%d\n", fd );
#endif
int err = 0;
ClosePacket out;
out.ntype = NTYPE_CLIENT;
out.transactionid = cState->currentTransactionId;
out.fd = fd;
err = sendPacket(PTYPE_CLOSE,(void*)&out,clientNetwork);
//wait a couple of attempts for a success packet
unsigned int numAttempts = ATTEMPT_COUNT;
PTYPE p_t;
void* packet;
while(numAttempts) {
packet = receivePacket(&p_t,clientNetwork);
if(p_t==PTYPE_CLOSESUCCESS) {
CloseSuccessPacket* p = (CloseSuccessPacket*) packet;
if(p->transactionid == cState->currentTransactionId) {
if(p->success){
cState->currentRevisionCount = 0;
cState->currentTransactionId = 0;
}
else err=-1;
break;
}
}
numAttempts--;
}
free(packet);
return(err);
}