mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/tsccm/RouteSpecificPool.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/tsccm/RouteSpecificPool.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,325 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + *
     1.7 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.8 + *  contributor license agreements.  See the NOTICE file distributed with
     1.9 + *  this work for additional information regarding copyright ownership.
    1.10 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.11 + *  (the "License"); you may not use this file except in compliance with
    1.12 + *  the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + *  Unless required by applicable law or agreed to in writing, software
    1.17 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + *  See the License for the specific language governing permissions and
    1.20 + *  limitations under the License.
    1.21 + * ====================================================================
    1.22 + *
    1.23 + * This software consists of voluntary contributions made by many
    1.24 + * individuals on behalf of the Apache Software Foundation.  For more
    1.25 + * information on the Apache Software Foundation, please see
    1.26 + * <http://www.apache.org/>.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package ch.boye.httpclientandroidlib.impl.conn.tsccm;
    1.31 +
    1.32 +import java.io.IOException;
    1.33 +import java.util.ListIterator;
    1.34 +import java.util.Queue;
    1.35 +import java.util.LinkedList;
    1.36 +
    1.37 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    1.38 +
    1.39 +import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
    1.40 +/* LogFactory removed by HttpClient for Android script. */
    1.41 +import ch.boye.httpclientandroidlib.conn.OperatedClientConnection;
    1.42 +import ch.boye.httpclientandroidlib.conn.params.ConnPerRoute;
    1.43 +import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
    1.44 +import ch.boye.httpclientandroidlib.util.LangUtils;
    1.45 +
    1.46 +
    1.47 +/**
    1.48 + * A connection sub-pool for a specific route, used by {@link ConnPoolByRoute}.
    1.49 + * The methods in this class are unsynchronized. It is expected that the
    1.50 + * containing pool takes care of synchronization.
    1.51 + *
    1.52 + * @since 4.0
    1.53 + */
    1.54 +@NotThreadSafe // e.g. numEntries, freeEntries,
    1.55 +public class RouteSpecificPool {
    1.56 +
    1.57 +    public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
    1.58 +
    1.59 +    /** The route this pool is for. */
    1.60 +    protected final HttpRoute route; //Immutable
    1.61 +
    1.62 +    @Deprecated
    1.63 +    protected final int maxEntries;
    1.64 +
    1.65 +    /** Connections per route */
    1.66 +    protected final ConnPerRoute connPerRoute;
    1.67 +
    1.68 +    /**
    1.69 +     * The list of free entries.
    1.70 +     * This list is managed LIFO, to increase idle times and
    1.71 +     * allow for closing connections that are not really needed.
    1.72 +     */
    1.73 +    protected final LinkedList<BasicPoolEntry> freeEntries;
    1.74 +
    1.75 +    /** The list of threads waiting for this pool. */
    1.76 +    protected final Queue<WaitingThread> waitingThreads;
    1.77 +
    1.78 +    /** The number of created entries. */
    1.79 +    protected int numEntries;
    1.80 +
    1.81 +
    1.82 +    /**
    1.83 +     * @deprecated use {@link RouteSpecificPool#RouteSpecificPool(HttpRoute, ConnPerRoute)}
    1.84 +     */
    1.85 +    @Deprecated
    1.86 +    public RouteSpecificPool(HttpRoute route, int maxEntries) {
    1.87 +        this.route = route;
    1.88 +        this.maxEntries = maxEntries;
    1.89 +        this.connPerRoute = new ConnPerRoute() {
    1.90 +            public int getMaxForRoute(HttpRoute route) {
    1.91 +                return RouteSpecificPool.this.maxEntries;
    1.92 +            }
    1.93 +        };
    1.94 +        this.freeEntries = new LinkedList<BasicPoolEntry>();
    1.95 +        this.waitingThreads = new LinkedList<WaitingThread>();
    1.96 +        this.numEntries = 0;
    1.97 +    }
    1.98 +
    1.99 +
   1.100 +    /**
   1.101 +     * Creates a new route-specific pool.
   1.102 +     *
   1.103 +     * @param route the route for which to pool
   1.104 +     * @param connPerRoute the connections per route configuration
   1.105 +     */
   1.106 +    public RouteSpecificPool(HttpRoute route, ConnPerRoute connPerRoute) {
   1.107 +        this.route = route;
   1.108 +        this.connPerRoute = connPerRoute;
   1.109 +        this.maxEntries = connPerRoute.getMaxForRoute(route);
   1.110 +        this.freeEntries = new LinkedList<BasicPoolEntry>();
   1.111 +        this.waitingThreads = new LinkedList<WaitingThread>();
   1.112 +        this.numEntries = 0;
   1.113 +    }
   1.114 +
   1.115 +
   1.116 +    /**
   1.117 +     * Obtains the route for which this pool is specific.
   1.118 +     *
   1.119 +     * @return  the route
   1.120 +     */
   1.121 +    public final HttpRoute getRoute() {
   1.122 +        return route;
   1.123 +    }
   1.124 +
   1.125 +
   1.126 +    /**
   1.127 +     * Obtains the maximum number of entries allowed for this pool.
   1.128 +     *
   1.129 +     * @return  the max entry number
   1.130 +     */
   1.131 +    public final int getMaxEntries() {
   1.132 +        return maxEntries;
   1.133 +    }
   1.134 +
   1.135 +
   1.136 +    /**
   1.137 +     * Indicates whether this pool is unused.
   1.138 +     * A pool is unused if there is neither an entry nor a waiting thread.
   1.139 +     * All entries count, not only the free but also the allocated ones.
   1.140 +     *
   1.141 +     * @return  <code>true</code> if this pool is unused,
   1.142 +     *          <code>false</code> otherwise
   1.143 +     */
   1.144 +    public boolean isUnused() {
   1.145 +        return (numEntries < 1) && waitingThreads.isEmpty();
   1.146 +    }
   1.147 +
   1.148 +
   1.149 +    /**
   1.150 +     * Return remaining capacity of this pool
   1.151 +     *
   1.152 +     * @return capacity
   1.153 +     */
   1.154 +    public int getCapacity() {
   1.155 +        return connPerRoute.getMaxForRoute(route) - numEntries;
   1.156 +    }
   1.157 +
   1.158 +
   1.159 +    /**
   1.160 +     * Obtains the number of entries.
   1.161 +     * This includes not only the free entries, but also those that
   1.162 +     * have been created and are currently issued to an application.
   1.163 +     *
   1.164 +     * @return  the number of entries for the route of this pool
   1.165 +     */
   1.166 +    public final int getEntryCount() {
   1.167 +        return numEntries;
   1.168 +    }
   1.169 +
   1.170 +
   1.171 +    /**
   1.172 +     * Obtains a free entry from this pool, if one is available.
   1.173 +     *
   1.174 +     * @return an available pool entry, or <code>null</code> if there is none
   1.175 +     */
   1.176 +    public BasicPoolEntry allocEntry(final Object state) {
   1.177 +        if (!freeEntries.isEmpty()) {
   1.178 +            ListIterator<BasicPoolEntry> it = freeEntries.listIterator(freeEntries.size());
   1.179 +            while (it.hasPrevious()) {
   1.180 +                BasicPoolEntry entry = it.previous();
   1.181 +                if (entry.getState() == null || LangUtils.equals(state, entry.getState())) {
   1.182 +                    it.remove();
   1.183 +                    return entry;
   1.184 +                }
   1.185 +            }
   1.186 +        }
   1.187 +        if (getCapacity() == 0 && !freeEntries.isEmpty()) {
   1.188 +            BasicPoolEntry entry = freeEntries.remove();
   1.189 +            entry.shutdownEntry();
   1.190 +            OperatedClientConnection conn = entry.getConnection();
   1.191 +            try {
   1.192 +                conn.close();
   1.193 +            } catch (IOException ex) {
   1.194 +                log.debug("I/O error closing connection", ex);
   1.195 +            }
   1.196 +            return entry;
   1.197 +        }
   1.198 +        return null;
   1.199 +    }
   1.200 +
   1.201 +
   1.202 +    /**
   1.203 +     * Returns an allocated entry to this pool.
   1.204 +     *
   1.205 +     * @param entry     the entry obtained from {@link #allocEntry allocEntry}
   1.206 +     *                  or presented to {@link #createdEntry createdEntry}
   1.207 +     */
   1.208 +    public void freeEntry(BasicPoolEntry entry) {
   1.209 +
   1.210 +        if (numEntries < 1) {
   1.211 +            throw new IllegalStateException
   1.212 +                ("No entry created for this pool. " + route);
   1.213 +        }
   1.214 +        if (numEntries <= freeEntries.size()) {
   1.215 +            throw new IllegalStateException
   1.216 +                ("No entry allocated from this pool. " + route);
   1.217 +        }
   1.218 +        freeEntries.add(entry);
   1.219 +    }
   1.220 +
   1.221 +
   1.222 +    /**
   1.223 +     * Indicates creation of an entry for this pool.
   1.224 +     * The entry will <i>not</i> be added to the list of free entries,
   1.225 +     * it is only recognized as belonging to this pool now. It can then
   1.226 +     * be passed to {@link #freeEntry freeEntry}.
   1.227 +     *
   1.228 +     * @param entry     the entry that was created for this pool
   1.229 +     */
   1.230 +    public void createdEntry(BasicPoolEntry entry) {
   1.231 +
   1.232 +        if (!route.equals(entry.getPlannedRoute())) {
   1.233 +            throw new IllegalArgumentException
   1.234 +                ("Entry not planned for this pool." +
   1.235 +                 "\npool: " + route +
   1.236 +                 "\nplan: " + entry.getPlannedRoute());
   1.237 +        }
   1.238 +
   1.239 +        numEntries++;
   1.240 +    }
   1.241 +
   1.242 +
   1.243 +    /**
   1.244 +     * Deletes an entry from this pool.
   1.245 +     * Only entries that are currently free in this pool can be deleted.
   1.246 +     * Allocated entries can not be deleted.
   1.247 +     *
   1.248 +     * @param entry     the entry to delete from this pool
   1.249 +     *
   1.250 +     * @return  <code>true</code> if the entry was found and deleted, or
   1.251 +     *          <code>false</code> if the entry was not found
   1.252 +     */
   1.253 +    public boolean deleteEntry(BasicPoolEntry entry) {
   1.254 +
   1.255 +        final boolean found = freeEntries.remove(entry);
   1.256 +        if (found)
   1.257 +            numEntries--;
   1.258 +        return found;
   1.259 +    }
   1.260 +
   1.261 +
   1.262 +    /**
   1.263 +     * Forgets about an entry from this pool.
   1.264 +     * This method is used to indicate that an entry
   1.265 +     * {@link #allocEntry allocated}
   1.266 +     * from this pool has been lost and will not be returned.
   1.267 +     */
   1.268 +    public void dropEntry() {
   1.269 +        if (numEntries < 1) {
   1.270 +            throw new IllegalStateException
   1.271 +                ("There is no entry that could be dropped.");
   1.272 +        }
   1.273 +        numEntries--;
   1.274 +    }
   1.275 +
   1.276 +
   1.277 +    /**
   1.278 +     * Adds a waiting thread.
   1.279 +     * This pool makes no attempt to match waiting threads with pool entries.
   1.280 +     * It is the caller's responsibility to check that there is no entry
   1.281 +     * before adding a waiting thread.
   1.282 +     *
   1.283 +     * @param wt        the waiting thread
   1.284 +     */
   1.285 +    public void queueThread(WaitingThread wt) {
   1.286 +        if (wt == null) {
   1.287 +            throw new IllegalArgumentException
   1.288 +                ("Waiting thread must not be null.");
   1.289 +        }
   1.290 +        this.waitingThreads.add(wt);
   1.291 +    }
   1.292 +
   1.293 +
   1.294 +    /**
   1.295 +     * Checks whether there is a waiting thread in this pool.
   1.296 +     *
   1.297 +     * @return  <code>true</code> if there is a waiting thread,
   1.298 +     *          <code>false</code> otherwise
   1.299 +     */
   1.300 +    public boolean hasThread() {
   1.301 +        return !this.waitingThreads.isEmpty();
   1.302 +    }
   1.303 +
   1.304 +
   1.305 +    /**
   1.306 +     * Returns the next thread in the queue.
   1.307 +     *
   1.308 +     * @return  a waiting thread, or <code>null</code> if there is none
   1.309 +     */
   1.310 +    public WaitingThread nextThread() {
   1.311 +        return this.waitingThreads.peek();
   1.312 +    }
   1.313 +
   1.314 +
   1.315 +    /**
   1.316 +     * Removes a waiting thread, if it is queued.
   1.317 +     *
   1.318 +     * @param wt        the waiting thread
   1.319 +     */
   1.320 +    public void removeThread(WaitingThread wt) {
   1.321 +        if (wt == null)
   1.322 +            return;
   1.323 +
   1.324 +        this.waitingThreads.remove(wt);
   1.325 +    }
   1.326 +
   1.327 +
   1.328 +} // class RouteSpecificPool

mercurial