From 936685b47e4944b2edee3e9c9c46232d0d59cef2 Mon Sep 17 00:00:00 2001
From: "isaacklugman@gmail.com" <ik1g19@soton.ac.uk>
Date: Tue, 4 May 2021 21:15:22 +0100
Subject: [PATCH] add remove operation

---
 ClientAndLoggers/ClientMain.class | Bin 2868 -> 2734 bytes
 ClientAndLoggers/ClientMain.java  |  16 +++----
 Dstores/dstore/test.txt           |   1 -
 Dstores/dstore2/test.txt          |   1 -
 Dstores/dstore3/test.txt          |   1 -
 src/ftp/Controller.java           |  74 +++++++++++++++++++++++++++---
 src/ftp/DStore.java               |  30 ++++++++++--
 src/ftp/DStoreFile.java           |  25 ++++++----
 8 files changed, 118 insertions(+), 30 deletions(-)
 delete mode 100644 Dstores/dstore/test.txt
 delete mode 100644 Dstores/dstore2/test.txt
 delete mode 100644 Dstores/dstore3/test.txt

diff --git a/ClientAndLoggers/ClientMain.class b/ClientAndLoggers/ClientMain.class
index 3c43107e0f47157f8c5d846fd50489c8ba1eb6a4..917b8b2fb573e063c4c48874232175fd7f090f8c 100644
GIT binary patch
delta 1274
zcmdlYwoa7m)W2Q(7#J9A7^ZLJYGdT(Vo+z$U}w<eVbEgGo_wCsNL-hZfz3H5Gc~V-
zlR=-sfStjRhrx)!c(MeOYrPJGDGviHgBc@(q=u)bk57JjdTNmhh|0`M52-9jwbl&d
zV$fl*;9;<2U}I!pPtMQFOHD3eXRu~u5Y9?0OVrQI*Z1^ytw>HSD9Oyv<6^L4u;pQ}
z11aV&D9X$$2`))Y&JHO`Oil$E<iNw=2$JJU$t+IJw}u+#3^ObzF)v*o$uK7dTMh;{
zMh1?O)Z!Aol8O?L86G?go(x`-#hI1)g*&(y{22V%83K410vUoPdorsqhD^?5R^|?4
z2xn)A;9-blh?+cy*_b<qA(ovXj)x(hAz|_@W-AdLh9n+_WCmeI2DYNq-2Ads4u;gp
zJ}e4CF%0P-Rl*>fGI<!X7_ukVu_$Zg@-Qef<S{acYIth;fISX&Yj8<XW?s6r5CaoK
z0Z2vR<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z`L}85|hw7~C0r83Gu>8KM{x8B!SR
z81fm48Oj*k7?>Dj85$TE7z`M)!E_r#7eg<D8$$y_6POPQ(|QI5I|hCR1_mhxCI&eM
zeg*{wSq4i6MFuMdeFg;v0|pfacLp_vC<c9oYz9k)d<H9q1_n!pHU=w(DGb&O(-~~Q
z0Ugi4|DQpOfdw4Y><k7BjO+|{{}}`sSYaaU40a$vH@0644F2p4c5EK(4B?!f;UK0L
zJA<1iJ45Y%1|9}>m<9$0c7|GrmLCjE44e%44E4|yG?#&yft`VY!Fv-!q)_HIhHR~E
z47om=7$UXz?P4foVA#fxJ^3}KRDGmO3lpOx+b)I@uviN#<MRLawKp&@Fo3+yz@P+<
zb9)931_uUS1}6q724@Bt1{Ve+23H1W1{VfT26wRUBp8_fGpI7Ku`?L3Gc+)QFe3xQ
z4+dp$a$;j(Vz6UqVqj)y0h`>)(8j>PAi}_`#n8^c$j}QW`@!TyhRF;J3_=V{48{yo
R7^XQ$GE8Tf$uOHi5&*n&7%>0<

diff --git a/ClientAndLoggers/ClientMain.java b/ClientAndLoggers/ClientMain.java
index 5b761bc..9e99b56 100644
--- a/ClientAndLoggers/ClientMain.java
+++ b/ClientAndLoggers/ClientMain.java
@@ -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)
diff --git a/Dstores/dstore/test.txt b/Dstores/dstore/test.txt
deleted file mode 100644
index abd91bd..0000000
--- a/Dstores/dstore/test.txt
+++ /dev/null
@@ -1 +0,0 @@
-a test file
\ No newline at end of file
diff --git a/Dstores/dstore2/test.txt b/Dstores/dstore2/test.txt
deleted file mode 100644
index abd91bd..0000000
--- a/Dstores/dstore2/test.txt
+++ /dev/null
@@ -1 +0,0 @@
-a test file
\ No newline at end of file
diff --git a/Dstores/dstore3/test.txt b/Dstores/dstore3/test.txt
deleted file mode 100644
index abd91bd..0000000
--- a/Dstores/dstore3/test.txt
+++ /dev/null
@@ -1 +0,0 @@
-a test file
\ No newline at end of file
diff --git a/src/ftp/Controller.java b/src/ftp/Controller.java
index ce797fc..30c4ecf 100644
--- a/src/ftp/Controller.java
+++ b/src/ftp/Controller.java
@@ -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
diff --git a/src/ftp/DStore.java b/src/ftp/DStore.java
index 8346c8d..d252896 100644
--- a/src/ftp/DStore.java
+++ b/src/ftp/DStore.java
@@ -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();
+    }
+
 }
diff --git a/src/ftp/DStoreFile.java b/src/ftp/DStoreFile.java
index ae22dcf..7539337 100644
--- a/src/ftp/DStoreFile.java
+++ b/src/ftp/DStoreFile.java
@@ -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; }
+
 }
-- 
GitLab