summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/model/Tunnel.java
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-11-20 11:44:34 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2019-11-20 12:44:54 +0100
commit8b0123042f54d843301fbef458b249fcf12466d2 (patch)
treeb3e7bd9f143568666d21014eafe544affb0a89b7 /app/src/main/java/com/wireguard/android/model/Tunnel.java
parent16890a659e6d7877b86e870fe4f0ef9cc19aee5b (diff)
Implement statistics
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/model/Tunnel.java')
-rw-r--r--app/src/main/java/com/wireguard/android/model/Tunnel.java55
1 files changed, 51 insertions, 4 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 49e78a22..87b607d0 100644
--- a/app/src/main/java/com/wireguard/android/model/Tunnel.java
+++ b/app/src/main/java/com/wireguard/android/model/Tunnel.java
@@ -5,6 +5,9 @@
package com.wireguard.android.model;
+import android.os.SystemClock;
+import android.util.Pair;
+
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.annotation.Nullable;
@@ -12,8 +15,11 @@ import androidx.annotation.Nullable;
import com.wireguard.android.BR;
import com.wireguard.android.util.ExceptionLoggers;
import com.wireguard.config.Config;
+import com.wireguard.crypto.Key;
import com.wireguard.util.Keyed;
+import java.util.HashMap;
+import java.util.Map;
import java.util.regex.Pattern;
import java9.util.concurrent.CompletableFuture;
@@ -85,15 +91,13 @@ public class Tunnel extends BaseObservable implements Keyed<String> {
@Bindable
@Nullable
public Statistics getStatistics() {
- // FIXME: Check age of statistics.
- if (statistics == null)
+ if (statistics == null || statistics.isStale())
TunnelManager.getTunnelStatistics(this).whenComplete(ExceptionLoggers.E);
return statistics;
}
public CompletionStage<Statistics> getStatisticsAsync() {
- // FIXME: Check age of statistics.
- if (statistics == null)
+ if (statistics == null || statistics.isStale())
return TunnelManager.getTunnelStatistics(this);
return CompletableFuture.completedFuture(statistics);
}
@@ -154,5 +158,48 @@ public class Tunnel extends BaseObservable implements Keyed<String> {
}
public static class Statistics extends BaseObservable {
+ private long lastTouched = SystemClock.elapsedRealtime();
+ private final Map<Key, Pair<Long, Long>> peerBytes = new HashMap<>();
+
+ public void add(final Key key, final long rx, final long tx) {
+ peerBytes.put(key, Pair.create(rx, tx));
+ lastTouched = SystemClock.elapsedRealtime();
+ }
+
+ private boolean isStale() {
+ return SystemClock.elapsedRealtime() - lastTouched > 900;
+ }
+
+ public Key[] peers() {
+ return peerBytes.keySet().toArray(new Key[0]);
+ }
+
+ public long peerRx(final Key peer) {
+ if (!peerBytes.containsKey(peer))
+ return 0;
+ return peerBytes.get(peer).first;
+ }
+
+ public long peerTx(final Key peer) {
+ if (!peerBytes.containsKey(peer))
+ return 0;
+ return peerBytes.get(peer).second;
+ }
+
+ public long totalRx() {
+ long rx = 0;
+ for (final Pair<Long, Long> val : peerBytes.values()) {
+ rx += val.first;
+ }
+ return rx;
+ }
+
+ public long totalTx() {
+ long tx = 0;
+ for (final Pair<Long, Long> val : peerBytes.values()) {
+ tx += val.second;
+ }
+ return tx;
+ }
}
}