All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yy.httpdns.DnsClient Maven / Gradle / Ivy

package com.yy.httpdns;

import android.content.Context;
import android.content.IntentFilter;

import com.yy.httpdns.bean.DnsKey;
import com.yy.httpdns.bean.DomainName;
import com.yy.httpdns.httpdns.DnsThread;
import com.yy.httpdns.httpdns.NetworkBroadcastReceiver;
import com.yy.httpdns.log.DnsLog;
import com.yy.httpdns.util.NetworkUtil;
import com.yy.httpdns.util.SharedPreferencesUtil;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by huangzhilong on 2016/8/11.
 */
public class DnsClient implements NetworkBroadcastReceiver.NetEventHandle {
    public static final String TAG = "DnsClient";
    private String dnsService;
    private Context context;
    private HashMap resultMap = new HashMap<>();
    private DnsThread dnsThread;
    private Map domainMap;//存储每个host信息
    private NetworkBroadcastReceiver networkBroadcastReceiver;

    public DnsClient(Config config) {
        this.dnsService = config.getDnsService();
        this.context = config.getContext();
        this.domainMap = config.getDomainMap();
        initDataFromSharePref();

        registerReceiver(context);//广播注册会触发onReceive
        networkBroadcastReceiver.setNetEventHandle(this);
    }

    private void initDataFromSharePref() {
        Set keys = domainMap.keySet();
        Iterator iterator = keys.iterator();
        while (iterator.hasNext()) {
            String hostName = (String) iterator.next();
            DnsKey dnsKey = getDnsKey(hostName);
            if (dnsKey == null) {
                continue;
            }
            String ip = SharedPreferencesUtil.get(context, dnsKey.getSharePreKey());
            if (ip != null) {
                resultMap.put(dnsKey, ip);
                DnsLog.i(TAG, "init data from sharePre " + hostName + "  " + ip);
            }
        }
    }

    public void startHttpDns() {
        dnsThread = new DnsThread(dnsService, domainMap);
        dnsThread.setThreadCallback(new DnsThread.ThreadCallback() {
            @Override
            public void finish(String hostName, String fastIP) {
                if (fastIP != null) {
                    handlerResult(hostName, fastIP);
                }
            }
        });
        dnsThread.start();
    }

    @Override
    public void netHandler() {
        stopDnsThread();
        if (checkHostInInterval()) {
            DnsLog.i(TAG, "checkHostInInterval true start HttpDns");
            startHttpDns();
        }
    }

    private boolean checkHostInInterval() {
        Set keys = domainMap.keySet();
        Iterator iterator = keys.iterator();
        while (iterator.hasNext()) {
            String hostName = (String) iterator.next();
            DomainName domainName = domainMap.get(hostName);
            if (domainName != null && domainName.checkInterval()) {
                return true;
            }
        }
        DnsLog.i(TAG, "checkHostInInterval false");
        return false;
    }

    private void handlerResult(String host, String fastestIP) {
        DnsKey dnsKey = getDnsKey(host);
        if (dnsKey == null) {
            return;
        }
        if (resultMap.get(dnsKey) != null) {
            resultMap.remove(dnsKey);
        }
        resultMap.put(dnsKey, fastestIP);
        SharedPreferencesUtil.put(context, dnsKey.getSharePreKey(), fastestIP);
        DnsLog.i(TAG, "storage success data " + host + " " + fastestIP + " resultMap size: " + resultMap.size());
    }

    public String lookup(String host) {
        String ip = null;
        DnsKey dnsKey = getDnsKey(host);
        //TYPE_NOT_NETWORK情况
        if (dnsKey != null) {
            ip = resultMap.get(dnsKey);
            if (ip == null) {
                ip = SharedPreferencesUtil.get(context, dnsKey.getSharePreKey());
            }
        }
        if (ip == null) {
            ip = host;
        }
        DnsLog.i(TAG, host + " lookup fastIP: " + ip);
        return ip;
    }

    private DnsKey getDnsKey(String host) {
        int netType = NetworkUtil.getNetworkStatus(context);
        DnsKey dnsKey = null;
        if (netType == NetworkUtil.TYPE_WIFI) {
            String wifSSID = NetworkUtil.getWifiSSID(context);
            if (wifSSID != null) {
                dnsKey = new DnsKey(netType, host, wifSSID);
            }
        } else if (netType == NetworkUtil.TYPE_MOBILE) {
            int mobileType = NetworkUtil.getMobileNetType(context);
            if( mobileType != NetworkUtil.TYPE_UNKNOWN) {
                dnsKey = new DnsKey(netType, host, mobileType);
            }
        }
        return dnsKey;
    }

    private void registerReceiver(Context context) {
        networkBroadcastReceiver = new NetworkBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        context.registerReceiver(networkBroadcastReceiver, intentFilter);
    }

    private void stopDnsThread() {
        if (dnsThread != null) {
            dnsThread.setFlag(true);
            dnsThread.setThreadCallback(null);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy