diff --git a/common/src/integrationTest/java/bisq/common/io_watcher/DirectoryWatcherTests.java b/common/src/integrationTest/java/bisq/common/io_watcher/DirectoryWatcherTests.java deleted file mode 100644 index 508456e6d6..0000000000 --- a/common/src/integrationTest/java/bisq/common/io_watcher/DirectoryWatcherTests.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.common.io_watcher; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardWatchEventKinds; -import java.util.Set; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import static org.assertj.core.api.Assertions.assertThat; - -public class DirectoryWatcherTests { - @Test - void detectFileCreation(@TempDir Path tempDir) throws IOException, ExecutionException, InterruptedException, TimeoutException { - try (var watcher = new DirectoryWatcher(tempDir, Set.of(StandardWatchEventKinds.ENTRY_CREATE))) { - var completableFuture = new CompletableFuture(); - watcher.initialize(completableFuture::complete); - - Path newFilePath = tempDir.resolve("newFile"); - Files.writeString(newFilePath, "Hello!"); - - Path path = completableFuture.get(30, TimeUnit.SECONDS); - assertThat(path).isEqualTo(newFilePath); - } - } - - @Test - void detectFileWrite(@TempDir Path tempDir) throws IOException, ExecutionException, InterruptedException, TimeoutException { - Path newFilePath = tempDir.resolve("newFile"); - Files.writeString(newFilePath, "Hello!"); - - try (var watcher = new DirectoryWatcher(tempDir, Set.of(StandardWatchEventKinds.ENTRY_MODIFY))) { - var completableFuture = new CompletableFuture(); - watcher.initialize(completableFuture::complete); - - Files.writeString(newFilePath, "World!"); - - Path path = completableFuture.get(30, TimeUnit.SECONDS); - assertThat(path).isEqualTo(newFilePath); - } - } -} diff --git a/common/src/main/java/bisq/common/io_watcher/CouldNotInitializeDirectoryWatcherException.java b/common/src/main/java/bisq/common/io_watcher/CouldNotInitializeDirectoryWatcherException.java deleted file mode 100644 index e9f7d084c2..0000000000 --- a/common/src/main/java/bisq/common/io_watcher/CouldNotInitializeDirectoryWatcherException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.common.io_watcher; - -public class CouldNotInitializeDirectoryWatcherException extends RuntimeException { - public CouldNotInitializeDirectoryWatcherException(String message) { - super(message); - } - - public CouldNotInitializeDirectoryWatcherException(Throwable cause) { - super(cause); - } -} diff --git a/common/src/main/java/bisq/common/io_watcher/DirectoryEventPublisher.java b/common/src/main/java/bisq/common/io_watcher/DirectoryEventPublisher.java deleted file mode 100644 index 356361a128..0000000000 --- a/common/src/main/java/bisq/common/io_watcher/DirectoryEventPublisher.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.common.io_watcher; - -import java.nio.file.Path; -import java.nio.file.WatchEvent; -import java.nio.file.WatchService; -import java.util.Set; -import java.util.concurrent.Flow; - -public class DirectoryEventPublisher implements Flow.Publisher { - private final WatchService watchService; - private final Path directoryPath; - private final Set> watchEventKinds; - - public DirectoryEventPublisher(WatchService watchService, Path directoryPath, Set> watchEventKinds) { - this.watchService = watchService; - this.directoryPath = directoryPath; - this.watchEventKinds = watchEventKinds; - } - - @Override - public void subscribe(Flow.Subscriber subscriber) { - var subscription = new DirectoryEventSubscription(subscriber, watchService, directoryPath, watchEventKinds); - subscriber.onSubscribe(subscription); - } -} diff --git a/common/src/main/java/bisq/common/io_watcher/DirectoryEventSubscriber.java b/common/src/main/java/bisq/common/io_watcher/DirectoryEventSubscriber.java deleted file mode 100644 index a7845e6c86..0000000000 --- a/common/src/main/java/bisq/common/io_watcher/DirectoryEventSubscriber.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.common.io_watcher; - -import java.nio.file.Path; -import java.util.concurrent.Flow; -import java.util.function.Consumer; - -public class DirectoryEventSubscriber implements Flow.Subscriber { - private final Consumer consumer; - private Flow.Subscription subscription; - - public DirectoryEventSubscriber(Consumer consumer) { - this.consumer = consumer; - } - - @Override - public void onSubscribe(Flow.Subscription subscription) { - this.subscription = subscription; - subscription.request(1); - } - - @Override - public void onNext(Path path) { - consumer.accept(path); - subscription.request(1); - } - - @Override - public void onError(Throwable throwable) { - throw new RuntimeException(throwable); - } - - @Override - public void onComplete() { - throw new UnsupportedOperationException(); - } -} diff --git a/common/src/main/java/bisq/common/io_watcher/DirectoryEventSubscription.java b/common/src/main/java/bisq/common/io_watcher/DirectoryEventSubscription.java deleted file mode 100644 index 2c7fc283a4..0000000000 --- a/common/src/main/java/bisq/common/io_watcher/DirectoryEventSubscription.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.common.io_watcher; - -import java.nio.file.Path; -import java.nio.file.WatchEvent; -import java.nio.file.WatchKey; -import java.nio.file.WatchService; -import java.util.Set; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Flow; -import java.util.concurrent.TimeUnit; - -public class DirectoryEventSubscription implements Flow.Subscription { - private final ExecutorService executorService = Executors.newSingleThreadExecutor(); - private final Flow.Subscriber subscriber; - private final WatchService watchService; - private final Path directoryPath; - private final Set> watchEventKinds; - - public DirectoryEventSubscription(Flow.Subscriber subscriber, - WatchService watchService, - Path directoryPath, - Set> watchEventKinds) { - this.subscriber = subscriber; - this.watchService = watchService; - this.directoryPath = directoryPath; - this.watchEventKinds = watchEventKinds; - } - - @Override - public void request(long l) { - executorService.submit(this::watchDirectoryForChanges); - } - - @Override - public void cancel() { - throw new UnsupportedOperationException(); - } - - private void watchDirectoryForChanges() { - try { - WatchKey key = watchService.poll(1, TimeUnit.MINUTES); - if (key == null) { - var error = new NoDirectoryChangesTimeoutException("No changes in directory for the last minute."); - subscriber.onError(error); - return; - } - - for (WatchEvent event : key.pollEvents()) { - if (watchEventKinds.contains(event.kind())) { - @SuppressWarnings("unchecked") - WatchEvent castedWatchEvent = (WatchEvent) event; - Path filename = castedWatchEvent.context(); - - Path filePath = directoryPath.resolve(filename); - subscriber.onNext(filePath); - } - } - } catch (InterruptedException e) { - subscriber.onError(e); - return; - } - var error = new IllegalStateException(); - subscriber.onError(error); - } -} diff --git a/common/src/main/java/bisq/common/io_watcher/DirectoryWatcher.java b/common/src/main/java/bisq/common/io_watcher/DirectoryWatcher.java deleted file mode 100644 index 1177770dbe..0000000000 --- a/common/src/main/java/bisq/common/io_watcher/DirectoryWatcher.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.common.io_watcher; - -import java.io.IOException; -import java.nio.file.FileSystems; -import java.nio.file.Path; -import java.nio.file.WatchEvent; -import java.nio.file.WatchService; -import java.util.Set; -import java.util.function.Consumer; - -public class DirectoryWatcher implements AutoCloseable { - private final Path directoryPath; - private final Set> watchEventKinds; - - private WatchService watchService; - - public DirectoryWatcher(Path directoryPath, Set> watchEventKinds) { - this.directoryPath = directoryPath; - this.watchEventKinds = watchEventKinds; - } - - public void initialize(Consumer eventConsumer) { - try { - watchService = FileSystems.getDefault().newWatchService(); - - WatchEvent.Kind[] eventKinds = new WatchEvent.Kind[watchEventKinds.size()]; - watchEventKinds.toArray(eventKinds); - directoryPath.register(watchService, eventKinds); - - subscribeToChangesAsync(eventConsumer); - - } catch (IOException e) { - throw new CouldNotInitializeDirectoryWatcherException(e); - } - } - - private void subscribeToChangesAsync(Consumer consumer) { - var directoryEventPublisher = new DirectoryEventPublisher(watchService, directoryPath, watchEventKinds); - var directoryEventSubscriber = new DirectoryEventSubscriber(consumer); - directoryEventPublisher.subscribe(directoryEventSubscriber); - } - - @Override - public void close() throws IOException { - watchService.close(); - } -} diff --git a/common/src/main/java/bisq/common/io_watcher/NoDirectoryChangesTimeoutException.java b/common/src/main/java/bisq/common/io_watcher/NoDirectoryChangesTimeoutException.java deleted file mode 100644 index 2c9fdf7cb8..0000000000 --- a/common/src/main/java/bisq/common/io_watcher/NoDirectoryChangesTimeoutException.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.common.io_watcher; - -public class NoDirectoryChangesTimeoutException extends RuntimeException { - public NoDirectoryChangesTimeoutException(String message) { - super(message); - } -} diff --git a/network/tor/tor/src/main/java/bisq/tor/TorService.java b/network/tor/tor/src/main/java/bisq/tor/TorService.java index 0a938a34fa..3f49140d98 100644 --- a/network/tor/tor/src/main/java/bisq/tor/TorService.java +++ b/network/tor/tor/src/main/java/bisq/tor/TorService.java @@ -32,7 +32,6 @@ import lombok.extern.slf4j.Slf4j; import net.freehaven.tor.control.PasswordDigest; -import java.io.File; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; @@ -79,15 +78,6 @@ public CompletableFuture initialize() { var nativeTorProcess = new NativeTorProcess(torDataDirPath); torProcess = Optional.of(nativeTorProcess); - - File debugLogFile = torDataDirPath.resolve("debug.log").toFile(); - if (debugLogFile.exists()) { - boolean isSuccess = debugLogFile.delete(); - if (!isSuccess) { - throw new IllegalStateException("Can't delete old debug.log file"); - } - } - nativeTorProcess.start(); Path controlDirPath = torDataDirPath.resolve(NativeTorProcess.CONTROL_DIR_NAME); diff --git a/network/tor/tor/src/main/java/bisq/tor/process/ControlPortReadyWaiter.java b/network/tor/tor/src/main/java/bisq/tor/process/ControlPortReadyWaiter.java deleted file mode 100644 index 4afa0d09d7..0000000000 --- a/network/tor/tor/src/main/java/bisq/tor/process/ControlPortReadyWaiter.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.tor.process; - -import bisq.common.io_watcher.CouldNotInitializeDirectoryWatcherException; -import bisq.common.io_watcher.DirectoryWatcher; -import lombok.Getter; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.StandardWatchEventKinds; -import java.nio.file.WatchEvent; -import java.util.Set; -import java.util.concurrent.CompletableFuture; - -public class ControlPortReadyWaiter { - @Getter - private final CompletableFuture portCompletableFuture = new CompletableFuture<>(); - private final DirectoryWatcher directoryWatcher; - private final Path controlDirPath; - private final Path controlPortFilePath; - - public ControlPortReadyWaiter(Path controlDirPath) { - this.controlDirPath = controlDirPath; - Set> watchEventKinds = Set.of( - StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY); - directoryWatcher = new DirectoryWatcher(controlDirPath, watchEventKinds); - controlPortFilePath = controlDirPath.resolve("control"); - } - - public void initialize() { - createTorControlDirectory(); - deleteControlPortFileFromPreviousRun(); - - directoryWatcher.initialize(path -> { - if (path.equals(controlPortFilePath)) { - try { - int controlPort = ControlPortFileParser.parse(controlPortFilePath); - portCompletableFuture.complete(controlPort); - close(); - } catch (IOException | ControlPortFileParseFailureException e) { - portCompletableFuture.completeExceptionally(e); - } - } - }); - } - - public void close() throws IOException { - directoryWatcher.close(); - } - - private void createTorControlDirectory() { - File controlDirFile = controlDirPath.toFile(); - if (controlDirFile.exists()) { - return; - } - - boolean isSuccess = controlDirFile.mkdirs(); - if (!isSuccess) { - throw new CouldNotInitializeDirectoryWatcherException("Couldn't create Tor control directory: " + controlDirPath); - } - } - - private void deleteControlPortFileFromPreviousRun() { - File controlPortFile = controlPortFilePath.toFile(); - if (!controlPortFile.exists()) { - return; - } - - boolean isSuccess = controlPortFile.delete(); - if (!isSuccess) { - throw new CouldNotInitializeDirectoryWatcherException("Couldn't delete Tor control port file from previous run: " + controlPortFilePath); - } - } -} diff --git a/network/tor/tor/src/main/java/bisq/tor/process/NativeTorProcess.java b/network/tor/tor/src/main/java/bisq/tor/process/NativeTorProcess.java index a5c08d2834..670170e36e 100644 --- a/network/tor/tor/src/main/java/bisq/tor/process/NativeTorProcess.java +++ b/network/tor/tor/src/main/java/bisq/tor/process/NativeTorProcess.java @@ -17,8 +17,6 @@ package bisq.tor.process; -import bisq.common.FileCreationWatcher; -import bisq.common.scanner.FileScanner; import lombok.extern.slf4j.Slf4j; import java.io.File; @@ -26,11 +24,7 @@ import java.nio.file.Path; import java.util.Map; import java.util.Optional; -import java.util.Set; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; @Slf4j public class NativeTorProcess { @@ -41,7 +35,6 @@ public class NativeTorProcess { private final Path torBinaryPath; private final Path torrcPath; private Optional process = Optional.empty(); - private Optional> logFileCreationWaiter = Optional.empty(); public NativeTorProcess(Path torDataDirPath) { this.torDataDirPath = torDataDirPath; @@ -66,8 +59,6 @@ public void start() { processBuilder.redirectError(ProcessBuilder.Redirect.DISCARD); processBuilder.redirectOutput(ProcessBuilder.Redirect.DISCARD); - logFileCreationWaiter = Optional.of(createLogFileCreationWaiter()); - try { Process torProcess = processBuilder.start(); process = Optional.of(torProcess); @@ -76,24 +67,6 @@ public void start() { } } - public void waitUntilControlPortReady() { - try { - if (logFileCreationWaiter.isPresent()) { - Future pathFuture = logFileCreationWaiter.get(); - - FileScanner fileScanner = new FileScanner( - Set.of("[notice] Opened Control listener connection (ready) on "), - pathFuture - ); - fileScanner.waitUntilLogContainsLines(); - } - - } catch (ExecutionException | IOException | InterruptedException | TimeoutException e) { - log.error("Couldn't wait for log file creation.", e); - throw new IllegalStateException("Couldn't wait for log file creation."); - } - } - public void waitUntilExited() { log.info("Wait until tor process has exited"); process.ifPresent(process -> { @@ -119,13 +92,4 @@ private void createTorControlDirectory() { } } } - - private Future createLogFileCreationWaiter() { - Path dataDir = torrcPath.getParent(); - Path logFilePath = torrcPath.getParent().resolve("debug.log"); - - FileCreationWatcher fileCreationWatcher = new FileCreationWatcher(dataDir); - return fileCreationWatcher.waitForFile(logFilePath); - } - }