Search in sources :

Example 1 with DaemonConfigTO

use of org.syncany.config.to.DaemonConfigTO in project syncany by syncany.

the class NoCliRequestsWhileSyncingTest method testNoCliRequestWhileSyncing.

@Test
public void testNoCliRequestWhileSyncing() throws Exception {
    final TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    final TestClient clientA = new TestClient("ClientA", testConnection);
    int port = 58444;
    // Load config template
    DaemonConfigTO daemonConfig = TestDaemonUtil.loadDaemonConfig("daemonOneFolderNoWebServer.xml");
    // Set port to prevent conflicts with default daemons
    daemonConfig.getWebServer().setBindPort(port);
    // Dynamically insert paths
    daemonConfig.getFolders().get(0).setPath(clientA.getConfig().getLocalDir().getAbsolutePath());
    // Create access token (not needed in this test, but prevents errors in daemon)
    daemonConfig.setPortTO(TestDaemonUtil.createPortTO(port));
    // Register to event bus
    LocalEventBus localEventBus = LocalEventBus.getInstance();
    localEventBus.register(this);
    // Prepare CLI request
    StatusFolderRequest cliStatusRequest = new StatusFolderRequest();
    cliStatusRequest.setId(2586);
    cliStatusRequest.setRoot(clientA.getConfig().getLocalDir().getAbsolutePath());
    // Create watchServer
    WatchServer watchServer = new WatchServer();
    watchServer.start(daemonConfig);
    // Settlement for watch server
    Thread.sleep(1000);
    // Create large file, then wait 3sec for the settlement timer and
    // send the CLI request at the same time
    clientA.createNewFile("largefile", 10 * 1024 * 1024);
    Response response = null;
    for (int i = 1; i < 100; i++) {
        cliStatusRequest.setId(i);
        localEventBus.post(cliStatusRequest);
        // Then, let's hope the response is "no, no, no!"
        response = waitForResponse(i);
        if (response instanceof AlreadySyncingResponse) {
            break;
        }
        Thread.sleep(40);
    }
    assertTrue(response instanceof AlreadySyncingResponse);
    watchServer.stop();
    clientA.deleteTestData();
}
Also used : WatchServer(org.syncany.operations.daemon.WatchServer) AlreadySyncingResponse(org.syncany.operations.daemon.messages.AlreadySyncingResponse) Response(org.syncany.operations.daemon.messages.api.Response) StatusFolderRequest(org.syncany.operations.daemon.messages.StatusFolderRequest) AlreadySyncingResponse(org.syncany.operations.daemon.messages.AlreadySyncingResponse) TestClient(org.syncany.tests.util.TestClient) DaemonConfigTO(org.syncany.config.to.DaemonConfigTO) TransferSettings(org.syncany.plugins.transfer.TransferSettings) LocalEventBus(org.syncany.config.LocalEventBus) Test(org.junit.Test)

Example 2 with DaemonConfigTO

use of org.syncany.config.to.DaemonConfigTO in project syncany by syncany.

the class DaemonConfigHelper method addFolder.

/**
	 * Adds the given folder to the user-specific daemon configuration (<tt>daemon.xml</tt>).
	 *
	 * <p>The method first reads the daemon configuration, checks if the folder is already present
	 * and adds it if it is not. If no daemon config file exists, a new default config file is created
	 * via {@link #createAndWriteDefaultDaemonConfig(File)}. If the folder is already present in
	 * the current daemon config, <tt>false</tt> is returned. If an error occurs (e.g. an I/O error
	 * or an invalid XML file), a {@link ConfigException} is thrown. If the folder was successfully added,
	 * <tt>true</tt> is returned.
	 *
	 * @param localDir Absolute path of the local folder to add to the daemon config
	 * @return Returns <tt>true</tt> if the folder was successfully added to the daemon config,
	 *         <tt>false</tt> otherwise
	 * @throws ConfigException If an error occurs, e.g. an I/O error or an invalid XML file
	 */
public static boolean addFolder(File localDir) throws ConfigException {
    File daemonConfigFile = new File(UserConfig.getUserConfigDir(), UserConfig.DAEMON_FILE);
    if (daemonConfigFile.exists()) {
        DaemonConfigTO daemonConfigTO = DaemonConfigTO.load(daemonConfigFile);
        String localDirPath = FileUtil.getCanonicalFile(localDir).getAbsolutePath();
        // Check if folder already exists
        boolean folderExists = false;
        for (FolderTO folderTO : daemonConfigTO.getFolders()) {
            if (localDirPath.equals(folderTO.getPath())) {
                folderExists = true;
                break;
            }
        }
        // Add to config if it's not already in there
        if (!folderExists) {
            logger.log(Level.INFO, "Adding folder to daemon config: " + localDirPath + ", and saving config at " + daemonConfigFile);
            daemonConfigTO.getFolders().add(new FolderTO(localDirPath));
            daemonConfigTO.save(daemonConfigFile);
            return true;
        } else {
            return false;
        }
    } else {
        FolderTO localDirFolderTO = new FolderTO(localDir.getAbsolutePath());
        createAndWriteDaemonConfig(daemonConfigFile, Arrays.asList(new FolderTO[] { localDirFolderTO }));
        return true;
    }
}
Also used : DaemonConfigTO(org.syncany.config.to.DaemonConfigTO) FolderTO(org.syncany.config.to.FolderTO) File(java.io.File)

Example 3 with DaemonConfigTO

use of org.syncany.config.to.DaemonConfigTO in project syncany by syncany.

the class DaemonConfigHelper method removeFolder.

public static boolean removeFolder(String localDirIdentifier) throws ConfigException {
    File daemonConfigFile = new File(UserConfig.getUserConfigDir(), UserConfig.DAEMON_FILE);
    if (daemonConfigFile.exists()) {
        DaemonConfigTO daemonConfigTO = DaemonConfigTO.load(daemonConfigFile);
        // Is index?
        Integer localDirIndex = Ints.tryParse(localDirIdentifier);
        boolean isLocalDirIndex = localDirIndex != null;
        boolean folderRemoved = false;
        // Remove by index
        if (isLocalDirIndex) {
            localDirIndex--;
            if (localDirIndex >= 0 && localDirIndex < daemonConfigTO.getFolders().size()) {
                logger.log(Level.INFO, "Given identifier (" + localDirIndex + ") is a valid index for " + daemonConfigTO.getFolders().get(localDirIndex).getPath() + ". REMOVING.");
                folderRemoved = null != daemonConfigTO.getFolders().remove((int) localDirIndex);
            } else {
                logger.log(Level.INFO, "Given identifier (" + localDirIndex + ") is a INVALID index. NOT REMOVING.");
            }
        } else // Remove by name/path
        {
            final String localDirPath = FileUtil.getCanonicalFile(new File(localDirIdentifier)).getAbsolutePath();
            folderRemoved = Iterables.removeIf(daemonConfigTO.getFolders(), new Predicate<FolderTO>() {

                @Override
                public boolean apply(FolderTO folder) {
                    return folder.getPath().equals(localDirPath);
                }
            });
        }
        // Save (if removed)
        if (folderRemoved) {
            logger.log(Level.INFO, "Folder was removed. Saving daemon.xml ...");
            daemonConfigTO.save(daemonConfigFile);
            return true;
        } else {
            return false;
        }
    } else {
        createAndWriteDaemonConfig(daemonConfigFile, Arrays.asList(new FolderTO[] {}));
        return true;
    }
}
Also used : DaemonConfigTO(org.syncany.config.to.DaemonConfigTO) FolderTO(org.syncany.config.to.FolderTO) File(java.io.File) Predicate(com.google.common.base.Predicate)

Example 4 with DaemonConfigTO

use of org.syncany.config.to.DaemonConfigTO in project syncany by syncany.

the class DaemonConfigHelper method createAndWriteDaemonConfig.

public static DaemonConfigTO createAndWriteDaemonConfig(File configFile, List<FolderTO> folders) throws ConfigException {
    UserTO defaultUserTO = new UserTO();
    defaultUserTO.setUsername(UserConfig.USER_ADMIN);
    defaultUserTO.setPassword(CipherUtil.createRandomAlphabeticString(12));
    ArrayList<UserTO> users = new ArrayList<>();
    users.add(defaultUserTO);
    DaemonConfigTO defaultDaemonConfigTO = new DaemonConfigTO();
    defaultDaemonConfigTO.setFolders(new ArrayList<>(folders));
    defaultDaemonConfigTO.setUsers(users);
    defaultDaemonConfigTO.save(configFile);
    return defaultDaemonConfigTO;
}
Also used : UserTO(org.syncany.config.to.UserTO) ArrayList(java.util.ArrayList) DaemonConfigTO(org.syncany.config.to.DaemonConfigTO)

Example 5 with DaemonConfigTO

use of org.syncany.config.to.DaemonConfigTO in project syncany by syncany.

the class BasicWatchServerTest method testWatchServer.

/**
	 * The WatchServerTest tests all things WatchServer.
	 * This is one single test to prevent issues with parallelism. (Occupied ports, EventBus mixups etc.)
	 */
@Test
public void testWatchServer() throws Exception {
    final TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    final TestClient clientA = new TestClient("ClientA", testConnection);
    final TestClient clientB = new TestClient("ClientB", testConnection);
    int port = 58443;
    // Load config template
    DaemonConfigTO daemonConfig = TestDaemonUtil.loadDaemonConfig("daemonTwoFoldersNoWebServer.xml");
    // Set port to prevent conflicts with default daemons
    daemonConfig.getWebServer().setBindPort(port);
    // Dynamically insert paths
    daemonConfig.getFolders().get(0).setPath(clientA.getConfig().getLocalDir().getAbsolutePath());
    daemonConfig.getFolders().get(1).setPath(clientB.getConfig().getLocalDir().getAbsolutePath());
    // Create access token (not needed in this test, but prevents errors in daemon)
    daemonConfig.setPortTO(TestDaemonUtil.createPortTO(port));
    // Create watchServer
    WatchServer watchServer = new WatchServer();
    clientA.createNewFile("file-1");
    watchServer.start(daemonConfig);
    for (int i = 0; i < 20; i++) {
        if (clientB.getLocalFile("file-1").exists()) {
            break;
        }
        Thread.sleep(1000);
    }
    assertTrue("File has not synced to clientB", clientB.getLocalFile("file-1").exists());
    assertEquals(clientA.getLocalFile("file-1").length(), clientB.getLocalFile("file-1").length());
    registerWithBus();
    // Create watchServer
    clientA.createNewFolder("folder");
    clientA.createNewFile("folder/file-2");
    // Allow server to settle
    Thread.sleep(100);
    // Repeat request until 3 files are found.
    FileVersion[] files = null;
    for (int i = 0; i < 20; i++) {
        LsFolderRequest request = new LsFolderRequest();
        LsOperationOptions lsOperationOption = new LsOperationOptions();
        request.setId(i);
        request.setRoot(clientA.getConfig().getLocalDir().getAbsolutePath());
        request.setOptions(lsOperationOption);
        eventBus.post(request);
        Response response = waitForResponse(i);
        if (response instanceof AlreadySyncingResponse) {
            Thread.sleep(1000);
            continue;
        }
        assertTrue(response instanceof LsFolderResponse);
        LsFolderResponse treeResponse = (LsFolderResponse) response;
        files = treeResponse.getResult().getFileList().toArray(new FileVersion[] {});
        if (files.length == 2) {
            break;
        }
        if (i == 19) {
            assertEquals(2, files.length);
        } else {
            Thread.sleep(1000);
        }
    }
    if (files[0].getName().equals("folder")) {
        files = new FileVersion[] { files[1], files[0] };
    }
    assertEquals(clientA.getLocalFile("file-1").getName(), files[0].getName());
    assertEquals(clientA.getLocalFile("file-1").length(), (long) files[0].getSize());
    assertEquals(clientA.getLocalFile("folder").getName(), files[1].getName());
    assertTrue(clientA.getLocalFile("folder").isDirectory());
    assertEquals(files[1].getType(), FileVersion.FileType.FOLDER);
    // Create GetFileHistoryRequest for the first returned file
    GetFileHistoryFolderRequest request = new GetFileHistoryFolderRequest();
    request.setId(21);
    request.setRoot(clientA.getConfig().getLocalDir().getAbsolutePath());
    request.setFileHistoryId(files[0].getFileHistoryId().toString());
    eventBus.post(request);
    Response response = waitForResponse(21);
    assertTrue(response instanceof GetFileHistoryFolderResponse);
    GetFileHistoryFolderResponse fileHistoryResponse = (GetFileHistoryFolderResponse) response;
    assertEquals(1, fileHistoryResponse.getFiles().size());
    assertEquals(files[0], fileHistoryResponse.getFiles().get(0));
    // Create GetFileRequest for the first returned file
    GetFileFolderRequest getFileRequest = new GetFileFolderRequest();
    getFileRequest.setId(22);
    getFileRequest.setRoot(clientA.getConfig().getLocalDir().getAbsolutePath());
    getFileRequest.setFileHistoryId(files[0].getFileHistoryId().toString());
    getFileRequest.setVersion(1);
    eventBus.post(getFileRequest);
    int i = 0;
    while (internalResponse == null && i < 40) {
        Thread.sleep(100);
        i++;
    }
    assertEquals((long) files[0].getSize(), internalResponse.getTempFile().length());
    // Cli Requests
    clientA.copyFile("file-1", "file-1.bak");
    // CLI request while running.
    StatusFolderRequest statusRequest = new StatusFolderRequest();
    StatusOperationOptions statusOperationOption = new StatusOperationOptions();
    statusOperationOption.setForceChecksum(true);
    statusRequest.setId(30);
    statusRequest.setRoot(clientA.getConfig().getLocalDir().getAbsolutePath());
    statusRequest.setOptions(statusOperationOption);
    // Create big file to trigger sync
    clientA.createNewFile("bigfileforlongsync", 10 * 1024 * 1024);
    // ^^ Now sync should start and we send 'status' requests
    boolean syncRunningMessageReceived = false;
    for (i = 30; i < 59; i++) {
        statusRequest.setId(i);
        eventBus.post(statusRequest);
        response = waitForResponse(i);
        if (response instanceof AlreadySyncingResponse) {
            syncRunningMessageReceived = true;
            break;
        }
        Thread.sleep(200);
    }
    assertTrue(syncRunningMessageReceived);
    // Allow daemon to sync
    Thread.sleep(10000);
    for (i = 59; i < 69; i++) {
        statusRequest.setId(i);
        eventBus.post(statusRequest);
        response = waitForResponse(i);
        if (response instanceof StatusFolderResponse) {
            break;
        }
        Thread.sleep(1000);
    }
    assertNotNull(response);
    //assertEquals("No local changes.\n", cliResponse.getOutput());
    // Restore file test
    RestoreFolderRequest restoreRequest = new RestoreFolderRequest();
    RestoreOperationOptions restoreOperationOption = new RestoreOperationOptions();
    restoreOperationOption.setFileHistoryId(files[0].getFileHistoryId());
    restoreOperationOption.setFileVersion(1);
    restoreRequest.setId(70);
    restoreRequest.setRoot(clientA.getConfig().getLocalDir().getAbsolutePath());
    restoreRequest.setOptions(restoreOperationOption);
    eventBus.post(restoreRequest);
    response = waitForResponse(70);
    assertTrue(response instanceof RestoreFolderResponse);
    RestoreFolderResponse restoreResponse = (RestoreFolderResponse) response;
    byte[] copyChecksum = FileUtil.createChecksum(clientA.getLocalFile("file-1.bak"), "SHA1");
    byte[] restoreChecksum = FileUtil.createChecksum(restoreResponse.getResult().getTargetFile(), "SHA1");
    assertArrayEquals(copyChecksum, restoreChecksum);
    watchServer.stop();
    clientA.deleteTestData();
    clientB.deleteTestData();
}
Also used : WatchServer(org.syncany.operations.daemon.WatchServer) RestoreOperationOptions(org.syncany.operations.restore.RestoreOperationOptions) LsFolderRequest(org.syncany.operations.daemon.messages.LsFolderRequest) GetFileHistoryFolderResponse(org.syncany.operations.daemon.messages.GetFileHistoryFolderResponse) StatusFolderRequest(org.syncany.operations.daemon.messages.StatusFolderRequest) LsFolderResponse(org.syncany.operations.daemon.messages.LsFolderResponse) StatusFolderResponse(org.syncany.operations.daemon.messages.StatusFolderResponse) DaemonConfigTO(org.syncany.config.to.DaemonConfigTO) TransferSettings(org.syncany.plugins.transfer.TransferSettings) LsOperationOptions(org.syncany.operations.ls.LsOperationOptions) RestoreFolderRequest(org.syncany.operations.daemon.messages.RestoreFolderRequest) StatusOperationOptions(org.syncany.operations.status.StatusOperationOptions) GetFileFolderRequest(org.syncany.operations.daemon.messages.GetFileFolderRequest) RestoreFolderResponse(org.syncany.operations.daemon.messages.RestoreFolderResponse) LsFolderResponse(org.syncany.operations.daemon.messages.LsFolderResponse) GetFileHistoryFolderResponse(org.syncany.operations.daemon.messages.GetFileHistoryFolderResponse) AlreadySyncingResponse(org.syncany.operations.daemon.messages.AlreadySyncingResponse) StatusFolderResponse(org.syncany.operations.daemon.messages.StatusFolderResponse) Response(org.syncany.operations.daemon.messages.api.Response) AlreadySyncingResponse(org.syncany.operations.daemon.messages.AlreadySyncingResponse) TestClient(org.syncany.tests.util.TestClient) FileVersion(org.syncany.database.FileVersion) GetFileHistoryFolderRequest(org.syncany.operations.daemon.messages.GetFileHistoryFolderRequest) RestoreFolderResponse(org.syncany.operations.daemon.messages.RestoreFolderResponse) Test(org.junit.Test)

Aggregations

DaemonConfigTO (org.syncany.config.to.DaemonConfigTO)5 File (java.io.File)2 Test (org.junit.Test)2 FolderTO (org.syncany.config.to.FolderTO)2 WatchServer (org.syncany.operations.daemon.WatchServer)2 AlreadySyncingResponse (org.syncany.operations.daemon.messages.AlreadySyncingResponse)2 StatusFolderRequest (org.syncany.operations.daemon.messages.StatusFolderRequest)2 Response (org.syncany.operations.daemon.messages.api.Response)2 TransferSettings (org.syncany.plugins.transfer.TransferSettings)2 TestClient (org.syncany.tests.util.TestClient)2 Predicate (com.google.common.base.Predicate)1 ArrayList (java.util.ArrayList)1 LocalEventBus (org.syncany.config.LocalEventBus)1 UserTO (org.syncany.config.to.UserTO)1 FileVersion (org.syncany.database.FileVersion)1 GetFileFolderRequest (org.syncany.operations.daemon.messages.GetFileFolderRequest)1 GetFileHistoryFolderRequest (org.syncany.operations.daemon.messages.GetFileHistoryFolderRequest)1 GetFileHistoryFolderResponse (org.syncany.operations.daemon.messages.GetFileHistoryFolderResponse)1 LsFolderRequest (org.syncany.operations.daemon.messages.LsFolderRequest)1 LsFolderResponse (org.syncany.operations.daemon.messages.LsFolderResponse)1