Skip to content

Commit

Permalink
Merge pull request #1141 from alvasw/NativeTorController_Switch_from_…
Browse files Browse the repository at this point in the history
…Lock_to_AtomicBoolean

NativeTorController: Switch from Lock to AtomicBoolean
  • Loading branch information
alvasw authored Aug 20, 2023
2 parents 3bca850 + 6248e79 commit 0570de9
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,33 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
public class NativeTorController implements BootstrapEventListener, HsDescUploadedEventListener {

private final AtomicBoolean isRunning = new AtomicBoolean();
private final CountDownLatch isBootstrappedCountdownLatch = new CountDownLatch(1);
private final CountDownLatch isHsDescUploadedCountdownLatch = new CountDownLatch(1);
private final ControllerEventHandler controllerEventHandler = new ControllerEventHandler();
private final CompletableFuture<String> hiddenServiceAddress = new CompletableFuture<>();
private Optional<TorControlConnection> torControlConnection = Optional.empty();

public synchronized void connect(int controlPort, PasswordDigest controlConnectionSecret) {
public void connect(int controlPort, PasswordDigest controlConnectionSecret) {
boolean readyToStart = isRunning.compareAndSet(false, true);
if (!readyToStart) {
return;
}

try {
var controlSocket = new Socket("127.0.0.1", controlPort);
var controlConnection = new TorControlConnection(controlSocket);
controlConnection.launchThread(true);
controlConnection.authenticate(controlConnectionSecret.getSecret());
torControlConnection = Optional.of(controlConnection);

} catch (IOException e) {
isRunning.set(false);
throw new ControlCommandFailedException("Couldn't connect to control port.", e);
}
}
Expand Down Expand Up @@ -129,11 +138,17 @@ public void waitUntilBootstrapped() {
}
}

public synchronized void shutdown() {
public void shutdown() {
boolean canShutdown = isRunning.compareAndSet(true, false);
if (!canShutdown) {
return;
}

try {
TorControlConnection controlConnection = torControlConnection.orElseThrow();
controlConnection.shutdownTor("SHUTDOWN");
} catch (IOException e) {
isRunning.set(true);
throw new ControlCommandFailedException("Couldn't send shutdown command to Tor.", e);
}
}
Expand Down

0 comments on commit 0570de9

Please sign in to comment.