package org.kne.cloud.network.klalb;

import java.io.IOException;
import java.net.*;
import java.nio.channels.SocketChannel;

import org.kne.cloud.network.VirtualSocket;
import org.kne.cloud.network.VirtualSocketImpl;

public class KLALBVirtualSocket extends VirtualSocket {

	@Override
	protected KLALBVirtualSocketImpl getVirtualImpl() {
		return (KLALBVirtualSocketImpl) super.getVirtualImpl();
	}

	private KLALBController controller;
	protected SocketChannel channel;

	public KLALBVirtualSocket(KLALBController controler) throws SocketException {
		super(controler.createVirtualSocketImpl());
		this.controller = controler;
	}

	protected KLALBController getController() {
		return controller;
	}

	public KLALBVirtualSocket(KLALBController controler,String host, int port) throws UnknownHostException, IOException {
		this(controler,host != null ? new InetSocketAddress(host, port)
				: new InetSocketAddress(InetAddress.getByName(null), port), (SocketAddress) null);
	}

	public KLALBVirtualSocket(KLALBController controler,SocketAddress address, SocketAddress localAddr) throws IOException {
		this(controler);
		  // backward compatibility
        if (address == null)
            throw new NullPointerException();

        try {
            if (localAddr != null)
                bind(localAddr);
            connect(address);
        } catch (IOException | IllegalArgumentException | SecurityException e) {
            try {
                close();
            } catch (IOException ce) {
                e.addSuppressed(ce);
            }
            throw e;
        }
	}

	public KLALBVirtualSocket(KLALBController controler,String host, int port, InetAddress localAddr, int localPort) throws IOException {
		this(controler,host != null ? new InetSocketAddress(host, port)
				: new InetSocketAddress(InetAddress.getByName(null), port),
				new InetSocketAddress(localAddr, localPort));
	}

	public KLALBVirtualSocket(KLALBController controler,InetAddress address, int port, InetAddress localAddr, int localPort) throws IOException {
		this(controler,address != null ? new InetSocketAddress(address, port) : null,
				new InetSocketAddress(localAddr, localPort));
	}
	
	public KLALBVirtualSocket(KLALBController controler,InetAddress address, int port) throws IOException {
        this(controler,address != null ? new InetSocketAddress(address, port) : null,
             (SocketAddress) null);
    }

	public void setCompress(int i) {
		((KLALBVirtualSocketImpl)getVirtualImpl()).setCompress(i);
	}
	public int setCompress() {
		return ((KLALBVirtualSocketImpl)getVirtualImpl()).getCompress();
	}


	
	@Override
	public SocketChannel getChannel() {
		return channel;
	}

	public boolean isConnectionPending() {
		return ((KLALBVirtualSocketImpl)getVirtualImpl()).isConnectionPending();
	}

	
	
	
}