Android Open Source - Volley-Ball Local Dispatcher






From Project

Back to project page Volley-Ball.

License

The source code is released under:

MIT License

If you think the Android project Volley-Ball listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.siu.android.volleyball.local;
//from ww  w. jav a2 s  .c o  m
import android.os.Process;

import com.android.volley.VolleyLog;
import com.siu.android.volleyball.BallRequest;
import com.siu.android.volleyball.BallResponse;
import com.siu.android.volleyball.BallResponseDelivery;

import java.util.concurrent.BlockingQueue;

/**
 * Created by lukas on 8/26/13.
 */
public class LocalDispatcher extends Thread {

    private static final boolean DEBUG = VolleyLog.DEBUG;

    /**
     * The queue of requests coming in for triage.
     */
    private final BlockingQueue<BallRequest> mRequestQueue;

    private final BlockingQueue<BallRequest> mNetworkQueue;

    /**
     * For posting responses.
     */
    private final BallResponseDelivery mDelivery;

    /**
     * Used for telling us to die.
     */
    private volatile boolean mQuit = false;

    /**
     * Creates a new cache triage dispatcher thread.  You must call {@link #start()}
     * in order to begin processing.
     *
     * @param requestQueue Queue of incoming requests for triage
     * @param delivery     Delivery interface to use for posting responses
     */
    public LocalDispatcher(BlockingQueue<BallRequest> requestQueue, BlockingQueue<BallRequest> networkQueue, BallResponseDelivery delivery) {
        mRequestQueue = requestQueue;
        mNetworkQueue = networkQueue;
        mDelivery = delivery;
    }

    /**
     * Forces this dispatcher to quit immediately.  If any requests are still in
     * the queue, they are not guaranteed to be processed.
     */
    public void quit() {
        mQuit = true;
        interrupt();
    }

    @Override
    public void run() {
        if (DEBUG) VolleyLog.v("start new dispatcher");
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

        while (dispatch()) {

        }
    }

    /**
     *
     * @return true to stay in the dispatch loop or false to quit
     */
    protected boolean dispatch() {
        try {
            // Get a request from the local triage queue, blocking until
            // at least one is available.
            final BallRequest request = mRequestQueue.take();
            request.addMarker("local-queue-take");

            // If the request has been canceled, don't bother dispatching it.
            if (request.isCanceled()) {
                request.finish("local-discard-canceled");
                return true;
            }

            Object responseContent = request.getLocalRequestProcessor().getLocalResponse();

            // Do not deliver local response if null
            // Let a chance to the cache to deliver a better response
            if (responseContent == null) {
                request.addMarker("local-response-content-null-exit");
                mDelivery.postEmptyIntermediateResponse(request, BallResponse.ResponseSource.LOCAL);
                return true;
            }

            request.addMarker("local-response-get-content-successful");

            BallResponse response = BallResponse.success(responseContent, null);
            response.setResponseSource(BallResponse.ResponseSource.LOCAL);
            response.setIntermediate(true);

            // Post the intermediate response back to the user and have
            // the delivery then forward the request along to the network.
            mDelivery.postResponse(request, response);

        } catch (InterruptedException e) {
            // We may have been interrupted because it was time to quit.
            if (mQuit) {
                return false;
            }
            return true;
        }

        return true;
    }
}




Java Source Code List

.BallExecutorDelivery.java
.LocalRequest.java
com.android.volley.CompatRequest.java
com.siu.android.volleyball.BallCacheDispatcher.java
com.siu.android.volleyball.BallMarkerLog.java
com.siu.android.volleyball.BallNetworkDispatcher.java
com.siu.android.volleyball.BallRequestQueue.java
com.siu.android.volleyball.BallRequest.java
com.siu.android.volleyball.BallResponseDelivery.java
com.siu.android.volleyball.BallResponse.java
com.siu.android.volleyball.ball.BallExecutorDelivery.java
com.siu.android.volleyball.exception.BallException.java
com.siu.android.volleyball.local.LocalDispatcher.java
com.siu.android.volleyball.local.LocalRequestProcessor.java
com.siu.android.volleyball.mock.FileMockNetwork.java
com.siu.android.volleyball.network.NetworkRequestProcessor.java
com.siu.android.volleyball.request.CompleteRequest.java
com.siu.android.volleyball.request.LocalRequest.java
com.siu.android.volleyball.request.NetworkRequest.java
com.siu.android.volleyball.response.ResponseListener.java
com.siu.android.volleyball.response.SingleResponseListener.java
com.siu.android.volleyball.samples.Application.java
com.siu.android.volleyball.samples.Constants.java
com.siu.android.volleyball.samples.activity.CompleteRequestActivity.java
com.siu.android.volleyball.samples.activity.LocalRequestActivity.java
com.siu.android.volleyball.samples.activity.NetworkRequestActivity.java
com.siu.android.volleyball.samples.activity.ScenariosActivity.java
com.siu.android.volleyball.samples.activity.scenarios.Scenario10Activity.java
com.siu.android.volleyball.samples.activity.scenarios.Scenario1Activity.java
com.siu.android.volleyball.samples.activity.scenarios.Scenario2Activity.java
com.siu.android.volleyball.samples.activity.scenarios.Scenario3Activity.java
com.siu.android.volleyball.samples.activity.scenarios.Scenario4Activity.java
com.siu.android.volleyball.samples.activity.scenarios.Scenario5Activity.java
com.siu.android.volleyball.samples.activity.scenarios.Scenario6Activity.java
com.siu.android.volleyball.samples.activity.scenarios.Scenario7Activity.java
com.siu.android.volleyball.samples.activity.scenarios.Scenario8Activity.java
com.siu.android.volleyball.samples.activity.scenarios.Scenario9Activity.java
com.siu.android.volleyball.samples.activity.scenarios.ScenarioActivity.java
com.siu.android.volleyball.samples.adapter.BindableAdapter.java
com.siu.android.volleyball.samples.adapter.EntriesAdapter.java
com.siu.android.volleyball.samples.adapter.LogsAdapter.java
com.siu.android.volleyball.samples.database.DatabaseHelper.java
com.siu.android.volleyball.samples.database.EntryDao.java
com.siu.android.volleyball.samples.database.mapping.EntryMapping.java
com.siu.android.volleyball.samples.fragment.LocalOnlyRequestFragment.java
com.siu.android.volleyball.samples.model.Entry.java
com.siu.android.volleyball.samples.model.Log.java
com.siu.android.volleyball.samples.util.ScenarioUtils.java
com.siu.android.volleyball.samples.util.SimpleLogger.java
com.siu.android.volleyball.samples.volley.OkHttpStack.java
com.siu.android.volleyball.samples.volley.ScenarioListener.java
com.siu.android.volleyball.samples.volley.fake.FakeCache.java
com.siu.android.volleyball.samples.volley.fake.FakeNetwork.java
com.siu.android.volleyball.samples.volley.request.CompleteEntryRequest.java
com.siu.android.volleyball.samples.volley.request.SampleErrorNetworkRequest.java
com.siu.android.volleyball.samples.volley.request.SampleLocalNoResultRequest.java
com.siu.android.volleyball.samples.volley.request.SampleLocalRequest.java
com.siu.android.volleyball.samples.volley.request.SampleNetworkRequest.java
com.siu.android.volleyball.samples.volley.request.SampleRequest.java
com.siu.android.volleyball.samples.volley.request.ScenarioRequest.java
com.siu.android.volleyball.toolbox.BallImageLoader.java
com.siu.android.volleyball.toolbox.BallImageRequest.java
com.siu.android.volleyball.toolbox.BallNetworkImageView.java
com.siu.android.volleyball.toolbox.BallRequestFuture.java
com.siu.android.volleyball.toolbox.VolleyBallConfig.java
com.siu.android.volleyball.toolbox.VolleyBall.java
com.siu.android.volleyball.util.BallLogger.java
com.siu.android.volleyball.util.ConfigUtils.java
com.siu.android.volleyball.util.RequestUtils.java