Home | History | Annotate | Download | only in core
      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 
     18 package android.filterfw.core;
     19 
     20 import android.filterfw.core.Filter;
     21 import android.filterfw.core.FrameFormat;
     22 import android.util.Log;
     23 
     24 /**
     25  * @hide
     26  */
     27 public abstract class FilterPort {
     28 
     29     protected Filter mFilter;
     30     protected String mName;
     31     protected FrameFormat mPortFormat;
     32     protected boolean mIsBlocking = true;
     33     protected boolean mIsOpen = false;
     34     protected boolean mChecksType = false;
     35     private boolean mLogVerbose;
     36     private static final String TAG = "FilterPort";
     37 
     38     public FilterPort(Filter filter, String name) {
     39         mName = name;
     40         mFilter = filter;
     41         mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE);
     42     }
     43 
     44     public boolean isAttached() {
     45         return mFilter != null;
     46     }
     47 
     48     public FrameFormat getPortFormat() {
     49         return mPortFormat;
     50     }
     51 
     52     public void setPortFormat(FrameFormat format) {
     53         mPortFormat = format;
     54     }
     55 
     56     public Filter getFilter() {
     57         return mFilter;
     58     }
     59 
     60     public String getName() {
     61         return mName;
     62     }
     63 
     64     public void setBlocking(boolean blocking) {
     65         mIsBlocking = blocking;
     66     }
     67 
     68     public void setChecksType(boolean checksType) {
     69         mChecksType = checksType;
     70     }
     71 
     72     public void open() {
     73         if (!mIsOpen) {
     74             if (mLogVerbose) Log.v(TAG, "Opening " + this);
     75         }
     76         mIsOpen = true;
     77     }
     78 
     79     public void close() {
     80         if (mIsOpen) {
     81             if (mLogVerbose) Log.v(TAG, "Closing " + this);
     82         }
     83         mIsOpen = false;
     84     }
     85 
     86     public boolean isOpen() {
     87         return mIsOpen;
     88     }
     89 
     90     public boolean isBlocking() {
     91         return mIsBlocking;
     92     }
     93 
     94     public abstract boolean filterMustClose();
     95 
     96     public abstract boolean isReady();
     97 
     98     public abstract void pushFrame(Frame frame);
     99 
    100     public abstract void setFrame(Frame frame);
    101 
    102     public abstract Frame pullFrame();
    103 
    104     public abstract boolean hasFrame();
    105 
    106     public abstract void clear();
    107 
    108     public String toString() {
    109         return "port '" + mName + "' of " + mFilter;
    110     }
    111 
    112     protected void assertPortIsOpen() {
    113         if (!isOpen()) {
    114             throw new RuntimeException("Illegal operation on closed " + this + "!");
    115         }
    116     }
    117 
    118     protected void checkFrameType(Frame frame, boolean forceCheck) {
    119         if ((mChecksType || forceCheck)
    120             && mPortFormat != null
    121             && !frame.getFormat().isCompatibleWith(mPortFormat)) {
    122             throw new RuntimeException("Frame passed to " + this + " is of incorrect type! "
    123                 + "Expected " + mPortFormat + " but got " + frame.getFormat());
    124         }
    125     }
    126 
    127     protected void checkFrameManager(Frame frame, FilterContext context) {
    128         if (frame.getFrameManager() != null
    129             && frame.getFrameManager() != context.getFrameManager()) {
    130             throw new RuntimeException("Frame " + frame + " is managed by foreign FrameManager! ");
    131         }
    132     }
    133 }
    134 
    135