OSDN Git Service

Add OffloadHardwareInterface.getForwardedStats() wrapper
authorErik Kline <ek@google.com>
Wed, 12 Jul 2017 06:46:54 +0000 (15:46 +0900)
committerErik Kline <ek@google.com>
Wed, 12 Jul 2017 14:25:27 +0000 (23:25 +0900)
Test: as follows
    - built
    - flashed
    - booted
    - "runtest frameworks-net" passes
Bug: 29337859
Bug: 32163131

Merged-In: I41f837e16eff9a4cd763d65155bd9b5d5d980acd
Merged-In: Id9766983a3bbd885ef0f0d3ba9e33de903939f2d
Merged-In: Id831bc99cadbd59d280b026866707dbbe3b0e542
(cherry picked from commit 9a5b02a89c02d5ff633d8456b22d3d5b7a15bd44)

Change-Id: I0752b6e2cf1e0b9a5075f7911408528538683c84

services/core/java/com/android/server/connectivity/tethering/OffloadHardwareInterface.java

index cd98b30..913096c 100644 (file)
@@ -35,6 +35,7 @@ import java.util.ArrayList;
  */
 public class OffloadHardwareInterface {
     private static final String TAG = OffloadHardwareInterface.class.getSimpleName();
+    private static final String YIELDS = " -> ";
     // Change this value to control whether tether offload is enabled or
     // disabled by default in the absence of an explicit Settings value.
     // See accompanying unittest to distinguish 0 from non-0 values.
@@ -59,6 +60,25 @@ public class OffloadHardwareInterface {
                                        String dstAddr, int dstPort) {}
     }
 
+    public static class ForwardedStats {
+        public long rxBytes;
+        public long txBytes;
+
+        public ForwardedStats() {
+            rxBytes = 0;
+            txBytes = 0;
+        }
+
+        public void add(ForwardedStats other) {
+            rxBytes += other.rxBytes;
+            txBytes += other.txBytes;
+        }
+
+        public String toString() {
+            return String.format("rx:%s tx:%s", rxBytes, txBytes);
+        }
+    }
+
     public OffloadHardwareInterface(Handler h, SharedLog log) {
         mHandler = h;
         mLog = log.forSubComponent(TAG);
@@ -123,6 +143,26 @@ public class OffloadHardwareInterface {
         mLog.log("stopOffloadControl()");
     }
 
+    public ForwardedStats getForwardedStats(String upstream) {
+        final String logmsg = String.format("getForwardedStats(%s)",  upstream);
+
+        final ForwardedStats stats = new ForwardedStats();
+        try {
+            mOffloadControl.getForwardedStats(
+                    upstream,
+                    (long rxBytes, long txBytes) -> {
+                        stats.rxBytes = (rxBytes > 0) ? rxBytes : 0;
+                        stats.txBytes = (txBytes > 0) ? txBytes : 0;
+                    });
+        } catch (RemoteException e) {
+            record(logmsg, e);
+            return stats;
+        }
+
+        mLog.log(logmsg + YIELDS + stats);
+        return stats;
+    }
+
     public boolean setUpstreamParameters(
             String iface, String v4addr, String v4gateway, ArrayList<String> v6gws) {
         iface = (iface != null) ? iface : NO_INTERFACE_NAME;
@@ -151,11 +191,11 @@ public class OffloadHardwareInterface {
     }
 
     private void record(String msg, Throwable t) {
-        mLog.e(msg + " -> exception: " + t);
+        mLog.e(msg + YIELDS + "exception: " + t);
     }
 
     private void record(String msg, CbResults results) {
-        final String logmsg = msg + " -> " + results;
+        final String logmsg = msg + YIELDS + results;
         if (!results.success) {
             mLog.e(logmsg);
         } else {