Search in sources :

Example 1 with ConnectOperationResult

use of org.syncany.operations.init.ConnectOperationResult in project syncany by syncany.

the class ConnectManagementRequestHandler method handleRequest.

@Override
public Response handleRequest(final ManagementRequest request) {
    final ConnectManagementRequest concreteRequest = (ConnectManagementRequest) request;
    logger.log(Level.SEVERE, "Executing ConnectOperation for folder " + concreteRequest.getOptions().getLocalDir() + " ...");
    Thread connectThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                ConnectOperation initOperation = new ConnectOperation(concreteRequest.getOptions(), new EventUserInteractionListener());
                ConnectOperationResult operationResult = initOperation.execute();
                switch(operationResult.getResultCode()) {
                    case OK:
                        eventBus.post(new ConnectManagementResponse(ConnectManagementResponse.OK, operationResult, request.getId()));
                        break;
                    case NOK_TEST_FAILED:
                        eventBus.post(new ConnectManagementResponse(ConnectManagementResponse.NOK_FAILED_TEST, operationResult, request.getId()));
                        break;
                    default:
                        eventBus.post(new ConnectManagementResponse(ConnectManagementResponse.NOK_FAILED_UNKNOWN, operationResult, request.getId()));
                        break;
                }
            } catch (Exception e) {
                logger.log(Level.WARNING, "Error adding watch to daemon config.", e);
                eventBus.post(new ConnectManagementResponse(ConnectManagementResponse.NOK_OPERATION_FAILED, new ConnectOperationResult(), request.getId()));
            }
        }
    }, "ConRq/" + concreteRequest.getOptions().getLocalDir().getName());
    connectThread.start();
    return null;
}
Also used : ConnectOperationResult(org.syncany.operations.init.ConnectOperationResult) EventUserInteractionListener(org.syncany.operations.daemon.EventUserInteractionListener) ConnectOperation(org.syncany.operations.init.ConnectOperation)

Example 2 with ConnectOperationResult

use of org.syncany.operations.init.ConnectOperationResult in project syncany by syncany.

the class ConnectOperationTest method testConnectOperationFailureNoConnection.

@Test
public void testConnectOperationFailureNoConnection() throws Exception {
    // A.init()
    InitOperationOptions initOperationOptionsA = TestConfigUtil.createTestInitOperationOptions("A");
    InitOperation initOperationA = new InitOperation(initOperationOptionsA, null);
    InitOperationResult initOperationResultA = initOperationA.execute();
    String connectLinkA = initOperationResultA.getGenLinkResult().getShareLink();
    assertNotNull(connectLinkA);
    // B.connect()
    File localDirB = TestFileUtil.createTempDirectoryInSystemTemp(TestConfigUtil.createUniqueName("client-B", initOperationOptionsA));
    File localConnectDirB = new File(localDirB, Config.DIR_APPLICATION);
    File repoDir = ((LocalTransferSettings) initOperationOptionsA.getConfigTO().getTransferSettings()).getPath();
    ConfigTO connectionConfigToB = initOperationOptionsA.getConfigTO();
    // <<< Point to non-existing repo
    ((LocalTransferSettings) connectionConfigToB.getTransferSettings()).setPath(new File("/does/not/exist"));
    connectionConfigToB.setMachineName("client-B" + Math.abs(new Random().nextInt()));
    connectionConfigToB.setMasterKey(null);
    ConnectOperationOptions connectOperationOptionsB = new ConnectOperationOptions();
    connectOperationOptionsB.setStrategy(ConnectOptionsStrategy.CONNECTION_TO);
    connectOperationOptionsB.setConfigTO(connectionConfigToB);
    connectOperationOptionsB.setPassword(initOperationOptionsA.getPassword());
    connectOperationOptionsB.setLocalDir(localDirB);
    ConnectOperation connectOperationB = new ConnectOperation(connectOperationOptionsB, null);
    ConnectOperationResult connectOperationResultB = connectOperationB.execute();
    assertEquals(ConnectResultCode.NOK_TEST_FAILED, connectOperationResultB.getResultCode());
    assertFalse(new File(localConnectDirB, Config.DIR_DATABASE).exists());
    assertFalse(new File(localConnectDirB, Config.DIR_CACHE).exists());
    assertFalse(new File(localConnectDirB, Config.FILE_CONFIG).exists());
    assertFalse(new File(localConnectDirB, Config.DIR_LOG).exists());
    assertFalse(new File(localConnectDirB, Config.FILE_REPO).exists());
    // Tear down
    TestFileUtil.deleteDirectory(repoDir);
    TestFileUtil.deleteDirectory(localConnectDirB);
    TestFileUtil.deleteDirectory(initOperationOptionsA.getLocalDir());
    TestFileUtil.deleteDirectory(connectOperationOptionsB.getLocalDir());
}
Also used : ConnectOperationResult(org.syncany.operations.init.ConnectOperationResult) InitOperationResult(org.syncany.operations.init.InitOperationResult) InitOperation(org.syncany.operations.init.InitOperation) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) Random(java.util.Random) InitOperationOptions(org.syncany.operations.init.InitOperationOptions) ConnectOperation(org.syncany.operations.init.ConnectOperation) ConfigTO(org.syncany.config.to.ConfigTO) File(java.io.File) ConnectOperationOptions(org.syncany.operations.init.ConnectOperationOptions) Test(org.junit.Test)

Example 3 with ConnectOperationResult

use of org.syncany.operations.init.ConnectOperationResult in project syncany by syncany.

the class ConnectCommand method execute.

@Override
public int execute(String[] operationArgs) throws Exception {
    boolean retryNeeded = true;
    boolean performOperation = true;
    ConnectOperationOptions operationOptions = parseOptions(operationArgs);
    while (retryNeeded && performOperation) {
        ConnectOperationResult operationResult = new ConnectOperation(operationOptions, this).execute();
        printResults(operationResult);
        retryNeeded = operationResult.getResultCode() != ConnectResultCode.OK && operationResult.getResultCode() != ConnectResultCode.NOK_DECRYPT_ERROR;
        if (retryNeeded) {
            performOperation = isInteractive && askRetryConnection();
            if (performOperation) {
                updateTransferSettings(operationOptions.getConfigTO().getTransferSettings());
            }
        }
    }
    return 0;
}
Also used : ConnectOperationResult(org.syncany.operations.init.ConnectOperationResult) ConnectOperation(org.syncany.operations.init.ConnectOperation) ConnectOperationOptions(org.syncany.operations.init.ConnectOperationOptions)

Example 4 with ConnectOperationResult

use of org.syncany.operations.init.ConnectOperationResult in project syncany by syncany.

the class ConnectCommand method printResults.

@Override
public void printResults(OperationResult operationResult) {
    ConnectOperationResult concreteOperationResult = (ConnectOperationResult) operationResult;
    if (concreteOperationResult.getResultCode() == ConnectResultCode.OK) {
        out.println();
        out.println("Repository connected, and local folder initialized.");
        out.println("You can now use 'sy up' and 'sy down' to sync your files.");
        out.println();
        if (concreteOperationResult.isAddedToDaemon()) {
            out.println("To automatically sync this folder, simply restart the daemon with 'sy daemon restart'.");
            out.println();
        }
    } else if (concreteOperationResult.getResultCode() == ConnectResultCode.NOK_TEST_FAILED) {
        StorageTestResult testResult = concreteOperationResult.getTestResult();
        out.println();
        if (!testResult.isTargetCanConnect()) {
            out.println("ERROR: Cannot connect to the repository, because the connection to the storage backend failed.");
            out.println("       Possible reasons for this could be connectivity issues (are you connect to the Internet?),");
            out.println("       or invalid user credentials (are username/password valid?).");
        } else if (!testResult.isTargetExists()) {
            out.println("ERROR: Cannot connect to the repository, because the target does not exist.");
            out.println("       Please check if it really exists and if you can read from it / write to it.");
        } else if (!testResult.isTargetCanWrite()) {
            out.println("ERROR: Cannot connect to the repository, because the target is not writable. This is probably");
            out.println("       a permission issue (does the user have write permissions to the target?).");
        } else if (!testResult.isRepoFileExists()) {
            out.println("ERROR: Cannot connect to the repository, because no repo file was found ('syncany' file).");
            out.println("       Are you sure that this is a valid Syncany repository? Use 'sy init' to create a new one.");
        } else {
            out.println("ERROR: Cannot connect to the repository.");
        }
        out.println();
        printTestResult(testResult);
    } else if (concreteOperationResult.getResultCode() == ConnectResultCode.NOK_DECRYPT_ERROR) {
        out.println();
        out.println("ERROR: Invalid password or corrupt ciphertext.");
        out.println();
        out.println("The reason for this might be an invalid password, or that the");
        out.println("link/files have been tampered with.");
        out.println();
    } else {
        out.println();
        out.println("ERROR: Cannot connect to repository. Unknown error code: " + operationResult);
        out.println();
    }
}
Also used : ConnectOperationResult(org.syncany.operations.init.ConnectOperationResult) StorageTestResult(org.syncany.plugins.transfer.StorageTestResult)

Example 5 with ConnectOperationResult

use of org.syncany.operations.init.ConnectOperationResult in project syncany by syncany.

the class ConnectOperationTest method testConnectOperationSuccess.

@Test
public void testConnectOperationSuccess() throws Exception {
    // A.init()
    InitOperationOptions initOperationOptionsA = TestConfigUtil.createTestInitOperationOptions("A");
    InitOperation initOperationA = new InitOperation(initOperationOptionsA, null);
    InitOperationResult initOperationResultA = initOperationA.execute();
    String connectLinkA = initOperationResultA.getGenLinkResult().getShareLink();
    assertNotNull(connectLinkA);
    // B.connect()
    File localDirB = TestFileUtil.createTempDirectoryInSystemTemp(TestConfigUtil.createUniqueName("clientB", initOperationOptionsA));
    File localConnectDirB = new File(localDirB, Config.DIR_APPLICATION);
    ConfigTO connectionConfigToB = initOperationOptionsA.getConfigTO();
    connectionConfigToB.setMachineName("clientB" + Math.abs(new Random().nextInt()));
    connectionConfigToB.setMasterKey(null);
    ConnectOperationOptions connectOperationOptionsB = new ConnectOperationOptions();
    connectOperationOptionsB.setStrategy(ConnectOptionsStrategy.CONNECTION_TO);
    connectOperationOptionsB.setConfigTO(connectionConfigToB);
    connectOperationOptionsB.setPassword(initOperationOptionsA.getPassword());
    connectOperationOptionsB.setLocalDir(localDirB);
    ConnectOperation connectOperationB = new ConnectOperation(connectOperationOptionsB, null);
    ConnectOperationResult connectOperationResultB = connectOperationB.execute();
    assertEquals(ConnectResultCode.OK, connectOperationResultB.getResultCode());
    assertTrue(new File(localConnectDirB, Config.DIR_DATABASE).exists());
    assertTrue(new File(localConnectDirB, Config.DIR_CACHE).exists());
    assertTrue(new File(localConnectDirB, Config.FILE_CONFIG).exists());
    assertTrue(new File(localConnectDirB, Config.DIR_LOG).exists());
    assertTrue(new File(localConnectDirB, Config.FILE_REPO).exists());
    assertEquals(new File(localConnectDirB, Config.FILE_MASTER).exists(), TestConfigUtil.getCrypto());
    File repoDir = ((LocalTransferSettings) initOperationOptionsA.getConfigTO().getTransferSettings()).getPath();
    // Tear down
    TestFileUtil.deleteDirectory(repoDir);
    TestFileUtil.deleteDirectory(localConnectDirB);
    TestFileUtil.deleteDirectory(initOperationOptionsA.getLocalDir());
    TestFileUtil.deleteDirectory(connectOperationOptionsB.getLocalDir());
}
Also used : ConnectOperationResult(org.syncany.operations.init.ConnectOperationResult) InitOperationResult(org.syncany.operations.init.InitOperationResult) InitOperation(org.syncany.operations.init.InitOperation) Random(java.util.Random) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) InitOperationOptions(org.syncany.operations.init.InitOperationOptions) ConnectOperation(org.syncany.operations.init.ConnectOperation) ConfigTO(org.syncany.config.to.ConfigTO) File(java.io.File) ConnectOperationOptions(org.syncany.operations.init.ConnectOperationOptions) Test(org.junit.Test)

Aggregations

ConnectOperationResult (org.syncany.operations.init.ConnectOperationResult)6 ConnectOperation (org.syncany.operations.init.ConnectOperation)5 ConnectOperationOptions (org.syncany.operations.init.ConnectOperationOptions)4 File (java.io.File)3 Random (java.util.Random)3 Test (org.junit.Test)3 ConfigTO (org.syncany.config.to.ConfigTO)3 InitOperation (org.syncany.operations.init.InitOperation)3 InitOperationOptions (org.syncany.operations.init.InitOperationOptions)3 InitOperationResult (org.syncany.operations.init.InitOperationResult)3 LocalTransferSettings (org.syncany.plugins.local.LocalTransferSettings)3 EventUserInteractionListener (org.syncany.operations.daemon.EventUserInteractionListener)1 StorageTestResult (org.syncany.plugins.transfer.StorageTestResult)1