1 /**
2 * AUTOHIT 2003
3 * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Additional license information can be found in the documentation.
19 * @author Erich P Gatejen
20 */
21 package autohit.common.channels;
22
23 import java.util.Hashtable;
24 import java.util.Enumeration;
25
26 /**
27 * Channel implementation
28 *
29 * Supports a simple, exclusive channel. There is no routing, but just a single
30 * priority level and type. Everything less than or equal to the priority set will
31 * match. Type is unimportant. It will accept multiple injectors, but only one Drain. Registering
32 * a new drain will replace the old. Only injection is thread safe. Nothing
33 * else is.
34 * <p>
35 * The injectors are unimporant. We'll just toss them in a table and
36 * let them rot.
37 * @author Erich P. Gatejen
38 * @version 1.0
39 * <i>Version History</i>
40 * <code>EPG - Initial - 25Apr03</code>
41 */
42 public class SimpleChannel implements Channel {
43
44 /**
45 * Injectors
46 */
47 private Hashtable injectors;
48
49 /**
50 * Drain
51 */
52 private Drain myDrain;
53
54 /**
55 * Priority level
56 */
57 private int priority;
58
59 /**
60 * Receipt values. let it overflow (java spec says it will do so ok)
61 */
62 private int next;
63
64 /**
65 * Default constructor
66 */
67 public SimpleChannel() {
68 myDrain = null;
69 injectors = new Hashtable();
70 next = 0;
71 priority = Atom.P_NONE;
72 }
73
74 /**
75 * Register an injector
76 * @param name reference
77 * @param i An injector
78 * @see autohit.common.channels.Injector
79 */
80 public void register(String name, Injector i) throws ChannelException {
81 if (!injectors.containsKey(name)) {
82 injectors.put(name, i);
83 }
84 i.setChannel(this);
85 }
86
87 /**
88 * Register a drain
89 * @param name reference
90 * @param d A drain
91 * @see autohit.common.channels.Drain
92 */
93 public void register(String name, Drain d) throws ChannelException {
94 synchronized(this) {
95 myDrain = d;
96 //don't care about the name
97 }
98 }
99
100 /**
101 * Get a drain by name
102 * @param name Name reference to the drain
103 * @return Drain reference or null if not found
104 * @see autohit.common.channels.Drain
105 */
106 public Drain getDrain(String name) throws ChannelException {
107 // Dont care about the name
108 return myDrain;
109 }
110
111 /**
112 * Get an injector by name
113 * @param name Name reference to the injector
114 * @return Injector reference or null if not found
115 * @see autohit.common.channels.Drain
116 */
117 public Injector getInjector(String name) throws ChannelException {
118 if (!injectors.containsKey(name)) {
119 return null;
120 } else {
121 return (Injector)injectors.get(name);
122 }
123 }
124
125 /**
126 * Remove an injector
127 * @param name reference
128 * @see autohit.common.channels.Injector
129 */
130 public void removeInjector(String name) throws ChannelException {
131 if (!injectors.containsKey(name)) {
132 throw new ChannelException("Injector " + name + "not registered", ChannelException.CODE_CHANNEL_INJECTOR_INVALID_ERROR);
133 } else {
134 Injector i = (Injector) injectors.remove(name);
135 i.setChannel(null);
136 }
137 }
138
139 /**
140 * Remove a drain
141 * @param name reference
142 * @see autohit.common.channels.Drain
143 */
144 public void removeDrain(String name) throws ChannelException {
145 synchronized(this) {
146 myDrain = null;
147 }
148 }
149
150 /**
151 * Typically called by an injector
152 * @param a An item
153 * @return receipt or null if failed
154 */
155 public Receipt inject(Atom a) throws ChannelException {
156 Receipt r = new Receipt();
157 r.setAsInteger(next++);
158
159 // Priority check
160 if (a.priority <= priority) {
161 synchronized(this) {
162 if (myDrain != null) {
163 myDrain.post(a);
164
165 } else {
166 r = null;
167 }
168
169 } // end critical section
170 }
171 return r;
172 }
173
174 /**
175 * Request priority level for named Drain
176 * @param name Drain's name
177 * @param level the level as specifies in an Atom
178 * @see autohit.common.channels.Atom
179 */
180 public Receipt requestLevel(String name, int level) throws ChannelException {
181 Receipt r = new Receipt();
182 r.setAsInteger(next++);
183
184 if ((level < Atom.P_NONE)||(level > Atom.P_TOP)) {
185 throw new ChannelException("SimpleChannel: Bad priority level requested:" + level, ChannelException.CODE_CHANNEL_BAD_PRIORITY_LEVEL_ERROR);
186 }
187
188 synchronized(this) {
189 priority = level;
190 }
191 return r;
192 }
193
194 /**
195 * Remove level for named Drain
196 * @param name Drain's name
197 * @param level the level as specifies in an Atom
198 * @see autohit.common.channels.Atom
199 */
200 public Receipt removeLevel(String name, int level) throws ChannelException {
201 // Don't care
202 Receipt r = new Receipt();
203 r.setAsInteger(next++);
204 return r;
205 }
206
207 /**
208 * Request type
209 * @param name Drain's name
210 * @param type the type as specified in Atom
211 * @see autohit.common.channels.Atom
212 */
213 public Receipt requestType(String name, int type) throws ChannelException {
214 // Don't care
215 Receipt r = new Receipt();
216 r.setAsInteger(next++);
217 return r;
218 }
219
220 /**
221 * Request type
222 * @param name Drain's name
223 * @param type the type as specified in Atom
224 * @see autohit.common.channels.Atom
225 */
226 public Receipt removeType(String name, int type) throws ChannelException {
227 // Don't care
228 Receipt r = new Receipt();
229 r.setAsInteger(next++);
230 return r;
231 }
232
233 /**
234 * Set exclusive
235 * @param name Drain's name
236 */
237 public Receipt setExclusive(String name) throws ChannelException {
238 // always exclusive
239 // Don't care
240 Receipt r = new Receipt();
241 r.setAsInteger(next++);
242 return r;
243 }
244
245 /**
246 * Remove exclusive
247 * @param name Drain's name
248 */
249 public Receipt removeExclusive(String name) throws ChannelException {
250 // always exclusive
251 // Don't care
252 Receipt r = new Receipt();
253 r.setAsInteger(next++);
254 return r;
255 }
256
257 /**
258 * Enumerate injectors
259 *
260 * @return an enumeration of injectors
261 * @see autohit.common.channels.Injector
262 */
263 public Enumeration enumInjector() throws ChannelException {
264 return injectors.keys();
265 }
266
267 }
|