package com.imofan.android.pns;

import javax.net.SocketFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Created with IntelliJ IDEA.
 * User: Rex
 * Date: 12-9-12
 * Time: 上午10:17
 * To change this template use File | Settings | File Templates.
 */
class XmppSocketFactory extends SocketFactory {
    private static SocketFactory defaultFactory = SocketFactory.getDefault();

    private Socket socket;
    @Override
    public Socket createSocket(String arg0, int arg1) throws IOException,
            UnknownHostException {
        socket = defaultFactory.createSocket(arg0, arg1);
        setSockOpt(socket);
        return socket;
    }

    @Override
    public Socket createSocket(InetAddress host, int port) throws IOException {
        socket = defaultFactory.createSocket(host, port);
        setSockOpt(socket);
        return socket;
    }

    @Override
    public Socket createSocket(String host, int port, InetAddress localHost,
                               int localPort) throws IOException, UnknownHostException {
        socket = defaultFactory.createSocket(host, port, localHost, localPort);
        setSockOpt(socket);
        return socket;
    }

    @Override
    public Socket createSocket(InetAddress address, int port,
                               InetAddress localAddress, int localPort) throws IOException {
        socket = defaultFactory.createSocket(address, port, localAddress, localPort);
        setSockOpt(socket);
        return socket;
    }

    private static void setSockOpt(Socket socket) throws IOException {
        socket.setKeepAlive(false);
        // Set sockek timeout to2 hours, should be more then the ping interval
        // to avoid Exceptions on read()
        socket.setSoTimeout(120*60*100);
        socket.setTcpNoDelay(false);
    }
}
