Skip to content
Snippets Groups Projects
Commit 27d7de0a authored by pr1n19's avatar pr1n19
Browse files

Join commad completed, and infrastructure for Remove command begun

parent 158eaef1
No related branches found
No related tags found
No related merge requests found
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 { 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) { public static void main(String[] args) {
int cPort = Integer.parseInt(args[0]); 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 timeOut = Integer.parseInt(args[2]);
int rebalancePeriod = Integer.parseInt(args[3]); 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) {
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment