From 61c5751f34049d2b4cd007b6e108433d49c20c75 Mon Sep 17 00:00:00 2001 From: Theodora-Mara Pislar <tmp1u19@soton.ac.uk> Date: Sat, 8 May 2021 18:30:59 +0100 Subject: [PATCH] Add the handler methods into the dstore --- src/Handler.java | 129 ----------------------------------------------- 1 file changed, 129 deletions(-) delete mode 100644 src/Handler.java diff --git a/src/Handler.java b/src/Handler.java deleted file mode 100644 index 042ee34..0000000 --- a/src/Handler.java +++ /dev/null @@ -1,129 +0,0 @@ -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"); - } - - } - } - } - } - - } -} -- GitLab