FileDocCategorySizeDatePackage
SyncRunner.javaAPI DocAndroid 5.1 API7484Thu Mar 12 22:22:30 GMT 2015android.filterfw.core

SyncRunner

public class SyncRunner extends GraphRunner
hide

Fields Summary
private Scheduler
mScheduler
private OnRunnerDoneListener
mDoneListener
private ScheduledThreadPoolExecutor
mWakeExecutor
private android.os.ConditionVariable
mWakeCondition
private StopWatchMap
mTimer
private final boolean
mLogVerbose
private static final String
TAG
Constructors Summary
public SyncRunner(FilterContext context, FilterGraph graph, Class schedulerClass)


    // TODO: Provide factory based constructor?
           
        super(context);

        mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE);

        if (mLogVerbose) Log.v(TAG, "Initializing SyncRunner");

        // Create the scheduler
        if (Scheduler.class.isAssignableFrom(schedulerClass)) {
            try {
                Constructor schedulerConstructor = schedulerClass.getConstructor(FilterGraph.class);
                mScheduler = (Scheduler)schedulerConstructor.newInstance(graph);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException("Scheduler does not have constructor <init>(FilterGraph)!", e);
            } catch (InstantiationException e) {
                throw new RuntimeException("Could not instantiate the Scheduler instance!", e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Cannot access Scheduler constructor!", e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException("Scheduler constructor threw an exception", e);
            } catch (Exception e) {
                throw new RuntimeException("Could not instantiate Scheduler", e);
            }
        } else {
            throw new IllegalArgumentException("Class provided is not a Scheduler subclass!");
        }

        // Associate this runner and the graph with the context
        mFilterContext = context;
        mFilterContext.addGraph(graph);

        mTimer = new StopWatchMap();

        if (mLogVerbose) Log.v(TAG, "Setting up filters");

        // Setup graph filters
        graph.setupFilters();
    
Methods Summary
voidassertReadyToStep()

        if (mScheduler == null) {
            throw new RuntimeException("Attempting to run schedule with no scheduler in place!");
        } else if (getGraph() == null) {
            throw new RuntimeException("Calling step on scheduler with no graph in place!");
        }
    
public voidbeginProcessing()

        mScheduler.reset();
        getGraph().beginProcessing();
    
public voidclose()

        // Close filters
        if (mLogVerbose) Log.v(TAG, "Closing graph.");
        getGraph().closeFilters(mFilterContext);
        mScheduler.reset();
    
protected intdeterminePostRunState()

        boolean isBlocked = false;
        for (Filter filter : mScheduler.getGraph().getFilters()) {
            if (filter.isOpen()) {
                if (filter.getStatus() == Filter.STATUS_SLEEPING) {
                    // If ANY node is sleeping, we return our state as sleeping
                    return RESULT_SLEEPING;
                } else {
                    // If a node is still open, it is blocked (by input or output)
                    return RESULT_BLOCKED;
                }
            }
        }
        return RESULT_FINISHED;
    
public synchronized java.lang.ExceptiongetError()

        return null;
    
public FilterGraphgetGraph()

        return mScheduler != null ? mScheduler.getGraph() : null;
    
public booleanisRunning()

        return false;
    
booleanperformStep()

        if (mLogVerbose) Log.v(TAG, "Performing one step.");
        Filter filter = mScheduler.scheduleNextNode();
        if (filter != null) {
            mTimer.start(filter.getName());
            processFilterNode(filter);
            mTimer.stop(filter.getName());
            return true;
        } else {
            return false;
        }
    
protected voidprocessFilterNode(Filter filter)

        if (mLogVerbose) Log.v(TAG, "Processing filter node");
        filter.performProcess(mFilterContext);
        if (filter.getStatus() == Filter.STATUS_ERROR) {
            throw new RuntimeException("There was an error executing " + filter + "!");
        } else if (filter.getStatus() == Filter.STATUS_SLEEPING) {
            if (mLogVerbose) Log.v(TAG, "Scheduling filter wakeup");
            scheduleFilterWake(filter, filter.getSleepDelay());
        }
    
public voidrun()

        if (mLogVerbose) Log.v(TAG, "Beginning run.");

        assertReadyToStep();

        // Preparation
        beginProcessing();
        boolean glActivated = activateGlContext();

        // Run
        boolean keepRunning = true;
        while (keepRunning) {
            keepRunning = performStep();
        }

        // Cleanup
        if (glActivated) {
            deactivateGlContext();
        }

        // Call completion callback if set
        if (mDoneListener != null) {
            if (mLogVerbose) Log.v(TAG, "Calling completion listener.");
            mDoneListener.onRunnerDone(determinePostRunState());
        }
        if (mLogVerbose) Log.v(TAG, "Run complete");
    
protected voidscheduleFilterWake(Filter filter, int delay)

        // Close the wake condition
        mWakeCondition.close();

        // Schedule the wake-up
        final Filter filterToSchedule = filter;
        final ConditionVariable conditionToWake = mWakeCondition;

        mWakeExecutor.schedule(new Runnable() {
          @Override
          public void run() {
                filterToSchedule.unsetStatus(Filter.STATUS_SLEEPING);
                conditionToWake.open();
            }
        }, delay, TimeUnit.MILLISECONDS);
    
public voidsetDoneCallback(OnRunnerDoneListener listener)

        mDoneListener = listener;
    
public intstep()

        assertReadyToStep();
        if (!getGraph().isReady() ) {
            throw new RuntimeException("Trying to process graph that is not open!");
        }
        return performStep() ? RESULT_RUNNING : determinePostRunState();
    
public voidstop()

        throw new RuntimeException("SyncRunner does not support stopping a graph!");
    
protected voidwaitUntilWake()

        mWakeCondition.block();