summaryrefslogtreecommitdiffhomepage
path: root/tunnel/src/main/java/com/wireguard/android/backend/Statistics.java
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-03-09 19:00:14 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-03-09 19:24:26 +0530
commitadc613d8011af7c508050badb1272e8326554c39 (patch)
tree4eadedc0767e1f4f482b7c22ec905329acab62a6 /tunnel/src/main/java/com/wireguard/android/backend/Statistics.java
parentfd573f6c1c37bcfcd09237dfcd55e08b1e0eaff9 (diff)
Migrate tunnel related classes to tunnel/ Gradle module
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'tunnel/src/main/java/com/wireguard/android/backend/Statistics.java')
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/Statistics.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/tunnel/src/main/java/com/wireguard/android/backend/Statistics.java b/tunnel/src/main/java/com/wireguard/android/backend/Statistics.java
new file mode 100644
index 00000000..2ca87d23
--- /dev/null
+++ b/tunnel/src/main/java/com/wireguard/android/backend/Statistics.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright © 2020 WireGuard LLC. All Rights Reserved.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package com.wireguard.android.backend;
+
+import android.os.SystemClock;
+import android.util.Pair;
+
+import com.wireguard.crypto.Key;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Statistics {
+ private long lastTouched = SystemClock.elapsedRealtime();
+ private final Map<Key, Pair<Long, Long>> peerBytes = new HashMap<>();
+
+ Statistics() { }
+
+ void add(final Key key, final long rx, final long tx) {
+ peerBytes.put(key, Pair.create(rx, tx));
+ lastTouched = SystemClock.elapsedRealtime();
+ }
+
+ public 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;
+ }
+}