diff options
author | Patrick Scott <phanna@android.com> | 2009-05-15 13:47:17 -0400 |
---|---|---|
committer | Patrick Scott <phanna@android.com> | 2009-05-20 12:10:25 -0400 |
commit | b5b33c734ab2a178f07c667a8c1daf8beab837aa (patch) | |
tree | 3a9181764c712df30cdb0ebd13f3ee7e2d058d76 /core/java/android/net/http/RequestQueue.java | |
parent | 82d8030c8eb52316b7bc16fded695c8f9dfb32a9 (diff) | |
download | frameworks_base-b5b33c734ab2a178f07c667a8c1daf8beab837aa.zip frameworks_base-b5b33c734ab2a178f07c667a8c1daf8beab837aa.tar.gz frameworks_base-b5b33c734ab2a178f07c667a8c1daf8beab837aa.tar.bz2 |
Order high priority requests in the same order they are requested.
Use a class to hold the high priority requests and low priority requests. Each
request is appended to the correct list in the order received. If a request
needs to be requeued after an error, it is placed at the head of the
corresponding list.
Diffstat (limited to 'core/java/android/net/http/RequestQueue.java')
-rw-r--r-- | core/java/android/net/http/RequestQueue.java | 73 |
1 files changed, 54 insertions, 19 deletions
diff --git a/core/java/android/net/http/RequestQueue.java b/core/java/android/net/http/RequestQueue.java index 66d5722..54a1cce 100644 --- a/core/java/android/net/http/RequestQueue.java +++ b/core/java/android/net/http/RequestQueue.java @@ -52,10 +52,44 @@ public class RequestQueue implements RequestFeeder { private Context mContext; + private static class RequestSet { + private final LinkedList<Request> mHighPriority; + private final LinkedList<Request> mLowPriority; + + RequestSet() { + mHighPriority = new LinkedList<Request>(); + mLowPriority = new LinkedList<Request>(); + } + + void add(Request req, boolean head) { + LinkedList l = mLowPriority; + if (req.mHighPriority) { + l = mHighPriority; + } + if (head) { + l.addFirst(req); + } else { + l.add(req); + } + } + + Request removeFirst() { + if (!mHighPriority.isEmpty()) { + return mHighPriority.removeFirst(); + } else if (!mLowPriority.isEmpty()) { + return mLowPriority.removeFirst(); + } + return null; + } + + boolean isEmpty() { + return mHighPriority.isEmpty() && mLowPriority.isEmpty(); + } + }; /** * Requests, indexed by HttpHost (scheme, host, port) */ - private LinkedHashMap<HttpHost, LinkedList<Request>> mPending; + private LinkedHashMap<HttpHost, RequestSet> mPending; /* Support for notifying a client when queue is empty */ private boolean mClientWaiting = false; @@ -344,7 +378,7 @@ public class RequestQueue implements RequestFeeder { public RequestQueue(Context context, int connectionCount) { mContext = context; - mPending = new LinkedHashMap<HttpHost, LinkedList<Request>>(32); + mPending = new LinkedHashMap<HttpHost, RequestSet>(32); mActivePool = new ActivePool(connectionCount); mActivePool.startup(); @@ -480,7 +514,7 @@ public class RequestQueue implements RequestFeeder { req = new Request(method, httpHost, mProxyHost, uri.mPath, bodyProvider, bodyLength, eventHandler, headers, highPriority); - queueRequest(req, highPriority); + queueRequest(req, false); mActivePool.mTotalRequest++; @@ -520,19 +554,24 @@ public class RequestQueue implements RequestFeeder { HttpLog.v("dump()"); StringBuilder dump = new StringBuilder(); int count = 0; - Iterator<Map.Entry<HttpHost, LinkedList<Request>>> iter; + Iterator<Map.Entry<HttpHost, RequestSet>> iter; // mActivePool.log(dump); if (!mPending.isEmpty()) { iter = mPending.entrySet().iterator(); while (iter.hasNext()) { - Map.Entry<HttpHost, LinkedList<Request>> entry = iter.next(); + Map.Entry<HttpHost, RequestSet> 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); + RequestSet reqList = entry.getValue(); + ListIterator reqIter = reqList.mHighPriority.listIterator(0); + while (iter.hasNext()) { + Request request = (Request)iter.next(); + line.append(request + " "); + } + reqIter = reqList.mLowPriority.listIterator(0); while (iter.hasNext()) { Request request = (Request)iter.next(); line.append(request + " "); @@ -564,7 +603,7 @@ public class RequestQueue implements RequestFeeder { Request ret = null; if (mNetworkConnected && mPending.containsKey(host)) { - LinkedList<Request> reqList = mPending.get(host); + RequestSet reqList = mPending.get(host); ret = reqList.removeFirst(); if (reqList.isEmpty()) { mPending.remove(host); @@ -597,18 +636,14 @@ public class RequestQueue implements RequestFeeder { protected synchronized void queueRequest(Request request, boolean head) { HttpHost host = request.mProxyHost == null ? request.mHost : request.mProxyHost; - LinkedList<Request> reqList; + RequestSet reqList; if (mPending.containsKey(host)) { reqList = mPending.get(host); } else { - reqList = new LinkedList<Request>(); + reqList = new RequestSet(); mPending.put(host, reqList); } - if (head) { - reqList.addFirst(request); - } else { - reqList.add(request); - } + reqList.add(request, head); } @@ -621,12 +656,12 @@ public class RequestQueue implements RequestFeeder { } /* helper */ - private Request removeFirst(LinkedHashMap<HttpHost, LinkedList<Request>> requestQueue) { + private Request removeFirst(LinkedHashMap<HttpHost, RequestSet> requestQueue) { Request ret = null; - Iterator<Map.Entry<HttpHost, LinkedList<Request>>> iter = requestQueue.entrySet().iterator(); + Iterator<Map.Entry<HttpHost, RequestSet>> iter = requestQueue.entrySet().iterator(); if (iter.hasNext()) { - Map.Entry<HttpHost, LinkedList<Request>> entry = iter.next(); - LinkedList<Request> reqList = entry.getValue(); + Map.Entry<HttpHost, RequestSet> entry = iter.next(); + RequestSet reqList = entry.getValue(); ret = reqList.removeFirst(); if (reqList.isEmpty()) { requestQueue.remove(entry.getKey()); |