
interface EventHandler {
	public void processEvent(SelectionKey key) throws IOException;
}	

class ConcurrentProxyBySelector2 implements EventHandler {
	private final ServerSocketChannel ssc;
	private final InetSocketAddress server;
	private final Selector sel;

	public ConcurrentProxyBySelector2(int localPort, String remoteHost, int remotePort) throws IOException {
		ssc = ServerSocketChannel.open();
		ssc.socket().bind(new InetSocketAddress(localPort));
		System.out.println("Proxy starts on " + ssc);
		server = new InetSocketAddress(remoteHost, remotePort);
		sel = Selector.open();
	}
	public void launch() throws IOException {
		ssc.configureBlocking(false);
		ssc.register(sel, SelectionKey.OP_ACCEPT, this);
		Set<SelectionKey> selectedKey = sel.selectedKeys();
		while(!Thread.interrupted()) {
			sel.select();
			for(SelectionKey key : selectedKey) {
				((EventHandler)(key.attachment())).processEvent(key);
			}
			selectedKey.clear();
		}
	}
	public void processEvent(SelectionKey key) throws IOException {
		if (key.isAcceptable()) 
			doAccept(key);
	}
	private void doAccept(SelectionKey key) throws IOException {
		//TODO
	}
	
	
	private class ClientEventHandler implements EventHandler {
		private static final int BB_SIZE = 8;
		private final ByteBuffer bb;
		private final SocketChannel scIn;
		private final SocketChannel scOut;
		
		//TODO
	}
}