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 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();
String path = syncDirectory.path;
state = "READ";
syncDirectory.readState();
syncDirectory.findState();
syncDirectory.makeListCreated();
syncDirectory.makeListDeleted();
@ -40,12 +42,14 @@ public class Flow implements Runnable {
syncDirectory.doDelete();
syncDirectory.doModify();
syncDirectory.writeStateFile(path);
syncDirectory.writeStateFile(syncDirectory.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. <p>
* 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();

@ -17,7 +17,7 @@ public class Main{
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.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<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<>();
Execute.TwoBr find = x.execute(new String[]{"find",
@ -95,7 +99,7 @@ public class SyncDirectory {
*/
public void makeListCreated() {
listCreated = new ArrayList<>();
Map<String, SyncFile> fromA = readState();
Map<String, SyncFile> fromA = findState();
Map<String, SyncFile> substractB = readStateFile();
listCreated = tools.mapMinus(fromA, substractB);
@ -115,7 +119,7 @@ public class SyncDirectory {
public void makeListDeleted() {
listDeleted = new ArrayList<>();
Map<String, SyncFile> fromA = readStateFile();
Map<String, SyncFile> substractB = readState();
Map<String, SyncFile> substractB = findState();
listDeleted = tools.mapMinus(fromA, substractB);
@ -132,7 +136,7 @@ public class SyncDirectory {
listModified = new ArrayList<>();
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.
String newFileKey = newFileEntry.getKey();

@ -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 {

@ -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()){
synchronized (MAP_OF_SYNCMAPS) {
for (var syncMap : MAP_OF_SYNCMAPS.entrySet()) {
syncMap.getValue().removeDirectory(directoryAbsolutePath);
}
FLOW_THREAD.notify();
break;
}
}
}
}
}

@ -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"));

Loading…
Cancel
Save