package org.kne.cloud.network.klalb;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.net.SocketOption;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.Set;

public class KLALBVirtualServerSocketChannel extends ServerSocketChannel {
	private KLALBController controller;

	private KLALBVirtualServerSocket serversocket;
	
	protected KLALBVirtualServerSocketChannel(KLALBController controller) throws IOException {
		super(null);
		this.controller=controller;
		this.serversocket=new KLALBVirtualServerSocket(controller);
		serversocket.channel=this;
	}

	public KLALBController getController() {
		return controller;
	}
	public static ServerSocketChannel open(KLALBController controller2) throws IOException {
		return new KLALBVirtualServerSocketChannel(controller2);
	}
	@Override
	public <T> T getOption(SocketOption<T> name) throws IOException {
		// TODO 自动生成的方法存根
		return null;
	}

	@Override
	public Set<SocketOption<?>> supportedOptions() {
		// TODO 自动生成的方法存根
		return null;
	}

	@Override
	public ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException {
		serversocket.bind(local, backlog);
		return this;
	}

	@Override
	public <T> ServerSocketChannel setOption(SocketOption<T> name, T value) throws IOException {
		// TODO 自动生成的方法存根
		return null;
	}

	@Override
	public ServerSocket socket() {
		return serversocket;
	}

	@Override
	public SocketChannel accept() throws IOException {
		KLALBVirtualSocket sock=serversocket.accept();
		return new KLALBVirtualSocketChannel(controller,sock);
	}

	@Override
	public SocketAddress getLocalAddress() throws IOException {
		return serversocket.getLocalSocketAddress();
	}

	@Override
	protected void implCloseSelectableChannel() throws IOException {
		serversocket.close();
	}

	@Override
	protected void implConfigureBlocking(boolean block) throws IOException {
		if(!block) {
			throw new UnsupportedOperationException("Please use VirtualThread to configure Asynchronous IO");
		}
	}

	

}
