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 /**
21  * @hide
22  */
23 public class OutputPort extends FilterPort {
24 
25     protected InputPort mTargetPort;
26     protected InputPort mBasePort;
27 
OutputPort(Filter filter, String name)28     public OutputPort(Filter filter, String name) {
29         super(filter, name);
30     }
31 
connectTo(InputPort target)32     public void connectTo(InputPort target) {
33         if (mTargetPort != null) {
34             throw new RuntimeException(this + " already connected to " + mTargetPort + "!");
35         }
36         mTargetPort = target;
37         mTargetPort.setSourcePort(this);
38     }
39 
isConnected()40     public boolean isConnected() {
41         return mTargetPort != null;
42     }
43 
open()44     public void open() {
45         super.open();
46         if (mTargetPort != null && !mTargetPort.isOpen()) {
47             mTargetPort.open();
48         }
49     }
50 
close()51     public void close() {
52         super.close();
53         if (mTargetPort != null && mTargetPort.isOpen()) {
54             mTargetPort.close();
55         }
56     }
57 
getTargetPort()58     public InputPort getTargetPort() {
59         return mTargetPort;
60     }
61 
getTargetFilter()62     public Filter getTargetFilter() {
63         return mTargetPort == null ? null : mTargetPort.getFilter();
64     }
65 
setBasePort(InputPort basePort)66     public void setBasePort(InputPort basePort) {
67         mBasePort = basePort;
68     }
69 
getBasePort()70     public InputPort getBasePort() {
71         return mBasePort;
72     }
73 
filterMustClose()74     public boolean filterMustClose() {
75         return !isOpen() && isBlocking();
76     }
77 
isReady()78     public boolean isReady() {
79         return (isOpen() && mTargetPort.acceptsFrame()) || !isBlocking();
80     }
81 
82     @Override
clear()83     public void clear() {
84         if (mTargetPort != null) {
85             mTargetPort.clear();
86         }
87     }
88 
89     @Override
pushFrame(Frame frame)90     public void pushFrame(Frame frame) {
91         if (mTargetPort == null) {
92             throw new RuntimeException(
93                 "Attempting to push frame on unconnected port: " + this + "!");
94         }
95         mTargetPort.pushFrame(frame);
96     }
97 
98     @Override
setFrame(Frame frame)99     public void setFrame(Frame frame) {
100         assertPortIsOpen();
101         if (mTargetPort == null) {
102             throw new RuntimeException(
103                 "Attempting to set frame on unconnected port: " + this + "!");
104         }
105         mTargetPort.setFrame(frame);
106     }
107 
108     @Override
pullFrame()109     public Frame pullFrame() {
110         throw new RuntimeException("Cannot pull frame on " + this + "!");
111     }
112 
113     @Override
hasFrame()114     public boolean hasFrame() {
115         return mTargetPort == null ? false : mTargetPort.hasFrame();
116     }
117 
118     @Override
toString()119     public String toString() {
120         return "output " + super.toString();
121     }
122 }
123