1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
package com.sun.grizzly.async; |
44 | |
|
45 | |
import com.sun.grizzly.Controller; |
46 | |
import com.sun.grizzly.SelectorHandler; |
47 | |
import com.sun.grizzly.async.AsyncQueue.AsyncQueueEntry; |
48 | |
import com.sun.grizzly.util.ByteBufferFactory; |
49 | |
import java.io.IOException; |
50 | |
import java.net.SocketAddress; |
51 | |
import java.nio.ByteBuffer; |
52 | |
import java.nio.channels.SelectableChannel; |
53 | |
import java.nio.channels.SelectionKey; |
54 | |
import java.nio.channels.WritableByteChannel; |
55 | |
import java.util.concurrent.ConcurrentLinkedQueue; |
56 | |
import java.util.concurrent.atomic.AtomicReference; |
57 | |
import java.util.concurrent.locks.ReentrantLock; |
58 | |
import java.util.logging.Level; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public abstract class AbstractAsyncQueueWriter implements AsyncQueueWriter { |
65 | |
private SelectorHandler selectorHandler; |
66 | |
private AsyncQueue<SelectableChannel, AsyncWriteQueueRecord> writeQueue; |
67 | |
private ConcurrentLinkedQueue<AsyncWriteQueueRecord> recordQueue; |
68 | |
|
69 | 136 | public AbstractAsyncQueueWriter(SelectorHandler selectorHandler) { |
70 | 136 | this.selectorHandler = selectorHandler; |
71 | 136 | writeQueue = new AsyncQueue<SelectableChannel, AsyncWriteQueueRecord>(); |
72 | 136 | recordQueue = new ConcurrentLinkedQueue<AsyncWriteQueueRecord>(); |
73 | 136 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public void write(SelectionKey key, ByteBuffer buffer) throws IOException { |
79 | 0 | write(key, null, buffer, null); |
80 | 0 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public void write(SelectionKey key, ByteBuffer buffer, |
86 | |
AsyncWriteCallbackHandler callbackHandler) throws IOException { |
87 | 0 | write(key, null, buffer, callbackHandler, null); |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public void write(SelectionKey key, ByteBuffer buffer, |
94 | |
AsyncWriteCallbackHandler callbackHandler, |
95 | |
AsyncQueueDataProcessor writePreProcessor) throws IOException { |
96 | 0 | write(key, null, buffer, callbackHandler, writePreProcessor, false); |
97 | 0 | } |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public void write(SelectionKey key, ByteBuffer buffer, |
103 | |
AsyncWriteCallbackHandler callbackHandler, |
104 | |
AsyncQueueDataProcessor writePreProcessor, boolean isCloneByteBuffer) |
105 | |
throws IOException { |
106 | 820902 | write(key, null, buffer, callbackHandler, writePreProcessor, isCloneByteBuffer); |
107 | 820866 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public void write(SelectionKey key, SocketAddress dstAddress, |
113 | |
ByteBuffer buffer) throws IOException { |
114 | 0 | write(key, dstAddress, buffer, null); |
115 | 0 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public void write(SelectionKey key, SocketAddress dstAddress, |
121 | |
ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler) |
122 | |
throws IOException { |
123 | 0 | write(key, dstAddress, buffer, callbackHandler, null); |
124 | 0 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public void write(SelectionKey key, SocketAddress dstAddress, |
130 | |
ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler, |
131 | |
AsyncQueueDataProcessor writePreProcessor) throws IOException { |
132 | 0 | write(key, dstAddress, buffer, callbackHandler, writePreProcessor, false); |
133 | 0 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public void write(SelectionKey key, SocketAddress dstAddress, |
139 | |
ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler, |
140 | |
AsyncQueueDataProcessor writePreProcessor, boolean isCloneByteBuffer) |
141 | |
throws IOException { |
142 | |
|
143 | 840898 | if (key == null) { |
144 | 0 | throw new IOException("SelectionKey is null! " + |
145 | |
"Probably key was cancelled or connection was closed?"); |
146 | |
} |
147 | |
|
148 | 840903 | SelectableChannel channel = key.channel(); |
149 | 840903 | AsyncQueueEntry channelEntry = |
150 | |
writeQueue.obtainAsyncQueueEntry(channel); |
151 | |
|
152 | 840898 | ConcurrentLinkedQueue<AsyncWriteQueueRecord> queue = channelEntry.queue; |
153 | 840903 | AtomicReference<AsyncWriteQueueRecord> currentElement = channelEntry.currentElement; |
154 | 840903 | ReentrantLock lock = channelEntry.queuedActionLock; |
155 | |
|
156 | |
|
157 | |
try { |
158 | 840903 | AsyncWriteQueueRecord record = null; |
159 | |
|
160 | 840903 | if (currentElement.get() == null && |
161 | |
lock.tryLock()) { |
162 | 112793 | record = obtainRecord(); |
163 | |
|
164 | 112793 | if (currentElement.compareAndSet(null, record)) { |
165 | 112790 | doWrite((WritableByteChannel) channel, dstAddress, buffer, |
166 | |
writePreProcessor); |
167 | |
} else { |
168 | 3 | lock.unlock(); |
169 | |
} |
170 | |
} |
171 | |
|
172 | 840888 | if (buffer.hasRemaining() || |
173 | |
(lock.isHeldByCurrentThread() && writePreProcessor != null && |
174 | |
writePreProcessor.getInternalByteBuffer().hasRemaining())) { |
175 | 728113 | if (record == null) { |
176 | 727971 | record = obtainRecord(); |
177 | |
} |
178 | |
|
179 | |
|
180 | 728113 | if (isCloneByteBuffer) { |
181 | 365 | int size = buffer.remaining(); |
182 | 365 | ByteBuffer newBuffer = ByteBufferFactory.allocateView( |
183 | |
size, buffer.isDirect()); |
184 | |
|
185 | 365 | newBuffer.put(buffer); |
186 | 365 | newBuffer.position(0); |
187 | 365 | buffer = newBuffer; |
188 | |
} |
189 | |
|
190 | 728113 | record.set(buffer, callbackHandler, writePreProcessor, dstAddress); |
191 | |
|
192 | 728113 | boolean isRegisterForWriting = false; |
193 | |
|
194 | |
|
195 | 728111 | if (currentElement.get() != record) { |
196 | 728111 | queue.offer(record); |
197 | 728112 | if (!lock.isLocked()) { |
198 | 271280 | isRegisterForWriting = true; |
199 | |
} |
200 | |
} else { |
201 | 0 | isRegisterForWriting = true; |
202 | 0 | lock.unlock(); |
203 | |
} |
204 | |
|
205 | 727820 | if (isRegisterForWriting) { |
206 | 271280 | registerForWriting(key); |
207 | |
} |
208 | 728110 | } else { |
209 | |
|
210 | |
|
211 | 112775 | if (callbackHandler != null) { |
212 | 0 | callbackHandler.onWriteCompleted(key, buffer); |
213 | |
} |
214 | |
|
215 | |
|
216 | 112775 | if (lock.isHeldByCurrentThread()) { |
217 | 112775 | AsyncWriteQueueRecord nextRecord = queue.poll(); |
218 | 112775 | if (nextRecord != null) { |
219 | 1493 | currentElement.set(nextRecord); |
220 | 1493 | lock.unlock(); |
221 | 1493 | registerForWriting(key); |
222 | |
} else { |
223 | 111282 | currentElement.set(null); |
224 | 111282 | lock.unlock(); |
225 | 111282 | if (queue.peek() != null) { |
226 | 21 | registerForWriting(key); |
227 | |
} |
228 | |
} |
229 | |
} |
230 | |
|
231 | |
|
232 | 112775 | if (record != null) { |
233 | 112775 | recordQueue.offer(record); |
234 | |
} |
235 | |
} |
236 | 15 | } catch(IOException e) { |
237 | 15 | onClose(channel); |
238 | 15 | throw e; |
239 | |
} finally { |
240 | 840899 | if (lock.isHeldByCurrentThread()) { |
241 | 15 | lock.unlock(); |
242 | |
} |
243 | |
} |
244 | 840888 | } |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
public boolean hasReadyAsyncWriteData(SelectionKey key) { |
250 | 2401 | AsyncQueueEntry channelEntry = |
251 | |
writeQueue.getAsyncQueueEntry(key.channel()); |
252 | |
|
253 | 2401 | return channelEntry != null && (channelEntry.currentElement.get() != null || |
254 | |
(channelEntry.queue != null && !channelEntry.queue.isEmpty())); |
255 | |
} |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
public void onWrite(SelectionKey key) throws IOException { |
261 | 2310 | SelectableChannel channel = key.channel(); |
262 | |
|
263 | 2310 | AsyncQueueEntry channelEntry = |
264 | |
writeQueue.obtainAsyncQueueEntry(channel); |
265 | |
|
266 | 2310 | ConcurrentLinkedQueue<AsyncWriteQueueRecord> queue = channelEntry.queue; |
267 | 2310 | AtomicReference<AsyncWriteQueueRecord> currentElement = channelEntry.currentElement; |
268 | 2310 | ReentrantLock lock = channelEntry.queuedActionLock; |
269 | |
|
270 | 2310 | if (currentElement.get() == null) { |
271 | 45 | AsyncWriteQueueRecord nextRecord = queue.peek(); |
272 | 45 | if (nextRecord != null && lock.tryLock()) { |
273 | 22 | if (!queue.isEmpty() && |
274 | |
currentElement.compareAndSet(null, nextRecord)) { |
275 | 21 | queue.remove(); |
276 | |
} |
277 | |
} else { |
278 | 23 | return; |
279 | |
} |
280 | 22 | } else if (!lock.tryLock()) { |
281 | 773 | return; |
282 | |
} |
283 | |
|
284 | |
try { |
285 | 727132 | while (currentElement.get() != null) { |
286 | 727132 | AsyncWriteQueueRecord queueRecord = currentElement.get(); |
287 | |
|
288 | 727132 | ByteBuffer byteBuffer = queueRecord.byteBuffer; |
289 | 727132 | AsyncQueueDataProcessor writePreProcessor = queueRecord.writePreProcessor; |
290 | |
try { |
291 | 727132 | doWrite((WritableByteChannel) channel, |
292 | |
queueRecord.dstAddress, byteBuffer, writePreProcessor); |
293 | 0 | } catch (IOException e) { |
294 | 0 | if (queueRecord.callbackHandler != null) { |
295 | 0 | queueRecord.callbackHandler.onIOException(e, key, |
296 | |
byteBuffer, queue); |
297 | |
} else { |
298 | 0 | Controller.logger().log(Level.SEVERE, |
299 | |
"Exception occured when executing " + |
300 | |
"asynchronous queue writing", e); |
301 | |
} |
302 | |
|
303 | 0 | onClose(channel); |
304 | 727132 | } |
305 | |
|
306 | |
|
307 | 727132 | if (!byteBuffer.hasRemaining() && |
308 | |
(writePreProcessor == null || |
309 | |
!writePreProcessor.getInternalByteBuffer().hasRemaining())) { |
310 | 727132 | if (queueRecord.callbackHandler != null) { |
311 | 0 | queueRecord.callbackHandler.onWriteCompleted(key, byteBuffer); |
312 | |
} |
313 | |
|
314 | 727132 | currentElement.set(queue.poll()); |
315 | 727132 | recordQueue.offer(queueRecord); |
316 | |
|
317 | |
|
318 | 727132 | if (currentElement.get() == null) { |
319 | 1517 | lock.unlock(); |
320 | 1517 | AsyncWriteQueueRecord nextRecord = queue.peek(); |
321 | 1517 | if (nextRecord != null && lock.tryLock()) { |
322 | 3 | if (!queue.isEmpty() && |
323 | |
currentElement.compareAndSet(null, nextRecord)) { |
324 | 3 | queue.remove(); |
325 | |
} |
326 | |
|
327 | |
continue; |
328 | |
} else { |
329 | |
break; |
330 | |
} |
331 | |
} |
332 | |
} else { |
333 | 0 | lock.unlock(); |
334 | 0 | registerForWriting(key); |
335 | 0 | break; |
336 | |
} |
337 | 725615 | } |
338 | |
} finally { |
339 | 1514 | if (lock.isHeldByCurrentThread()) { |
340 | 0 | channelEntry.queuedActionLock.unlock(); |
341 | |
} |
342 | |
} |
343 | 1514 | } |
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
public void onClose(SelectableChannel channel) { |
349 | 438 | writeQueue.removeEntry(channel); |
350 | 438 | } |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
public void close() { |
356 | 136 | writeQueue.clear(); |
357 | 136 | } |
358 | |
|
359 | |
protected void doWrite(WritableByteChannel channel, SocketAddress dstAddress, |
360 | |
ByteBuffer byteBuffer, AsyncQueueDataProcessor writePreProcessor) |
361 | |
throws IOException { |
362 | 839906 | if (writePreProcessor != null) { |
363 | 563486 | ByteBuffer resultByteBuffer = null; |
364 | |
do { |
365 | 563486 | if (byteBuffer.hasRemaining()) { |
366 | 563487 | writePreProcessor.process(byteBuffer); |
367 | |
} |
368 | |
|
369 | 563486 | resultByteBuffer = writePreProcessor.getInternalByteBuffer(); |
370 | 563487 | doWrite(channel, dstAddress, resultByteBuffer); |
371 | 563470 | } while(byteBuffer.hasRemaining() && |
372 | |
!resultByteBuffer.hasRemaining()); |
373 | 563460 | } else { |
374 | 276435 | doWrite(channel, dstAddress, byteBuffer); |
375 | |
} |
376 | 839887 | } |
377 | |
|
378 | |
protected abstract void doWrite(WritableByteChannel channel, SocketAddress dstAddress, |
379 | |
ByteBuffer byteBuffer) throws IOException; |
380 | |
|
381 | |
protected void registerForWriting(SelectionKey key) { |
382 | 272794 | selectorHandler.register(key, SelectionKey.OP_WRITE); |
383 | 272703 | } |
384 | |
|
385 | |
private AsyncWriteQueueRecord obtainRecord() { |
386 | 840903 | AsyncWriteQueueRecord record = recordQueue.poll(); |
387 | 840903 | if (record == null) { |
388 | 4186 | record = new AsyncWriteQueueRecord(); |
389 | |
} |
390 | |
|
391 | 840876 | return record; |
392 | |
} |
393 | |
} |