Skip to content
Snippets Groups Projects
Commit 936685b4 authored by ik1g19's avatar ik1g19
Browse files

add remove operation

parent a4af4983
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -13,7 +13,7 @@ public class ClientMain {
if (!downloadFolder.mkdir()) throw new RuntimeException("Cannot create download folder (folder absolute path: " + downloadFolder.getAbsolutePath() + ")");
testClient(cport, timeout, downloadFolder, uploadFolder);
testClient(cport, timeout, downloadFolder);
}
......@@ -31,7 +31,7 @@ public class ClientMain {
try { client.store(new File("test.txt")); } catch(IOException e) { e.printStackTrace(); }
try {Thread.sleep(10000);} catch (InterruptedException e) {System.out.println(e);}
try {Thread.sleep(5000);} catch (InterruptedException e) {System.out.println(e);}
// try { client.store(new File("Clipboard01.pdf")); } catch(IOException e) { e.printStackTrace(); }
//
......@@ -40,14 +40,14 @@ public class ClientMain {
// String list[] = null;
// try { list = list(client); } catch(IOException e) { e.printStackTrace(); }
//
try { client.load("test.txt", downloadFolder); } catch(IOException e) { e.printStackTrace(); }
// try { client.load("test.txt", downloadFolder); } catch(IOException e) { e.printStackTrace(); }
/*if (list != null)
for (String filename : list)
try { client.remove(filename); } catch(IOException e) { e.printStackTrace(); }
try { client.remove(list[0]); } catch(IOException e) { e.printStackTrace(); }
// if (list != null)
// for (String filename : list)
// try { client.remove(filename); } catch(IOException e) { e.printStackTrace(); }
try { client.remove("test.txt"); } catch(IOException e) { e.printStackTrace(); }
try { list(client); } catch(IOException e) { e.printStackTrace(); }*/
// try { list(client); } catch(IOException e) { e.printStackTrace(); }
} finally {
if (client != null)
......
a test file
\ No newline at end of file
a test file
\ No newline at end of file
a test file
\ No newline at end of file
......@@ -60,11 +60,13 @@ public class Controller extends Server {
String command = args[0];
if (command.equals("JOIN")) {
Socket dStoreSock = client;
Integer port = Integer.parseInt(args[1]);
send("LIST", client);
send("LIST", dStoreSock);
String files = readSocket(client);
String files = readSocket(dStoreSock);
DStoreConnection dStore;
......@@ -89,7 +91,7 @@ public class Controller extends Server {
nextID++;
send("ACK", client);
send("JOINED", dStoreSock);
}
else if (command.equals("STORE")) {
......@@ -114,16 +116,15 @@ public class Controller extends Server {
collect(Collectors.joining(" "));
file.setStoreAcksQuota(r);
file.setAcksQuota(r);
send("STORE_TO " + ports, client);
Boolean fileStored = false;
while (!fileStored) {
if (file.ackCheck()) {
if (file.storeAckCheck()) {
file.setStoreInProgress(false);
file.setStoreComplete(true);
fileStored = true;
threadIDOutput("Store of file " + filename + " complete");
......@@ -148,6 +149,67 @@ public class Controller extends Server {
send("LOAD_FROM " + dStorePort + " " + file.getFilesize(), client);
}
else if (command.equals("REMOVE")) {
String filename = args[1];
DStoreFile file = fileIndex.get(filename);
file.setRemoveInProgress(true);
DStoreIndex dStores = file.getDStores();
dStores.entrySet().stream().
forEach(x -> {
Socket dStore = connectToDStore(x.getValue());
if (dStore != null) {
send("REMOVE " + filename, dStore);
} else threadIDErr("Unable to connect to DataStore");
closeConnection("DataStore", dStore);
});
Boolean fileRemoved = false;
while (!fileRemoved) {
if (file.removeAckCheck()) {
file.setRemoveInProgress(false);
fileRemoved = true;
threadIDOutput("Remove of file " + filename + " complete");
send("STORE_COMPLETE", client);
}
}
}
else if (command.equals("REMOVE_ACK")) {
String filename = args[1];
DStoreFile file = fileIndex.get(filename);
file.removeAck();
}
}
public Socket connectToDStore(DStoreConnection dStoreConnection) {
Socket dStore = null;
Boolean joined = false;
for (int i = 0; (i < 10) && !joined; i++) {
try { dStore = new Socket("localhost", dStoreConnection.getPort()); joined = true; }
catch (IOException e) {
threadIDErr(e.getMessage());
try {threadIDErr("Retrying Connection..."); Thread.sleep(1000);}
catch (InterruptedException exc) {threadIDErr(exc.getMessage());}
}
}
return dStore;
}
}
\ No newline at end of file
......@@ -84,6 +84,8 @@ public class DStore extends Server {
String command = args[0];
if (command.equals("LIST")) {
Socket controller = client;
File folder = new File(file_folder);
String fileMessage = "empty";
......@@ -99,14 +101,14 @@ public class DStore extends Server {
}
send(fileMessage, client);
send(fileMessage, controller);
String response = readSocket(client);
if (response.equals("ACK")) threadIDOutput("Successfully joined Controller");
String response = readSocket(controller);
if (response.equals("JOINED")) threadIDOutput("Successfully joined Controller");
closeConnection("Client", client);
closeConnection("Client", controller);
}
else if (command.equals("STORE")) {
......@@ -135,13 +137,23 @@ public class DStore extends Server {
else if (command.equals("LOAD_DATA")) {
String filename = args[1];
try {
sendFile(client, file_folder + "\\" + filename);
} catch (IOException e) {
threadIDErr(e.getMessage());
}
}
else if (command.equals("REMOVE")) {
Socket controller = client;
String filename = args[1];
if (deleteFile(file_folder + "\\" + filename)) threadIDOutput("Deleted file " + filename);
else threadIDErr("Failed to delete file " + filename);
send("REMOVE_ACK", controller);
}
}
......@@ -193,4 +205,12 @@ public class DStore extends Server {
//out.close();
}
public Boolean deleteFile(String filepath) {
File file = new File(filepath);
return file.delete();
}
}
......@@ -6,13 +6,14 @@ public class DStoreFile {
private Long filesize;
private boolean storeInProgress;
private boolean storeComplete;
private boolean removeInProgress;
private boolean removeComplete;
private int storeAcksQuota;
private int storeAcks = 0;
private int removeAcksQuota;
private int removeAcks = 0;
private DStoreIndex dStoreIndex;
......@@ -27,16 +28,12 @@ public class DStoreFile {
public boolean isStoreInProgress() {return storeInProgress;}
public boolean isStoreComplete() {return storeComplete;}
public boolean isRemoveInProgress() {return removeInProgress;}
public boolean isRemoveComplete() {return removeComplete;}
public void setStoreInProgress(Boolean store) { storeInProgress = store; }
public void setStoreComplete(Boolean complete) { storeComplete = complete; }
public void setRemoveInProgress(Boolean remove) { removeInProgress = remove; }
public void setRemoveComplete(Boolean complete) { removeComplete = complete; }
......@@ -45,17 +42,25 @@ public class DStoreFile {
public void setAcksQuota(int quota) { storeAcksQuota = quota; removeAcksQuota = quota; }
public void setStoreAcksQuota(int quota) { storeAcksQuota = quota; }
public int getStoreAcks() { return storeAcks; }
public void setRemoveAcksQuota(int quota) { removeAcksQuota = quota; }
public int getStoreAcksQuota() { return storeAcksQuota; }
public int storeAck() { return storeAcks++; }
public Boolean storeAckCheck() { return storeAcks == storeAcksQuota; }
public Boolean storeAckCheck() { return storeAcks++ == storeAcksQuota; }
public Boolean ackCheck() { return storeAcks == storeAcksQuota; }
public int removeAck() { return removeAcks++; }
public Boolean removeAckCheck() { return removeAcks == removeAcksQuota; }
......@@ -67,4 +72,8 @@ public class DStoreFile {
return dStoreIndex.getFirstAvailable();
}
public DStoreIndex getDStores() { return dStoreIndex; }
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment