-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dstore.java
405 lines (340 loc) · 17.4 KB
/
Dstore.java
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import java.io.*;
import java.net.*;
import java.util.*;
/**
* Dstore class - this will be the node of the Distributed Storage.
* A singleton design pattern will be used for this class, where each property will be accessible && dynamically changed by the request threads.
*
* @author Andrei23f(ap4u19@soton.ac.uk)
*/
class Dstore{
static int port = 0;
static int cport = 0;
static int timeout = 0;
static String file_folder = null;
// (fileName, fileSize) is a tuple from the file_details hashtable
static Hashtable<String, Integer> file_details = new Hashtable<>();
static ServerSocket ss;
static Socket socket_to_controller;
private static Object lock = new Object();//for dealing with Thread race conditions
/**
* The class that will validate && format each request that comes from Client / Controller.
* @author Andrei123f(ap4u19@soton.ac.uk)
*/
class INCOMING_REQUEST {
public String operation;
public Map<String, String> arguments = new HashMap<String, String>();
public boolean invalidOperation = false;
public boolean invalidArguments = false;
public INCOMING_REQUEST(String request) {
this.initRequestStructure(request);
}
private void initRequestStructure(String request) {
DstoreLogger.getInstance().log("Incoming request from client : " + request);
//split the request by spaces
String segments[] = request.split(" ");
//get the request type
String request_type = segments[0];
//decide if the the request type is correct or not
switch (request_type) {
case Protocol.REMOVE_TOKEN:
this.operation = Protocol.REMOVE_TOKEN;
this.prepareRemoveOperation(segments);
break;
case Protocol.LOAD_DATA_TOKEN:
this.operation = Protocol.LOAD_DATA_TOKEN;
this.prepareLoadDataOperation(segments);
break;
case Protocol.STORE_TOKEN:
this.operation = Protocol.STORE_TOKEN;
this.prepareStoreOperation(segments);
break;
}
}
private void prepareRemoveOperation(String[] requestSegments) {
//expected request : REMOVE filename
if (requestSegments.length != 2) {
this.invalidArguments = true;
return;
}
this.arguments.put("filename", requestSegments[1]);
}
private void prepareLoadDataOperation(String[] requestSegments) {
//expected request : LOAD_DATA filename
if (requestSegments.length != 2) {
this.invalidArguments = true;
return;
}
this.arguments.put("filename", requestSegments[1]);
}
private void prepareStoreOperation(String[] requestSegments) {
//expected request : STORE filename filesize
if (requestSegments.length != 3) {
this.invalidArguments = true;
return;
}
this.arguments.put("filename", requestSegments[1]);
this.arguments.put("filesize", requestSegments[2]);
}
}
/**
* The class that will be only used for Controller requests.
*
* @author Andrei123f(ap4u19@soton.ac.uk)
*/
public class CONTROLLER_THREAD implements Runnable
{
private Socket socketTo_controller;
private OutputStream outFileStream_controller;
private InputStream inFileStream_controller;
private BufferedReader inTextStream_controller;
private PrintWriter outTextStream_controller;
public CONTROLLER_THREAD (Socket socketTo_controller) {this.socketTo_controller = socketTo_controller;}
public void run()
{
try
{
this.outFileStream_controller = this.socketTo_controller.getOutputStream();
this.inFileStream_controller = this.socketTo_controller.getInputStream();
this.inTextStream_controller = new BufferedReader(new InputStreamReader(this.inFileStream_controller));
this.outTextStream_controller = new PrintWriter(new OutputStreamWriter(this.outFileStream_controller));
//join the controller server
this.outTextStream_controller.println(Protocol.JOIN_TOKEN + " " + Dstore.port);
this.outTextStream_controller.flush();
//waiting for commands from Controller
String line;
while((line = this.inTextStream_controller.readLine()) != null)
{
System.out.println("Incoming request from Controller: " + line);
DstoreLogger.getInstance().log("Incoming request from Controller: " + line);
INCOMING_REQUEST formattedRequest = new INCOMING_REQUEST(line);
if(formattedRequest.invalidOperation || formattedRequest.invalidArguments)
throw new Exception("Invalid Request from Controller" + line);
switch (formattedRequest.operation) {
case (Protocol.REMOVE_TOKEN) :
this.processRemoveOperation(formattedRequest.arguments.get("filename"));
break;
}
}
}catch (Throwable e) {
String error = ("Error when receiving request from Controller: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
DstoreLogger.getInstance().log(error);
}
}
private void processRemoveOperation(String filename)
{
File file = new File(Dstore.file_folder + File.separator + filename);
String responseTo_controller;
if(file.delete()) {
Dstore.file_details.remove(filename);
responseTo_controller = Protocol.REMOVE_ACK_TOKEN + " " + filename;
} else {
responseTo_controller = Protocol.ERROR_FILE_DOES_NOT_EXIST_TOKEN + " " + filename;
}
this.outTextStream_controller.println(responseTo_controller);
this.outTextStream_controller.flush();
}
}
/**
* The class that will be only used for Controller/Client requests.
*
* @author Andrei123f(ap4u19@soton.ac.uk)
*/
class CLIENT_THREAD implements Runnable
{
private Socket socketTo_client;
//for Client communication
private OutputStream outFileStream_client;
private InputStream inFileStream_client;
private BufferedReader inTextStream_client;
private PrintWriter outTextStream_client;
//for Controller communication
private OutputStream outFileStream_controller;
private InputStream inFileStream_controller;
private BufferedReader inTextStream_controller;
private PrintWriter outTextStream_controller;
public CLIENT_THREAD (Socket socket) {this.socketTo_client = socket;}
public void run()
{
try
{
this.outFileStream_controller = socket_to_controller.getOutputStream();
this.inFileStream_controller = socket_to_controller.getInputStream();
this.inTextStream_controller = new BufferedReader(new InputStreamReader(this.inFileStream_controller));
this.outTextStream_controller = new PrintWriter(new OutputStreamWriter(this.outFileStream_controller));
this.outFileStream_client = this.socketTo_client.getOutputStream();
this.inFileStream_client = this.socketTo_client.getInputStream();
this.inTextStream_client = new BufferedReader(new InputStreamReader(this.inFileStream_client));
this.outTextStream_client = new PrintWriter(new OutputStreamWriter(this.outFileStream_client));
int bufLen;
byte[] buffer = new byte[1000];
while((bufLen = this.inFileStream_client.read(buffer)) != -1){
String request = new String(buffer, 0, bufLen);
request = request.replaceAll("\n","");
System.out.println("Incoming request from client : " + request);
INCOMING_REQUEST formattedRequest = new INCOMING_REQUEST(request);
if(formattedRequest.invalidOperation || formattedRequest.invalidArguments)
throw new Exception("Invalid Request from Client/Controller" + request);
switch (formattedRequest.operation) {
case (Protocol.STORE_TOKEN):
this.processStoreOperation(formattedRequest.arguments.get("filename"), formattedRequest.arguments.get("filesize"));
break;
case (Protocol.LOAD_DATA_TOKEN):
this.processLoadOperation(formattedRequest.arguments.get("filename"));
break;
case(Protocol.REMOVE_TOKEN):
this.processRemoveOperation(formattedRequest.arguments.get("filename"));
break;
}
}
} catch (Throwable e){
String error = ("Error when receiving request from Client/Controller: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
DstoreLogger.getInstance().log(error);
}
}
public void processStoreOperation(String filename, String filesize) throws Throwable
{
int fileSize = Integer.parseInt(filesize);
File file = new File(file_folder + File.separator + filename);
try {
if (file.createNewFile()) {
System.out.println("Sending ACK response to client ...");
//send ack response to client
this.outTextStream_client.println(Protocol.ACK_TOKEN);
this.outTextStream_client.flush();
System.out.println("Sending ACK response to client - success");
FileOutputStream outFile = new FileOutputStream(file);
byte[] data;
System.out.println("Reading N bytes ...");
data = this.inFileStream_client.readNBytes(fileSize);
System.out.println("Reading N bytes - success");
System.out.println("Writing the file ...");
outFile.write(data);
outFile.close();
System.out.println("Writing the file - success");
System.out.println("Sending Store ACK response to Controller for file " + filename + " ...");
//send ack response to controller.
this.outTextStream_controller.println(Protocol.STORE_ACK_TOKEN + " " + filename);
this.outTextStream_controller.flush();
System.out.println("Sending Store ACK response to Controller for file " + filename + " ... - success");
file_details.put(filename, Integer.valueOf(fileSize));
} else {
//todo should you close the socket client here?
System.out.println("File already exists.");
this.socketTo_client.close();
throw new Error("File already exists");
}
} catch (Throwable e) {
throw new Error("Error when storing file : " + (e.getMessage() != null ? e.getMessage() : e.toString()));
}
}
public void processLoadOperation(String filename) throws Throwable
{
synchronized (Dstore.lock) {
try {
System.out.println("LOADING FILE OPERATION ...");
if (file_details.containsKey(filename)) {
File inputFile = new File(filename);
FileInputStream inf = new FileInputStream(Dstore.file_folder + File.separator + inputFile);
int buflen;
byte[] buf = new byte[1000];
System.out.println("LOADING FILE OPERATION started...");
while ((buflen = inf.read(buf)) != -1) {
this.outFileStream_client.write(buf, 0, buflen);
}
inf.close();
System.out.println("LOADING FILE OPERATION finished...");
} else {
//todo should you close the socket client here?
this.socketTo_client.close();
throw new Error("Dstore does not have the requested file : " + filename);
}
} catch (Throwable e) {
throw new Error("Error when loading file : " + (e.getMessage() != null ? e.getMessage() : e.toString()));
}
}
}
public void processRemoveOperation(String filename) throws Throwable
{
System.out.println("Deleting file " + filename + " ...");
File file = new File(Dstore.file_folder + File.separator + filename);
String responseTo_controller;
if(file.delete()) {
System.out.println("Deleting file " + filename + " - finished");
Dstore.file_details.remove(filename);
responseTo_controller = Protocol.REMOVE_ACK_TOKEN + " " + filename;
} else {
//todo should you close the socket client here?
responseTo_controller = Protocol.ERROR_FILE_DOES_NOT_EXIST_TOKEN + " " + filename;
}
System.out.println("Sending response to Controller... : " + responseTo_controller);
this.outTextStream_controller.println(responseTo_controller);
this.outTextStream_controller.flush();
System.out.println("Sending response to Controller... : " + responseTo_controller + " - finished");
}
}
/**
* The entrypoint of the program when the Dstore node starts
*
* @throws Exception | void
*/
public static void main (String[] args ) throws Throwable
{
Dstore sys = new Dstore();
sys.main2(args);
}
public void main2(String[] args) throws IOException {
try{
System.out.println(args[0]+" "+args[1]+" "+args[2]+" "+args[3]);
//dstore port
port = Integer.parseInt(args[0]);
//controller port
cport = Integer.parseInt(args[1]);
timeout = Integer.parseInt(args[2]);
file_folder = args[3];
File dstoreFolder = new File(file_folder);
DstoreLogger.init(Logger.LoggingType.ON_FILE_AND_TERMINAL, port);
DstoreLogger.getInstance().log("Starting Dstore server ... on port" + port);
if (!dstoreFolder.exists())
if (!dstoreFolder.mkdir()) throw new RuntimeException("Cannot create dstore folder (folder absolute path: " + dstoreFolder.getAbsolutePath() + ")");
try {
try {
socket_to_controller = new Socket("localhost", cport);
//communication tools for controller <-> client
Thread controllerThread = new Thread(new CONTROLLER_THREAD(socket_to_controller));
controllerThread.start();
} catch (Throwable e) {
String error = ("Unexpected System error when listening for Controller requests : " + (e.getMessage() != null ? e.getMessage() : e.toString()));
DstoreLogger.getInstance().log(error);
}
try {
ss = new ServerSocket(port);
for(;;) {
try {
if (socket_to_controller.isConnected()) {
System.out.println("Dstore is connected to the Controller server. Listening to Client Requests.");
Socket socket_to_client = ss.accept();
Thread clientThread = new Thread(new CLIENT_THREAD(socket_to_client));
clientThread.start();
} else {
}
System.out.println("Dstore is not connected to the Controller server.");
}catch (Throwable e){
String error = ("error"+e);
//DstoreLogger.getInstance().log(error);
}
}
} catch (Throwable e){
String error = ("Unexpected System error when listening for Client requests : " + (e.getMessage() != null ? e.getMessage() : e.toString()));
//DstoreLogger.getInstance().log(error);
}
} catch (Throwable e){
String error = ("Unexpected System error when listening for overall requests : " + (e.getMessage() != null ? e.getMessage() : e.toString()));
//DstoreLogger.getInstance().log(error);
}
}catch(Throwable e){
String error = ("Unexpected System error when initialising the server : " + (e.getMessage() != null ? e.getMessage() : e.toString()));
//DstoreLogger.getInstance().log(error);
}
}
}