NetworkDispatcherpublic class NetworkDispatcher extends Thread Provides a thread for performing network dispatch from a queue of requests.
Requests added to the specified queue are processed from the network via a
specified {@link Network} interface. Responses are committed to cache, if
eligible, using a specified {@link Cache} interface. Valid responses and
errors are posted back to the caller via a {@link ResponseDelivery}. |
Fields Summary |
---|
private final BlockingQueue | mQueueThe queue of requests to service. | private final Network | mNetworkThe network interface for processing requests. | private final Cache | mCacheThe cache to write to. | private final ResponseDelivery | mDeliveryFor posting responses and errors. | private volatile boolean | mQuitUsed for telling us to die. |
Constructors Summary |
---|
public NetworkDispatcher(BlockingQueue queue, Network network, Cache cache, ResponseDelivery delivery)Creates a new network dispatcher thread. You must call {@link #start()}
in order to begin processing.
mQueue = queue;
mNetwork = network;
mCache = cache;
mDelivery = delivery;
|
Methods Summary |
---|
private void | addTrafficStatsTag(Request request)
// Tag the request (if API >= 14)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
TrafficStats.setThreadStatsTag(request.getTrafficStatsTag());
}
| private void | parseAndDeliverNetworkError(Request request, VolleyError error)
error = request.parseNetworkError(error);
mDelivery.postError(request, error);
| public void | quit()Forces this dispatcher to quit immediately. If any requests are still in
the queue, they are not guaranteed to be processed.
mQuit = true;
interrupt();
| public void | run()
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Request<?> request;
while (true) {
try {
// Take a request from the queue.
request = mQueue.take();
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
continue;
}
try {
request.addMarker("network-queue-take");
// If the request was cancelled already, do not perform the
// network request.
if (request.isCanceled()) {
request.finish("network-discard-cancelled");
continue;
}
addTrafficStatsTag(request);
// Perform the network request.
NetworkResponse networkResponse = mNetwork.performRequest(request);
request.addMarker("network-http-complete");
// If the server returned 304 AND we delivered a response already,
// we're done -- don't deliver a second identical response.
if (networkResponse.notModified && request.hasHadResponseDelivered()) {
request.finish("not-modified");
continue;
}
// Parse the response here on the worker thread.
Response<?> response = request.parseNetworkResponse(networkResponse);
request.addMarker("network-parse-complete");
// Write to cache if applicable.
// TODO: Only update cache metadata instead of entire record for 304s.
if (request.shouldCache() && response.cacheEntry != null) {
mCache.put(request.getCacheKey(), response.cacheEntry);
request.addMarker("network-cache-written");
}
// Post the response back.
request.markDelivered();
mDelivery.postResponse(request, response);
} catch (VolleyError volleyError) {
parseAndDeliverNetworkError(request, volleyError);
} catch (Exception e) {
VolleyLog.e(e, "Unhandled exception %s", e.toString());
mDelivery.postError(request, new VolleyError(e));
}
}
|
|