From 27d7de0a23a9b5ecc8286b52624590e77f75a84a Mon Sep 17 00:00:00 2001
From: pr1n19 <pr1n19@soton.ac.uk>
Date: Wed, 27 Apr 2022 02:10:07 +0100
Subject: [PATCH] Join commad completed, and infrastructure for Remove command
 begun

---
 src/Controller.java | 84 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 83 insertions(+), 1 deletion(-)

diff --git a/src/Controller.java b/src/Controller.java
index 80b9912..f62ee4f 100644
--- a/src/Controller.java
+++ b/src/Controller.java
@@ -1,12 +1,94 @@
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.HashMap;
+
 public class Controller {
 
+    //Ports of each dStore, and associated socket
+    private static final HashMap<Integer,Socket> dStores = new HashMap<>();
+    //Ports of each dStore, and associated number of files
+    private static final HashMap<Integer,Integer> storeNumbers = new HashMap<>();
+
+    //Store filename & ports
+    private static final HashMap<String,int[]> index = new HashMap<>();
+
+
+
     public static void main(String[] args) {
         int cPort = Integer.parseInt(args[0]);
-        int r = Integer.parseInt(args[1]);
+        int replicationFactor = Integer.parseInt(args[1]);
         int timeOut = Integer.parseInt(args[2]);
         int rebalancePeriod = Integer.parseInt(args[3]);
 
 
+        try{
+            //Create socket for listening
+            ServerSocket listeningSocket = new ServerSocket(cPort);
+
+            new Thread(() -> {
+                //Continually listen for new commands
+                while (true) {
+                    try {
+                        //Accept new TCP connection & get command
+                        Socket client = listeningSocket.accept();
+                        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
+                        String received = in.readLine();
+                        String command = received.split(" ")[0];
+
+                        //Process depending on command received
+                        switch (command) {
+                            case "STORE" -> System.out.println("STORE");
+                            case "LOAD" -> System.out.println("LOAD");
+                            case "REMOVE" -> removeFile(received.split(" ")[1], client);
+                            case "LIST" -> System.out.println("LIST");
+                            case "JOIN" -> {
+                                int portNo = Integer.parseInt(received.split(" ")[1]);
+                                dStores.put(portNo, client);
+                                storeNumbers.put(portNo, 0);
+                            }
+                        }
+                    }catch (Exception e) {
+                        System.out.println("error "+e);
+                    }
+                }
+            }).start();
+
+
+
+        }catch (Exception e){
+            System.out.println("error "+e);
+        }
     }
 
+    //TODO Get r least populated DStores
+    private static ArrayList<Integer> emptyDStores() {
+        return new ArrayList<>();
+    }
+
+    //Check for all working dStores
+    private static ArrayList<Integer> workingDStores() {
+        ArrayList<Integer> workingPorts = new ArrayList<>();
+
+        //If dstore is still working add it to the list
+        for (Integer i:dStores.keySet()) {
+            if(checkDStore(dStores.get(i))){
+                workingPorts.add(i);
+            }
+        }
+
+        return workingPorts;
+    }
+
+    //TODO Test if given DStore is still active
+    private static boolean checkDStore(Socket dStore) {
+        return false;
+    }
+
+    //TODO Remove file from given DStores
+    private static void removeFile(String fileName, Socket client) {
+
+    }
 }
-- 
GitLab