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 | |
package com.sun.grizzly; |
40 | |
|
41 | |
import com.sun.grizzly.async.AsyncQueueDataProcessor; |
42 | |
import com.sun.grizzly.async.AsyncQueueReadable; |
43 | |
import com.sun.grizzly.async.AsyncQueueReader; |
44 | |
import com.sun.grizzly.async.AsyncQueueWritable; |
45 | |
import com.sun.grizzly.async.AsyncQueueWriter; |
46 | |
import com.sun.grizzly.async.AsyncReadCallbackHandler; |
47 | |
import com.sun.grizzly.async.AsyncReadCondition; |
48 | |
import com.sun.grizzly.async.AsyncWriteCallbackHandler; |
49 | |
import com.sun.grizzly.util.AttributeHolder; |
50 | |
import com.sun.grizzly.util.Copyable; |
51 | |
import com.sun.grizzly.util.SelectionKeyAttachment; |
52 | |
import java.io.IOException; |
53 | |
import java.net.SocketAddress; |
54 | |
import java.nio.ByteBuffer; |
55 | |
import java.nio.channels.SelectionKey; |
56 | |
import java.util.HashMap; |
57 | |
import java.util.Map; |
58 | |
import java.util.logging.Level; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 219806 | public class Context implements AttributeHolder, Copyable { |
67 | |
|
68 | |
|
69 | 6 | public enum AttributeScope { |
70 | 1 | REQUEST, |
71 | 1 | CONNECTION, |
72 | 1 | SELECTOR, |
73 | 1 | CONTROLLER |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | 4 | public enum KeyRegistrationState { |
80 | |
|
81 | 1 | CANCEL, |
82 | |
|
83 | 1 | REGISTER, |
84 | |
|
85 | 1 | NONE } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | 7 | public enum OpType { OP_READ, |
92 | 1 | OP_WRITE, |
93 | 1 | OP_CONNECT, |
94 | 1 | OP_READ_WRITE, |
95 | 1 | OP_ACCEPT |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
private OpType currentOpType; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
private ProtocolChain protocolChain; |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public final static String THROWABLE ="throwable"; |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | 261 | private Map<String,Object> attributes = null; |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
private SelectionKey key; |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
private SelectorHandler selectorHandler; |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
private Controller controller; |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | 261 | private KeyRegistrationState keyRegistrationState |
147 | |
= KeyRegistrationState.REGISTER; |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
private Pipeline pipeline; |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
private IOEvent<Context> ioEvent; |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
private AsyncQueueReader asyncQueueReader; |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
private AsyncQueueWriter asyncQueueWriter; |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
private AsyncQueueReadable asyncQueueReadable; |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
private AsyncQueueWritable asyncQueueWritable; |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | 261 | public Context() { |
187 | 261 | } |
188 | |
|
189 | |
|
190 | |
public void copyTo(Copyable copy) { |
191 | 0 | Context copyContext = (Context) copy; |
192 | 0 | copyContext.currentOpType = currentOpType; |
193 | 0 | copyContext.protocolChain = protocolChain; |
194 | 0 | if (attributes != null) { |
195 | 0 | copyContext.attributes = new HashMap<String, Object>(attributes); |
196 | |
} |
197 | 0 | copyContext.key = key; |
198 | 0 | copyContext.selectorHandler = selectorHandler; |
199 | 0 | copyContext.controller = controller; |
200 | 0 | copyContext.keyRegistrationState = keyRegistrationState; |
201 | 0 | copyContext.pipeline = pipeline; |
202 | 0 | copyContext.ioEvent = ioEvent; |
203 | 0 | copyContext.asyncQueueReader = asyncQueueReader; |
204 | 0 | copyContext.asyncQueueWriter = asyncQueueWriter; |
205 | 0 | copyContext.asyncQueueReadable = asyncQueueReadable; |
206 | 0 | copyContext.asyncQueueWritable = asyncQueueWritable; |
207 | 0 | } |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
public Object removeAttribute(String key){ |
215 | 249305 | if (attributes == null){ |
216 | 7606 | return null; |
217 | |
} |
218 | 241699 | return attributes.remove(key); |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public void setAttribute(String key,Object value){ |
228 | 67736 | if (attributes == null){ |
229 | 112 | attributes = new HashMap<String,Object>(); |
230 | |
} |
231 | 67736 | attributes.put(key,value); |
232 | 67736 | } |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
public Object getAttribute(String key){ |
242 | 67626 | if (attributes == null){ |
243 | 68 | return null; |
244 | |
} |
245 | 67558 | return attributes.get(key); |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
public AttributeHolder getAttributeHolderByScope(AttributeScope scope){ |
258 | 53471 | AttributeHolder holder = null; |
259 | 1 | switch(scope) { |
260 | |
case REQUEST: |
261 | 0 | holder = this; |
262 | 0 | break; |
263 | |
case CONNECTION: |
264 | 53471 | Object attachment = getSelectionKey().attachment(); |
265 | 53471 | if (attachment instanceof AttributeHolder) { |
266 | 53470 | holder = (AttributeHolder) attachment; |
267 | |
} |
268 | |
break; |
269 | |
case SELECTOR: |
270 | 0 | holder = selectorHandler; |
271 | 0 | break; |
272 | |
case CONTROLLER: |
273 | 0 | holder = controller; |
274 | |
break; |
275 | |
} |
276 | |
|
277 | 53471 | return holder; |
278 | |
} |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
public void setAttributes(Map<String, Object> attributes) { |
290 | 0 | this.attributes = attributes; |
291 | 0 | } |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
public Map<String, Object> getAttributes() { |
302 | 0 | return attributes; |
303 | |
} |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
public SelectionKey getSelectionKey() { |
311 | 676713 | return key; |
312 | |
} |
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
public void setSelectionKey(SelectionKey key) { |
320 | 229765 | this.key = key; |
321 | 229765 | } |
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
public Controller getController() { |
329 | 303451 | return controller; |
330 | |
} |
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
public void setController(Controller controller) { |
338 | 405998 | this.controller = controller; |
339 | 405998 | } |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
public void recycle(){ |
346 | 156189 | getProtocolChainInstanceHandler().offer(protocolChain); |
347 | 156188 | key = null; |
348 | 156189 | keyRegistrationState = KeyRegistrationState.REGISTER; |
349 | 156188 | currentOpType = null; |
350 | 156189 | protocolChain = null; |
351 | 156189 | ioEvent = null; |
352 | 156189 | asyncQueueReader = null; |
353 | 156188 | asyncQueueWriter = null; |
354 | 156188 | if (attributes != null) { |
355 | 144942 | attributes.clear(); |
356 | |
} |
357 | 156189 | } |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
public KeyRegistrationState getKeyRegistrationState() { |
365 | 183142 | return keyRegistrationState; |
366 | |
} |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
public void setKeyRegistrationState(KeyRegistrationState keyRegistrationState) { |
375 | 54050 | this.keyRegistrationState = keyRegistrationState; |
376 | 54050 | } |
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
public ProtocolChain getProtocolChain() { |
384 | 82631 | return protocolChain; |
385 | |
} |
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
public void setProtocolChain(ProtocolChain protocolChain) { |
393 | 73567 | this.protocolChain = protocolChain; |
394 | 73567 | } |
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
public OpType getCurrentOpType() { |
402 | 129202 | return currentOpType; |
403 | |
} |
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
public void setCurrentOpType(OpType currentOpType) { |
411 | 156186 | this.currentOpType = currentOpType; |
412 | 156186 | } |
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
protected void configureOpType(SelectionKey key) { |
421 | 12 | int readyOps = key.readyOps(); |
422 | 12 | switch (readyOps){ |
423 | |
case SelectionKey.OP_CONNECT: |
424 | 0 | currentOpType = OpType.OP_CONNECT; |
425 | 0 | break; |
426 | |
case SelectionKey.OP_READ: |
427 | 12 | currentOpType = OpType.OP_READ; |
428 | 12 | break; |
429 | |
case SelectionKey.OP_WRITE: |
430 | 0 | currentOpType = OpType.OP_WRITE; |
431 | 0 | break; |
432 | |
case (SelectionKey.OP_WRITE | SelectionKey.OP_READ): |
433 | 0 | currentOpType = OpType.OP_READ_WRITE; |
434 | 0 | break; |
435 | |
case (SelectionKey.OP_ACCEPT): |
436 | 0 | currentOpType = OpType.OP_ACCEPT; |
437 | 0 | break; |
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
default: |
445 | 0 | currentOpType = OpType.OP_CONNECT; |
446 | |
break; |
447 | |
|
448 | |
} |
449 | 12 | } |
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
public void execute() throws PipelineFullException { |
459 | |
|
460 | |
|
461 | |
|
462 | 0 | Object attachment = SelectionKeyAttachment.getAttachment(key); |
463 | 0 | if (ioEvent != null && (attachment instanceof CallbackHandler)) { |
464 | 0 | CallbackHandlerContextTask task = CallbackHandlerContextTask.poll(); |
465 | 0 | task.setCallBackHandler((CallbackHandler) attachment); |
466 | 0 | execute(task); |
467 | 0 | } else { |
468 | 0 | execute(ProtocolChainContextTask.poll()); |
469 | |
} |
470 | 0 | } |
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
public void execute(ContextTask contextTask) throws PipelineFullException { |
480 | 94669 | execute(contextTask, true); |
481 | 94669 | } |
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
@SuppressWarnings("unchecked") |
493 | |
public void execute(ContextTask contextTask, boolean runInSeparateThread) throws PipelineFullException { |
494 | 156189 | if (protocolChain == null){ |
495 | |
ProtocolChainInstanceHandler |
496 | 82622 | pciHandler = getProtocolChainInstanceHandler(); |
497 | 82622 | protocolChain = pciHandler.poll(); |
498 | |
} |
499 | |
|
500 | 156189 | if (contextTask != null) { |
501 | 156189 | contextTask.setContext(this); |
502 | 156189 | if (runInSeparateThread) { |
503 | 156189 | getPipeline().execute(contextTask); |
504 | |
} else { |
505 | |
try { |
506 | 0 | contextTask.call(); |
507 | 0 | } catch(Exception e) { |
508 | 0 | Controller.logger().log(Level.SEVERE, |
509 | |
"Unexpected exception occured, when executing task: " + |
510 | |
contextTask, e); |
511 | 0 | } |
512 | |
} |
513 | |
} |
514 | 156189 | } |
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
public ProtocolChainInstanceHandler getProtocolChainInstanceHandler() { |
523 | 238810 | ProtocolChainInstanceHandler protocolChainInstanceHandler = |
524 | |
selectorHandler.getProtocolChainInstanceHandler(); |
525 | 238811 | return protocolChainInstanceHandler != null ? |
526 | |
protocolChainInstanceHandler : |
527 | |
controller.getProtocolChainInstanceHandler(); |
528 | |
} |
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
public Pipeline getPipeline() { |
536 | 156189 | if (pipeline == null && controller != null){ |
537 | 82701 | pipeline = controller.getPipeline(); |
538 | |
} |
539 | 156189 | return pipeline; |
540 | |
} |
541 | |
|
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
public void setPipeline(Pipeline pipeline) { |
548 | 82622 | this.pipeline = pipeline; |
549 | 82622 | } |
550 | |
|
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
protected void setIOEvent(IOEvent<Context> ioEvent){ |
557 | 61520 | this.ioEvent = ioEvent; |
558 | 61520 | } |
559 | |
|
560 | |
|
561 | |
|
562 | |
|
563 | |
|
564 | |
|
565 | |
|
566 | |
protected IOEvent getIOEvent(){ |
567 | 61520 | return ioEvent; |
568 | |
} |
569 | |
|
570 | |
|
571 | |
|
572 | |
|
573 | |
|
574 | |
|
575 | |
public Controller.Protocol getProtocol() { |
576 | 216474 | return selectorHandler.protocol(); |
577 | |
} |
578 | |
|
579 | |
|
580 | |
|
581 | |
|
582 | |
|
583 | |
|
584 | |
|
585 | |
|
586 | |
public void setProtocol(Controller.Protocol protocol) { |
587 | 0 | } |
588 | |
|
589 | |
|
590 | |
|
591 | |
|
592 | |
|
593 | |
|
594 | |
public SelectorHandler getSelectorHandler() { |
595 | 450889 | return selectorHandler; |
596 | |
} |
597 | |
|
598 | |
|
599 | |
|
600 | |
|
601 | |
|
602 | |
public void setSelectorHandler(SelectorHandler selectorHandler) { |
603 | 467509 | this.selectorHandler = selectorHandler; |
604 | 467509 | } |
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
|
610 | |
|
611 | |
|
612 | |
|
613 | |
public AsyncQueueReadable getAsyncQueueReadable() { |
614 | 0 | if (asyncQueueReadable == null) { |
615 | 0 | asyncQueueReadable = new AsyncQueueReadableContextWrapper(); |
616 | |
} |
617 | |
|
618 | 0 | return asyncQueueReadable; |
619 | |
} |
620 | |
|
621 | |
|
622 | |
|
623 | |
|
624 | |
|
625 | |
|
626 | |
|
627 | |
public AsyncQueueWritable getAsyncQueueWritable() { |
628 | 109903 | if (asyncQueueWritable == null) { |
629 | 40 | asyncQueueWritable = new AsyncQueueWritableContextWrapper(); |
630 | |
} |
631 | |
|
632 | 109903 | return asyncQueueWritable; |
633 | |
} |
634 | |
|
635 | |
|
636 | |
|
637 | |
|
638 | |
|
639 | |
protected AsyncQueueReader getAsyncQueueReader() { |
640 | 0 | return asyncQueueReader; |
641 | |
} |
642 | |
|
643 | |
|
644 | |
|
645 | |
|
646 | |
|
647 | |
protected void setAsyncQueueReader(AsyncQueueReader asyncQueueReader) { |
648 | 156189 | this.asyncQueueReader = asyncQueueReader; |
649 | 156189 | } |
650 | |
|
651 | |
|
652 | |
|
653 | |
|
654 | |
|
655 | |
protected AsyncQueueWriter getAsyncQueueWriter() { |
656 | 0 | return asyncQueueWriter; |
657 | |
} |
658 | |
|
659 | |
|
660 | |
|
661 | |
|
662 | |
|
663 | |
protected void setAsyncQueueWriter(AsyncQueueWriter asyncQueueWriter) { |
664 | 156189 | this.asyncQueueWriter = asyncQueueWriter; |
665 | 156189 | } |
666 | |
|
667 | 80 | private class AsyncQueueWritableContextWrapper implements AsyncQueueWritable { |
668 | |
|
669 | |
|
670 | |
|
671 | |
public void writeToAsyncQueue(ByteBuffer buffer) throws IOException { |
672 | 0 | asyncQueueWriter.write(key, buffer); |
673 | 0 | } |
674 | |
|
675 | |
|
676 | |
|
677 | |
|
678 | |
public void writeToAsyncQueue(ByteBuffer buffer, |
679 | |
AsyncWriteCallbackHandler callbackHandler) throws IOException { |
680 | 0 | asyncQueueWriter.write(key, buffer, callbackHandler); |
681 | 0 | } |
682 | |
|
683 | |
|
684 | |
|
685 | |
|
686 | |
public void writeToAsyncQueue(ByteBuffer buffer, |
687 | |
AsyncWriteCallbackHandler callbackHandler, |
688 | |
AsyncQueueDataProcessor writePreProcessor) throws IOException { |
689 | 0 | asyncQueueWriter.write(key, buffer, callbackHandler, writePreProcessor); |
690 | 0 | } |
691 | |
|
692 | |
|
693 | |
|
694 | |
|
695 | |
public void writeToAsyncQueue(ByteBuffer buffer, |
696 | |
AsyncWriteCallbackHandler callbackHandler, |
697 | |
AsyncQueueDataProcessor writePreProcessor, |
698 | |
boolean isCloneByteBuffer) throws IOException { |
699 | 89903 | asyncQueueWriter.write(key, buffer, callbackHandler, |
700 | |
writePreProcessor, isCloneByteBuffer); |
701 | 89903 | } |
702 | |
|
703 | |
|
704 | |
|
705 | |
|
706 | |
public void writeToAsyncQueue(SocketAddress dstAddress, ByteBuffer buffer) |
707 | |
throws IOException { |
708 | 0 | asyncQueueWriter.write(key, dstAddress, buffer); |
709 | 0 | } |
710 | |
|
711 | |
|
712 | |
|
713 | |
|
714 | |
public void writeToAsyncQueue(SocketAddress dstAddress, ByteBuffer buffer, |
715 | |
AsyncWriteCallbackHandler callbackHandler) throws IOException { |
716 | 0 | asyncQueueWriter.write(key, dstAddress, buffer, |
717 | |
callbackHandler); |
718 | 0 | } |
719 | |
|
720 | |
|
721 | |
|
722 | |
|
723 | |
public void writeToAsyncQueue(SocketAddress dstAddress, ByteBuffer buffer, |
724 | |
AsyncWriteCallbackHandler callbackHandler, |
725 | |
AsyncQueueDataProcessor writePreProcessor) throws IOException { |
726 | 0 | asyncQueueWriter.write(key, dstAddress, buffer, |
727 | |
callbackHandler, writePreProcessor); |
728 | 0 | } |
729 | |
|
730 | |
|
731 | |
|
732 | |
|
733 | |
public void writeToAsyncQueue(SocketAddress dstAddress, ByteBuffer buffer, |
734 | |
AsyncWriteCallbackHandler callbackHandler, |
735 | |
AsyncQueueDataProcessor writePreProcessor, boolean isCloneByteBuffer) |
736 | |
throws IOException { |
737 | 20000 | asyncQueueWriter.write(key, dstAddress, buffer, |
738 | |
callbackHandler, writePreProcessor, isCloneByteBuffer); |
739 | 20000 | } |
740 | |
} |
741 | |
|
742 | 0 | private class AsyncQueueReadableContextWrapper implements AsyncQueueReadable { |
743 | |
|
744 | |
|
745 | |
|
746 | |
public void readFromAsyncQueue(ByteBuffer buffer, |
747 | |
AsyncReadCallbackHandler callbackHandler) throws IOException { |
748 | 0 | asyncQueueReader.read(key, buffer, callbackHandler); |
749 | 0 | } |
750 | |
|
751 | |
|
752 | |
|
753 | |
|
754 | |
public void readFromAsyncQueue(ByteBuffer buffer, |
755 | |
AsyncReadCallbackHandler callbackHandler, |
756 | |
AsyncReadCondition condition) throws IOException { |
757 | 0 | asyncQueueReader.read(key, buffer, callbackHandler, condition); |
758 | 0 | } |
759 | |
|
760 | |
|
761 | |
|
762 | |
|
763 | |
public void readFromAsyncQueue(ByteBuffer buffer, |
764 | |
AsyncReadCallbackHandler callbackHandler, |
765 | |
AsyncReadCondition condition, |
766 | |
AsyncQueueDataProcessor readPostProcessor) throws IOException { |
767 | 0 | asyncQueueReader.read(key, buffer, callbackHandler, |
768 | |
condition, readPostProcessor); |
769 | 0 | } |
770 | |
} |
771 | |
} |