From 227d861ad0ad38e32caeef28860f0e2381fa75fe Mon Sep 17 00:00:00 2001 From: Ivan Olexyn Date: Sun, 17 May 2020 04:13:57 +0200 Subject: [PATCH] ~ reopen --- src/com/olexyn/ensync/Flow.java | 42 ++++++++++--------- src/com/olexyn/ensync/Main.java | 2 +- .../ensync/artifacts/SyncDirectory.java | 14 ++++--- src/com/olexyn/ensync/artifacts/SyncMap.java | 2 +- src/com/olexyn/ensync/ui/Bridge.java | 40 +++++++++--------- src/com/olexyn/ensync/ui/UI.java | 2 - 6 files changed, 53 insertions(+), 49 deletions(-) diff --git a/src/com/olexyn/ensync/Flow.java b/src/com/olexyn/ensync/Flow.java index 277c9a7..6f57b20 100644 --- a/src/com/olexyn/ensync/Flow.java +++ b/src/com/olexyn/ensync/Flow.java @@ -4,48 +4,52 @@ import com.olexyn.ensync.artifacts.SyncDirectory; import com.olexyn.ensync.artifacts.SyncMap; import java.io.File; +import static com.olexyn.ensync.Main.MAP_OF_SYNCMAPS; public class Flow implements Runnable { - private String state; - public void run() { + private String state; + public void run() { + while (true) { - initialize(); - for (var syncMapEntry : Main.SYNC.entrySet()) { + synchronized (MAP_OF_SYNCMAPS) { + readOrMakeStateFile(); + for (var syncMapEntry : MAP_OF_SYNCMAPS.entrySet()) { - for (var syncDirectoryEntry : syncMapEntry.getValue().syncDirectories.entrySet()) { - SyncDirectory syncDirectory = syncDirectoryEntry.getValue(); + for (var syncDirectoryEntry : syncMapEntry.getValue().syncDirectories.entrySet()) { - String path = syncDirectory.path; + SyncDirectory syncDirectory = syncDirectoryEntry.getValue(); - state = "READ"; - syncDirectory.readState(); + state = "READ"; + syncDirectory.findState(); - syncDirectory.makeListCreated(); - syncDirectory.makeListDeleted(); - syncDirectory.makeListModified(); + syncDirectory.makeListCreated(); + syncDirectory.makeListDeleted(); + syncDirectory.makeListModified(); - syncDirectory.doCreate(); - syncDirectory.doDelete(); - syncDirectory.doModify(); + syncDirectory.doCreate(); + syncDirectory.doDelete(); + syncDirectory.doModify(); + + syncDirectory.writeStateFile(syncDirectory.path); + } - syncDirectory.writeStateFile(path); } + } - } try { System.out.println("Pausing..."); Thread.sleep(2000); @@ -66,8 +70,8 @@ public class Flow implements Runnable { * For every single SyncDirectory try to read it's StateFile.

* If the StateFile is missing, then create a StateFile. */ - private void initialize() { - for (var syncMapEntry : Main.SYNC.entrySet()) { + private void readOrMakeStateFile() { + for (var syncMapEntry : MAP_OF_SYNCMAPS.entrySet()) { SyncMap syncMap = syncMapEntry.getValue(); state = syncMap.toString(); diff --git a/src/com/olexyn/ensync/Main.java b/src/com/olexyn/ensync/Main.java index 0080952..00ea739 100644 --- a/src/com/olexyn/ensync/Main.java +++ b/src/com/olexyn/ensync/Main.java @@ -17,7 +17,7 @@ public class Main{ final public static Thread FLOW_THREAD = new Thread(new Flow(), "flow"); - final public static HashMap SYNC = new HashMap<>(); + final public static HashMap MAP_OF_SYNCMAPS = new HashMap<>(); diff --git a/src/com/olexyn/ensync/artifacts/SyncDirectory.java b/src/com/olexyn/ensync/artifacts/SyncDirectory.java index a22818b..8fa92fd 100644 --- a/src/com/olexyn/ensync/artifacts/SyncDirectory.java +++ b/src/com/olexyn/ensync/artifacts/SyncDirectory.java @@ -6,6 +6,9 @@ import com.olexyn.ensync.Tools; import java.io.File; import java.util.*; +/** + * A SyncDirectory is an occurrence of a particular directory somewhere across the filesystems. + */ public class SyncDirectory { private String flowState; @@ -39,9 +42,10 @@ public class SyncDirectory { /** - * NOTE that the SFile().lastModifiedOld is not set here, so it is 0 by default. + * Get the current state by using the `find` command. */ - public Map readState() { + public Map findState() { + //NOTE that the SFile().lastModifiedOld is not set here, so it is 0 by default. Map filemap = new HashMap<>(); Execute.TwoBr find = x.execute(new String[]{"find", @@ -95,7 +99,7 @@ public class SyncDirectory { */ public void makeListCreated() { listCreated = new ArrayList<>(); - Map fromA = readState(); + Map fromA = findState(); Map substractB = readStateFile(); listCreated = tools.mapMinus(fromA, substractB); @@ -115,7 +119,7 @@ public class SyncDirectory { public void makeListDeleted() { listDeleted = new ArrayList<>(); Map fromA = readStateFile(); - Map substractB = readState(); + Map substractB = findState(); listDeleted = tools.mapMinus(fromA, substractB); @@ -132,7 +136,7 @@ public class SyncDirectory { listModified = new ArrayList<>(); Map oldMap = readStateFile(); - for (Map.Entry newFileEntry : readState().entrySet()) { + for (Map.Entry newFileEntry : findState().entrySet()) { // If KEY exists in OLD , thus FILE was NOT created. String newFileKey = newFileEntry.getKey(); diff --git a/src/com/olexyn/ensync/artifacts/SyncMap.java b/src/com/olexyn/ensync/artifacts/SyncMap.java index e016516..0b63148 100644 --- a/src/com/olexyn/ensync/artifacts/SyncMap.java +++ b/src/com/olexyn/ensync/artifacts/SyncMap.java @@ -9,7 +9,7 @@ import java.util.Map; /** * What is a SyncMap? - * A SyncDirectory is an occurrence of a particular directory somewhere across the filesystems. + * * A SyncMap is the set of such SyncDirectories. The purpose of the SyncMap is to help syncronize the SyncDirectories. */ public class SyncMap { diff --git a/src/com/olexyn/ensync/ui/Bridge.java b/src/com/olexyn/ensync/ui/Bridge.java index 806216b..9bfc0f9 100644 --- a/src/com/olexyn/ensync/ui/Bridge.java +++ b/src/com/olexyn/ensync/ui/Bridge.java @@ -3,31 +3,36 @@ package com.olexyn.ensync.ui; import com.olexyn.ensync.artifacts.SyncMap; -import static com.olexyn.ensync.Main.SYNC; -import static com.olexyn.ensync.Main.FLOW_THREAD; -import static com.olexyn.ensync.Main.UI_THREAD; import java.io.File; + import static com.olexyn.ensync.Main.MAP_OF_SYNCMAPS; + /** * Connect the Controller and the Flow */ public class Bridge { + void newCollection(String collectionName) { - void newCollection(String collectionName){ - SYNC.put(collectionName, new SyncMap(collectionName)); + synchronized (MAP_OF_SYNCMAPS) { + MAP_OF_SYNCMAPS.put(collectionName, new SyncMap(collectionName)); + } } - void removeCollection(String collectionName){ - SYNC.remove(collectionName); + void removeCollection(String collectionName) { + synchronized (MAP_OF_SYNCMAPS) { + MAP_OF_SYNCMAPS.remove(collectionName); + } } - void addDirectory(String collectionName, File diretory){ - SYNC.get(collectionName).addDirectory(diretory.getAbsolutePath()); + void addDirectory(String collectionName, File diretory) { + synchronized (MAP_OF_SYNCMAPS) { + MAP_OF_SYNCMAPS.get(collectionName).addDirectory(diretory.getAbsolutePath()); + } //TODO pause syning when adding } @@ -36,21 +41,14 @@ public class Bridge { * This works, because a directory, which here is an unique ablsolute path, * is only supposed to present once across entire SYNC. */ - void removeDirectory(String directoryAbsolutePath){ + void removeDirectory(String directoryAbsolutePath) { //TODO fix ConcurrentModificationException. This will possibly resolve further errors. - while (true) { - if (FLOW_THREAD.getState().equals(Thread.State.TIMED_WAITING)) { - try { - FLOW_THREAD.wait(); - } catch (InterruptedException e) { - for (var syncMap : SYNC.entrySet()){ - syncMap.getValue().removeDirectory(directoryAbsolutePath); - } - FLOW_THREAD.notify(); - break; - } + synchronized (MAP_OF_SYNCMAPS) { + for (var syncMap : MAP_OF_SYNCMAPS.entrySet()) { + syncMap.getValue().removeDirectory(directoryAbsolutePath); } } + } } diff --git a/src/com/olexyn/ensync/ui/UI.java b/src/com/olexyn/ensync/ui/UI.java index 6e1c1a0..9df73d6 100644 --- a/src/com/olexyn/ensync/ui/UI.java +++ b/src/com/olexyn/ensync/ui/UI.java @@ -13,8 +13,6 @@ public class UI extends Application implements Runnable { - - @Override public void start(Stage primaryStage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("layout.fxml"));