Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Merge pull request #74 from GooBox/register-login
Browse files Browse the repository at this point in the history
Implement GUI protocol for register account and login
  • Loading branch information
kaloyan-raev authored Jan 9, 2018
2 parents a6cdb0d + 9139194 commit d35e15f
Show file tree
Hide file tree
Showing 21 changed files with 520 additions and 88 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.goobox</groupId>
<artifactId>goobox-sync-storj</artifactId>
<version>0.0.12</version>
<version>0.0.13</version>
<packaging>jar</packaging>

<name>Goobox sync app for Storj</name>
Expand Down Expand Up @@ -99,6 +99,11 @@
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
51 changes: 29 additions & 22 deletions src/main/java/io/goobox/sync/storj/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.goobox.sync.common.systemtray.ShutdownListener;
import io.goobox.sync.common.systemtray.SystemTrayHelper;
import io.goobox.sync.storj.db.DB;
import io.goobox.sync.storj.ipc.StdinReader;
import io.goobox.sync.storj.overlay.OverlayHelper;
import io.storj.libstorj.Bucket;
import io.storj.libstorj.CreateBucketCallback;
Expand All @@ -61,6 +62,7 @@ public class App implements ShutdownListener {
private TaskQueue tasks;
private TaskExecutor taskExecutor;
private FileWatcher fileWatcher;
private StdinReader stdinReader;

public App() {
this.syncDir = Utils.getSyncDir();
Expand Down Expand Up @@ -95,7 +97,7 @@ public static void main(String[] args) {
instance = new App();
}
} catch (ParseException e) {
logger.error("Failed to parse command line options: {}", e.getMessage());
logger.error("Failed to parse command line options", e);
System.exit(1);
}

Expand Down Expand Up @@ -178,9 +180,12 @@ private void init() {
SystemTrayHelper.setShutdownListener(this);

storj = new Storj();
storj.setConfigDirectory(StorjUtil.getStorjConfigDir().toFile());
storj.setConfigDirectory(Utils.getDataDir().toFile());
storj.setDownloadDirectory(syncDir.toFile());

stdinReader = new StdinReader();
stdinReader.start();

if (!checkAndCreateSyncDir()) {
System.exit(1);
}
Expand Down Expand Up @@ -212,63 +217,63 @@ public void shutdown() {
}

private boolean checkAndCreateSyncDir() {
System.out.print("Checking if local Goobox sync folder exists... ");
logger.info("Checking if local Goobox sync folder exists");
return checkAndCreateFolder(getSyncDir());
}

private boolean checkAndCreateDataDir() {
System.out.print("Checking if Goobox data folder exists... ");
logger.info("Checking if Goobox data folder exists");
return checkAndCreateFolder(Utils.getDataDir());
}

private boolean checkAndCreateFolder(Path path) {
if (Files.exists(path)) {
System.out.println("yes");
logger.info("Folder exists");
return true;
} else {
System.out.print("no. ");
logger.info("Folder does not exist");
try {
Files.createDirectory(path);
System.out.println("Folder created.");
logger.info("Folder created");
return true;
} catch (IOException e) {
System.out.println("Failed creating folder: " + e.getMessage());
logger.error("Failed creating folder", e);
return false;
}
}
}

private Bucket checkAndCreateCloudBucket() {
System.out.print("Checking if cloud Goobox bucket exists... ");
logger.info("Checking if cloud Goobox bucket exists");
final Bucket[] result = { null };

try {
while (result[0] == null) {
final CountDownLatch latch = new CountDownLatch(1);
while (result[0] == null) {
final CountDownLatch latch = new CountDownLatch(1);

try {
storj.getBuckets(new GetBucketsCallback() {
@Override
public void onBucketsReceived(Bucket[] buckets) {
for (Bucket bucket : buckets) {
if ("Goobox".equals(bucket.getName())) {
result[0] = bucket;
System.out.println("yes");
logger.info("Goobox bucket exists");
latch.countDown();
return;
}
}

System.out.print("no. ");
logger.info("Goobox bucket does not exist");
storj.createBucket("Goobox", new CreateBucketCallback() {
@Override
public void onError(String message) {
System.out.println("Failed creating cloud Goobox bucket.");
logger.error("Failed creating cloud Goobox bucket: {}", message);
latch.countDown();
}

@Override
public void onBucketCreated(Bucket bucket) {
System.out.println("Cloud Goobox bucket created.");
logger.info("Cloud Goobox bucket created");
result[0] = bucket;
latch.countDown();
}
Expand All @@ -277,23 +282,25 @@ public void onBucketCreated(Bucket bucket) {

@Override
public void onError(String message) {
System.out.println(message);
logger.error(message);
latch.countDown();
}
});
} catch (KeysNotFoundException e) {
logger.error("No keys found. Waiting for keys to be imported.");
latch.countDown();
}

try {
latch.await();

if (result[0] == null) {
// error - wait 3 seconds before trying again
Thread.sleep(3000);
}
} catch (InterruptedException e) {
break;
}
} catch (KeysNotFoundException e) {
System.out.println(
"No keys found. Have your imported your keys using libstorj? Make sure you don't specify a passcode.");
} catch (InterruptedException e) {
// do nothing
}

return result[0];
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/io/goobox/sync/storj/CheckStateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.Deque;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.goobox.sync.common.Utils;
import io.goobox.sync.common.systemtray.SystemTrayHelper;
import io.goobox.sync.storj.db.DB;
Expand All @@ -38,6 +41,8 @@

public class CheckStateTask implements Runnable {

private static final Logger logger = LoggerFactory.getLogger(CheckStateTask.class);

private Bucket gooboxBucket;
private TaskQueue tasks;

Expand All @@ -50,11 +55,11 @@ public CheckStateTask() {
public void run() {
// check if there are local file operations in progress
if (App.getInstance().getFileWatcher().isInProgress()) {
System.out.println("Skip checking for changes - local file operations in progress...");
logger.info("Skip checking for changes - local file operations in progress");
return;
}

System.out.println("Checking for changes...");
logger.info("Checking for changes");
SystemTrayHelper.setSynchronizing();
OverlayHelper.getInstance().setSynchronizing();

Expand All @@ -77,7 +82,7 @@ public void onFilesReceived(File[] files) {

@Override
public void onError(String message) {
System.out.println(" " + message);
logger.error(message);
// Try again
tasks.add(CheckStateTask.this);
}
Expand Down Expand Up @@ -135,7 +140,7 @@ private void processFiles(File[] files) {
}
}
} catch (ParseException e) {
e.printStackTrace();
logger.error("Cannot parse timestamp", e);
}
}

Expand All @@ -144,7 +149,7 @@ private void processFiles(File[] files) {
localPaths.remove(localPath);
}
} catch (IOException e) {
e.printStackTrace();
logger.error("I/O error", e);
}
}

Expand All @@ -169,7 +174,7 @@ private void processFiles(File[] files) {
}
}
} catch (IOException e) {
e.printStackTrace();
logger.error("I/O error", e);
}
}
}
Expand Down Expand Up @@ -198,7 +203,7 @@ private List<Path> getLocalPaths() {
paths.add(path);
}
} catch (IOException e) {
e.printStackTrace();
logger.error("I/O error", e);
}
}

Expand Down
23 changes: 14 additions & 9 deletions src/main/java/io/goobox/sync/storj/CreateCloudDirTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.nio.file.Path;
import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.goobox.sync.storj.db.DB;
import io.storj.libstorj.Bucket;
import io.storj.libstorj.File;
Expand All @@ -29,6 +32,8 @@

public class CreateCloudDirTask implements Runnable {

private static final Logger logger = LoggerFactory.getLogger(CreateCloudDirTask.class);

private Bucket bucket;
private Path path;

Expand All @@ -48,14 +53,14 @@ public void run() {
} else {
final Path tmp = createTempDirFile();

System.out.println("Creating cloud directory " + dirName + "... ");
logger.info("Creating cloud directory {}", dirName);

App.getInstance().getStorj().uploadFile(bucket, dirName, tmp.toString(), new UploadFileCallback() {
@Override
public void onProgress(String filePath, double progress, long uploadedBytes, long totalBytes) {
String progressMessage = String.format(" %3d%% %15d/%d bytes",
(int) (progress * 100), uploadedBytes, totalBytes);
System.out.println(progressMessage);
logger.info(progressMessage);
}

@Override
Expand All @@ -66,7 +71,7 @@ public void onComplete(final String filePath, final File file) {
DB.setSynced(file, path);
DB.commit();
} catch (IOException e) {
e.printStackTrace();
logger.error("I/O error", e);
}
}

Expand All @@ -78,15 +83,15 @@ public void onError(String filePath, String message) {
DB.setUploadFailed(path);
DB.commit();

System.out.println(" " + message);
logger.error(message);
} catch (IOException e) {
e.printStackTrace();
logger.error("I/O error", e);
}
}
});
}
} catch (IOException e) {
System.out.println("Failed creating temp file: " + e.getMessage());
logger.error("Failed creating temp file", e);
} catch (InterruptedException e) {
// interrupted - stop execution
return;
Expand Down Expand Up @@ -114,8 +119,8 @@ public void onFilesReceived(File[] files) {

@Override
public void onError(String message) {
System.out.printf("Error checking if directory with name %s exists: %s. Trying again...\n",
dirName, message);
logger.error("Error checking if directory with name {} exists: {}. Trying again.", dirName,
message);
latch.countDown();
}
});
Expand All @@ -136,7 +141,7 @@ private void deleteTempDirFile(Path tmp) {
try {
Files.deleteIfExists(tmp);
} catch (IOException e) {
System.out.println("Failed deleting temp file: " + e.getMessage());
logger.error("Failed deleting temp file", e);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/io/goobox/sync/storj/CreateLocalDirTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
import java.nio.file.Files;
import java.nio.file.Path;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.goobox.sync.storj.db.DB;
import io.storj.libstorj.File;

public class CreateLocalDirTask implements Runnable {

private static final Logger logger = LoggerFactory.getLogger(CreateLocalDirTask.class);

private File storjDir;

public CreateLocalDirTask(File storjDir) {
Expand All @@ -32,15 +37,15 @@ public CreateLocalDirTask(File storjDir) {

@Override
public void run() {
System.out.print("Creating local directory " + storjDir.getName() + "... ");
logger.info("Creating local directory {}", storjDir.getName());

try {
Path localDir = Files.createDirectories(App.getInstance().getSyncDir().resolve(storjDir.getName()));
System.out.println("done");
logger.info("Locla directory created");
DB.setSynced(storjDir, localDir);
DB.commit();
} catch (Exception e) {
System.out.println(e.getMessage());
logger.error("Failed creating local directory", e);
}
}

Expand Down
Loading

0 comments on commit d35e15f

Please sign in to comment.