pull/1/head
Ivan Olexyn 5 years ago
parent 8fc9e9b514
commit 227d861ad0

@ -4,33 +4,35 @@ import com.olexyn.ensync.artifacts.SyncDirectory;
import com.olexyn.ensync.artifacts.SyncMap; import com.olexyn.ensync.artifacts.SyncMap;
import java.io.File; import java.io.File;
import static com.olexyn.ensync.Main.MAP_OF_SYNCMAPS;
public class Flow implements Runnable { public class Flow implements Runnable {
private String state;
public void run() {
private String state;
public void run() {
while (true) { 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()) { for (var syncDirectoryEntry : syncMapEntry.getValue().syncDirectories.entrySet()) {
SyncDirectory syncDirectory = syncDirectoryEntry.getValue(); SyncDirectory syncDirectory = syncDirectoryEntry.getValue();
String path = syncDirectory.path;
state = "READ"; state = "READ";
syncDirectory.readState(); syncDirectory.findState();
syncDirectory.makeListCreated(); syncDirectory.makeListCreated();
syncDirectory.makeListDeleted(); syncDirectory.makeListDeleted();
@ -40,12 +42,14 @@ public class Flow implements Runnable {
syncDirectory.doDelete(); syncDirectory.doDelete();
syncDirectory.doModify(); syncDirectory.doModify();
syncDirectory.writeStateFile(path); syncDirectory.writeStateFile(syncDirectory.path);
} }
} }
}
try { try {
System.out.println("Pausing..."); System.out.println("Pausing...");
Thread.sleep(2000); Thread.sleep(2000);
@ -66,8 +70,8 @@ public class Flow implements Runnable {
* For every single SyncDirectory try to read it's StateFile. <p> * For every single SyncDirectory try to read it's StateFile. <p>
* If the StateFile is missing, then create a StateFile. * If the StateFile is missing, then create a StateFile.
*/ */
private void initialize() { private void readOrMakeStateFile() {
for (var syncMapEntry : Main.SYNC.entrySet()) { for (var syncMapEntry : MAP_OF_SYNCMAPS.entrySet()) {
SyncMap syncMap = syncMapEntry.getValue(); SyncMap syncMap = syncMapEntry.getValue();
state = syncMap.toString(); state = syncMap.toString();

@ -17,7 +17,7 @@ public class Main{
final public static Thread FLOW_THREAD = new Thread(new Flow(), "flow"); final public static Thread FLOW_THREAD = new Thread(new Flow(), "flow");
final public static HashMap<String, SyncMap> SYNC = new HashMap<>(); final public static HashMap<String, SyncMap> MAP_OF_SYNCMAPS = new HashMap<>();

@ -6,6 +6,9 @@ import com.olexyn.ensync.Tools;
import java.io.File; import java.io.File;
import java.util.*; import java.util.*;
/**
* A SyncDirectory is an occurrence of a particular directory somewhere across the filesystems.
*/
public class SyncDirectory { public class SyncDirectory {
private String flowState; 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<String, SyncFile> readState() { public Map<String, SyncFile> findState() {
//NOTE that the SFile().lastModifiedOld is not set here, so it is 0 by default.
Map<String, SyncFile> filemap = new HashMap<>(); Map<String, SyncFile> filemap = new HashMap<>();
Execute.TwoBr find = x.execute(new String[]{"find", Execute.TwoBr find = x.execute(new String[]{"find",
@ -95,7 +99,7 @@ public class SyncDirectory {
*/ */
public void makeListCreated() { public void makeListCreated() {
listCreated = new ArrayList<>(); listCreated = new ArrayList<>();
Map<String, SyncFile> fromA = readState(); Map<String, SyncFile> fromA = findState();
Map<String, SyncFile> substractB = readStateFile(); Map<String, SyncFile> substractB = readStateFile();
listCreated = tools.mapMinus(fromA, substractB); listCreated = tools.mapMinus(fromA, substractB);
@ -115,7 +119,7 @@ public class SyncDirectory {
public void makeListDeleted() { public void makeListDeleted() {
listDeleted = new ArrayList<>(); listDeleted = new ArrayList<>();
Map<String, SyncFile> fromA = readStateFile(); Map<String, SyncFile> fromA = readStateFile();
Map<String, SyncFile> substractB = readState(); Map<String, SyncFile> substractB = findState();
listDeleted = tools.mapMinus(fromA, substractB); listDeleted = tools.mapMinus(fromA, substractB);
@ -132,7 +136,7 @@ public class SyncDirectory {
listModified = new ArrayList<>(); listModified = new ArrayList<>();
Map<String, SyncFile> oldMap = readStateFile(); Map<String, SyncFile> oldMap = readStateFile();
for (Map.Entry<String, SyncFile> newFileEntry : readState().entrySet()) { for (Map.Entry<String, SyncFile> newFileEntry : findState().entrySet()) {
// If KEY exists in OLD , thus FILE was NOT created. // If KEY exists in OLD , thus FILE was NOT created.
String newFileKey = newFileEntry.getKey(); String newFileKey = newFileEntry.getKey();

@ -9,7 +9,7 @@ import java.util.Map;
/** /**
* What is a SyncMap? * 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. * A SyncMap is the set of such SyncDirectories. The purpose of the SyncMap is to help syncronize the SyncDirectories.
*/ */
public class SyncMap { public class SyncMap {

@ -3,31 +3,36 @@ package com.olexyn.ensync.ui;
import com.olexyn.ensync.artifacts.SyncMap; 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 java.io.File;
import static com.olexyn.ensync.Main.MAP_OF_SYNCMAPS;
/** /**
* Connect the Controller and the Flow * Connect the Controller and the Flow
*/ */
public class Bridge { public class Bridge {
void newCollection(String collectionName) {
void newCollection(String collectionName){ synchronized (MAP_OF_SYNCMAPS) {
SYNC.put(collectionName, new SyncMap(collectionName)); MAP_OF_SYNCMAPS.put(collectionName, new SyncMap(collectionName));
}
} }
void removeCollection(String collectionName){ void removeCollection(String collectionName) {
SYNC.remove(collectionName); synchronized (MAP_OF_SYNCMAPS) {
MAP_OF_SYNCMAPS.remove(collectionName);
}
} }
void addDirectory(String collectionName, File diretory){ void addDirectory(String collectionName, File diretory) {
SYNC.get(collectionName).addDirectory(diretory.getAbsolutePath()); synchronized (MAP_OF_SYNCMAPS) {
MAP_OF_SYNCMAPS.get(collectionName).addDirectory(diretory.getAbsolutePath());
}
//TODO pause syning when adding //TODO pause syning when adding
} }
@ -36,21 +41,14 @@ public class Bridge {
* This works, because a directory, which here is an unique ablsolute path, * This works, because a directory, which here is an unique ablsolute path,
* is only supposed to present once across entire SYNC. * 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. //TODO fix ConcurrentModificationException. This will possibly resolve further errors.
while (true) { synchronized (MAP_OF_SYNCMAPS) {
if (FLOW_THREAD.getState().equals(Thread.State.TIMED_WAITING)) { for (var syncMap : MAP_OF_SYNCMAPS.entrySet()) {
try {
FLOW_THREAD.wait();
} catch (InterruptedException e) {
for (var syncMap : SYNC.entrySet()){
syncMap.getValue().removeDirectory(directoryAbsolutePath); syncMap.getValue().removeDirectory(directoryAbsolutePath);
} }
FLOW_THREAD.notify();
break;
}
}
} }
} }
} }

@ -13,8 +13,6 @@ public class UI extends Application implements Runnable {
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("layout.fxml")); Parent root = FXMLLoader.load(getClass().getResource("layout.fxml"));

Loading…
Cancel
Save