Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ReadFilter |
|
| 0.0;0 |
1 | /* | |
2 | * | |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
4 | * | |
5 | * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved. | |
6 | * | |
7 | * The contents of this file are subject to the terms of either the GNU | |
8 | * General Public License Version 2 only ("GPL") or the Common Development | |
9 | * and Distribution License("CDDL") (collectively, the "License"). You | |
10 | * may not use this file except in compliance with the License. You can obtain | |
11 | * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html | |
12 | * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific | |
13 | * language governing permissions and limitations under the License. | |
14 | * | |
15 | * When distributing the software, include this License Header Notice in each | |
16 | * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. | |
17 | * Sun designates this particular file as subject to the "Classpath" exception | |
18 | * as provided by Sun in the GPL Version 2 section of the License file that | |
19 | * accompanied this code. If applicable, add the following below the License | |
20 | * Header, with the fields enclosed by brackets [] replaced by your own | |
21 | * identifying information: "Portions Copyrighted [year] | |
22 | * [name of copyright owner]" | |
23 | * | |
24 | * Contributor(s): | |
25 | * | |
26 | * If you wish your version of this file to be governed by only the CDDL or | |
27 | * only the GPL Version 2, indicate your decision by adding "[Contributor] | |
28 | * elects to include this software in this distribution under the [CDDL or GPL | |
29 | * Version 2] license." If you don't indicate a single choice of license, a | |
30 | * recipient has the option to distribute your version of this file under | |
31 | * either the CDDL, the GPL Version 2 or to extend the choice of license to | |
32 | * its licensees as provided above. However, if you add GPL Version 2 code | |
33 | * and therefore, elected the GPL Version 2 license, then the option applies | |
34 | * only if the new code is made subject to such option by the copyright | |
35 | * holder. | |
36 | * | |
37 | */ | |
38 | ||
39 | package com.sun.grizzly.filter; | |
40 | ||
41 | import com.sun.grizzly.Context; | |
42 | import com.sun.grizzly.Controller; | |
43 | import com.sun.grizzly.ProtocolFilter; | |
44 | import com.sun.grizzly.util.WorkerThread; | |
45 | import java.net.SocketAddress; | |
46 | import java.io.IOException; | |
47 | import java.nio.ByteBuffer; | |
48 | import java.nio.channels.DatagramChannel; | |
49 | import java.nio.channels.SelectionKey; | |
50 | import java.nio.channels.SocketChannel; | |
51 | import java.util.logging.Level; | |
52 | ||
53 | import static com.sun.grizzly.Controller.Protocol.TCP; | |
54 | import static com.sun.grizzly.Controller.Protocol.TLS; | |
55 | import static com.sun.grizzly.Controller.Protocol.UDP; | |
56 | import com.sun.grizzly.Controller.Protocol; | |
57 | import com.sun.grizzly.SelectorHandler; | |
58 | ||
59 | /** | |
60 | * Simple {@link ProtocolFilter} implementation which read the available bytes | |
61 | * and delegate the processing to the next {@link ProtocolFilter} in the {@link ProtocolChain}. | |
62 | * If no bytes are available, no new {@link ProtocolFilter} will be a invoked and | |
63 | * the connection (SelectionKey) will be cancelled. This filter can be used | |
64 | * for both UDP (reveive) and TCP (read). | |
65 | * | |
66 | * Note that all ready OP_WRITE operations will be ignored. | |
67 | * | |
68 | * @author Jeanfrancois Arcand | |
69 | */ | |
70 | public class ReadFilter implements ProtocolFilter{ | |
71 | ||
72 | public final static String UDP_SOCKETADDRESS = "socketAddress"; | |
73 | ||
74 | ||
75 | /** | |
76 | * <tt>true</tt> if a pipelined execution is required. A pipelined execution | |
77 | * occurs when a ProtocolFilter implementation set the | |
78 | * ProtocolFilter.READ_SUCCESS as an attribute to a Context. When this | |
79 | * attribute is present, the ProtocolChain will not release the current | |
80 | * running Thread and will re-execute all its ProtocolFilter. | |
81 | */ | |
82 | 33 | protected boolean continousExecution = true; |
83 | ||
84 | ||
85 | 33 | public ReadFilter(){ |
86 | 33 | } |
87 | ||
88 | /** | |
89 | * Read available bytes and delegate the processing of them to the next | |
90 | * {@link ProtocolFilter} in the {@link ProtocolChain}. | |
91 | * @return <tt>true</tt> if the next ProtocolFilter on the ProtocolChain | |
92 | * need to bve invoked. | |
93 | */ | |
94 | public boolean execute(Context ctx) throws IOException { | |
95 | 67682 | return execute(ctx, null); |
96 | } | |
97 | ||
98 | ||
99 | /** | |
100 | * Read available bytes to the specific {@link ByteBuffer} and delegate | |
101 | * the processing of them to the next ProtocolFilter in the ProtocolChain. | |
102 | * @return <tt>true</tt> if the next ProtocolFilter on the ProtocolChain | |
103 | * need to bve invoked. | |
104 | */ | |
105 | protected boolean execute(Context ctx, ByteBuffer byteBuffer) throws IOException { | |
106 | ||
107 | 67682 | if (ctx.getCurrentOpType() == Context.OpType.OP_WRITE){ |
108 | 5 | if (Controller.logger().isLoggable(Level.FINE)){ |
109 | 0 | Controller.logger().fine("ReadFilter cannont handle OP_WRITE"); |
110 | } | |
111 | 5 | return false; |
112 | } | |
113 | ||
114 | ||
115 | 67677 | if (byteBuffer == null) { |
116 | 67677 | byteBuffer = ((WorkerThread)Thread.currentThread()).getByteBuffer(); |
117 | } | |
118 | ||
119 | 67677 | if (!byteBuffer.hasRemaining()){ |
120 | 0 | throw new IllegalStateException("ByteBuffer is full: " + byteBuffer); |
121 | } | |
122 | ||
123 | ||
124 | 67677 | boolean invokeNextFilter = true; |
125 | 67677 | int count = -1; |
126 | 67677 | SocketAddress socketAddress = null; |
127 | 67677 | Exception exception = null; |
128 | 67677 | SelectionKey key = ctx.getSelectionKey(); |
129 | ||
130 | 67677 | Protocol protocol = ctx.getProtocol(); |
131 | try { | |
132 | 67677 | int loop = 0; |
133 | 67677 | if (protocol == TCP || protocol == TLS){ |
134 | 46675 | SocketChannel channel = (SocketChannel)key.channel(); |
135 | ||
136 | // As soon as bytes are ready, invoke the next ProtocolFilter. | |
137 | 64061 | while ((count = channel.read(byteBuffer)) == 0) { |
138 | ||
139 | // Avoid calling the Selector. | |
140 | 24947 | if (++loop > 2){ |
141 | 7561 | if (ctx.getKeyRegistrationState() |
142 | != Context.KeyRegistrationState.NONE){ | |
143 | 7561 | ctx.setAttribute(ProtocolFilter.SUCCESSFUL_READ, |
144 | Boolean.FALSE); | |
145 | 7561 | invokeNextFilter = false; |
146 | } | |
147 | break; | |
148 | } | |
149 | } | |
150 | 46673 | } else if (protocol == UDP){ |
151 | 21002 | DatagramChannel datagramChannel = (DatagramChannel)key.channel(); |
152 | 21002 | socketAddress = datagramChannel.receive(byteBuffer); |
153 | 21002 | ctx.getSelectorHandler().register(key, SelectionKey.OP_READ); |
154 | } | |
155 | 2 | } catch (IOException ex) { |
156 | 2 | exception = ex; |
157 | 2 | log("ReadFilter.execute",ex); |
158 | 0 | } catch (RuntimeException ex) { |
159 | 0 | exception = ex; |
160 | 0 | log("ReadFilter.execute",ex); |
161 | } finally { | |
162 | 67677 | if (exception != null){ |
163 | 2 | ctx.setAttribute(Context.THROWABLE,exception); |
164 | 2 | if (protocol != UDP){ |
165 | 2 | ctx.setKeyRegistrationState( |
166 | Context.KeyRegistrationState.CANCEL); | |
167 | } | |
168 | 2 | invokeNextFilter = false; |
169 | 67675 | } else if (count == -1 && protocol != UDP){ |
170 | 45 | ctx.setKeyRegistrationState( |
171 | Context.KeyRegistrationState.CANCEL); | |
172 | 45 | invokeNextFilter = false; |
173 | 67630 | } else if (socketAddress == null && protocol == UDP ){ |
174 | 0 | ctx.setKeyRegistrationState(Context.KeyRegistrationState.REGISTER); |
175 | 0 | invokeNextFilter = false; |
176 | 67630 | } else if (protocol == UDP) { |
177 | 21002 | ctx.setAttribute(UDP_SOCKETADDRESS,socketAddress); |
178 | } | |
179 | } | |
180 | 67677 | return invokeNextFilter; |
181 | } | |
182 | ||
183 | ||
184 | /** | |
185 | * If no bytes were available, close the connection by cancelling the | |
186 | * SelectionKey. If bytes were available, register the SelectionKey | |
187 | * for new bytes. | |
188 | * | |
189 | * @return <tt>true</tt> if the previous ProtocolFilter postExecute method | |
190 | * needs to be invoked. | |
191 | */ | |
192 | public boolean postExecute(Context ctx) throws IOException { | |
193 | ||
194 | 67679 | final SelectorHandler selectorHandler = |
195 | ctx.getSelectorHandler(); | |
196 | 67679 | final SelectionKey key = ctx.getSelectionKey(); |
197 | 67679 | final Context.KeyRegistrationState state = ctx.getKeyRegistrationState(); |
198 | 67679 | final Protocol protocol = ctx.getProtocol(); |
199 | ||
200 | try{ | |
201 | //For UDP, we don't have to do anything as the OP_READ operations | |
202 | //as already been handled, and cencelling the key is not allowed. | |
203 | 67679 | if (protocol == UDP){ |
204 | 21007 | return true; |
205 | } | |
206 | ||
207 | // The ProtocolChain associated with this ProtocolFilter will re-invoke | |
208 | // the execute method. Do not register the SelectionKey in that case | |
209 | // to avoid thread races. | |
210 | 46672 | if (continousExecution |
211 | && state == Context.KeyRegistrationState.REGISTER | |
212 | && Boolean.FALSE != | |
213 | (Boolean)ctx.getAttribute(ProtocolFilter.SUCCESSFUL_READ)){ | |
214 | 39057 | ctx.setAttribute(ProtocolFilter.SUCCESSFUL_READ, |
215 | Boolean.TRUE); | |
216 | } else { | |
217 | 7615 | if (state == Context.KeyRegistrationState.CANCEL){ |
218 | 47 | selectorHandler.getSelectionKeyHandler().cancel(key); |
219 | 7568 | } else if (state == Context.KeyRegistrationState.REGISTER){ |
220 | 7564 | selectorHandler.register(key, SelectionKey.OP_READ); |
221 | } | |
222 | } | |
223 | 46672 | return true; |
224 | } finally { | |
225 | 67679 | ctx.removeAttribute(Context.THROWABLE); |
226 | 67679 | ctx.removeAttribute(UDP_SOCKETADDRESS); |
227 | } | |
228 | } | |
229 | ||
230 | ||
231 | /** | |
232 | * Set to <tt>true</tt> if the current {@link Pipeline} can | |
233 | * re-execute its ProtocolFilter(s) after a successful execution. Enabling | |
234 | * this property is useful for protocol that needs to support pipelined | |
235 | * message requests as the ProtocolFilter are automatically re-executed, | |
236 | * avoiding the overhead of releasing the current Thread, registering | |
237 | * back the SelectionKey to the {@link SelectorHandler} and waiting for a new | |
238 | * NIO event. | |
239 | * | |
240 | * Some protocols (like http) can get the http headers in one | |
241 | * SocketChannel.read, parse the message and then get the next http message | |
242 | * on the second SocketChannel.read(). Not having to release the Thread | |
243 | * and re-execute the ProtocolFilter greatly improve performance. | |
244 | * @param continousExecution true to enable continuous execution. | |
245 | * (default is false). | |
246 | */ | |
247 | public void setContinuousExecution(boolean continousExecution){ | |
248 | 6 | this.continousExecution = continousExecution; |
249 | 6 | } |
250 | ||
251 | ||
252 | /** | |
253 | * Return <tt>true</tt> if the current {@link Pipeline} can | |
254 | * re-execute its ProtocolFilter after a successful execution. | |
255 | */ | |
256 | public boolean isContinuousExecution(){ | |
257 | 0 | return continousExecution; |
258 | } | |
259 | ||
260 | ||
261 | /** | |
262 | * Log a message/exception. | |
263 | * @param msg <code>String</code> | |
264 | * @param t <code>Throwable</code> | |
265 | */ | |
266 | protected void log(String msg,Throwable t){ | |
267 | 2 | if (Controller.logger().isLoggable(Level.FINE)){ |
268 | 0 | Controller.logger().log(Level.FINE, msg, t); |
269 | } | |
270 | 2 | } |
271 | } |