com.elite.tools.soar.NetworkDispatcher.java Source code

Java tutorial

Introduction

Here is the source code for com.elite.tools.soar.NetworkDispatcher.java

Source

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.elite.tools.soar;

import com.elite.tools.soar.exception.SoarError;
import com.google.common.base.Stopwatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 
 * ??
 * {@link Network}???{@link Cache}
 * ???{@link ResponseDelivery}
 */
public class NetworkDispatcher extends Thread {
    /**
     * ??
     */
    private final BlockingQueue<InnerRequest<?>> mQueue;
    /**
     * ??Network
     */
    private final Network mNetwork;
    /**
     * ?
     */
    private final Cache mCache;
    /**
     * ??
     */
    private final ResponseDelivery mDelivery;
    /**
     * ??
     */
    private volatile boolean mQuit = false;

    private static final Logger LOG = LoggerFactory.getLogger(NetworkDispatcher.class);

    /**
     * {@link #start()}?
     *
     * @param queue    ?
     * @param network  Network
     * @param cache    ?
     * @param delivery ?
     */
    public NetworkDispatcher(BlockingQueue<InnerRequest<?>> queue, Network network, Cache cache,
            ResponseDelivery delivery) {
        mQueue = queue;
        mNetwork = network;
        mCache = cache;
        mDelivery = delivery;
    }

    /**
     * ??
     * ?
     */
    public void quit() {
        mQuit = true;
        interrupt();
    }

    @Override
    public void run() {
        InnerRequest<?> request;
        while (true) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            // release previous request object to avoid leaking request object when mQueue is drained.
            // ???queue?
            request = null;
            try {
                // ?
                request = mQueue.take();
            } catch (InterruptedException e) {
                if (mQuit) {
                    return;
                }
                continue;
            }

            try {
                // ????
                if (request.isCanceled()) {
                    request.finish("network-discard-cancelled");
                    continue;
                }

                // 
                NetworkResponse networkResponse = mNetwork.performRequest(request);

                // ?304???
                // ?????????
                if (networkResponse.notModified && request.hasHadResponseDelivered()) {
                    request.finish("not-modified");
                    continue;
                }

                // ??
                InnerResponse<?> response = request.parseNetworkResponse(networkResponse);

                // 
                // TODO: 304??metadata??
                if (request.shouldCache() && response.cacheEntry != null) {
                    mCache.put(request.getCacheKey(), response.cacheEntry);
                }

                // ??
                request.markDelivered();
                mDelivery.postResponse(request, response);
            } catch (SoarError soarError) {
                //??
                soarError.setNetworkTimeMs(stopwatch.stop().elapsed(TimeUnit.MILLISECONDS));
                parseAndDeliverNetworkError(request, soarError);
            } catch (Exception e) {
                //???SoarError
                LOG.error("Unhandled exception {}", e);
                SoarError soarError = new SoarError(e);
                soarError.setNetworkTimeMs(stopwatch.stop().elapsed(TimeUnit.MILLISECONDS));
                mDelivery.postError(request, soarError);
            } finally {
                if (stopwatch.isRunning()) {
                    stopwatch.stop();
                }
            }
        }
    }

    private void parseAndDeliverNetworkError(InnerRequest<?> request, SoarError error) {
        error = request.parseNetworkError(error);
        mDelivery.postError(request, error);
    }
}