Skip to content

Commit

Permalink
Logging for OkHttpNetworkFetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
IanChilds authored and tyronen committed May 15, 2015
1 parent c0c63e0 commit aaae354
Showing 1 changed file with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import android.net.Uri;
import android.os.Looper;
import android.os.SystemClock;

import com.facebook.common.logging.FLog;
import com.facebook.common.references.CloseableReference;
Expand All @@ -28,14 +29,33 @@
import com.squareup.okhttp.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;

/**
* Network fetcher that uses OkHttp as a backend.
*/
public class OkHttpNetworkFetcher extends BaseNetworkFetcher<FetchState> {
public class OkHttpNetworkFetcher extends
BaseNetworkFetcher<OkHttpNetworkFetcher.OkHttpNetworkFetchState> {

public static class OkHttpNetworkFetchState extends FetchState {
public long submitTime;
public long responseTime;
public long fetchCompleteTime;

public OkHttpNetworkFetchState(
Consumer<CloseableReference<PooledByteBuffer>> consumer,
ProducerContext producerContext) {
super(consumer, producerContext);
}
}

private static final String TAG = "OkHttpNetworkFetchProducer";
private static final String QUEUE_TIME = "queue_time";
private static final String FETCH_TIME = "fetch_time";
private static final String TOTAL_TIME = "total_time";
private static final String IMAGE_SIZE = "image_size";

private final OkHttpClient mOkHttpClient;

Expand All @@ -50,23 +70,24 @@ public OkHttpNetworkFetcher(OkHttpClient okHttpClient) {
}

@Override
public FetchState createFetchState(
public OkHttpNetworkFetchState createFetchState(
Consumer<CloseableReference<PooledByteBuffer>> consumer,
ProducerContext context) {
return new FetchState(consumer, context);
return new OkHttpNetworkFetchState(consumer, context);
}

@Override
public void fetch(final FetchState requestState, final Callback callback) {
final Uri uri = requestState.getUri();
public void fetch(final OkHttpNetworkFetchState fetchState, final Callback callback) {
fetchState.submitTime = SystemClock.elapsedRealtime();
final Uri uri = fetchState.getUri();
final Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder().noStore().build())
.url(uri.toString())
.get()
.build();
final Call call = mOkHttpClient.newCall(request);

requestState.getContext().addCallbacks(
fetchState.getContext().addCallbacks(
new BaseProducerContextCallbacks() {
@Override
public void onCancellationRequested() {
Expand All @@ -86,6 +107,7 @@ public void onCancellationRequested() {
new com.squareup.okhttp.Callback() {
@Override
public void onResponse(Response response) {
fetchState.responseTime = SystemClock.elapsedRealtime();
final ResponseBody body = response.body();
try {
long contentLength = body.contentLength();
Expand All @@ -111,6 +133,21 @@ public void onFailure(final Request request, final IOException e) {
});
}

@Override
public void onFetchCompletion(OkHttpNetworkFetchState fetchState, int byteSize) {
fetchState.fetchCompleteTime = SystemClock.elapsedRealtime();
}

@Override
public Map<String, String> getExtraMap(OkHttpNetworkFetchState fetchState, int byteSize) {
Map<String, String> extraMap = new HashMap<>(4);
extraMap.put(QUEUE_TIME, Long.toString(fetchState.responseTime - fetchState.submitTime));
extraMap.put(FETCH_TIME, Long.toString(fetchState.fetchCompleteTime - fetchState.responseTime));
extraMap.put(TOTAL_TIME, Long.toString(fetchState.fetchCompleteTime - fetchState.submitTime));
extraMap.put(IMAGE_SIZE, Integer.toString(byteSize));
return extraMap;
}

/**
* Handles IOExceptions.
*
Expand Down

0 comments on commit aaae354

Please sign in to comment.