package org.komamitsu.fluency.sender.heartbeat;

import org.komamitsu.fluency.sender.SSLSocketBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLSocket;

import java.io.IOException;

public class SSLHeartbeater
        extends Heartbeater
{
    private static final Logger LOG = LoggerFactory.getLogger(SSLHeartbeater.class);
    private final Config config;
    private final SSLSocketBuilder sslSocketBuilder;

    protected SSLHeartbeater(final Config config)
            throws IOException
    {
        super(config.getBaseConfig());
        this.config = config;
        sslSocketBuilder = new SSLSocketBuilder(
                config.getHost(),
                config.getPort(),
                // TODO: Make these configurable
                5000, 5000);
    }

    @Override
    protected void invoke()
            throws IOException
    {
        SSLSocket sslSocket = null;
        try {
            sslSocket = sslSocketBuilder.build();
            LOG.trace("SSLHeartbeat: remotePort={}, localPort={}", sslSocket.getPort(), sslSocket.getLocalPort());
            // Try SSL handshake
            sslSocket.getSession();
            pong();
        }
        finally {
            if (sslSocket != null) {
                sslSocket.close();
            }
        }
    }

    @Override
    public String toString()
    {
        return "SSLHeartbeater{" +
                "config=" + config +
                ", sslSocketBuilder=" + sslSocketBuilder +
                "} " + super.toString();
    }

    public static class Config
            implements Instantiator
    {
        private final Heartbeater.Config baseConfig = new Heartbeater.Config();

        public Heartbeater.Config getBaseConfig()
        {
            return baseConfig;
        }

        public String getHost()
        {
            return baseConfig.getHost();
        }

        public Config setHost(String host)
        {
            baseConfig.setHost(host);
            return this;
        }

        public int getPort()
        {
            return baseConfig.getPort();
        }

        public Config setPort(int port)
        {
            baseConfig.setPort(port);
            return this;
        }

        public int getIntervalMillis()
        {
            return baseConfig.getIntervalMillis();
        }

        public Config setIntervalMillis(int intervalMillis)
        {
            baseConfig.setIntervalMillis(intervalMillis);
            return this;
        }

        @Override
        public SSLHeartbeater createInstance()
                throws IOException
        {
            return new SSLHeartbeater(this);
        }
    }
}


@@ -739,67 +739,78 @@
     
-// TODO: Make these configurable%0A                5000, 5000
+config.connectionTimeoutMilli,%0A                config.readTimeoutMilli
 );%0A 
@@ -1709,16 +1709,112 @@
 onfig();
+%0A        private int connectionTimeoutMilli = 5000;%0A        private int readTimeoutMilli = 5000;
 %0A%0A      
@@ -2518,32 +2518,32 @@
 llis)%0A        %7B%0A
-
             base
@@ -2612,32 +2612,594 @@
 his;%0A        %7D%0A%0A
+        public int getConnectionTimeoutMilli()%0A        %7B%0A            return connectionTimeoutMilli;%0A        %7D%0A%0A        public Config setConnectionTimeoutMilli(int connectionTimeoutMilli)%0A        %7B%0A            this.connectionTimeoutMilli = connectionTimeoutMilli;%0A            return this;%0A        %7D%0A%0A        public int getReadTimeoutMilli()%0A        %7B%0A            return readTimeoutMilli;%0A        %7D%0A%0A        public Config setReadTimeoutMilli(int readTimeoutMilli)%0A        %7B%0A            this.readTimeoutMilli = readTimeoutMilli;%0A            return this;%0A        %7D%0A%0A
         @Overrid

Make connection/read timeout configurable in SSLHeartbeater