Browse Source

Tunnel: Require passing a state to the constructor

Signed-off-by: Samuel Holland <samuel@sholland.org>
master
Samuel Holland 6 years ago
parent
commit
df7d18fb5d
2 changed files with 8 additions and 6 deletions
  1. +3
    -2
      app/src/main/java/com/wireguard/android/model/Tunnel.java
  2. +5
    -4
      app/src/main/java/com/wireguard/android/model/TunnelManager.java

+ 3
- 2
app/src/main/java/com/wireguard/android/model/Tunnel.java View File

@@ -31,15 +31,16 @@ public class Tunnel extends BaseObservable implements Keyed<String> {
private final ConfigStore configStore; private final ConfigStore configStore;
private final String name; private final String name;
private Config config; private Config config;
private State state = State.UNKNOWN;
private State state;
private Statistics statistics; private Statistics statistics;


Tunnel(@NonNull final Backend backend, @NonNull final ConfigStore configStore, Tunnel(@NonNull final Backend backend, @NonNull final ConfigStore configStore,
@NonNull final String name, @Nullable final Config config) {
@NonNull final String name, @Nullable final Config config, @NonNull final State state) {
this.backend = backend; this.backend = backend;
this.configStore = configStore; this.configStore = configStore;
this.name = name; this.name = name;
this.config = config; this.config = config;
this.state = state;
} }


public static boolean isNameValid(final CharSequence name) { public static boolean isNameValid(final CharSequence name) {


+ 5
- 4
app/src/main/java/com/wireguard/android/model/TunnelManager.java View File

@@ -48,14 +48,14 @@ public final class TunnelManager {
this.preferences = preferences; this.preferences = preferences;
} }


private Tunnel add(final String name, final Config config) {
final Tunnel tunnel = new Tunnel(backend, configStore, name, config);
private Tunnel add(final String name, final Config config, final State state) {
final Tunnel tunnel = new Tunnel(backend, configStore, name, config, state);
tunnels.add(tunnel); tunnels.add(tunnel);
return tunnel; return tunnel;
} }


private Tunnel add(final String name) { private Tunnel add(final String name) {
return add(name, null);
return add(name, null, State.UNKNOWN);
} }


public CompletionStage<Tunnel> create(final String name, final Config config) { public CompletionStage<Tunnel> create(final String name, final Config config) {
@@ -66,7 +66,8 @@ public final class TunnelManager {
final String message = "Tunnel " + name + " already exists"; final String message = "Tunnel " + name + " already exists";
return CompletableFuture.failedFuture(new IllegalArgumentException(message)); return CompletableFuture.failedFuture(new IllegalArgumentException(message));
} }
return configStore.create(name, config).thenApply(savedConfig -> add(name, savedConfig));
return configStore.create(name, config)
.thenApply(savedConfig -> add(name, savedConfig, State.DOWN));
} }


public CompletionStage<Void> delete(final Tunnel tunnel) { public CompletionStage<Void> delete(final Tunnel tunnel) {


Loading…
Cancel
Save