// Path: src/main/java/com/biasedbit/hotpotato/client/connection/HttpConnection.java
// public interface HttpConnection extends ChannelHandler {
// 
//     /**
//      * Shut down the connection, cancelling all requests executing meanwhile.
//      * <p/>
//      * After a connection is shut down, no more requests will be accepted.
//      */
//     void terminate();
// 
//     /**
//      * Returns the unique identifier of this connection.
//      *
//      * @return An identifier of the connection.
//      */
//     String getId();
// 
//     /**
//      * Returns the host address to which this connection is connected to.
//      *
//      * @return The host address to which this connection is currently connected to.
//      */
//     String getHost();
// 
//     /**
//      * Returns the port to which this connection is connected to.
//      *
//      * @return The port of to which this connection is connected to.
//      */
//     int getPort();
// 
//     /**
//      * Returns whether this connection is available to process a request. Connections that execute HTTP 1.0 requests
//      * will <strong>never</strong> return true after the request has been approved for processing as the socket will
//      * be closed by the server.
//      *
//      * @return true if this connection is connected and ready to execute a request
//      */
//     boolean isAvailable();
// 
//     /**
//      * Execute a given request context in this connection. All calls to this method should first test whether this
//      * connection is available or not by calling {@link #isAvailable()} first. If this method is called while
//      * {@link #isAvailable()} would return {@code false}, then implementations will instantly cause a failure on the
//      * request with the reason {@link com.biasedbit.hotpotato.request.HttpRequestFuture#EXECUTION_REJECTED} and return
//      * {@code true}, meaning the request was consumed (and failed).
//      * <p/>
//      * The exception to the above rule is when the request is submitted and the connection goes down meanwhile. In this
//      * case, the request <strong>will not</strong> be marked as failed and this method will return {@code false} so that
//      * the caller may retry the same request in another connection.
//      * <p/>
//      * You should always, <strong>always</strong> test first with {@link #isAvailable()}.
//      * <p/>
//      * This is a non-blocking call.
//      *
//      * @param context Request execution context.
//      *
//      * @return {@code true} if the request was accepted, {@code false} otherwise. If a request is accepted, the
//      * {@code HttpConnection} becomes responsible for calling {@link
//      * com.biasedbit.hotpotato.request.HttpRequestFuture#setFailure(Throwable) setFailure()} or {@link
//      * com.biasedbit.hotpotato.request.HttpRequestFuture#setSuccess(Object,
//      * org.jboss.netty.handler.codec.http.HttpResponse) setSuccess()} on it.
//      */
//     boolean execute(HttpRequestContext context);
// }


import com.biasedbit.hotpotato.client.connection.HttpConnection;
import java.util.Collection;
import java.util.LinkedList;
/*
 * Copyright 2010 Bruno de Carvalho
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.biasedbit.hotpotato.client;



/**
 * Helper class to hold both active connections and the number of connections opening to a given host.
 *
 * This class is not thread-safe and should only be updated by a thread at a time unless manual external synchronization
 * is used.
 *
 * @author <a href="http://bruno.biasedbit.com/">Bruno de Carvalho</a>
 */
public class ConnectionPool {

    // internal vars --------------------------------------------------------------------------------------------------

    private boolean connectionFailures;
    private int connectionsOpening;
// Path: src/main/java/com/biasedbit/hotpotato/client/connection/HttpConnection.java
// public interface HttpConnection extends ChannelHandler {
// 
//     /**
//      * Shut down the connection, cancelling all requests executing meanwhile.
//      * <p/>
//      * After a connection is shut down, no more requests will be accepted.
//      */
//     void terminate();
// 
//     /**
//      * Returns the unique identifier of this connection.
//      *
//      * @return An identifier of the connection.
//      */
//     String getId();
// 
//     /**
//      * Returns the host address to which this connection is connected to.
//      *
//      * @return The host address to which this connection is currently connected to.
//      */
//     String getHost();
// 
//     /**
//      * Returns the port to which this connection is connected to.
//      *
//      * @return The port of to which this connection is connected to.
//      */
//     int getPort();
// 
//     /**
//      * Returns whether this connection is available to process a request. Connections that execute HTTP 1.0 requests
//      * will <strong>never</strong> return true after the request has been approved for processing as the socket will
//      * be closed by the server.
//      *
//      * @return true if this connection is connected and ready to execute a request
//      */
//     boolean isAvailable();
// 
//     /**
//      * Execute a given request context in this connection. All calls to this method should first test whether this
//      * connection is available or not by calling {@link #isAvailable()} first. If this method is called while
//      * {@link #isAvailable()} would return {@code false}, then implementations will instantly cause a failure on the
//      * request with the reason {@link com.biasedbit.hotpotato.request.HttpRequestFuture#EXECUTION_REJECTED} and return
//      * {@code true}, meaning the request was consumed (and failed).
//      * <p/>
//      * The exception to the above rule is when the request is submitted and the connection goes down meanwhile. In this
//      * case, the request <strong>will not</strong> be marked as failed and this method will return {@code false} so that
//      * the caller may retry the same request in another connection.
//      * <p/>
//      * You should always, <strong>always</strong> test first with {@link #isAvailable()}.
//      * <p/>
//      * This is a non-blocking call.
//      *
//      * @param context Request execution context.
//      *
//      * @return {@code true} if the request was accepted, {@code false} otherwise. If a request is accepted, the
//      * {@code HttpConnection} becomes responsible for calling {@link
//      * com.biasedbit.hotpotato.request.HttpRequestFuture#setFailure(Throwable) setFailure()} or {@link
//      * com.biasedbit.hotpotato.request.HttpRequestFuture#setSuccess(Object,
//      * org.jboss.netty.handler.codec.http.HttpResponse) setSuccess()} on it.
//      */
//     boolean execute(HttpRequestContext context);
// }


// Path: src/main/java/com/biasedbit/hotpotato/client/ConnectionPool.java
import com.biasedbit.hotpotato.client.connection.HttpConnection;
import java.util.Collection;
import java.util.LinkedList;

/*
 * Copyright 2010 Bruno de Carvalho
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.biasedbit.hotpotato.client;



/**
 * Helper class to hold both active connections and the number of connections opening to a given host.
 *
 * This class is not thread-safe and should only be updated by a thread at a time unless manual external synchronization
 * is used.
 *
 * @author <a href="http://bruno.biasedbit.com/">Bruno de Carvalho</a>
 */
public class ConnectionPool {

    // internal vars --------------------------------------------------------------------------------------------------

    private boolean connectionFailures;
    private int connectionsOpening;
    private final Collection<HttpConnection> connections;