package org.zstack.console;

import org.springframework.beans.factory.annotation.Autowired;
import org.zstack.core.cloudbus.CloudBusCallBack;
import org.zstack.core.config.GlobalConfig;
import org.zstack.core.config.GlobalConfigUpdateExtensionPoint;
import org.zstack.core.tacker.PingTracker;
import org.zstack.header.console.ConsoleBackend;
import org.zstack.header.console.PingConsoleProxyAgentMsg;
import org.zstack.header.console.PingConsoleProxyAgentReply;
import org.zstack.header.console.ReconnectConsoleProxyMsg;
import org.zstack.header.message.MessageReply;
import org.zstack.header.message.NeedReplyMessage;
import org.zstack.utils.Utils;
import org.zstack.utils.logging.CLogger;

/**
 * Created by xing5 on 2016/4/8.
 */
public class ConsoleProxyAgentTracker extends PingTracker {
    private static final CLogger logger = Utils.getLogger(ConsoleProxyAgentTracker.class);

    @Autowired
    private ConsoleManager cmgr;

    @Override
    public String getResourceName() {
        return "console proxy agent";
    }

    @Override
    public NeedReplyMessage getPingMessage(String resUuid) {
        PingConsoleProxyAgentMsg msg = new PingConsoleProxyAgentMsg();
        msg.setAgentUuid(resUuid);

        ConsoleBackend bkd = cmgr.getConsoleBackend();
        msg.setServiceId(bkd.returnServiceIdForConsoleAgentMsg(msg, resUuid));
        return msg;
    }

    @Override
    public int getPingInterval() {
        return ConsoleGlobalConfig.PING_INTERVAL.value(Integer.class);
    }

    @Override
    public int getParallelismDegree() {
        return Integer.MAX_VALUE;
    }

    @Override
    protected void startHook() {
        ConsoleGlobalConfig.PING_INTERVAL.installLocalUpdateExtension(new GlobalConfigUpdateExtensionPoint() {
            @Override
            public void updateGlobalConfig(GlobalConfig oldConfig, GlobalConfig newConfig) {
                pingIntervalChanged();
            }
        });
    }

    @Override
    public void handleReply(final String resourceUuid, MessageReply reply) {
        if (!reply.isSuccess()) {
            //TODO
            logger.warn(String.format("unable to ping the console proxy agent[uuid:%s], %s", resourceUuid, reply.getError()));
            return;
        }

        PingConsoleProxyAgentReply pr = reply.castReply();
        if (pr.isDoReconnect()) {
            ReconnectConsoleProxyMsg rmsg = new ReconnectConsoleProxyMsg();
            rmsg.setAgentUuid(resourceUuid);

            ConsoleBackend bkd = cmgr.getConsoleBackend();
            rmsg.setServiceId(bkd.returnServiceIdForConsoleAgentMsg(rmsg, resourceUuid));
            bus.send(rmsg, new CloudBusCallBack(null) {
                @Override
                public void run(MessageReply reply) {
                    if (!reply.isSuccess()) {
                        logger.warn(String.format("failed to reconnect console proxy agent[uuid:%s], %s", resourceUuid,
                                reply.getError()));
                    } else {
                        logger.info(String.format("successfully reconnected the console proxy agent[uuid:%s]", resourceUuid));
                    }
                }
            });
        }
    }
}

@@ -685,16 +685,55 @@
 ogger;%0A%0A
+import java.util.concurrent.TimeUnit;%0A%0A
 /**%0A * C
@@ -2102,27 +2102,8 @@
 ) %7B%0A
-            //TODO%0A
     
@@ -2217,32 +2217,203 @@
 y.getError()));%0A
+            if (timeToReconnect()) %7B%0A                logger.debug(%22It's time to reconnect console proxy agent.%22);%0A                doReconnect(resourceUuid);%0A            %7D%0A
             retu
@@ -2516,32 +2516,136 @@
 onnect()) %7B%0A    
+        doReconnect(resourceUuid);%0A        %7D%0A    %7D%0A%0A    private void doReconnect(String resourceUuid) %7B%0A
         Reconnec
@@ -2700,36 +2700,32 @@
 yMsg();%0A        
-   
- 
 rmsg.setAgentUui
@@ -2738,28 +2738,24 @@
 urceUuid);%0A%0A
-    
         Cons
@@ -2801,28 +2801,24 @@
 ();%0A        
-  
-  
 rmsg.setServ
@@ -2891,20 +2891,16 @@
         
-    
 bus.send
@@ -2939,36 +2939,32 @@
 ) %7B%0A            
-  
-  
 @Override%0A      
@@ -2949,36 +2949,32 @@
       @Override%0A
-    
             publ
@@ -3019,28 +3019,24 @@
             
- 
-   
 if (!reply.i
@@ -3041,36 +3041,32 @@
 .isSuccess()) %7B%0A
-    
                 
@@ -3193,20 +3193,16 @@
         
-    
 reply.ge
@@ -3205,36 +3205,32 @@
 y.getError()));%0A
-    
                 
@@ -3254,28 +3254,24 @@
             
-    
 logger.info(
@@ -3373,34 +3373,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -3405,25 +3405,180 @@
     
-    %7D);%0A        %7D
+%7D);%0A    %7D%0A%0A    private boolean timeToReconnect() %7B%0A        return System.currentTimeMillis() %25 TimeUnit.MINUTES.toMillis(30) %3C TimeUnit.SECONDS.toMillis(getPingInterval());
 %0A   

reconnect every 30 minutes Resolves ZSTAC-20721