diff --git a/src/Controller.java b/src/Controller.java index 80b99127926caf11cba567d8e2124533e9209a48..f62ee4f0c543d2cbd84ae3e449055f5df4571141 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) { + + } }