Peer: Associate with a Config

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland 2017-08-19 18:50:35 -05:00
parent fbd923a060
commit 9026317b0e
2 changed files with 29 additions and 4 deletions

View File

@ -34,6 +34,18 @@ public class Config extends BaseObservable
private String name; private String name;
private final ObservableList<Peer> peers = new ObservableArrayList<>(); private final ObservableList<Peer> peers = new ObservableArrayList<>();
public Peer addPeer() {
final Peer peer = new Peer(this);
peers.add(peer);
return peer;
}
private Peer addPeer(final Peer peer) {
final Peer copy = peer.copy(this);
peers.add(copy);
return copy;
}
@Override @Override
public int compareTo(@NonNull final Config config) { public int compareTo(@NonNull final Config config) {
return getName().compareTo(config.getName()); return getName().compareTo(config.getName());
@ -53,7 +65,7 @@ public class Config extends BaseObservable
name = source.name; name = source.name;
peers.clear(); peers.clear();
for (final Peer peer : source.peers) for (final Peer peer : source.peers)
peers.add(peer.copy()); addPeer(peer);
} }
public Interface getInterface() { public Interface getInterface() {
@ -92,8 +104,7 @@ public class Config extends BaseObservable
if ("[Interface]".equals(line)) { if ("[Interface]".equals(line)) {
currentPeer = null; currentPeer = null;
} else if ("[Peer]".equals(line)) { } else if ("[Peer]".equals(line)) {
currentPeer = new Peer(); currentPeer = addPeer();
peers.add(currentPeer);
} else if (currentPeer == null) { } else if (currentPeer == null) {
iface.parseFrom(line); iface.parseFrom(line);
} else { } else {

View File

@ -12,13 +12,22 @@ import com.android.databinding.library.baseAdapters.BR;
public class Peer extends BaseObservable implements Copyable<Peer>, Observable { public class Peer extends BaseObservable implements Copyable<Peer>, Observable {
private String allowedIPs; private String allowedIPs;
private Config config;
private String endpoint; private String endpoint;
private String persistentKeepalive; private String persistentKeepalive;
private String publicKey; private String publicKey;
public Peer(final Config config) {
this.config = config;
}
@Override @Override
public Peer copy() { public Peer copy() {
final Peer copy = new Peer(); return copy(config);
}
public Peer copy(final Config config) {
final Peer copy = new Peer(config);
copy.copyFrom(this); copy.copyFrom(this);
return copy; return copy;
} }
@ -65,6 +74,11 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable {
throw new IllegalArgumentException(line); throw new IllegalArgumentException(line);
} }
public void removeSelf() {
config.getPeers().remove(this);
config = null;
}
public void setAllowedIPs(String allowedIPs) { public void setAllowedIPs(String allowedIPs) {
if (allowedIPs != null && allowedIPs.isEmpty()) if (allowedIPs != null && allowedIPs.isEmpty())
allowedIPs = null; allowedIPs = null;