RequestQueuepublic class RequestQueue extends Object implements RequestFeeder
Fields Summary |
---|
private android.content.Context | mContext | private LinkedHashMap | mPendingRequests, indexed by HttpHost (scheme, host, port) | private boolean | mClientWaiting | boolean | mNetworkConnectedtrue 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_INTENTThis intent broadcast when http is paused or unpaused due to
net availability toggling | public static final String | HTTP_NETWORK_STATE_UP | private NetworkStateTracker | mNetworkStateTrackerListen 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.
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.
mContext = context;
mPending = new LinkedHashMap<HttpHost, LinkedList<Request>>(32);
mActivePool = new ActivePool(connectionCount);
mActivePool.startup();
|
Methods Summary |
---|
public synchronized void | disablePlatformNotifications()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 void | dump()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 void | enablePlatformNotifications()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.HttpHost | getProxyHost()used by webkit
return mProxyHost;
| public synchronized Request | getRequest()
Request ret = null;
if (mNetworkConnected && !mPending.isEmpty()) {
ret = removeFirst(mPending);
}
if (HttpLog.LOGV) HttpLog.v("RequestQueue.getRequest() => " + ret);
return ret;
| public synchronized Request | getRequest(org.apache.http.HttpHost host)
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 boolean | haveRequest(org.apache.http.HttpHost host)
return mPending.containsKey(host);
| protected synchronized void | queueRequest(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 RequestHandle | queueRequest(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
WebAddress uri = new WebAddress(url);
return queueRequest(url, uri, method, headers, eventHandler,
bodyProvider, bodyLength, highPriority);
| public RequestHandle | queueRequest(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
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 Request | removeFirst(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 boolean | requestsPending()
return !mPending.isEmpty();
| public void | requeueRequest(Request request)Put request back on head of queue
queueRequest(request, true);
| public void | setNetworkState(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 void | setProxyConfig()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 void | shutdown()This must be called to cleanly shutdown RequestQueue
mActivePool.shutdown();
| public void | startTiming()
mActivePool.startTiming();
| public void | stopTiming()
mActivePool.stopTiming();
|
|