FileDocCategorySizeDatePackage
RequestQueue.javaAPI DocAndroid 1.5 API21781Wed May 06 22:41:56 BST 2009android.net.http

RequestQueue

public class RequestQueue extends Object implements RequestFeeder
{@hide}

Fields Summary
private android.content.Context
mContext
private LinkedHashMap
mPending
Requests, indexed by HttpHost (scheme, host, port)
private boolean
mClientWaiting
boolean
mNetworkConnected
true if connected
private HttpHost
mProxyHost
private android.content.BroadcastReceiver
mProxyChangeReceiver
private ActivePool
mActivePool
private static final int
CONNECTION_COUNT
public static final String
HTTP_NETWORK_STATE_CHANGED_INTENT
This intent broadcast when http is paused or unpaused due to net availability toggling
public static final String
HTTP_NETWORK_STATE_UP
private NetworkStateTracker
mNetworkStateTracker
Listen to platform network state. On a change, (1) kick stack on or off as appropriate (2) send an intent to my host app telling it what I've done
Constructors Summary
public RequestQueue(android.content.Context context)
A RequestQueue class instance maintains a set of queued requests. It orders them, makes the requests against HTTP servers, and makes callbacks to supplied eventHandlers as data is read. It supports request prioritization, connection reuse and pipelining.

param
context application context

        this(context, CONNECTION_COUNT);
    
public RequestQueue(android.content.Context context, int connectionCount)
A RequestQueue class instance maintains a set of queued requests. It orders them, makes the requests against HTTP servers, and makes callbacks to supplied eventHandlers as data is read. It supports request prioritization, connection reuse and pipelining.

param
context application context
param
connectionCount The number of simultaneous connections

        mContext = context;

        mPending = new LinkedHashMap<HttpHost, LinkedList<Request>>(32);

        mActivePool = new ActivePool(connectionCount);
        mActivePool.startup();
    
Methods Summary
public synchronized voiddisablePlatformNotifications()
If platform notifications have been enabled, call this method to disable before destroying RequestQueue

        if (HttpLog.LOGV) HttpLog.v("RequestQueue.disablePlatformNotifications() network");

        if (mNetworkStateTracker != null) {
            mNetworkStateTracker.disable();
        }

        if (mProxyChangeReceiver != null) {
            mContext.unregisterReceiver(mProxyChangeReceiver);
            mProxyChangeReceiver = null;
        }
    
synchronized voiddump()
debug tool: prints request queue to log

        HttpLog.v("dump()");
        StringBuilder dump = new StringBuilder();
        int count = 0;
        Iterator<Map.Entry<HttpHost, LinkedList<Request>>> iter;

        // mActivePool.log(dump);

        if (!mPending.isEmpty()) {
            iter = mPending.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<HttpHost, LinkedList<Request>> entry = iter.next();
                String hostName = entry.getKey().getHostName();
                StringBuilder line = new StringBuilder("p" + count++ + " " + hostName + " ");

                LinkedList<Request> reqList = entry.getValue();
                ListIterator reqIter = reqList.listIterator(0);
                while (iter.hasNext()) {
                    Request request = (Request)iter.next();
                    line.append(request + " ");
                }
                dump.append(line);
                dump.append("\n");
            }
        }
        HttpLog.v(dump.toString());
    
public synchronized voidenablePlatformNotifications()
Enables data state and proxy tracking

        if (HttpLog.LOGV) HttpLog.v("RequestQueue.enablePlatformNotifications() network");

        if (mProxyChangeReceiver == null) {
            mProxyChangeReceiver =
                    new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context ctx, Intent intent) {
                            setProxyConfig();
                        }
                    };
            mContext.registerReceiver(mProxyChangeReceiver,
                                      new IntentFilter(Proxy.PROXY_CHANGE_ACTION));
        }

        /* Network state notification is broken on the simulator
           don't register for notifications on SIM */
        String device = SystemProperties.get("ro.product.device");
        boolean simulation = TextUtils.isEmpty(device);

        if (!simulation) {
            if (mNetworkStateTracker == null) {
                mNetworkStateTracker = new NetworkStateTracker(mContext);
            }
            mNetworkStateTracker.enable();
        }
    
public org.apache.http.HttpHostgetProxyHost()
used by webkit

return
proxy host if set, null otherwise

        return mProxyHost;
    
public synchronized RequestgetRequest()

        Request ret = null;

        if (mNetworkConnected && !mPending.isEmpty()) {
            ret = removeFirst(mPending);
        }
        if (HttpLog.LOGV) HttpLog.v("RequestQueue.getRequest() => " + ret);
        return ret;
    
public synchronized RequestgetRequest(org.apache.http.HttpHost host)

return
a request for given host if possible

        Request ret = null;

        if (mNetworkConnected && mPending.containsKey(host)) {
            LinkedList<Request> reqList = mPending.get(host);
            ret = reqList.removeFirst();
            if (reqList.isEmpty()) {
                mPending.remove(host);
            }
        }
        if (HttpLog.LOGV) HttpLog.v("RequestQueue.getRequest(" + host + ") => " + ret);
        return ret;
    
public synchronized booleanhaveRequest(org.apache.http.HttpHost host)

return
true if a request for this host is available

        return mPending.containsKey(host);
    
protected synchronized voidqueueRequest(Request request, boolean head)

        HttpHost host = request.mProxyHost == null ? request.mHost : request.mProxyHost;
        LinkedList<Request> reqList;
        if (mPending.containsKey(host)) {
            reqList = mPending.get(host);
        } else {
            reqList = new LinkedList<Request>();
            mPending.put(host, reqList);
        }
        if (head) {
            reqList.addFirst(request);
        } else {
            reqList.add(request);
        }
    
public RequestHandlequeueRequest(java.lang.String url, java.lang.String method, java.util.Map headers, EventHandler eventHandler, java.io.InputStream bodyProvider, int bodyLength, boolean highPriority)
Queues an HTTP request

param
url The url to load.
param
method "GET" or "POST."
param
headers A hashmap of http headers.
param
eventHandler The event handler for handling returned data. Callbacks will be made on the supplied instance.
param
bodyProvider InputStream providing HTTP body, null if none
param
bodyLength length of body, must be 0 if bodyProvider is null
param
highPriority If true, queues before low priority requests if possible

        WebAddress uri = new WebAddress(url);
        return queueRequest(url, uri, method, headers, eventHandler,
                            bodyProvider, bodyLength, highPriority);
    
public RequestHandlequeueRequest(java.lang.String url, android.net.WebAddress uri, java.lang.String method, java.util.Map headers, EventHandler eventHandler, java.io.InputStream bodyProvider, int bodyLength, boolean highPriority)
Queues an HTTP request

param
url The url to load.
param
uri The uri of the url to load.
param
method "GET" or "POST."
param
headers A hashmap of http headers.
param
eventHandler The event handler for handling returned data. Callbacks will be made on the supplied instance.
param
bodyProvider InputStream providing HTTP body, null if none
param
bodyLength length of body, must be 0 if bodyProvider is null
param
highPriority If true, queues before low priority requests if possible


        if (HttpLog.LOGV) HttpLog.v("RequestQueue.queueRequest " + uri);

        // Ensure there is an eventHandler set
        if (eventHandler == null) {
            eventHandler = new LoggingEventHandler();
        }

        /* Create and queue request */
        Request req;
        HttpHost httpHost = new HttpHost(uri.mHost, uri.mPort, uri.mScheme);

        // set up request
        req = new Request(method, httpHost, mProxyHost, uri.mPath, bodyProvider,
                          bodyLength, eventHandler, headers, highPriority);

        queueRequest(req, highPriority);

        mActivePool.mTotalRequest++;

        // dump();
        mActivePool.startConnectionThread();

        return new RequestHandle(
                this, url, uri, method, headers, bodyProvider, bodyLength,
                req);
    
private RequestremoveFirst(java.util.LinkedHashMap requestQueue)

        Request ret = null;
        Iterator<Map.Entry<HttpHost, LinkedList<Request>>> iter = requestQueue.entrySet().iterator();
        if (iter.hasNext()) {
            Map.Entry<HttpHost, LinkedList<Request>> entry = iter.next();
            LinkedList<Request> reqList = entry.getValue();
            ret = reqList.removeFirst();
            if (reqList.isEmpty()) {
                requestQueue.remove(entry.getKey());
            }
        }
        return ret;
    
synchronized booleanrequestsPending()

return
true iff there are any non-active requests pending

        return !mPending.isEmpty();
    
public voidrequeueRequest(Request request)
Put request back on head of queue

        queueRequest(request, true);
    
public voidsetNetworkState(boolean isNetworkConnected)
Called by the NetworkStateTracker -- updates when network connectivity is lost/restored. If isNetworkConnected is true, start processing requests

        if (HttpLog.LOGV) HttpLog.v("RequestQueue.setNetworkState() " + isNetworkConnected);
        mNetworkConnected = isNetworkConnected;
        if (isNetworkConnected)
            mActivePool.startConnectionThread();
    
private synchronized voidsetProxyConfig()
Because our IntentReceiver can run within a different thread, synchronize setting the proxy

        if (mNetworkStateTracker.getCurrentNetworkType() == ConnectivityManager.TYPE_WIFI) {
            mProxyHost = null;
        } else {
            String host = Proxy.getHost(mContext);
            if (HttpLog.LOGV) HttpLog.v("RequestQueue.setProxyConfig " + host);
            if (host == null) {
                mProxyHost = null;
            } else {
                mActivePool.disablePersistence();
                mProxyHost = new HttpHost(host, Proxy.getPort(mContext), "http");
            }
        }
    
public voidshutdown()
This must be called to cleanly shutdown RequestQueue

        mActivePool.shutdown();
    
public voidstartTiming()

        mActivePool.startTiming();
    
public voidstopTiming()

        mActivePool.stopTiming();