package com.qiniu.android.storage.serverConfig;

import com.qiniu.android.common.AutoZone;
import com.qiniu.android.http.dns.DnsPrefetcher;
import com.qiniu.android.storage.GlobalConfiguration;
import com.qiniu.android.transaction.TransactionManager;

import java.util.List;

public class ServerConfigMonitor {
    private static final String TransactionKey = "ServerConfig";

    private String token;
    private ServerConfigCache cache = new ServerConfigCache();

    private int monitorTaskCount = 0;
    private static ServerConfigMonitor configMonitor = new ServerConfigMonitor();

    public static ServerConfigMonitor getInstance() {
        return configMonitor;
    }


    // 开始监控
    public synchronized void startMonitor() {
        TransactionManager transactionManager = TransactionManager.getInstance();
        boolean isExist = transactionManager.existTransactionsForName(TransactionKey);
        if (isExist) {
            return;
        }

        TransactionManager.Transaction transaction = new TransactionManager.Transaction(TransactionKey, 0, 10, new Runnable() {
            @Override
            public void run() {
                monitor();
            }
        });
        transactionManager.addTransaction(transaction);
    }

    // 停止监控
    public synchronized void endMonitor() {
        TransactionManager transactionManager = TransactionManager.getInstance();
        List<TransactionManager.Transaction> transactions = transactionManager.transactionsForName(TransactionKey);
        if (transactions != null) {
            for (TransactionManager.Transaction transaction : transactions) {
                transactionManager.removeTransaction(transaction);
            }
        }
    }

    private void monitor() {
        synchronized (this) {
            if (monitorTaskCount > 0) {
                return;
            }
            monitorTaskCount = 2;
        }

        final ServerConfig serverConfig = cache.getConfig();
        if (serverConfig == null || !serverConfig.isValid()) {
            ServerConfigSynchronizer.getServerConfigFromServer(new ServerConfigSynchronizer.ServerConfigHandler() {
                @Override
                public void handle(ServerConfig config) {
                    if (config == null) {
                        return;
                    }

                    // 清理 region 缓存
                    ServerConfig.RegionConfig regionConfig = config.getRegionConfig();
                    ServerConfig.RegionConfig oldRegionConfig = serverConfig != null ? serverConfig.getRegionConfig() : null;
                    if (regionConfig != null) {
                        if (oldRegionConfig != null && regionConfig.getClearId() > oldRegionConfig.getClearId() && regionConfig.getClearCache()) {
                            AutoZone.clearCache();
                        }
                    }

                    // dns 配置
                    ServerConfig.DnsConfig dnsConfig = config.getDnsConfig();
                    if (dnsConfig != null) {
                        if (dnsConfig.getEnable() != null) {
                            GlobalConfiguration.getInstance().isDnsOpen = dnsConfig.getEnable();
                        }

                        // 清理 dns 缓存
                        ServerConfig.DnsConfig oldDnsConfig = serverConfig != null ? serverConfig.getDnsConfig() : null;
                        if (oldDnsConfig != null && dnsConfig.getClearId() > oldDnsConfig.getClearId() && dnsConfig.getClearCache()) {
                            try {
                                DnsPrefetcher.getInstance().clearDnsCache();
                            } catch (Exception ignored) {
                            }
                        }

                        // udp 配置
                        ServerConfig.UdpDnsConfig udpDnsConfig = dnsConfig.getUdpDnsConfig();
                        if (udpDnsConfig != null) {
                            if (udpDnsConfig.getEnable() != null) {
                                GlobalConfiguration.getInstance().udpDnsEnable = udpDnsConfig.getEnable();;
                            }

                            ServerConfig.DnsServer ipv4Servers = udpDnsConfig.getIpv4Server();
                            if (ipv4Servers != null && ipv4Servers.getEnable() != null) {
                                if (ipv4Servers.getEnable()) {
                                    GlobalConfiguration.getInstance().udpDnsIpv4Servers = ipv4Servers.getServers();
                                } else {
                                    GlobalConfiguration.getInstance().udpDnsIpv4Servers = new String[]{};
                                }
                            }

                            ServerConfig.DnsServer ipv6Servers = udpDnsConfig.getIpv6Server();
                            if (ipv6Servers != null && ipv6Servers.getEnable() != null) {
                                if (ipv6Servers.getEnable()) {
                                    GlobalConfiguration.getInstance().udpDnsIpv6Servers = ipv6Servers.getServers();
                                } else {
                                    GlobalConfiguration.getInstance().udpDnsIpv6Servers = new String[]{};
                                }
                            }
                        }

                        // doh 配置
                        ServerConfig.DohDnsConfig dohConfig = dnsConfig.getDohDnsConfig();
                        if (dohConfig != null) {
                            if (dohConfig.getEnable() != null) {
                                GlobalConfiguration.getInstance().dohEnable = dohConfig.getEnable();
                            }

                            ServerConfig.DnsServer ipv4Servers = dohConfig.getIpv4Server();
                            if (ipv4Servers != null && ipv4Servers.getEnable() != null) {
                                if (ipv4Servers.getEnable()) {
                                    GlobalConfiguration.getInstance().dohIpv4Servers = ipv4Servers.getServers();
                                } else {
                                    GlobalConfiguration.getInstance().dohIpv4Servers = new String[]{};
                                }
                            }

                            ServerConfig.DnsServer ipv6Servers = dohConfig.getIpv6Server();
                            if (ipv6Servers != null && ipv6Servers.getEnable() != null) {
                                if (ipv6Servers.getEnable()) {
                                    GlobalConfiguration.getInstance().dohIpv6Servers = ipv6Servers.getServers();
                                } else {
                                    GlobalConfiguration.getInstance().dohIpv6Servers = new String[]{};
                                }
                            }
                        }
                    }

                    cache.setConfig(config);
                    synchronized (ServerConfigMonitor.this) {
                        monitorTaskCount--;
                    }
                }
            });
        }


        ServerUserConfig serverUserConfig = cache.getUserConfig();
        if (serverUserConfig == null || !serverUserConfig.isValid()) {
            ServerConfigSynchronizer.getServerUserConfigFromServer(new ServerConfigSynchronizer.ServerUserConfigHandler() {
                @Override
                public void handle(ServerUserConfig config) {
                    if (config.getNetworkCheckEnable() != null) {
                        GlobalConfiguration.getInstance().connectCheckEnable = config.getNetworkCheckEnable();
                    }
                    cache.setUserConfig(config);
                    synchronized (ServerConfigMonitor.this) {
                        monitorTaskCount--;
                    }
                }
            });
        }
    }
}

@@ -386,20 +386,29 @@
 ate 
-String token
+boolean enable = true
 ;%0A  
@@ -471,47 +471,8 @@
 e();
-%0A%0A    private int monitorTaskCount = 0;
 %0A   
@@ -573,72 +573,183 @@
 tic 
-ServerConfigMonitor getInstance() %7B%0A        return configMonitor
+void setEnable(boolean enable) %7B%0A        configMonitor.enable = enable;%0A    %7D%0A%0A    public static void setToken(String token) %7B%0A        ServerConfigSynchronizer.setToken(token)
 ;%0A  
@@ -749,25 +749,24 @@
 en);%0A    %7D%0A%0A
-%0A
     // %E5%BC%80%E5%A7%8B%E7%9B%91%E6%8E%A7%0A
@@ -785,24 +785,31 @@
 ynchronized 
+static 
 void startMo
@@ -810,32 +810,100 @@
 tartMonitor() %7B%0A
+        if (!configMonitor.enable) %7B%0A            return;%0A        %7D%0A%0A
         Transact
@@ -1303,24 +1303,38 @@
             
+configMonitor.
 monitor();%0A 
@@ -1457,16 +1457,23 @@
 ronized 
+static 
 void end
@@ -1935,140 +1935,41 @@
     
-synchronized (this) %7B%0A            if (monitorTaskCount %3E 0) %7B%0A                return;%0A            %7D%0A            monitorTaskCount = 2
+if (!enable) %7B%0A            return
 ;%0A  
@@ -4153,16 +4153,49 @@
 nable();
+%0A                                
 ;%0A      
@@ -6999,136 +6999,8 @@
 g);%0A
-                    synchronized (ServerConfigMonitor.this) %7B%0A                        monitorTaskCount--;%0A                    %7D%0A
     
@@ -7643,136 +7643,8 @@
 g);%0A
-                    synchronized (ServerConfigMonitor.this) %7B%0A                        monitorTaskCount--;%0A                    %7D%0A
     

optimize server config monitor