-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWriterThread.java
More file actions
342 lines (277 loc) · 11.9 KB
/
FileWriterThread.java
File metadata and controls
342 lines (277 loc) · 11.9 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
package unimelb.bitbox;
import java.net.Socket;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import unimelb.bitbox.util.FileSystemManager;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.util.Queue;
import java.util.LinkedList;
import java.nio.ByteBuffer;
import java.util.Base64;
import java.security.NoSuchAlgorithmException;
import java.lang.InterruptedException;
// A thread accepts FILE_BYTES_RESPONSE from its Master ReceiverThread
// then deocdes the bytes content and wtites the target file
public class FileWriterThread extends Thread {
public final String fRequest;
public final String fPathName;
private String fMD5;
private long fLastModified;
private long fFileSize;
public final String hostR;
public final int portR;
private Socket socket;
private BufferedWriter out;
private FileSystemManager fileManager;
private final long maxBlockSize;
//this thread's own FILE_BYTES_RESPONSE message buffer
protected Queue<String> byteResponseBuffer;
public FileWriterThread(WriteFileEvent event, Socket socket, FileSystemManager fileManager, long maxBlockSize) {
fRequest = event.request;
fPathName = event.pathName;
fMD5 = event.md5;
fLastModified = event.lastModified;
fFileSize = event.fileSize;
hostR = event.host;
portR = event.port;
this.socket = socket;
//no input stream, input comes from ReceiverThread
//output
try {
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
}
catch(IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
this.fileManager = fileManager;
this.maxBlockSize = maxBlockSize;
byteResponseBuffer = new LinkedList<String>();
}
public void run() {
//true if the file is writed successful
boolean writeSucc = writeFile(fPathName);
//cancels file loader to prevent blocking further updates
if(!writeSucc) {
try {
fileManager.cancelFileLoader(fPathName);
}
catch(IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
//writes the file until complete or abort
private synchronized boolean writeFile(String pathName) {
try {
int failCount = 0;
long position = 0;
long length;
long remainingSize = fFileSize;
//abbort if failure count > 3
while(!fileManager.checkWriteComplete(pathName) && remainingSize>0 && failCount<=3) {
//prevents from request excessive length
if(remainingSize < maxBlockSize) {
length = remainingSize;
}
else {
length = maxBlockSize;
}
//sends byte request
boolean req = sendByteReq(pathName, fMD5, fLastModified, fFileSize, position, length);
//retries if fails up to total 3 times
while(!req && failCount<=3) {
failCount++;
req = sendByteReq(pathName, fMD5, fLastModified, fFileSize, position, length);
}
//waits for FILE_BYTES_RESPONSE distributed by the ReceiverThread
while(byteResponseBuffer.isEmpty()) {
try {
Thread.sleep(50);
}
catch(InterruptedException e) {
System.out.println(e.getMessage());
}
}
String byteRes = byteResponseBuffer.remove();
//processes JSON message, writes the file, and checks if any failure
int result = byteResHandler(byteRes, pathName, position, length);
//write bytes successfully
if(result==0){
position+=length;
remainingSize-=length;
}
//updates failure count
failCount += result;
}
return fileManager.checkWriteComplete(pathName);
}
catch(IOException | NoSuchAlgorithmException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return false;
}
}
//sends FILE_BYTES_REQUEST
private boolean sendByteReq(String pathName, String md5, long lastModified, long fileSize, long position, long length) {
try {
JSONObject fileDescriptor = new JSONObject();
fileDescriptor.put("md5", md5);
fileDescriptor.put("lastModified", lastModified);
fileDescriptor.put("fileSize", fileSize);
JSONObject byteReq = new JSONObject();
byteReq.put("command", "FILE_BYTES_REQUEST");
byteReq.put("pathName", pathName);
byteReq.put("fileDescriptor", fileDescriptor);
byteReq.put("position", position);
byteReq.put("length", length);
out.write(byteReq.toJSONString() +"\n");
out.flush();
System.out.println(byteReq.toJSONString());
return true;
}
catch(IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return false;
}
}
//processes FILE_BYTES_RESPONSE and writes file
private int byteResHandler(String byteRes, String reqPathName, long reqPosition, long reqLength) {
JSONParser parser = new JSONParser();
try {
JSONObject res = (JSONObject) parser.parse(byteRes);
//checks JSON format, aborts whole writing if invalid message received
if(!res.containsKey("command")) {
sendInvalid("message must contain a command field as string");
return 99;
}
else {
int syncStatus;
String command = (String) res.get("command");
String validRes = "FILE_BYTES_RESPONSE";
if(command.equals(validRes)) {
if(!res.containsKey("pathName")) {
sendInvalid(validRes+" message must contain a pathName field");
syncStatus = 99;
}
else if(!res.containsKey("message")) {
sendInvalid(validRes+" message must contain a message field");
syncStatus = 99;
}
else if(!res.containsKey("status")) {
sendInvalid(validRes+" message must contain a status field");
syncStatus = 99;
}
else if(!res.containsKey("fileDescriptor")) {
sendInvalid(validRes+" message must contain a fileDescriptor field");
syncStatus = 99;
}
else if(!res.containsKey("position")) {
sendInvalid(validRes+" message must contain a position field");
syncStatus = 99;
}
else if(!res.containsKey("length")) {
sendInvalid(validRes+" message must contain a length field");
syncStatus = 99;
}
else if(!res.containsKey("content")) {
sendInvalid(validRes+" message must contain a content field");
syncStatus = 99;
}
else {
String pathName = (String) res.get("pathName");
long position = ((Number) res.get("position")).longValue();
long length = ((Number) res.get("length")).longValue();
String content = (String) res.get("content");
boolean status = (boolean) res.get("status");
String message = (String) res.get("message");
ByteBuffer src;
if(content!=null && status) {
byte[] decodedContent = Base64.getDecoder().decode(content);
src = ByteBuffer.wrap(decodedContent);
}
else {
src = ByteBuffer.allocate(0);
}
JSONObject fileDescriptor = (JSONObject) res.get("fileDescriptor");
if(!pathName.equals(reqPathName)) {
sendInvalid("invalid pathName value to our FILE_BYTES_REQUEST");
syncStatus = 99;
}
else if(position != reqPosition) {
sendInvalid("invalid position value to our FILE_BYTES_REQUEST");
syncStatus = 99;
}
else if(length != reqLength) {
sendInvalid("invalid length value to our FILE_BYTES_REQUEST");
syncStatus = 99;
}
else if(!fileDescriptor.containsKey("md5")) {
sendInvalid(validRes+" message must contain a md5 field");
syncStatus = 99;
}
else if(!fileDescriptor.containsKey("lastModified")) {
sendInvalid(validRes+" message must contain a lastModified field");
syncStatus = 99;
}
else if(!fileDescriptor.containsKey("fileSize")) {
sendInvalid(validRes+" message must contain a fileSize field");
syncStatus = 99;
}
else {
String md5 = (String) fileDescriptor.get("md5");
long lastModified = ((Number) fileDescriptor.get("lastModified")).longValue();
long fileSize = ((Number) fileDescriptor.get("fileSize")).longValue();
//the format is correct, writes the file
if(status) {
boolean writeStatus = fileManager.writeFile(pathName, src, position);
syncStatus = (writeStatus)? 0:1;
}
else {
syncStatus = 1;
}
}
}
}
else {
sendInvalid("invalid response to our FILE_BYTES_REQUEST");
syncStatus = 99;
}
return syncStatus;
}
}
catch(ParseException e) {
sendInvalid("JSON parsing failure");
return 99;
}
catch(ClassCastException e){
sendInvalid("JSON parsing failure");
return 99;
}
catch(IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return 99;
}
}
//sends INVALID_PROTOCOL
private void sendInvalid(String msg) {
try {
JSONObject invalidPro = new JSONObject();
invalidPro.put("command", "INVALID_PROTOCOL");
invalidPro.put("message", msg);
out.write(invalidPro.toJSONString() +"\n");
out.flush();
System.out.println(invalidPro.toJSONString());
}
catch(IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}