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

Wrapped Store and Load processes into runnable classes

parent 906fc8fa
No related branches found
No related tags found
No related merge requests found
import java.io.*;
import java.net.*;
import java.util.HashMap;
public class DStore {
private static String fileFolder;
private static int timeout;
private final static HashMap<String, String> fileList = new HashMap<>();
private static PrintWriter controller;
public static void main(String[] args) {
//Take arguments from command line
int port = Integer.parseInt(args[0]);
int cPort = Integer.parseInt(args[1]);
timeout = Integer.parseInt(args[2]);
fileFolder = args[3];
Float timeout = Float.parseFloat(args[2]);
String fileFolder = args[3];
//TODO Make connection with controller & listen to send acknowledgments
try{
//Socket socket = new Socket(InetAddress.getLocalHost(),cPort);
//PrintWriter toController = new PrintWriter(socket.getOutputStream());
Socket socket = new Socket(InetAddress.getLocalHost(),cPort);
controller = new PrintWriter(socket.getOutputStream());
//Send "JOIN port" to controller
//New thread to listen for "LIST" on cport
//New thread to listen for "LIST" or "REMOVE filename" on cport
//Reply with "LIST file_list" then go back to listening
......@@ -34,84 +35,51 @@ public class DStore {
ServerSocket ss = new ServerSocket(port);
//Listen for connections
for(;;){
while(true){
try{
//Accept connection & take inputs
//Accept connection to new thread
Socket client = ss.accept();
//Make output stream returning to client
OutputStream out = client.getOutputStream();
//Get instruction from connection
InputStream in = client.getInputStream();
byte[] buf = new byte[1000];
int bufLen = in.read(buf);
String firstBuffer = new String(buf, 0, bufLen);
//Make output stream returning to client
OutputStream out = client.getOutputStream();
//Get command (text before first space)
//Get command
int firstSpace = firstBuffer.indexOf(" ");
String command = firstBuffer.substring(0, firstSpace);
//Get file name (text between first and second space)
//Get filename
int secondSpace = firstBuffer.indexOf(" ", firstSpace + 1);
String fileName = firstBuffer.substring(firstSpace, secondSpace);
//Proceed depending on command
switch (command) {
case "STORE" -> {
//Get file size (text after third space)
int thirdSpace = firstBuffer.indexOf(" ", secondSpace + 1);
String fileSize = firstBuffer.substring(secondSpace, thirdSpace);
//If command = store, load or remove
switch (command) {
case "STORE" -> storeFile(in,out,fileName);
case "LOAD" -> loadFile(in,out,fileName);
case "REMOVE" -> removeFile(in,out,fileName);
//Store file in new thread and update list
new Thread(new StoreThread(controller,fileFolder,fileName,in,out,client)).start();
fileList.put(fileName, fileSize);
}
//Close everything after use
in.close();
out.close();
client.close();
}catch (Exception e){
System.out.println("error "+e);
case "LOAD_DATA" -> {
//Remove file in new thread and update list
new Thread(new LoadThread((fileFolder+fileName),in,out,client)).start();
fileList.remove(fileName);
}
}
}catch (Exception e){
System.out.println("error "+e);
}
}
private static void storeFile(InputStream in, OutputStream out, String fileName) {
try {
//Send ACK to client
out.write("ACK".getBytes());
//Prepare new buffer for more data
byte[] buf = new byte[1000];
int bufLen;
//Prepare to write to file
File outputFile = new File(fileFolder + fileName);
FileOutputStream fileOutput = new FileOutputStream(outputFile);
//Wait for receipt of new data
while((bufLen = in.read(buf)) != -1) {
fileOutput.write(buf,0,bufLen);
}
//Close file output
fileOutput.close();
}catch (Exception e){
System.out.println("error "+e);
}
}
private static void loadFile(InputStream in, OutputStream out, String fileName) {
}
private static void removeFile(InputStream in, OutputStream out, String fileName) {
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class LoadThread implements Runnable{
private final String filePath;
private final InputStream in;
private final OutputStream out;
private final Socket client;
public LoadThread(String f, InputStream i, OutputStream o, Socket c){
filePath = f;
in = i;
out = o;
client = c;
}
@Override
public void run() {
try {
//Load file and prepare input stream
File inputFile = new File(filePath);
FileInputStream inf = new FileInputStream(inputFile);
//Prepare byte stream for
byte[] buf = new byte[1000];
int bufLen;
//Send the file out
while((bufLen = inf.read(buf)) != -1){
out.write(buf,0,bufLen);
}
//Close streams
in.close();
out.close();
client.close();
}catch (Exception e){
System.out.println("error "+e);
}
}
}
import java.io.*;
import java.net.Socket;
public class StoreThread implements Runnable{
private final PrintWriter controller;
private final String filePath;
private final String fileName;
private final InputStream in;
private final OutputStream out;
private final Socket client;
public StoreThread(PrintWriter co, String p, String n, InputStream i, OutputStream o, Socket c) {
controller = co;
filePath = p;
fileName = n;
in = i;
out = o;
client = c;
}
@Override
public void run() {
try {
//Send ACK to client
out.write("ACK".getBytes());
//Prepare to write to file
File outputFile = new File(filePath+fileName);
FileOutputStream fileOutput = new FileOutputStream(outputFile);
//Prepare new buffer for data from client
byte[] buf = new byte[1000];
int bufLen;
//Wait for receipt of new data
while((bufLen = in.read(buf)) != -1) {
fileOutput.write(buf,0,bufLen);
}
//Send completion ack to controller
controller.write("STORE_ACK "+fileName);
//Close file output
fileOutput.close();
//Close streams
in.close();
out.close();
client.close();
}catch (Exception e){
System.out.println("error "+e);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment