Skip to content
Snippets Groups Projects
Commit 2bf9e310 authored by pr1n19's avatar pr1n19
Browse files

Added functionality for controller commands

parent 7d3bc914
No related branches found
No related tags found
No related merge requests found
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashSet;
import java.util.List; import java.util.Iterator;
public class DStore { public class DStore {
private final static HashMap<String, String> fileList = new HashMap<>(); private final static HashSet<String> fileList = new HashSet<>();
private static PrintWriter controller; private static PrintWriter controller;
public static void main(String[] args) { public static void main(String[] args) {
...@@ -16,16 +16,31 @@ public class DStore { ...@@ -16,16 +16,31 @@ public class DStore {
Float timeout = Float.parseFloat(args[2]); Float timeout = Float.parseFloat(args[2]);
String fileFolder = args[3]; String fileFolder = args[3];
//TODO Make connection with controller & listen to send acknowledgments //Make connection with controller & listen to send acknowledgments
try{ try{
Socket socket = new Socket(InetAddress.getLocalHost(),cPort); Socket socket = new Socket(InetAddress.getLocalHost(),cPort);
controller = new PrintWriter(socket.getOutputStream()); controller = new PrintWriter(socket.getOutputStream());
//Send "JOIN port" to controller //Send "JOIN port" to controller
//New thread to listen for "LIST" or "REMOVE filename" on cport controller.write("JOIN "+port);
//Reply with "LIST file_list" then go back to listening
//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){ }catch (Exception e){
System.out.println("error "+e); System.out.println("error "+e);
...@@ -64,11 +79,11 @@ public class DStore { ...@@ -64,11 +79,11 @@ public class DStore {
case "STORE" -> { case "STORE" -> {
//Get file size (text after third space) //Get file size (text after third space)
int thirdSpace = firstBuffer.indexOf(" ", secondSpace + 1); int thirdSpace = firstBuffer.indexOf(" ", secondSpace + 1);
String fileSize = firstBuffer.substring(secondSpace, thirdSpace); int fileSize = Integer.parseInt(firstBuffer.substring(secondSpace, thirdSpace));
//Store file in new thread and update list //Store file in new thread and update list
new Thread(new StoreThread(controller,fileFolder,fileName,in,out,client)).start(); new Thread(new StoreThread(controller,fileFolder,fileName,in,out,client,fileSize)).start();
fileList.put(fileName, fileSize); fileList.add(fileName);
} }
case "LOAD_DATA" -> //Load file in new thread case "LOAD_DATA" -> //Load file in new thread
new Thread(new LoadThread((fileFolder+fileName),in,out,client)).start(); new Thread(new LoadThread((fileFolder+fileName),in,out,client)).start();
...@@ -82,19 +97,19 @@ public class DStore { ...@@ -82,19 +97,19 @@ public class DStore {
} }
} }
private void listToController() { private static void listToController() {
//Compile list as string //Compile list as string
StringBuilder stringList = new StringBuilder(); StringBuilder stringList = new StringBuilder();
ArrayList<String> keys = new ArrayList<>(fileList.keySet());
for(String file : keys){ for (String s : fileList) {
stringList.append(file).append(" ").append(fileList.get(file)).append(";"); stringList.append(s).append(" ");
} }
//Return value //Return value
controller.write(stringList.toString()); controller.write(stringList.toString());
} }
private void removeFile(String filePath, String fileName){ private static void removeFile(String filePath, String fileName){
//Remove file from folder //Remove file from folder
File file = new File(filePath+fileName); File file = new File(filePath+fileName);
file.delete(); file.delete();
......
...@@ -9,14 +9,16 @@ public class StoreThread implements Runnable{ ...@@ -9,14 +9,16 @@ public class StoreThread implements Runnable{
private final InputStream in; private final InputStream in;
private final OutputStream out; private final OutputStream out;
private final Socket client; private final Socket client;
private final int fileSize;
public StoreThread(PrintWriter co, String p, String n, InputStream i, OutputStream o, Socket c) { public StoreThread(PrintWriter co, String p, String n, InputStream i, OutputStream o, Socket c, int fs) {
controller = co; controller = co;
filePath = p; filePath = p;
fileName = n; fileName = n;
in = i; in = i;
out = o; out = o;
client = c; client = c;
fileSize = fs;
} }
@Override @Override
...@@ -29,14 +31,9 @@ public class StoreThread implements Runnable{ ...@@ -29,14 +31,9 @@ public class StoreThread implements Runnable{
File outputFile = new File(filePath+fileName); File outputFile = new File(filePath+fileName);
FileOutputStream fileOutput = new FileOutputStream(outputFile); FileOutputStream fileOutput = new FileOutputStream(outputFile);
//Prepare new buffer for data from client //Read correct bytes from input stream and write to file
byte[] buf = new byte[1000]; byte[] buf = in.readNBytes(fileSize);
int bufLen; fileOutput.write(buf);
//Wait for receipt of new data
while((bufLen = in.read(buf)) != -1) {
fileOutput.write(buf,0,bufLen);
}
//Send completion ack to controller //Send completion ack to controller
controller.write("STORE_ACK "+fileName); controller.write("STORE_ACK "+fileName);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment