package free.yonghu.java.misc;

import junit.framework.TestCase;

import java.net.InetSocketAddress;

import free.yonghu.phone.logging.MyLog;

/**
 * Created by Yong Hu on 2016/4/2.
 */
public class PerfServiceTest extends TestCase {
    private static final String TAG = "PerfServiceTest";

    @Override
    public void setUp() throws Exception {
        super.setUp();
        MyLog.createAndroidLikeConsoleLogger("PerfServiceTest");
    }

    public void testName() throws Exception {
        String[] args = {"-r ", "192.168.1.105:12345", "-w", "192.168.1.105:22345", "10000000"};
        MyLog.createAndroidLikeConsoleLogger("PerfService");
        StringBuilder builder = new StringBuilder("Input: ");
        for (String arg : args) {
            builder.append(arg).append(" ");
        }
        MyLog.d(TAG, builder.toString());

        try {
            InetSocketAddress readAddr = null;
            InetSocketAddress writeAddr = null;
            int Bps = 0;
            for (int i = 0; i < args.length; i++) {
                if ("-r".equals(args[i])) {
                    readAddr = getSocketAddress(args[++i]);
                } else if ("-w".equals(args[i])) {
                    writeAddr = getSocketAddress(args[++i]);
                    Bps = Integer.valueOf(args[++i]);
                }
            }

            if (readAddr != null || (writeAddr != null && Bps > 0)) {
                PerfService ps = new PerfService();
                Thread.sleep(100);
                if (readAddr != null) {
                    ps.startUdpRead(readAddr);
                }
                if (writeAddr != null && Bps > 0) {
                    ps.startUdpWrite(writeAddr, Bps);
                }
                ps.startMonitor();
            } else {
                MyLog.i(TAG, "Quit! Invalid parameters");
            }
            Thread.sleep(100 * 1000);
        } catch (Exception e) {
            MyLog.e(TAG, "main: error", e);
            e.printStackTrace();
        }
    }
    private static InetSocketAddress getSocketAddress(String input) throws Exception {
        String s[] = input.split(":");
        return new InetSocketAddress(s[0], Integer.valueOf(s[1]));
    }
}