package upem.net.tcp.nonblocking;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Set;

public class ServerSumOneShot {

    private final ServerSocketChannel serverSocketChannel;
    private final Selector selector;
    private final Set<SelectionKey> selectedKeys;

    public ServerSumOneShot(int port) throws IOException {
        serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(port));
        selector = Selector.open();
        selectedKeys = selector.selectedKeys();
    }

    public void launch() throws IOException {
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        Set<SelectionKey> selectedKeys = selector.selectedKeys();
        while (!Thread.interrupted()) {
            selector.select();
            processSelectedKeys();
            selectedKeys.clear();
        }
    }

    private void processSelectedKeys() throws IOException {
        for (SelectionKey key : selectedKeys) {
            if (key.isValid() && key.isAcceptable()) {
                doAccept(key);
            }
            try {
                if (key.isValid() && key.isWritable()) {
                 doWrite(key);
                }
                if (key.isValid() && key.isReadable()) {
                 doRead(key);
                }

            } catch (IOException e) {
                key.attach(null);
                silentlyClose(key.channel());
            }
        }
    }


    private void doRead(SelectionKey key) throws IOException {
        SocketChannel sc = (SocketChannel) key.channel();
        ByteBuffer bb = (ByteBuffer) key.attachment();
        sc.read(bb);
        if (!bb.hasRemaining()) {
            key.interestOps(SelectionKey.OP_WRITE);
        }
    }

    private void doWrite(SelectionKey key) throws IOException {
        SocketChannel sc = (SocketChannel) key.channel();
        ByteBuffer bb = (ByteBuffer) key.attachment();
        bb.flip();
        sc.write(bb);
        if (!bb.hasRemaining()){
            silentlyClose(sc);
            key.attach(null);
        }
        bb.compact();
    }

    private void silentlyClose(Closeable sc) {
        if (sc!=null) {
            try {
                sc.close();
            } catch (Exception e) {
                // Ignore exceptions
            }
        }
    }

    private void doAccept(SelectionKey key) throws IOException {
        SocketChannel sc = serverSocketChannel.accept();
        sc.configureBlocking(false);
        sc.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(8));
    }

    public static void main(String[] args) throws NumberFormatException, IOException {
        new ServerSumOneShot(Integer.parseInt(args[0])).launch();

    }
}
