OneShotScheduler.java revision 65953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package android.filterfw.core;
19
20import android.filterfw.core.Filter;
21import android.filterfw.core.Scheduler;
22import android.filterfw.core.RoundRobinScheduler;
23import android.util.Log;
24
25import java.util.HashMap;
26
27/**
28 * This OneShotScheduler only schedules source filters at most once. All other
29 * filters will be scheduled, and possibly repeatedly, until there is no filter
30 * that can be scheduled.
31 *
32 * @hide
33 */
34public class OneShotScheduler extends RoundRobinScheduler {
35    private HashMap <String, Integer> scheduled;
36
37    private final boolean mLogVerbose;
38    private static final String TAG = "OneShotScheduler";
39
40    public OneShotScheduler(FilterGraph graph) {
41        super(graph);
42        scheduled = new HashMap<String, Integer>();
43        mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE);
44    }
45
46    @Override
47    public void reset() {
48        super.reset();
49        scheduled.clear();
50    }
51
52    @Override
53    public Filter scheduleNextNode() {
54        Filter first = null;
55        // return the first filter that is not scheduled before.
56        while (true) {
57            Filter filter = super.scheduleNextNode();
58            if (filter == null) {
59                if (mLogVerbose) Log.v(TAG, "No filters available to run.");
60                return null;
61            }
62            if (!scheduled.containsKey(filter.getName())) {
63                if (filter.getNumberOfConnectedInputs() == 0)
64                    scheduled.put(filter.getName(),1);
65                if (mLogVerbose) Log.v(TAG, "Scheduling filter \"" + filter.getName() + "\" of type " + filter.getFilterClassName());
66                return filter;
67            }
68            // if loop back, nothing available
69            if (first == filter) {
70                break;
71            }
72            // save the first scheduled one
73            if (first == null) first = filter;
74        }
75        if (mLogVerbose) Log.v(TAG, "One pass through graph completed.");
76        return null;
77    }
78}
79