• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.pmc;
2 
3 import android.app.AlarmManager;
4 import android.app.PendingIntent;
5 import android.content.BroadcastReceiver;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.content.IntentFilter;
9 import android.net.wifi.WifiManager;
10 import android.os.AsyncTask;
11 import android.os.PowerManager;
12 import android.os.SystemClock;
13 import android.util.Log;
14 
15 import java.io.File;
16 import java.util.ArrayList;
17 import java.util.List;
18 
19 /**
20  * Starts an iperf client with the provided params for data transmission tests.
21  * The client starts a UDP data transfer with the provided server with the max possible timeout.t a
22  */
23 public class IperfClient {
24 
25     private static final String IPERF_COMMAND = "iperf3";
26     private static final String IPERF_OPTION_SERVER_FLAG = "-c";
27     private static final String IPERF_OPTION_PORT_FLAG = "-p";
28     private static final String IPERF_OPTION_BANDWIDTH_FLAG = "-b";
29     private static final String IPERF_OPTION_INTERVAL_FLAG = "-i";
30     private static final String IPERF_OPTION_VERBOSE_FLAG = "-V";
31     private static final String IPERF_OPTION_UDP_FLAG = "-u";
32     private static final String IPERF_OPTION_JSON_OUTPUT_FLAG = "-J";
33     private static final String IPERF_OPTION_TIMEOUT_FLAG = "-t";
34     private static final String IPERF_OPTION_LOGFILE_FLAG = "--logfile";
35     private static final String IPERF_OPTION_TMPDIR_FLAG = "--tmpdir";
36     private static final int IPERF_OPTION_INTERVAL = 2;
37     // This is the max value supported by iperf3.
38     private static final int IPERF_OPTION_TIMEOUT = 86400;
39 
40     private final PMCMainActivity mPMCMainActivity;
41     private final ProcessBuilder mProcessBuilder;
42     private PowerManager.WakeLock mWakeLock;
43     private Process mProcess;
44     private File mLogFile;
45 
IperfClient(PMCMainActivity activity, String serverAddress, String serverPort, String bandWidthInMbps, String logFile)46     public IperfClient(PMCMainActivity activity, String serverAddress,
47             String serverPort, String bandWidthInMbps, String logFile) {
48         mPMCMainActivity = activity;
49         List<String> cmdList = new ArrayList<>();
50         cmdList.add(IPERF_COMMAND);
51         cmdList.add(IPERF_OPTION_VERBOSE_FLAG);
52         cmdList.add(IPERF_OPTION_UDP_FLAG);
53         cmdList.add(IPERF_OPTION_JSON_OUTPUT_FLAG);
54         cmdList.add(IPERF_OPTION_INTERVAL_FLAG);
55         cmdList.add(Integer.toString(IPERF_OPTION_INTERVAL));
56         cmdList.add(IPERF_OPTION_TIMEOUT_FLAG);
57         cmdList.add(Integer.toString(IPERF_OPTION_TIMEOUT));
58         cmdList.add(IPERF_OPTION_TMPDIR_FLAG);
59         cmdList.add(activity.getCacheDir().getPath());
60         if (serverAddress != null && serverAddress.length() > 0) {
61             cmdList.add(IPERF_OPTION_SERVER_FLAG);
62             cmdList.add(serverAddress);
63         }
64         if (serverPort != null && serverPort.length() > 0) {
65             cmdList.add(IPERF_OPTION_PORT_FLAG);
66             cmdList.add(serverPort);
67         }
68         if (bandWidthInMbps != null && bandWidthInMbps.length() > 0) {
69             cmdList.add(IPERF_OPTION_BANDWIDTH_FLAG);
70             cmdList.add(bandWidthInMbps);
71         }
72         if (logFile != null && logFile.length() > 0) {
73             mLogFile = new File(logFile);
74         }
75         mProcessBuilder = new ProcessBuilder(cmdList);
76     }
77 
78     /**
79      * Start the iperf client
80      */
startClient()81     public void startClient() {
82         Log.i(PMCMainActivity.TAG, "Starting iperf client: " + mProcessBuilder.command());
83         PowerManager pm = (PowerManager) mPMCMainActivity.getSystemService(Context.POWER_SERVICE);
84         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WIFITEST");
85         // Acquire the lock
86         mWakeLock.acquire();
87         try {
88             mProcessBuilder.redirectOutput(mLogFile);
89             mProcessBuilder.redirectError(mLogFile);
90             mProcess = mProcessBuilder.start();
91         } catch (Exception e) {
92             Log.e(PMCMainActivity.TAG, "Starting iperf client failed: " + e);
93             mPMCMainActivity.updateProgressStatus("Starting iperf client failed");
94         }
95     }
96 
97     /**
98      * Stop the iperf client
99      */
stopClient()100     public void stopClient() {
101         if (mProcess != null) {
102             Log.i(PMCMainActivity.TAG, "Stopping iperf client: " + mProcessBuilder.command());
103             try {
104                 mProcess.destroy();
105                 mProcess.waitFor();
106             } catch (Exception e) {
107                 Log.e(PMCMainActivity.TAG, "Stopping iperf client failed: " + e);
108             }
109             mWakeLock.release();
110             mProcess = null;
111         }
112     }
113 }
114