Android Open Source - android_network_discovery Rate Control






From Project

Back to project page android_network_discovery.

License

The source code is released under:

GNU General Public License

If you think the Android project android_network_discovery listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2009-2010 Aubort Jean-Baptiste (Rorist)
 * Licensed under GNU's GPL 2, see README
 *//*  ww  w.  j av  a  2  s .  com*/

package info.lamatricexiste.network.Network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;

public class RateControl {

    private final String TAG = "RateControl";
    private static final int BUF = 512;
    private final int REACH_TIMEOUT = 5000;
    private final String CMD = "/system/bin/ping -A -q -n -w 3 -W 2 -c 3 ";
    private final String PTN = "^rtt min\\/avg\\/max\\/mdev = [0-9\\.]+\\/[0-9\\.]+\\/([0-9\\.]+)\\/[0-9\\.]+ ms.*";
    private Pattern mPattern;
    private String line;
    public String indicator = null;
    public int rate = 800; // Slow start

    public RateControl() {
        mPattern = Pattern.compile(PTN);
    }

    public void adaptRate() {
        int response_time = 0;
        if ((response_time = getAvgResponseTime(indicator)) > 0) {
            if (response_time > 100) { // Most distanced hosts
                rate = response_time * 5; // Minimum 500ms
            } else {
                rate = response_time * 10; // Maximum 1000ms
            }
            if (rate > REACH_TIMEOUT) {
                rate = REACH_TIMEOUT;
            }
        }
    }

    private int getAvgResponseTime(String host) {
        // TODO: Reduce allocation
        BufferedReader reader = null;
        Matcher matcher;
        try {
            final Process proc = Runtime.getRuntime().exec(CMD + host);
            reader = new BufferedReader(new InputStreamReader(proc.getInputStream()), BUF);
            while ((line = reader.readLine()) != null) {
                matcher = mPattern.matcher(line);
                if (matcher.matches()) {
                    reader.close();
                    return (int) Float.parseFloat(matcher.group(1));
                }
            }
            reader.close();
        } catch (Exception e) {
            Log.e(TAG, "Can't use native ping: " + e.getMessage());
            try {
                final long start = System.nanoTime();
                if (InetAddress.getByName(host).isReachable(REACH_TIMEOUT)) {
                    Log.i(TAG, "Using Java ICMP request instead ...");
                    return (int) ((System.nanoTime() - start) / 1000);
                }
            } catch (Exception e1) {
                Log.e(TAG, e1.getMessage());
            }
        } finally {
            try {
            if (reader != null) {
                reader.close();
            }
            } catch(IOException e){
            }
        }
        return rate;
    }
}




Java Source Code List

info.lamatricexiste.network.AbstractDiscovery.java
info.lamatricexiste.network.ActivityDiscovery.java
info.lamatricexiste.network.ActivityMain.java
info.lamatricexiste.network.ActivityNet.java
info.lamatricexiste.network.ActivityPortscan.java
info.lamatricexiste.network.AsyncPortscan.java
info.lamatricexiste.network.DatabaseHelper.java
info.lamatricexiste.network.DefaultDiscovery.java
info.lamatricexiste.network.DiscoverActivity.java
info.lamatricexiste.network.DnsDiscovery.java
info.lamatricexiste.network.NetworkChange.java
info.lamatricexiste.network.Network.Banner.java
info.lamatricexiste.network.Network.DownloadFile.java
info.lamatricexiste.network.Network.HardwareAddress.java
info.lamatricexiste.network.Network.HostBean.java
info.lamatricexiste.network.Network.NetInfo.java
info.lamatricexiste.network.Network.OsFingerprint.java
info.lamatricexiste.network.Network.Ping.java
info.lamatricexiste.network.Network.RateControl.java
info.lamatricexiste.network.Network.SendSmbNegotiate.java
info.lamatricexiste.network.Utils.DbUpdate.java
info.lamatricexiste.network.Utils.Db.java
info.lamatricexiste.network.Utils.Export.java
info.lamatricexiste.network.Utils.Help.java
info.lamatricexiste.network.Utils.Prefs.java
info.lamatricexiste.network.Utils.Save.java
info.lamatricexiste.network.Utils.UpdateNicDb.java
info.lamatricexiste.network.connectivity.java
info.lamatricexiste.network.portscan.java
info.lamatricexiste.network.wifiinfo.java