diff --git a/src/DStore.java b/src/DStore.java index b826a7161ee78e86e5fe8de23ca2d9035de4297a..0d327cb12f134b203e04f956a670ed8641c47f7a 100644 --- a/src/DStore.java +++ b/src/DStore.java @@ -1,26 +1,27 @@ import java.io.*; import java.net.*; +import java.util.HashMap; public class DStore { - private static String fileFolder; - private static int timeout; + private final static HashMap<String, String> fileList = new HashMap<>(); + private static PrintWriter controller; public static void main(String[] args) { //Take arguments from command line int port = Integer.parseInt(args[0]); int cPort = Integer.parseInt(args[1]); - timeout = Integer.parseInt(args[2]); - fileFolder = args[3]; + Float timeout = Float.parseFloat(args[2]); + String fileFolder = args[3]; //TODO Make connection with controller & listen to send acknowledgments try{ - //Socket socket = new Socket(InetAddress.getLocalHost(),cPort); - //PrintWriter toController = new PrintWriter(socket.getOutputStream()); + Socket socket = new Socket(InetAddress.getLocalHost(),cPort); + controller = new PrintWriter(socket.getOutputStream()); //Send "JOIN port" to controller - //New thread to listen for "LIST" on cport + //New thread to listen for "LIST" or "REMOVE filename" on cport //Reply with "LIST file_list" then go back to listening @@ -34,84 +35,51 @@ public class DStore { ServerSocket ss = new ServerSocket(port); //Listen for connections - for(;;){ + while(true){ try{ - //Accept connection & take inputs + //Accept connection to new thread Socket client = ss.accept(); - InputStream in = client.getInputStream(); - byte[] buf = new byte[1000]; - int bufLen = in.read(buf); - String firstBuffer = new String(buf,0,bufLen); //Make output stream returning to client OutputStream out = client.getOutputStream(); - //Get command (text before first space) - int firstSpace = firstBuffer.indexOf(" "); - String command = firstBuffer.substring(0,firstSpace); + //Get instruction from connection + InputStream in = client.getInputStream(); + byte[] buf = new byte[1000]; + int bufLen = in.read(buf); + String firstBuffer = new String(buf, 0, bufLen); - //Get file name (text between first and second space) - int secondSpace = firstBuffer.indexOf(" ",firstSpace+1); - String fileName = firstBuffer.substring(firstSpace,secondSpace); + //Get command + int firstSpace = firstBuffer.indexOf(" "); + String command = firstBuffer.substring(0, firstSpace); - //Get file size (text after third space) - int thirdSpace = firstBuffer.indexOf(" ",secondSpace+1); - String fileSize = firstBuffer.substring(secondSpace,thirdSpace); + //Get filename + int secondSpace = firstBuffer.indexOf(" ", firstSpace + 1); + String fileName = firstBuffer.substring(firstSpace, secondSpace); - //If command = store, load or remove + //Proceed depending on command switch (command) { - case "STORE" -> storeFile(in,out,fileName); - case "LOAD" -> loadFile(in,out,fileName); - case "REMOVE" -> removeFile(in,out,fileName); + case "STORE" -> { + //Get file size (text after third space) + int thirdSpace = firstBuffer.indexOf(" ", secondSpace + 1); + String fileSize = firstBuffer.substring(secondSpace, thirdSpace); + + //Store file in new thread and update list + new Thread(new StoreThread(controller,fileFolder,fileName,in,out,client)).start(); + fileList.put(fileName, fileSize); + } + case "LOAD_DATA" -> { + //Remove file in new thread and update list + new Thread(new LoadThread((fileFolder+fileName),in,out,client)).start(); + fileList.remove(fileName); + } } - - //Close everything after use - in.close(); - out.close(); - client.close(); - }catch (Exception e){ System.out.println("error "+e); } } - }catch (Exception e){ System.out.println("error "+e); } } - - - private static void storeFile(InputStream in, OutputStream out, String fileName) { - try { - //Send ACK to client - out.write("ACK".getBytes()); - - //Prepare new buffer for more data - byte[] buf = new byte[1000]; - int bufLen; - - //Prepare to write to file - File outputFile = new File(fileFolder + fileName); - FileOutputStream fileOutput = new FileOutputStream(outputFile); - - //Wait for receipt of new data - while((bufLen = in.read(buf)) != -1) { - fileOutput.write(buf,0,bufLen); - } - - //Close file output - fileOutput.close(); - - }catch (Exception e){ - System.out.println("error "+e); - } - } - - private static void loadFile(InputStream in, OutputStream out, String fileName) { - - } - - private static void removeFile(InputStream in, OutputStream out, String fileName) { - - } } diff --git a/src/LoadThread.java b/src/LoadThread.java new file mode 100644 index 0000000000000000000000000000000000000000..442a513a9b18704fed44d35f74b341141e8448ec --- /dev/null +++ b/src/LoadThread.java @@ -0,0 +1,45 @@ +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +public class LoadThread implements Runnable{ + + private final String filePath; + private final InputStream in; + private final OutputStream out; + private final Socket client; + + public LoadThread(String f, InputStream i, OutputStream o, Socket c){ + filePath = f; + in = i; + out = o; + client = c; + } + + @Override + public void run() { + try { + //Load file and prepare input stream + File inputFile = new File(filePath); + FileInputStream inf = new FileInputStream(inputFile); + + //Prepare byte stream for + byte[] buf = new byte[1000]; + int bufLen; + + //Send the file out + while((bufLen = inf.read(buf)) != -1){ + out.write(buf,0,bufLen); + } + + //Close streams + in.close(); + out.close(); + client.close(); + }catch (Exception e){ + System.out.println("error "+e); + } + } +} diff --git a/src/StoreThread.java b/src/StoreThread.java new file mode 100644 index 0000000000000000000000000000000000000000..bdec09a9801f8224ff25e6381149dffb9db76d11 --- /dev/null +++ b/src/StoreThread.java @@ -0,0 +1,55 @@ +import java.io.*; +import java.net.Socket; + +public class StoreThread implements Runnable{ + + private final PrintWriter controller; + private final String filePath; + private final String fileName; + private final InputStream in; + private final OutputStream out; + private final Socket client; + + public StoreThread(PrintWriter co, String p, String n, InputStream i, OutputStream o, Socket c) { + controller = co; + filePath = p; + fileName = n; + in = i; + out = o; + client = c; + } + + @Override + public void run() { + try { + //Send ACK to client + out.write("ACK".getBytes()); + + //Prepare to write to file + File outputFile = new File(filePath+fileName); + FileOutputStream fileOutput = new FileOutputStream(outputFile); + + //Prepare new buffer for data from client + byte[] buf = new byte[1000]; + int bufLen; + + //Wait for receipt of new data + while((bufLen = in.read(buf)) != -1) { + fileOutput.write(buf,0,bufLen); + } + + //Send completion ack to controller + controller.write("STORE_ACK "+fileName); + + //Close file output + fileOutput.close(); + + //Close streams + in.close(); + out.close(); + client.close(); + }catch (Exception e){ + System.out.println("error "+e); + } + } +}