package org.mortbay.jetty.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.LinkedList;

import org.mortbay.io.nio.ChannelEndPoint;

public class HttpDestination
{
    private InetSocketAddress _address;
    private ArrayList<HttpConnection> _connections=new ArrayList<HttpConnection>();
    private LinkedList<HttpExchange> _exchanges=new LinkedList<HttpExchange>();
    private HttpConnectionPool _pool;

    /* ------------------------------------------------------------ */
    HttpDestination(HttpConnectionPool pool, InetSocketAddress address)
    {
        _pool=pool;
        _address=address;
    }

    /* ------------------------------------------------------------ */
    public InetSocketAddress getAddress()
    {
        return _address;
    }

    /* ------------------------------------------------------------------------------- */
    public HttpConnection getConnection(boolean ssl, boolean blockForIdle) throws UnknownHostException, IOException
    {
        HttpConnection connection=null; // TODO search for existing idle connection

        // If a connection doesn't exist, create a new one.
        if (connection==null)
        {
            return newConnection(ssl);
        }
        else
        {
            return connection;
        }
    }

    /* ------------------------------------------------------------------------------- */
    public HttpConnection newConnection(boolean ssl) throws UnknownHostException, IOException
    {
        SocketChannel channel=SocketChannel.open();
        channel.configureBlocking(false);
        channel.connect(_address);

        // TODO return REAL http connection
        return new HttpConnection(this,_pool,new ChannelEndPoint(channel),_pool.getHeaderBufferSize(),_pool.getRequestBufferSize());

    }

    /* ------------------------------------------------------------ */
    public void send(HttpExchange ex) throws IOException
    {
        _exchanges.add(ex);

        // TODO schedule stuff

    }

    /* ------------------------------------------------------------ */
    public void setAddress(InetSocketAddress address)
    {
        _address=address;
    }
}