diff options
author | Samuel Holland <samuel@sholland.org> | 2018-01-07 01:22:31 -0600 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-01-07 01:22:31 -0600 |
commit | 5adcac1606d978f5574d80c4dbd3e728ec0e49c7 (patch) | |
tree | addc28dddffa9846f956c0c24283750ca4fb38d9 /app/src/main/java | |
parent | a6595a273afd50524cc66765c6bfbdcc34cb12e4 (diff) |
model: Allow renaming tunnels
Signed-off-by: Samuel Holland <samuel@sholland.org>
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/com/wireguard/android/model/Tunnel.java | 6 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/android/model/TunnelManager.java | 26 |
2 files changed, 32 insertions, 0 deletions
diff --git a/app/src/main/java/com/wireguard/android/model/Tunnel.java b/app/src/main/java/com/wireguard/android/model/Tunnel.java index e61ea095..9cdcf71f 100644 --- a/app/src/main/java/com/wireguard/android/model/Tunnel.java +++ b/app/src/main/java/com/wireguard/android/model/Tunnel.java @@ -112,6 +112,12 @@ public class Tunnel extends BaseObservable implements Keyed<String> { notifyPropertyChanged(BR.statistics); } + public CompletionStage<Tunnel> rename(@NonNull final String name) { + if (!name.equals(this.name)) + return manager.rename(this, name); + return CompletableFuture.completedFuture(this); + } + public CompletionStage<Config> setConfig(@NonNull final Config config) { if (!config.equals(this.config)) return manager.setTunnelConfig(this, config); 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 beed16fe..b0e6ead2 100644 --- a/app/src/main/java/com/wireguard/android/model/TunnelManager.java +++ b/app/src/main/java/com/wireguard/android/model/TunnelManager.java @@ -120,6 +120,32 @@ public final class TunnelManager { addToList(name, null, running.contains(name) ? State.UP : State.DOWN); } + 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(); + return asyncWorker.supplyAsync(() -> { + backend.setState(tunnel, State.DOWN); + final Config newConfig = configStore.create(name, tunnel.getConfig()); + final Tunnel newTunnel = new Tunnel(this, name, newConfig, State.DOWN); + if (originalState == State.UP) { + backend.setState(newTunnel, originalState); + newTunnel.onStateChanged(originalState); + } + configStore.delete(tunnel.getName()); + return newTunnel; + }).whenComplete((newTunnel, e) -> { + if (e != null) + return; + tunnels.remove(tunnel); + tunnels.add(newTunnel); + }); + } + public CompletionStage<Void> restoreState() { if (!preferences.getBoolean(KEY_RESTORE_ON_BOOT, false)) return CompletableFuture.completedFuture(null); |