Select Git revision
Dstore.java
Dstore.java 11.74 KiB
import java.io.*;
import java.lang.Runnable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.net.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class Dstore {
protected int port; //Port to listen on
protected int cport; //Controller's port to talk to
protected int timeout; //in milliseconds
protected File fileFolder; //Where to store the data locally
protected Map<String,Long> fileSizes;
protected Socket controllerSocket;
protected BufferedReader controllerIn;
protected PrintWriter controllerOut;
protected final int BUFFER_SIZE = 256;
public Dstore(int port, int cport, int timeout, String fileFolderName) throws Exception {
this.port = port;
this.cport = cport;
this.timeout = timeout;
DstoreLogger.init(Logger.LoggingType.ON_FILE_AND_TERMINAL, port);
fileFolder = new File(fileFolderName);
if(fileFolder.exists() && !fileFolder.isDirectory()) {
throw new Exception("Folder name provided exists as a file and not a directory");
}
else if(!fileFolder.exists()) {
System.out.println("New folder will be created");
if(!fileFolder.mkdir()) throw new Exception("Folder could not be created");
}
fileSizes = new HashMap<String,Long>();
for(File file : fileFolder.listFiles()) {
if(!file.delete()) throw new Exception("Directory specified has undeletable files; please try a different directory");
}
}
public static void main(String[] args) {
try {
int port = Integer.parseInt(args[0]);
int cport = Integer.parseInt(args[1]);
int timeout = Integer.parseInt(args[2]);
String fileFolder = args[3];
if(port < 0 || cport < 0 || timeout < 0) {
throw new Exception("Infeasible values provided as arguments");
}
Dstore dstore = new Dstore(port, cport, timeout, fileFolder);
dstore.start();
}
catch(IndexOutOfBoundsException e) {
System.out.println("Command line arguments have not been provided");
return;
}
catch(NumberFormatException e) {
System.out.println("Command line arguments must be integers");
return;
}