Skip to content
Snippets Groups Projects
Select Git revision
  • 368fc820245babbfdebe6245737d194a456a53d3
  • master default protected
2 results

DStore.java

Blame
  • DStore.java 4.38 KiB
    import java.io.*;
    import java.net.*;
    import java.util.HashSet;
    
    public class DStore {
    
        private final static HashSet<String> fileList = new HashSet<>();
        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]);
            Float timeout = Float.parseFloat(args[2]);
            String fileFolder = args[3];
    
            //Make connection with controller & listen to send acknowledgments
            try{
                Socket socket = new Socket(InetAddress.getLocalHost(),cPort);
                controller = new PrintWriter(socket.getOutputStream());
    
                //Send "JOIN port" to  controller
                controller.write("JOIN "+port);
    
                //New thread to listen for "LIST" & "REMOVE FILENAME"
                new Thread(() -> {
                    try {
                        BufferedReader contIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        while(true){
                            String received = contIn.readLine();
                            String command = received.split(" ")[0];
    
                            switch (command) {
                                case "LIST" -> listToController();
                                case "REMOVE" -> removeFile(fileFolder,(received.split(" ")[1]));
                            }
                        }
                    }catch (Exception e){
                        System.out.println("error "+e);
                    }
                }).start();
    
            }catch (Exception e){
                System.out.println("error "+e);
            }
    
            //Create socket to listen for store, load & remove requests
            try{
                //Create server socket to listen for connections
                ServerSocket ss = new ServerSocket(port);
    
                //Listen for connections
                while(true){
                    try{
                        //Accept connection to new thread
                        Socket client = ss.accept();
    
                        //Make output stream returning to client
                        OutputStream out = client.getOutputStream();
    
                        //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 command
                        int firstSpace = firstBuffer.indexOf(" ");
                        String command = firstBuffer.substring(0, firstSpace);
    
                        //Get filename
                        int secondSpace = firstBuffer.indexOf(" ", firstSpace + 1);
                        String fileName = firstBuffer.substring(firstSpace, secondSpace);
    
                        //Proceed depending on command
                        switch (command) {
                            case "STORE" -> {
                                //Get file size (text after third space)
                                int thirdSpace = firstBuffer.indexOf(" ", secondSpace + 1);
                                int fileSize = Integer.parseInt(firstBuffer.substring(secondSpace, thirdSpace));
    
                                //Store file in new thread and update list
                                new Thread(new StoreThread(controller,fileFolder,fileName,in,out,client,fileSize)).start();
                                fileList.add(fileName);
                            }
                            case "LOAD_DATA" -> //Load file in new thread
                                    new Thread(new LoadThread((fileFolder+File.separator+fileName),in,out,client)).start();
                        }
                    }catch (Exception e){
                        System.out.println("error "+e);
                    }
                }
            }catch (Exception e){
                System.out.println("error "+e);
            }
        }
    
        private static void listToController() {
            //Compile list as string
            StringBuilder stringList = new StringBuilder();
    
            for (String s : fileList) {
                stringList.append(s).append(" ");
            }
    
            //Return value
            controller.write(stringList.toString());
        }
    
        private static void removeFile(String filePath, String fileName){
            //Remove file from folder
            File file = new File(filePath+File.separator+fileName);
            file.delete();
    
            //Remove file from list
            fileList.remove(fileName);
        }
    }