Skip to content
Snippets Groups Projects
Commit 7842cf8a authored by cg4g19's avatar cg4g19
Browse files

Multiple clients storing loading and removing now working

parent eb3412db
No related branches found
No related tags found
No related merge requests found
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
......@@ -46,6 +47,17 @@ public class Controller {
DstoreListener dstoreListener = new DstoreListener(client, port);
dstores.add(dstoreListener);
dstoreListener.start();
/*for(DstoreListener listener : dstores){
Socket dstore = listener.dstore;
System.out.println("DSTORES: " + dstores.size());
line = sendMessageAndAwaitReply(dstore, Protocol.LIST_TOKEN);
parsedLine = Parser.parse(line);
command = parsedLine[0];
String[] dstorefile_list = Arrays.copyOfRange(parsedLine, 1, parsedLine.length);
}*/
}else{
ClientListener clientListener = new ClientListener(client, line);
clientListener.start();
......@@ -56,9 +68,15 @@ public class Controller {
//SOCKET SEND AND RECEIVE
public static void sendMessage(Socket socket, String message) throws IOException {
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(message);
ControllerLogger.getInstance().messageSent(socket, message);
} catch (Exception e) {
System.out.println("ERROR SENDING MESSAGE:\n");
e.printStackTrace();
}
}
public static String receiveMessage(Socket socket) throws IOException{
......@@ -88,6 +106,7 @@ public class Controller {
}
static class DstoreListener extends Thread {
public Socket dstore;
public int port;
......@@ -101,6 +120,10 @@ public class Controller {
sendMessage(dstore, message);
}
public void remove(){
dstores.remove(this);
}
@Override
public void run() {
ExecutorService executor = Executors.newSingleThreadExecutor();
......@@ -117,6 +140,8 @@ public class Controller {
String filename = parsedLine[1];
ongoingRemoves.get(filename).ack(port);
}
}else{
break;
}
} catch (Exception e) {
......@@ -124,6 +149,7 @@ public class Controller {
//TODO: handle exception
}
}
System.out.println("Dstore disconnected!");
}
}
......@@ -196,24 +222,33 @@ public class Controller {
index.changeStatus(filename, "remove in progress");
int[] ports = index.getFileInfo(filename).storePorts;
for(int port : ports){
getDstoreListener(port).send(Protocol.REMOVE_TOKEN + " " + filename);
sendMessage(getDstoreListener(port).dstore, Protocol.REMOVE_TOKEN + " " + filename);
}
GetRemoveAcks removeAck = new GetRemoveAcks(ports);
ongoingRemoves.put(filename, removeAck);
Future<Boolean> future = executor.submit(removeAck);
if(future.get(timeout, TimeUnit.MILLISECONDS)){
System.out.println("REMOVE SUCCESS");
sendMessage(client, Protocol.REMOVE_COMPLETE_TOKEN);
index.remove(filename);
ongoingRemoves.remove(filename);
}
}
}else if(command.equals(Protocol.RELOAD_TOKEN)){
String filename = parsedLine[1];
if(index.removeFirstPort(filename)){
int port = index.getFileInfo(filename).storePorts[0];
int filesize = index.getFileInfo(filename).filesize;
sendMessage(client, Protocol.LOAD_FROM_TOKEN + " " + port + " " + filesize);
}else{
sendMessage(client, Protocol.ERROR_LOAD_TOKEN);
}
}
line = receiveMessage(client);
}
} catch (Exception e) {
e.printStackTrace();
//If future times out
break;
}
......
......@@ -24,6 +24,8 @@ public class Dstore {
try {
Socket controller = new Socket("localhost", cport);
sendMessage(controller, "JOIN " + port);
ControllerListener controllerListener = new ControllerListener(controller);
controllerListener.start();
while(true){
Socket client = dstoreServer.accept();
ClientListener clientListener = new ClientListener(client, controller);
......@@ -85,6 +87,9 @@ public class Dstore {
String filename = parsedLine[1];
File file = new File(file_folder, filename);
if(!file.exists()){
client.close();
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] bytearray = bis.readAllBytes();
......@@ -93,23 +98,56 @@ public class Dstore {
os.write(bytearray);
os.flush();
bis.close();
}else if(command.equals(Protocol.REMOVE_TOKEN)){
}
}
} catch (Exception e) {
e.printStackTrace();
//TODO: handle exception
}
}
}
}
static class ControllerListener extends Thread {
Socket controller;
public ControllerListener(Socket controller){
this.controller = controller;
}
@Override
public void run(){
while(controller.isConnected()){
try {
String line = receiveMessage(controller);
if(line != null){
String[] parsedLine = Parser.parse(line);
String command = parsedLine[0];
if(command.equals(Protocol.REMOVE_TOKEN)){
String filename = parsedLine[1];
File file = new File(file_folder, filename);
if(file.delete()){
sendMessage(controller, Protocol.REMOVE_ACK_TOKEN + " " + filename);
}else{
sendMessage(controller, Protocol.ERROR_FILE_DOES_NOT_EXIST_TOKEN + " " + filename);
}
}else if(command.equals(Protocol.LIST_TOKEN)){
String file_list = "";
for(File file : file_folder.listFiles()){
file_list += file.getName() + " ";
}
file_list = file_list.trim();
sendMessage(controller, Protocol.LIST_TOKEN + " " + file_list);
}
}
} catch (Exception e) {
e.printStackTrace();
//TODO: handle exception
}
}
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
public class Index {
class FileInfo {
......@@ -34,6 +35,16 @@ public class Index {
index.remove(getFileInfo(name));
}
public boolean removeFirstPort(String name){
int[] storePorts = getFileInfo(name).storePorts;
if(storePorts.length == 1){
return false;
}else{
getFileInfo(name).storePorts = Arrays.copyOfRange(storePorts, 1, storePorts.length);
return true;
}
}
public String getFileList(){
String file_list = "";
for(FileInfo fInfo : index){
......@@ -60,4 +71,5 @@ public class Index {
public boolean fileExists(String name){
return (getFileInfo(name) != null);
}
}
\ No newline at end of file
import java.io.IOException;
public class Parser {
public static void main(String[] args) throws IOException {
}
public static String[] parse(String args, int amount) throws IOException {
String[] splitArgs = args.split(" ");
if(splitArgs.length != (amount + 1)) throw new IOException("Invalid paramters\nExpected:" + amount + "\nActual:" + splitArgs.length);
return splitArgs;
}
public static String[] parse(String[] args, int amount) throws IOException {
if(args.length != (amount)) throw new IOException("Invalid paramters\nExpected:" + amount + "\nActual:" + args.length);
return args;
}
public static String[] parse(String args){
return args.split(" ");
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment