6
6
package com .wireguard .android .backend ;
7
7
8
8
import android .os .SystemClock ;
9
- import android .util .Pair ;
10
9
11
10
import com .wireguard .crypto .Key ;
12
11
import com .wireguard .util .NonNullForAll ;
13
12
14
13
import java .util .HashMap ;
15
14
import java .util .Map ;
15
+ import java .util .Objects ;
16
+
17
+ import androidx .annotation .Nullable ;
16
18
17
19
/**
18
20
* Class representing transfer statistics for a {@link Tunnel} instance.
19
21
*/
20
22
@ NonNullForAll
21
23
public class Statistics {
22
- private final Map <Key , Pair <Long , Long >> peerBytes = new HashMap <>();
24
+
25
+ // TODO: switch to Java Record class once R8 supports desugaring those.
26
+ public final class PeerStats {
27
+ public final long rxBytes , txBytes , latestHandshakeEpochMillis ;
28
+
29
+ PeerStats (final long rxBytes , final long txBytes , final long latestHandshakeEpochMillis ) {
30
+ this .rxBytes = rxBytes ;
31
+ this .txBytes = txBytes ;
32
+ this .latestHandshakeEpochMillis = latestHandshakeEpochMillis ;
33
+ }
34
+
35
+ @ Override public boolean equals (final Object o ) {
36
+ if (this == o )
37
+ return true ;
38
+ if (o == null || getClass () != o .getClass ())
39
+ return false ;
40
+ final PeerStats stats = (PeerStats ) o ;
41
+ return rxBytes == stats .rxBytes && txBytes == stats .txBytes && latestHandshakeEpochMillis == stats .latestHandshakeEpochMillis ;
42
+ }
43
+
44
+ @ Override public int hashCode () {
45
+ return Objects .hash (rxBytes , txBytes , latestHandshakeEpochMillis );
46
+ }
47
+ }
48
+
49
+ private final Map <Key , PeerStats > stats = new HashMap <>();
23
50
private long lastTouched = SystemClock .elapsedRealtime ();
24
51
25
52
Statistics () {
26
53
}
27
54
28
55
/**
29
- * Add a peer and its current data usage to the internal map.
56
+ * Add a peer and its current stats to the internal map.
30
57
*
31
- * @param key A WireGuard public key bound to a particular peer
32
- * @param rx The received traffic for the {@link com.wireguard.config.Peer} referenced by
33
- * the provided {@link Key}. This value is in bytes
34
- * @param tx The transmitted traffic for the {@link com.wireguard.config.Peer} referenced by
35
- * the provided {@link Key}. This value is in bytes.
58
+ * @param key A WireGuard public key bound to a particular peer
59
+ * @param rxBytes The received traffic for the {@link com.wireguard.config.Peer} referenced by
60
+ * the provided {@link Key}. This value is in bytes
61
+ * @param txBytes The transmitted traffic for the {@link com.wireguard.config.Peer} referenced by
62
+ * the provided {@link Key}. This value is in bytes.
63
+ * @param latestHandshake The timestamp of the latest handshake for the {@link com.wireguard.config.Peer}
64
+ * referenced by the provided {@link Key}. The value is in epoch milliseconds.
36
65
*/
37
- void add (final Key key , final long rx , final long tx ) {
38
- peerBytes .put (key , Pair . create ( rx , tx ));
66
+ void add (final Key key , final long rxBytes , final long txBytes , final long latestHandshake ) {
67
+ stats .put (key , new PeerStats ( rxBytes , txBytes , latestHandshake ));
39
68
lastTouched = SystemClock .elapsedRealtime ();
40
69
}
41
70
@@ -49,31 +78,14 @@ public boolean isStale() {
49
78
}
50
79
51
80
/**
52
- * Get the received traffic (in bytes) for the {@link com.wireguard.config.Peer} referenced by
53
- * the provided {@link Key}
54
- *
55
- * @param peer A {@link Key} representing a {@link com.wireguard.config.Peer}.
56
- * @return a long representing the number of bytes received by this peer.
57
- */
58
- public long peerRx (final Key peer ) {
59
- final Pair <Long , Long > rxTx = peerBytes .get (peer );
60
- if (rxTx == null )
61
- return 0 ;
62
- return rxTx .first ;
63
- }
64
-
65
- /**
66
- * Get the transmitted traffic (in bytes) for the {@link com.wireguard.config.Peer} referenced by
67
- * the provided {@link Key}
81
+ * Get the statistics for the {@link com.wireguard.config.Peer} referenced by the provided {@link Key}
68
82
*
69
83
* @param peer A {@link Key} representing a {@link com.wireguard.config.Peer}.
70
- * @return a long representing the number of bytes transmitted by this peer.
84
+ * @return a {@link PeerStats} representing various statistics about this peer.
71
85
*/
72
- public long peerTx (final Key peer ) {
73
- final Pair <Long , Long > rxTx = peerBytes .get (peer );
74
- if (rxTx == null )
75
- return 0 ;
76
- return rxTx .second ;
86
+ @ Nullable
87
+ public PeerStats peer (final Key peer ) {
88
+ return this .stats .get (peer );
77
89
}
78
90
79
91
/**
@@ -83,7 +95,7 @@ public long peerTx(final Key peer) {
83
95
* {@link com.wireguard.config.Peer}s
84
96
*/
85
97
public Key [] peers () {
86
- return peerBytes .keySet ().toArray (new Key [0 ]);
98
+ return stats .keySet ().toArray (new Key [0 ]);
87
99
}
88
100
89
101
/**
@@ -93,8 +105,8 @@ public Key[] peers() {
93
105
*/
94
106
public long totalRx () {
95
107
long rx = 0 ;
96
- for (final Pair < Long , Long > val : peerBytes .values ()) {
97
- rx += val .first ;
108
+ for (final PeerStats val : stats .values ()) {
109
+ rx += val .rxBytes ;
98
110
}
99
111
return rx ;
100
112
}
@@ -106,8 +118,8 @@ public long totalRx() {
106
118
*/
107
119
public long totalTx () {
108
120
long tx = 0 ;
109
- for (final Pair < Long , Long > val : peerBytes .values ()) {
110
- tx += val .second ;
121
+ for (final PeerStats val : stats .values ()) {
122
+ tx += val .txBytes ;
111
123
}
112
124
return tx ;
113
125
}
0 commit comments