Skip to content
Snippets Groups Projects
Commit 61c5751f authored by tmp1u19's avatar tmp1u19 :octopus:
Browse files

Add the handler methods into the dstore

parent 03c30ec7
No related branches found
No related tags found
No related merge requests found
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
public class Handler {
public void handleDstoreClientReq(Socket client, Dstore dstore) throws IOException {
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream();
BufferedReader req = new BufferedReader(new InputStreamReader(in));
PrintWriter res = new PrintWriter(new OutputStreamWriter(out));
String line;
while((line = req.readLine()) != null) {
String[] tokens = line.split(" ");
String command = tokens[0];
if(dstore.getFiles().size() < Controller.R) {
res.println("ERROR_NOT_ENOUGH_DSTORES");
res.flush();
} else {
switch(command) {
case "STORE" : {
try {
res.println("ACK");
res.flush();
String filename = tokens[1];
int filesize = Integer.parseInt(tokens[2]);
// read data and store the file
byte[] data = new byte[filesize];
client.getInputStream().readNBytes(data, 0, filesize);
FileOutputStream o = new FileOutputStream(dstore.getFile_folder() +
"/" + filename);
o.write(data);
o.flush();
o.close();
dstore.getFiles().add(filename);
if(Controller.store.containsKey(filename)) {
Controller.store.get(filename).add(dstore);
} else {
List<Dstore> d = new ArrayList<>();
d.add(dstore);
Controller.store.put(filename, d);
}
PrintWriter r = new PrintWriter(new OutputStreamWriter(dstore.getControllerSocket().getOutputStream()));
r.println("STORE_COMPLETE");
} catch (IndexOutOfBoundsException e) {
System.out.println("Arguments don;t match the STORE operation");
}
}
case "LOAD_DATA" : {
try {
String filename = tokens[1];
if(dstore.getFiles().contains(filename)) {
File file = new File(dstore.getFile_folder() + "/" + filename);
int n;
while((n = in.read())!= -1) {
client.getOutputStream().write(n);
}
} else {
client.close();
}
} catch (IndexOutOfBoundsException e) {
System.out.println("Arguments don't match in LOAD operation");
}
}
}
}
}
}
public void handleDstoreControllerReq(Dstore dstore) throws IOException {
OutputStream out = dstore.getControllerSocket().getOutputStream();
InputStream in = dstore.getControllerSocket().getInputStream();
BufferedReader req = new BufferedReader(new InputStreamReader(in));
PrintWriter res = new PrintWriter(new OutputStreamWriter(out));
String line;
while((line = req.readLine()) != null) {
String[] tokens = line.split(" ");
String command = tokens[0];
if(dstore.getFiles().size() < Controller.R) {
res.println("ERROR_NOT_ENOUGH_DSTORES");
res.flush();
} else {
switch (command) {
case "REMOVE" : {
try {
String filename = tokens[1];
if(dstore.getFiles().contains(filename)) {
File file = new File(dstore.getFile_folder() + "/" + filename);
file.delete();
Controller.store.get(filename).remove(dstore);
res.println("REMOVE_ACK");
res.flush();
} else {
res.println("ERROR_FILE_DOES_NOT_EXIST");
res.flush();
}
} catch(IndexOutOfBoundsException e) {
System.out.println("Arguments don't match in REMOVE opretaion");
}
}
}
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment