summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/model/TunnelManager.java
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2018-01-10 00:03:03 -0600
committerSamuel Holland <samuel@sholland.org>2018-01-10 00:03:03 -0600
commit7b9c1a536ceb1527edda8be90cefc1f830dbbf81 (patch)
tree0239985dff63d68f282023abd03034a3a11296e1 /app/src/main/java/com/wireguard/android/model/TunnelManager.java
parent951afaa9b29637d1abb3b6a60f9fdeb6bc27bb1c (diff)
model: Use ConfigStore.rename() to avoid recreating tunnels
Rename all of the functions to be in line with setConfig/setState Signed-off-by: Samuel Holland <samuel@sholland.org>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/model/TunnelManager.java')
-rw-r--r--app/src/main/java/com/wireguard/android/model/TunnelManager.java79
1 files changed, 32 insertions, 47 deletions
diff --git a/app/src/main/java/com/wireguard/android/model/TunnelManager.java b/app/src/main/java/com/wireguard/android/model/TunnelManager.java
index 4c053e9a..9e03cc39 100644
--- a/app/src/main/java/com/wireguard/android/model/TunnelManager.java
+++ b/app/src/main/java/com/wireguard/android/model/TunnelManager.java
@@ -142,53 +142,6 @@ public final class TunnelManager extends BaseObservable {
setLastUsedTunnel(tunnels.get(lastUsedName));
}
- CompletionStage<Tunnel> rename(final Tunnel tunnel, final String name) {
- if (!Tunnel.isNameValid(name))
- return CompletableFuture.failedFuture(new IllegalArgumentException("Invalid name"));
- if (tunnels.containsKey(name)) {
- final String message = "Tunnel " + name + " already exists";
- return CompletableFuture.failedFuture(new IllegalArgumentException(message));
- }
- final State originalState = tunnel.getState();
- final boolean wasLastUsed = tunnel == lastUsedTunnel;
- // Make sure nothing touches the tunnel.
- if (wasLastUsed)
- setLastUsedTunnel(null);
- tunnels.remove(tunnel);
- return asyncWorker.supplyAsync(() -> {
- if (originalState == State.UP)
- backend.setState(tunnel, State.DOWN);
- final Config newConfig = configStore.create(name, tunnel.getConfig());
- final Tunnel newTunnel = new Tunnel(this, name, newConfig, State.DOWN);
- try {
- if (originalState == State.UP)
- backend.setState(newTunnel, originalState);
- configStore.delete(tunnel.getName());
- } catch (final Exception e) {
- // Clean up.
- configStore.delete(name);
- if (originalState == State.UP)
- backend.setState(tunnel, originalState);
- // Re-throw the exception to fail the completion.
- throw e;
- }
- return newTunnel;
- }).whenComplete((newTunnel, e) -> {
- if (e == null) {
- // Success, add the new tunnel.
- newTunnel.onStateChanged(originalState);
- tunnels.add(newTunnel);
- if (wasLastUsed)
- setLastUsedTunnel(newTunnel);
- } else {
- // Failure, put the old tunnel back.
- tunnels.add(tunnel);
- if (wasLastUsed)
- setLastUsedTunnel(tunnel);
- }
- });
- }
-
public CompletionStage<Void> restoreState() {
if (!preferences.getBoolean(KEY_RESTORE_ON_BOOT, false))
return CompletableFuture.completedFuture(null);
@@ -227,6 +180,38 @@ public final class TunnelManager extends BaseObservable {
}).thenApply(tunnel::onConfigChanged);
}
+ CompletionStage<String> setTunnelName(final Tunnel tunnel, final String name) {
+ if (!Tunnel.isNameValid(name))
+ return CompletableFuture.failedFuture(new IllegalArgumentException("Invalid name"));
+ if (tunnels.containsKey(name)) {
+ final String message = "Tunnel " + name + " already exists";
+ return CompletableFuture.failedFuture(new IllegalArgumentException(message));
+ }
+ final State originalState = tunnel.getState();
+ final boolean wasLastUsed = tunnel == lastUsedTunnel;
+ // Make sure nothing touches the tunnel.
+ if (wasLastUsed)
+ setLastUsedTunnel(null);
+ tunnels.remove(tunnel);
+ return asyncWorker.supplyAsync(() -> {
+ if (originalState == State.UP)
+ backend.setState(tunnel, State.DOWN);
+ configStore.rename(tunnel.getName(), name);
+ final String newName = tunnel.onNameChanged(name);
+ if (originalState == State.UP)
+ backend.setState(tunnel, originalState);
+ return newName;
+ }).whenComplete((newName, e) -> {
+ // On failure, we don't know what state the tunnel might be in. Fix that.
+ if (e != null)
+ getTunnelState(tunnel);
+ // Add the tunnel back to the manager, under whatever name it thinks it has.
+ tunnels.add(tunnel);
+ if (wasLastUsed)
+ setLastUsedTunnel(tunnel);
+ });
+ }
+
CompletionStage<State> setTunnelState(final Tunnel tunnel, final State state) {
// Ensure the configuration is loaded before trying to use it.
return tunnel.getConfigAsync().thenCompose(x ->