package ei.engine.network;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;

import ei.engine.network.message.type.ResponseRequestMessage;
import ei.engine.network.response.ResponseEvent;

public class NioClient extends NioNetwork{
	private SocketChannel socket;
	
	public NioClient(InetAddress hostAddress, int port) throws IOException {
		super(hostAddress, port, CLIENT);
		socket = initiateConnection(new InetSocketAddress(hostAddress, port));
	}

	protected Selector initSelector() throws IOException {
		// Create a new selector
		return SelectorProvider.provider().openSelector();
	}
	
	/**
	 * This method is for the Client to send a message to the server
	 * 
	 * @param handler The response handler
	 * @param data The data to send
	 * @throws IOException
	 */
	public void send(ResponseEvent handler, ResponseRequestMessage data) throws IOException {
		send(socket, handler, data);
	}
}
