RequestQueuepublic class RequestQueue extends Object implements RequestFeeder
Fields Summary |
---|
private final LinkedHashMap | mPendingRequests, indexed by HttpHost (scheme, host, port) | private final android.content.Context | mContext | private final ActivePool | mActivePool | private final android.net.ConnectivityManager | mConnectivityManager | private HttpHost | mProxyHost | private android.content.BroadcastReceiver | mProxyChangeReceiver | private static final int | CONNECTION_COUNT |
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();
mConnectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
Methods Summary |
---|
private org.apache.http.HttpHost | determineHost(org.apache.http.HttpHost host)
// There used to be a comment in ConnectionThread about t-mob's proxy
// being really bad about https. But, HttpsConnection actually looks
// for a proxy and connects through it anyway. I think that this check
// is still valid because if a site is https, we will use
// HttpsConnection rather than HttpConnection if the proxy address is
// not secure.
return (mProxyHost == null || "https".equals(host.getSchemeName()))
? host : mProxyHost;
| 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 (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));
}
// we need to resample the current proxy setup
setProxyConfig();
| public org.apache.http.HttpHost | getProxyHost()used by webkit
return mProxyHost;
| public synchronized Request | getRequest()
Request ret = null;
if (!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 (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)Queues an HTTP request
WebAddress uri = new WebAddress(url);
return queueRequest(url, uri, method, headers, eventHandler,
bodyProvider, bodyLength);
| 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)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.getHost(), uri.getPort(), uri.getScheme());
// set up request
req = new Request(method, httpHost, mProxyHost, uri.getPath(), bodyProvider,
bodyLength, eventHandler, headers);
queueRequest(req, false);
mActivePool.mTotalRequest++;
// dump();
mActivePool.startConnectionThread();
return new RequestHandle(
this, url, uri, method, headers, bodyProvider, bodyLength,
req);
| public RequestHandle | queueSynchronousRequest(java.lang.String url, android.net.WebAddress uri, java.lang.String method, java.util.Map headers, EventHandler eventHandler, java.io.InputStream bodyProvider, int bodyLength)
if (HttpLog.LOGV) {
HttpLog.v("RequestQueue.dispatchSynchronousRequest " + uri);
}
HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
Request req = new Request(method, host, mProxyHost, uri.getPath(),
bodyProvider, bodyLength, eventHandler, headers);
// Open a new connection that uses our special RequestFeeder
// implementation.
host = determineHost(host);
Connection conn = Connection.getConnection(mContext, host, mProxyHost,
new SyncFeeder());
// TODO: I would like to process the request here but LoadListener
// needs a RequestHandle to process some messages.
return new RequestHandle(this, url, uri, method, headers, bodyProvider,
bodyLength, req, conn);
| 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);
| private synchronized void | setProxyConfig()Because our IntentReceiver can run within a different thread,
synchronize setting the proxy
NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == 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();
|
|