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.util.AttributeHolder; |
42 | |
import com.sun.grizzly.util.Cloner; |
43 | |
import com.sun.grizzly.util.ConcurrentLinkedQueuePool; |
44 | |
import com.sun.grizzly.util.Copyable; |
45 | |
import com.sun.grizzly.util.State; |
46 | |
import com.sun.grizzly.util.StateHolder; |
47 | |
import com.sun.grizzly.util.SupportStateHolder; |
48 | |
import java.io.IOException; |
49 | |
import java.nio.channels.ClosedChannelException; |
50 | |
import java.nio.channels.ClosedSelectorException; |
51 | |
import java.nio.channels.SelectionKey; |
52 | |
import java.nio.channels.Selector; |
53 | |
import java.util.Collection; |
54 | |
import java.util.HashMap; |
55 | |
import java.util.Iterator; |
56 | |
import java.util.Map; |
57 | |
import java.util.Set; |
58 | |
import java.util.concurrent.Callable; |
59 | |
import java.util.concurrent.ConcurrentLinkedQueue; |
60 | |
import java.util.concurrent.CountDownLatch; |
61 | |
import java.util.concurrent.atomic.AtomicInteger; |
62 | |
import java.util.logging.Level; |
63 | |
import java.util.logging.Logger; |
64 | |
|
65 | |
import static com.sun.grizzly.Context.OpType; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public class Controller implements Runnable, Lifecycle, Copyable, |
148 | |
ConnectorHandlerPool, AttributeHolder, SupportStateHolder<State> { |
149 | |
|
150 | 5 | public enum Protocol { UDP, TCP , TLS, CUSTOM } |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
private ConcurrentLinkedQueuePool<Context> contexts; |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
protected ProtocolChainInstanceHandler instanceHandler; |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
protected SelectionKeyHandler selectionKeyHandler; |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | 85 | protected ComplexSelectorHandler multiReadThreadSelectorHandler = null; |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | 85 | protected ConnectorHandlerPool connectorHandlerPool = null; |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
protected ConcurrentLinkedQueue<SelectorHandler> selectorHandlers; |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
protected StateHolder<State> stateHolder; |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | 85 | protected int readThreadsCount = 0; |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
protected ReadController[] readThreadControllers; |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | 1 | private static Logger logger = Logger.getLogger("grizzly"); |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
private Pipeline<Callable> pipeline; |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | 85 | protected Collection<ControllerStateListener> stateListeners = |
231 | |
new ConcurrentLinkedQueue<ControllerStateListener>(); |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
protected AtomicInteger readySelectorHandlerCounter; |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
protected AtomicInteger stoppedSelectorHandlerCounter; |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | 85 | private boolean handleReadWriteConcurrently = true; |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
protected Map<String, Object> attributes; |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | 1 | private final static ConcurrentLinkedQueue<Controller> controllers = |
263 | |
new ConcurrentLinkedQueue<Controller>(); |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | 85 | public Controller() { |
272 | 85 | contexts = new ConcurrentLinkedQueuePool<Context>() { |
273 | |
@Override |
274 | |
public Context newInstance() { |
275 | 261 | return new Context(); |
276 | |
} |
277 | |
}; |
278 | |
|
279 | 85 | stateHolder = new StateHolder<State>(true); |
280 | 85 | initializeDefaults(); |
281 | 85 | } |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
private void initializeDefaults() { |
294 | 85 | if (pipeline == null) { |
295 | 85 | pipeline = new DefaultPipeline(); |
296 | |
} |
297 | 85 | if (instanceHandler == null) { |
298 | 85 | instanceHandler = new DefaultProtocolChainInstanceHandler(); |
299 | |
} |
300 | 85 | if (selectorHandlers == null){ |
301 | 85 | selectorHandlers = new ConcurrentLinkedQueue<SelectorHandler>(); |
302 | |
} |
303 | 85 | if (connectorHandlerPool == null) { |
304 | 85 | connectorHandlerPool = new DefaultConnectorHandlerPool(this); |
305 | |
} |
306 | 85 | controllers.add(this); |
307 | 85 | } |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
protected void doSelect(SelectorHandler selectorHandler){ |
320 | 266289 | SelectionKey key = null; |
321 | |
Set<SelectionKey> readyKeys; |
322 | |
Iterator<SelectionKey> iterator; |
323 | |
int selectorState; |
324 | 266289 | boolean delegateToWorkerThread = false; |
325 | 266289 | Context serverCtx = contexts.poll(); |
326 | 266289 | serverCtx.setController(this); |
327 | |
|
328 | 266289 | serverCtx.setSelectorHandler(selectorHandler); |
329 | |
|
330 | |
try { |
331 | 266289 | selectorState = 0; |
332 | |
|
333 | |
|
334 | |
|
335 | 266289 | if (selectorHandler.getSelectionKeyHandler() == null){ |
336 | 123 | if (logger.isLoggable(Level.FINE)) { |
337 | 0 | logger.log(Level.FINE, "Set DefaultSelectionKeyHandler to SelectorHandler: " + selectorHandler); |
338 | |
} |
339 | |
|
340 | |
|
341 | 123 | SelectionKeyHandler assgnSelectionKeyHandler = null; |
342 | |
|
343 | 123 | if (selectorHandler.getPreferredSelectionKeyHandler() != null) { |
344 | 123 | Class<? extends SelectionKeyHandler> keyHandlerClass = |
345 | |
selectorHandler.getPreferredSelectionKeyHandler(); |
346 | |
try { |
347 | 123 | assgnSelectionKeyHandler = keyHandlerClass.newInstance(); |
348 | 123 | assgnSelectionKeyHandler.setSelectorHandler(selectorHandler); |
349 | 0 | } catch (Exception e) { |
350 | 0 | if (logger.isLoggable(Level.WARNING)) { |
351 | 0 | logger.log(Level.WARNING, |
352 | |
"Exception initializing preffered SelectionKeyHandler '" + |
353 | |
keyHandlerClass + "' for the SelectorHandler '" + |
354 | |
selectorHandler + "'"); |
355 | |
} |
356 | 123 | } |
357 | |
} |
358 | |
|
359 | 123 | if (assgnSelectionKeyHandler == null) { |
360 | 0 | assgnSelectionKeyHandler = |
361 | |
new DefaultSelectionKeyHandler(selectorHandler); |
362 | |
} |
363 | |
|
364 | 123 | selectorHandler.setSelectionKeyHandler(assgnSelectionKeyHandler); |
365 | |
} |
366 | |
|
367 | 266289 | selectorHandler.preSelect(serverCtx); |
368 | |
|
369 | 266289 | readyKeys = selectorHandler.select(serverCtx); |
370 | 266289 | selectorState = readyKeys.size(); |
371 | 266289 | if (stateHolder.getState(false) == State.STARTED && |
372 | |
selectorHandler.getStateHolder().getState(false) == State.STARTED && |
373 | |
selectorState != 0) { |
374 | 138808 | iterator = readyKeys.iterator(); |
375 | 300214 | while (iterator.hasNext()) { |
376 | 161406 | key = iterator.next(); |
377 | 161406 | iterator.remove(); |
378 | 161406 | OpType opType = null; |
379 | 161406 | boolean skipOpWrite = false; |
380 | 161406 | delegateToWorkerThread = false; |
381 | 161406 | if (key.isValid()) { |
382 | 161406 | if ((key.readyOps() & SelectionKey.OP_ACCEPT) |
383 | |
== SelectionKey.OP_ACCEPT){ |
384 | 199 | if (readThreadsCount > 0 && |
385 | |
multiReadThreadSelectorHandler.supportsProtocol(selectorHandler.protocol())) { |
386 | 19 | if (logger.isLoggable(Level.FINE)) { |
387 | 0 | logger.log(Level.FINE, "OP_ACCEPT on " + key + " passed to multi readthread handler"); |
388 | |
} |
389 | 19 | delegateToWorkerThread = multiReadThreadSelectorHandler. |
390 | |
onAcceptInterest(key, serverCtx); |
391 | |
} else { |
392 | 180 | if (logger.isLoggable(Level.FINE)) { |
393 | 0 | logger.log(Level.FINE, "OP_ACCEPT on " + key); |
394 | |
} |
395 | |
|
396 | 180 | delegateToWorkerThread = selectorHandler. |
397 | |
onAcceptInterest(key, serverCtx); |
398 | |
} |
399 | 180 | continue; |
400 | |
} |
401 | |
|
402 | 161207 | if ((key.readyOps() & SelectionKey.OP_CONNECT) |
403 | |
== SelectionKey.OP_CONNECT) { |
404 | 174 | if (logger.isLoggable(Level.FINE)) { |
405 | 0 | logger.log(Level.FINE, "OP_CONNECT on " + key); |
406 | |
} |
407 | |
|
408 | 174 | delegateToWorkerThread = selectorHandler. |
409 | |
onConnectInterest(key, serverCtx); |
410 | 174 | continue; |
411 | |
} |
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | 161033 | if ((key.readyOps() & SelectionKey.OP_READ) |
418 | |
== SelectionKey.OP_READ) { |
419 | 158935 | if (logger.isLoggable(Level.FINE)) { |
420 | 0 | logger.log(Level.FINE, "OP_READ on " + key); |
421 | |
} |
422 | 158935 | delegateToWorkerThread = selectorHandler. |
423 | |
onReadInterest(key,serverCtx); |
424 | |
|
425 | 158935 | if (delegateToWorkerThread) { |
426 | 90799 | opType = OpType.OP_READ; |
427 | |
} |
428 | |
|
429 | 158935 | if (!handleReadWriteConcurrently){ |
430 | 137 | skipOpWrite = true; |
431 | |
} |
432 | |
} |
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | 161033 | if (!skipOpWrite && key.isValid() |
438 | |
&& (key.readyOps() & SelectionKey.OP_WRITE) |
439 | |
== SelectionKey.OP_WRITE) { |
440 | 2241 | if (logger.isLoggable(Level.FINE)) { |
441 | 0 | logger.log(Level.FINE, "OP_WRITE on " + key); |
442 | |
} |
443 | 2241 | boolean opWriteDelegateToWorkerThread = selectorHandler. |
444 | |
onWriteInterest(key,serverCtx); |
445 | 2241 | delegateToWorkerThread |= opWriteDelegateToWorkerThread; |
446 | 2241 | if (opWriteDelegateToWorkerThread) { |
447 | 16 | if (opType == OpType.OP_READ) { |
448 | 14 | opType = OpType.OP_READ_WRITE; |
449 | |
} else { |
450 | 2 | opType = OpType.OP_WRITE; |
451 | |
} |
452 | |
} |
453 | |
} |
454 | |
|
455 | 161033 | if (delegateToWorkerThread){ |
456 | 90801 | Context ctx = pollContext(key, opType); |
457 | 90801 | configureContext(ctx,selectorHandler); |
458 | 90801 | ctx.execute(ProtocolChainContextTask.poll()); |
459 | 90801 | } |
460 | |
} else { |
461 | 0 | selectorHandler.getSelectionKeyHandler().cancel(key); |
462 | |
} |
463 | 161033 | } |
464 | |
} |
465 | |
|
466 | 266289 | delegateToWorkerThread = false; |
467 | 266289 | selectorHandler.postSelect(serverCtx); |
468 | 266289 | contexts.offer(serverCtx); |
469 | 0 | } catch (ClosedSelectorException e) { |
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | 0 | if (stateHolder.getState() == State.STARTED && |
475 | |
selectorHandler.getStateHolder().getState() == State.STARTED) { |
476 | 0 | logger.log(Level.SEVERE, "Selector was unexpectedly closed."); |
477 | 0 | notifyException(e); |
478 | |
} else { |
479 | 0 | logger.log(Level.FINE, "doSelect Selector closed"); |
480 | |
} |
481 | 0 | } catch (ClosedChannelException e) { |
482 | |
|
483 | 0 | if (stateHolder.getState() == State.STARTED && |
484 | |
selectorHandler.getStateHolder().getState() == State.STARTED) { |
485 | 0 | logger.log(Level.WARNING, "Channel was unexpectedly closed"); |
486 | 0 | if (key != null){ |
487 | 0 | selectorHandler.getSelectionKeyHandler().cancel(key); |
488 | |
} |
489 | |
|
490 | 0 | notifyException(e); |
491 | |
} |
492 | 0 | } catch (Throwable t) { |
493 | |
try{ |
494 | 0 | logger.log(Level.SEVERE,"doSelect exception",t); |
495 | 0 | if (key != null){ |
496 | 0 | selectorHandler.getSelectionKeyHandler().cancel(key); |
497 | |
} |
498 | |
|
499 | 0 | notifyException(t); |
500 | 0 | } catch (Throwable t2){ |
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | 0 | t2.printStackTrace(); |
506 | 0 | } |
507 | 266289 | } |
508 | 266289 | } |
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
public void registerKey(SelectionKey key){ |
516 | 0 | registerKey(key,SelectionKey.OP_READ); |
517 | 0 | } |
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
|
526 | |
public void registerKey(SelectionKey key, int ops){ |
527 | 0 | registerKey(key, ops, selectorHandlers.peek().protocol()); |
528 | 0 | } |
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
|
536 | |
|
537 | |
public void registerKey(SelectionKey key, int ops, Protocol protocol){ |
538 | 21 | if (stateHolder.getState() == State.STOPPED) { |
539 | 0 | return; |
540 | |
} |
541 | |
|
542 | 21 | getSelectorHandler(protocol).register(key,ops); |
543 | 21 | } |
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
|
549 | |
|
550 | |
|
551 | |
public void cancelKey(SelectionKey key){ |
552 | 0 | if (stateHolder.getState() == State.STOPPED) { |
553 | 0 | return; |
554 | |
} |
555 | |
|
556 | 0 | SelectorHandler selectorHandler = getSelectorHandler(key.selector()); |
557 | 0 | if (selectorHandler != null) { |
558 | 0 | selectorHandler.getSelectionKeyHandler().cancel(key); |
559 | |
} else { |
560 | 0 | throw new IllegalStateException("SelectionKey is not associated " + |
561 | |
"with known SelectorHandler"); |
562 | |
} |
563 | 0 | } |
564 | |
|
565 | |
|
566 | |
|
567 | |
|
568 | |
|
569 | |
|
570 | |
public Context pollContext(SelectionKey key) { |
571 | 12 | return pollContext(key,null); |
572 | |
} |
573 | |
|
574 | |
|
575 | |
|
576 | |
|
577 | |
|
578 | |
|
579 | |
|
580 | |
|
581 | |
public Context pollContext(SelectionKey key, OpType opType) { |
582 | 161379 | Context ctx = contexts.poll(); |
583 | 161379 | ctx.setController(this); |
584 | 161379 | ctx.setSelectionKey(key); |
585 | 161379 | if (opType != null) { |
586 | 161367 | ctx.setCurrentOpType(opType); |
587 | |
} else { |
588 | 12 | ctx.configureOpType(key); |
589 | |
} |
590 | |
|
591 | 161379 | if (logger.isLoggable(Level.FINE)) { |
592 | 0 | logger.log(Level.FINE, "pollContext(..) Context : "+ctx); |
593 | |
} |
594 | |
|
595 | 161379 | return ctx; |
596 | |
} |
597 | |
|
598 | |
public void configureContext(Context ctx,SelectorHandler selectorHandler){ |
599 | 90804 | ctx.setSelectorHandler(selectorHandler); |
600 | 90804 | ctx.setPipeline(selectorHandler.pipeline()); |
601 | 90804 | ctx.setAsyncQueueReader(selectorHandler.getAsyncQueueReader()); |
602 | 90804 | ctx.setAsyncQueueWriter(selectorHandler.getAsyncQueueWriter()); |
603 | 90804 | } |
604 | |
|
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
|
610 | |
public void returnContext(Context ctx){ |
611 | 161370 | if(ctx.decrementRefCount()>0) { |
612 | 0 | return; |
613 | |
} |
614 | 161369 | if (logger.isLoggable(Level.FINE)) { |
615 | 0 | logger.log(Level.FINE, "returnContext() Context : "+ctx); |
616 | |
} |
617 | 161370 | ctx.recycle(); |
618 | 161370 | contexts.offer(ctx); |
619 | 161370 | } |
620 | |
|
621 | |
|
622 | |
|
623 | |
|
624 | |
|
625 | |
public static Logger logger() { |
626 | 1256513 | return logger; |
627 | |
} |
628 | |
|
629 | |
|
630 | |
|
631 | |
|
632 | |
|
633 | |
public static void setLogger(Logger l){ |
634 | 0 | logger = l; |
635 | 0 | } |
636 | |
|
637 | |
|
638 | |
|
639 | |
|
640 | |
|
641 | |
|
642 | |
|
643 | |
|
644 | |
public void setProtocolChainInstanceHandler(ProtocolChainInstanceHandler |
645 | |
instanceHandler){ |
646 | 39 | this.instanceHandler = instanceHandler; |
647 | 39 | } |
648 | |
|
649 | |
|
650 | |
|
651 | |
|
652 | |
public ProtocolChainInstanceHandler getProtocolChainInstanceHandler(){ |
653 | 322740 | return instanceHandler; |
654 | |
} |
655 | |
|
656 | |
|
657 | |
|
658 | |
|
659 | |
|
660 | |
|
661 | |
|
662 | |
|
663 | |
public void setSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler){ |
664 | 0 | this.selectionKeyHandler = selectionKeyHandler; |
665 | 0 | } |
666 | |
|
667 | |
|
668 | |
|
669 | |
|
670 | |
|
671 | |
|
672 | |
|
673 | |
public SelectionKeyHandler getSelectionKeyHandler(){ |
674 | 0 | return selectionKeyHandler; |
675 | |
} |
676 | |
|
677 | |
|
678 | |
|
679 | |
|
680 | |
|
681 | |
|
682 | |
public void addSelectorHandler(SelectorHandler selectorHandler) { |
683 | 134 | selectorHandlers.add(selectorHandler); |
684 | 134 | if (stateHolder.getState(false) != null && |
685 | |
!State.STOPPED.equals(stateHolder.getState())) { |
686 | 48 | addSelectorHandlerOnReadControllers(selectorHandler); |
687 | 48 | if (readySelectorHandlerCounter != null) { |
688 | 48 | readySelectorHandlerCounter.incrementAndGet(); |
689 | |
} |
690 | 48 | if (stoppedSelectorHandlerCounter != null) { |
691 | 48 | stoppedSelectorHandlerCounter.incrementAndGet(); |
692 | |
} |
693 | 48 | startSelectorHandlerRunner(selectorHandler, true); |
694 | |
} |
695 | 134 | } |
696 | |
|
697 | |
|
698 | |
|
699 | |
|
700 | |
|
701 | |
|
702 | |
public void setSelectorHandler(SelectorHandler selectorHandler){ |
703 | 68 | addSelectorHandler(selectorHandler); |
704 | 68 | } |
705 | |
|
706 | |
|
707 | |
|
708 | |
|
709 | |
|
710 | |
|
711 | |
|
712 | |
public SelectorHandler getSelectorHandler(Protocol protocol){ |
713 | 583 | for (SelectorHandler selectorHandler: selectorHandlers){ |
714 | 583 | if (selectorHandler.protocol() == protocol){ |
715 | 583 | return selectorHandler; |
716 | |
} |
717 | |
} |
718 | 0 | return null; |
719 | |
} |
720 | |
|
721 | |
|
722 | |
|
723 | |
|
724 | |
|
725 | |
|
726 | |
|
727 | |
public SelectorHandler getSelectorHandler(Selector selector){ |
728 | 0 | for (SelectorHandler selectorHandler: selectorHandlers){ |
729 | 0 | if (selectorHandler.getSelector() == selector){ |
730 | 0 | return selectorHandler; |
731 | |
} |
732 | |
} |
733 | 0 | return null; |
734 | |
} |
735 | |
|
736 | |
|
737 | |
|
738 | |
|
739 | |
|
740 | |
public ConcurrentLinkedQueue getSelectorHandlers(){ |
741 | 7487 | return selectorHandlers; |
742 | |
} |
743 | |
|
744 | |
|
745 | |
|
746 | |
|
747 | |
|
748 | |
|
749 | |
|
750 | |
public void removeSelectorHandler(SelectorHandler selectorHandler) { |
751 | 1 | if (selectorHandlers.remove(selectorHandler)) { |
752 | 1 | removeSelectorHandlerOnReadControllers(selectorHandler); |
753 | 1 | selectorHandler.shutdown(); |
754 | |
} |
755 | 1 | } |
756 | |
|
757 | |
|
758 | |
|
759 | |
|
760 | |
|
761 | |
public Pipeline getPipeline() { |
762 | 90877 | return pipeline; |
763 | |
} |
764 | |
|
765 | |
|
766 | |
|
767 | |
|
768 | |
|
769 | |
public void setPipeline(Pipeline<Callable> pipeline) { |
770 | 34 | this.pipeline = pipeline; |
771 | 34 | } |
772 | |
|
773 | |
|
774 | |
|
775 | |
|
776 | |
|
777 | |
public int getReadThreadsCount() { |
778 | 0 | return readThreadsCount; |
779 | |
} |
780 | |
|
781 | |
|
782 | |
|
783 | |
|
784 | |
|
785 | |
public void setReadThreadsCount(int readThreadsCount) { |
786 | 21 | this.readThreadsCount = readThreadsCount; |
787 | 21 | } |
788 | |
|
789 | |
|
790 | |
|
791 | |
|
792 | |
|
793 | |
public ConnectorHandlerPool getConnectorHandlerPool() { |
794 | 0 | return connectorHandlerPool; |
795 | |
} |
796 | |
|
797 | |
|
798 | |
|
799 | |
|
800 | |
|
801 | |
public void setConnectorHandlerPool(ConnectorHandlerPool connectorHandlerPool) { |
802 | 1 | this.connectorHandlerPool = connectorHandlerPool; |
803 | 1 | } |
804 | |
|
805 | |
|
806 | |
|
807 | |
|
808 | |
|
809 | |
|
810 | |
|
811 | |
public void run() { |
812 | |
try{ |
813 | 84 | start(); |
814 | 0 | } catch(IOException e){ |
815 | 0 | notifyException(e); |
816 | 0 | throw new RuntimeException(e.getCause()); |
817 | 84 | } |
818 | 84 | } |
819 | |
|
820 | |
|
821 | |
|
822 | |
|
823 | |
|
824 | |
|
825 | |
|
826 | |
public void copyTo(Copyable copy) { |
827 | 17 | Controller copyController = (Controller) copy; |
828 | 17 | copyController.contexts = contexts; |
829 | 17 | copyController.attributes = attributes; |
830 | 17 | copyController.instanceHandler = instanceHandler; |
831 | 17 | copyController.pipeline = pipeline; |
832 | 17 | copyController.readThreadControllers = readThreadControllers; |
833 | 17 | copyController.readThreadsCount = readThreadsCount; |
834 | 17 | copyController.selectionKeyHandler = selectionKeyHandler; |
835 | 17 | copyController.stateHolder = stateHolder; |
836 | 17 | } |
837 | |
|
838 | |
|
839 | |
|
840 | |
|
841 | |
|
842 | |
|
843 | |
public void addStateListener(ControllerStateListener stateListener) { |
844 | 139 | stateListeners.add(stateListener); |
845 | 139 | } |
846 | |
|
847 | |
|
848 | |
|
849 | |
|
850 | |
public void removeStateListener(ControllerStateListener stateListener) { |
851 | 69 | stateListeners.remove(stateListener); |
852 | 69 | } |
853 | |
|
854 | |
|
855 | |
|
856 | |
|
857 | |
void notifyStarted() { |
858 | 86 | for(ControllerStateListener stateListener : stateListeners) { |
859 | 70 | stateListener.onStarted(); |
860 | |
} |
861 | 86 | } |
862 | |
|
863 | |
|
864 | |
|
865 | |
|
866 | |
|
867 | |
void notifyReady() { |
868 | 136 | if (readySelectorHandlerCounter.decrementAndGet() == 0) { |
869 | 87 | for (ControllerStateListener stateListener : stateListeners) { |
870 | 72 | stateListener.onReady(); |
871 | |
} |
872 | |
} |
873 | 136 | } |
874 | |
|
875 | |
|
876 | |
|
877 | |
|
878 | |
|
879 | |
void notifyStopped() { |
880 | 136 | if (stoppedSelectorHandlerCounter.decrementAndGet() == 0) { |
881 | |
|
882 | 87 | synchronized(stoppedSelectorHandlerCounter) { |
883 | 87 | stoppedSelectorHandlerCounter.notifyAll(); |
884 | 87 | } |
885 | |
} |
886 | 136 | } |
887 | |
|
888 | |
|
889 | |
|
890 | |
|
891 | |
|
892 | |
void notifyException(Throwable e) { |
893 | 0 | for(ControllerStateListener stateListener : stateListeners) { |
894 | 0 | stateListener.onException(e); |
895 | |
} |
896 | 0 | } |
897 | |
|
898 | |
|
899 | |
|
900 | |
|
901 | |
|
902 | |
|
903 | |
public void start() throws IOException { |
904 | 117 | stateHolder.getStateLocker().writeLock().lock(); |
905 | 117 | boolean isUnlocked = false; |
906 | |
try { |
907 | 117 | if (stateHolder.getState(false) == null || |
908 | |
stateHolder.getState(false) == State.STOPPED) { |
909 | |
|
910 | |
|
911 | 69 | if (selectorHandlers.isEmpty()) { |
912 | 2 | SelectorHandler selectorHandler = new TCPSelectorHandler(); |
913 | 2 | selectorHandlers.add(selectorHandler); |
914 | |
} |
915 | |
|
916 | 69 | pipeline.initPipeline(); |
917 | 69 | pipeline.startPipeline(); |
918 | |
|
919 | 69 | if (readThreadsCount > 0) { |
920 | 6 | initReadThreads(); |
921 | 6 | multiReadThreadSelectorHandler = |
922 | |
new RoundRobinSelectorHandler(readThreadControllers); |
923 | |
} |
924 | |
|
925 | 69 | stateHolder.setState(State.STARTED, false); |
926 | 69 | notifyStarted(); |
927 | |
|
928 | 69 | int selectorHandlerCount = selectorHandlers.size(); |
929 | 69 | readySelectorHandlerCounter = new AtomicInteger(selectorHandlerCount); |
930 | 69 | stoppedSelectorHandlerCounter = new AtomicInteger(selectorHandlerCount); |
931 | |
|
932 | |
|
933 | 69 | Iterator<SelectorHandler> it = selectorHandlers.iterator(); |
934 | 69 | if (selectorHandlerCount > 1) { |
935 | 4 | for (; it.hasNext() && selectorHandlerCount-- > 0;) { |
936 | 3 | SelectorHandler selectorHandler = it.next(); |
937 | 3 | startSelectorHandlerRunner(selectorHandler, true); |
938 | 3 | } |
939 | 68 | } else if (it.hasNext()) { |
940 | 68 | SelectorHandler selectorHandler = it.next(); |
941 | 68 | stateHolder.getStateLocker().writeLock().unlock(); |
942 | 68 | isUnlocked = true; |
943 | 68 | startSelectorHandlerRunner(selectorHandler, false); |
944 | |
} |
945 | |
} |
946 | |
} finally { |
947 | 117 | if (!isUnlocked) { |
948 | 49 | stateHolder.getStateLocker().writeLock().unlock(); |
949 | |
} |
950 | |
} |
951 | |
|
952 | 117 | waitUntilSeletorHandlersStop(); |
953 | |
|
954 | 117 | if (readThreadsCount > 0) { |
955 | 6 | multiReadThreadSelectorHandler.shutdown(); |
956 | 6 | multiReadThreadSelectorHandler = null; |
957 | |
|
958 | 21 | for (Controller readController : readThreadControllers) { |
959 | |
try { |
960 | 15 | readController.stop(); |
961 | 0 | } catch (IOException e) { |
962 | 0 | logger.log(Level.WARNING, "Exception occured when stopping read Controller!", e); |
963 | 15 | } |
964 | |
} |
965 | |
|
966 | 6 | readThreadControllers = null; |
967 | |
} |
968 | |
|
969 | 117 | selectorHandlers.clear(); |
970 | 117 | pipeline.stopPipeline(); |
971 | 117 | attributes = null; |
972 | |
|
973 | |
|
974 | 117 | for (ControllerStateListener stateListener : stateListeners) { |
975 | 138 | stateListener.onStopped(); |
976 | |
} |
977 | 117 | } |
978 | |
|
979 | |
|
980 | |
|
981 | |
|
982 | |
|
983 | |
public void stop() throws IOException { |
984 | 70 | stop(false); |
985 | 70 | } |
986 | |
|
987 | |
|
988 | |
|
989 | |
|
990 | |
|
991 | |
|
992 | |
|
993 | |
|
994 | |
public void stop(boolean isAsync) throws IOException { |
995 | 70 | final CountDownLatch latch = new CountDownLatch(1); |
996 | 70 | stateHolder.getStateLocker().writeLock().lock(); |
997 | |
try { |
998 | 70 | if (stateHolder.getState(false) == State.STOPPED) { |
999 | 1 | logger.log(Level.FINE, "Controller is already in stopped state"); |
1000 | |
return; |
1001 | |
} |
1002 | |
|
1003 | 69 | if (!isAsync) { |
1004 | 69 | addStateListener(new ControllerStateListenerAdapter() { |
1005 | |
@Override |
1006 | |
public void onException(Throwable e) { |
1007 | 0 | removeStateListener(this); |
1008 | 0 | latch.countDown(); |
1009 | 0 | } |
1010 | |
|
1011 | |
@Override |
1012 | |
public void onStopped() { |
1013 | 69 | removeStateListener(this); |
1014 | 69 | latch.countDown(); |
1015 | 69 | } |
1016 | |
|
1017 | |
}); |
1018 | |
} |
1019 | 69 | stateHolder.setState(State.STOPPED, false); |
1020 | |
} finally { |
1021 | 70 | stateHolder.getStateLocker().writeLock().unlock(); |
1022 | 69 | } |
1023 | |
|
1024 | 69 | if (!isAsync) { |
1025 | |
try { |
1026 | 69 | latch.await(); |
1027 | 69 | } catch(InterruptedException e) {} |
1028 | |
} |
1029 | 69 | } |
1030 | |
|
1031 | |
|
1032 | |
|
1033 | |
|
1034 | |
|
1035 | |
public void pause() throws IOException { |
1036 | 0 | stateHolder.setState(State.PAUSED); |
1037 | 0 | } |
1038 | |
|
1039 | |
|
1040 | |
|
1041 | |
|
1042 | |
|
1043 | |
public void resume() throws IOException { |
1044 | 0 | if (!State.PAUSED.equals(stateHolder.getState(false))) { |
1045 | 0 | throw new IllegalStateException("Controller is not in PAUSED state, but: " + |
1046 | |
stateHolder.getState(false)); |
1047 | |
} |
1048 | |
|
1049 | 0 | stateHolder.setState(State.STARTED); |
1050 | 0 | } |
1051 | |
|
1052 | |
|
1053 | |
|
1054 | |
|
1055 | |
|
1056 | |
|
1057 | |
public StateHolder<State> getStateHolder() { |
1058 | 136 | return stateHolder; |
1059 | |
} |
1060 | |
|
1061 | |
|
1062 | |
|
1063 | |
|
1064 | |
|
1065 | |
private void initReadThreads() throws IOException { |
1066 | |
|
1067 | 6 | if (attributes == null) { |
1068 | 6 | attributes = new HashMap<String, Object>(2); |
1069 | |
} |
1070 | |
|
1071 | 6 | readThreadControllers = new ReadController[readThreadsCount]; |
1072 | 21 | for(int i=0; i<readThreadsCount; i++) { |
1073 | 15 | ReadController controller = new ReadController(); |
1074 | 15 | copyTo(controller); |
1075 | 15 | controller.setReadThreadsCount(0); |
1076 | 15 | readThreadControllers[i] = controller; |
1077 | |
} |
1078 | |
|
1079 | 6 | for (SelectorHandler selectorHandler : selectorHandlers) { |
1080 | 6 | addSelectorHandlerOnReadControllers(selectorHandler); |
1081 | |
} |
1082 | |
|
1083 | 21 | for (int i=0; i < readThreadControllers.length; i++) { |
1084 | |
|
1085 | 15 | new Thread(readThreadControllers[i], "GrizzlyReadController-" + i).start(); |
1086 | |
} |
1087 | 6 | } |
1088 | |
|
1089 | |
|
1090 | |
|
1091 | |
|
1092 | |
|
1093 | |
|
1094 | |
private void addSelectorHandlerOnReadControllers(SelectorHandler selectorHandler) { |
1095 | 54 | if (readThreadControllers == null || readThreadsCount == 0) return; |
1096 | |
|
1097 | |
|
1098 | 6 | if (selectorHandler.getAttributes() == null) { |
1099 | 6 | selectorHandler.setAttributes(new HashMap<String, Object>(2)); |
1100 | |
} |
1101 | |
|
1102 | 21 | for (Controller readController : readThreadControllers) { |
1103 | 15 | SelectorHandler copySelectorHandler = Cloner.clone(selectorHandler); |
1104 | |
try { |
1105 | 15 | copySelectorHandler.setSelector(Selector.open()); |
1106 | 0 | } catch(IOException e) { |
1107 | 0 | logger.log(Level.SEVERE, "Error opening selector!", e); |
1108 | 15 | } |
1109 | |
|
1110 | 15 | readController.addSelectorHandler(copySelectorHandler); |
1111 | |
} |
1112 | 6 | } |
1113 | |
|
1114 | |
|
1115 | |
|
1116 | |
|
1117 | |
|
1118 | |
|
1119 | |
|
1120 | |
|
1121 | |
private void startSelectorHandlerRunner(SelectorHandler selectorHandler, |
1122 | |
boolean isRunAsync) { |
1123 | |
|
1124 | |
|
1125 | |
|
1126 | 119 | if (selectorHandler.getSelector() != null) { |
1127 | 0 | notifyReady(); |
1128 | |
} |
1129 | 119 | Runnable selectorRunner = new SelectorHandlerRunner(this, selectorHandler); |
1130 | 119 | if (isRunAsync) { |
1131 | |
|
1132 | |
|
1133 | 51 | new Thread(selectorRunner, "GrizzlySelectorRunner-" + selectorHandler.protocol()).start(); |
1134 | |
} else { |
1135 | |
|
1136 | 68 | selectorRunner.run(); |
1137 | |
} |
1138 | 119 | } |
1139 | |
|
1140 | |
|
1141 | |
|
1142 | |
|
1143 | |
|
1144 | |
|
1145 | |
private void removeSelectorHandlerOnReadControllers(SelectorHandler selectorHandler) { |
1146 | 1 | if (readThreadControllers == null) return; |
1147 | |
|
1148 | 0 | for (ReadController readController : readThreadControllers) { |
1149 | 0 | readController.removeSelectorHandlerClone(selectorHandler); |
1150 | |
} |
1151 | 0 | } |
1152 | |
|
1153 | |
|
1154 | |
|
1155 | |
|
1156 | |
|
1157 | |
|
1158 | |
public boolean isStarted() { |
1159 | 39 | return stateHolder.getState() == State.STARTED; |
1160 | |
} |
1161 | |
|
1162 | |
|
1163 | |
|
1164 | |
|
1165 | |
|
1166 | |
|
1167 | |
|
1168 | |
|
1169 | |
|
1170 | |
public ConnectorHandler acquireConnectorHandler(Protocol protocol){ |
1171 | 184 | return connectorHandlerPool.acquireConnectorHandler(protocol); |
1172 | |
} |
1173 | |
|
1174 | |
|
1175 | |
|
1176 | |
|
1177 | |
|
1178 | |
|
1179 | |
|
1180 | |
public void releaseConnectorHandler(ConnectorHandler connectorHandler){ |
1181 | 184 | connectorHandlerPool.releaseConnectorHandler(connectorHandler); |
1182 | 184 | } |
1183 | |
|
1184 | |
|
1185 | |
|
1186 | |
|
1187 | |
|
1188 | |
|
1189 | |
|
1190 | |
public boolean isHandleReadWriteConcurrently() { |
1191 | 0 | return handleReadWriteConcurrently; |
1192 | |
} |
1193 | |
|
1194 | |
|
1195 | |
|
1196 | |
|
1197 | |
|
1198 | |
|
1199 | |
|
1200 | |
public void setHandleReadWriteConcurrently(boolean handleReadWriteConcurrently) { |
1201 | 7 | this.handleReadWriteConcurrently = handleReadWriteConcurrently; |
1202 | 7 | } |
1203 | |
|
1204 | |
|
1205 | |
|
1206 | |
|
1207 | |
|
1208 | |
protected void waitUntilSeletorHandlersStop() { |
1209 | 151 | synchronized(stoppedSelectorHandlerCounter) { |
1210 | 742 | while(stoppedSelectorHandlerCounter.get() > 0 || |
1211 | |
!State.STOPPED.equals(stateHolder.getState())) { |
1212 | |
try { |
1213 | 591 | stoppedSelectorHandlerCounter.wait(1000); |
1214 | 0 | } catch (InterruptedException ex) { |
1215 | 591 | } |
1216 | |
} |
1217 | 151 | } |
1218 | 151 | } |
1219 | |
|
1220 | |
|
1221 | |
|
1222 | |
|
1223 | |
|
1224 | |
|
1225 | |
|
1226 | |
|
1227 | |
|
1228 | |
|
1229 | |
public Object removeAttribute(String key) { |
1230 | 0 | if (attributes == null) return null; |
1231 | |
|
1232 | 0 | return attributes.remove(key); |
1233 | |
} |
1234 | |
|
1235 | |
|
1236 | |
|
1237 | |
|
1238 | |
|
1239 | |
|
1240 | |
|
1241 | |
|
1242 | |
public void setAttribute(String key, Object value) { |
1243 | 0 | if (attributes == null) { |
1244 | 0 | attributes = new HashMap<String, Object>(); |
1245 | |
} |
1246 | |
|
1247 | 0 | attributes.put(key, value); |
1248 | 0 | } |
1249 | |
|
1250 | |
|
1251 | |
|
1252 | |
|
1253 | |
|
1254 | |
|
1255 | |
|
1256 | |
|
1257 | |
|
1258 | |
public Object getAttribute(String key) { |
1259 | 0 | if (attributes == null) return null; |
1260 | |
|
1261 | 0 | return attributes.get(key); |
1262 | |
} |
1263 | |
|
1264 | |
|
1265 | |
|
1266 | |
|
1267 | |
|
1268 | |
|
1269 | |
|
1270 | |
|
1271 | |
|
1272 | |
public void setAttributes(Map<String, Object> attributes) { |
1273 | 0 | this.attributes = attributes; |
1274 | 0 | } |
1275 | |
|
1276 | |
|
1277 | |
|
1278 | |
|
1279 | |
|
1280 | |
|
1281 | |
|
1282 | |
|
1283 | |
|
1284 | |
public Map<String, Object> getAttributes() { |
1285 | 0 | return attributes; |
1286 | |
} |
1287 | |
|
1288 | |
|
1289 | |
|
1290 | |
|
1291 | |
|
1292 | |
|
1293 | |
|
1294 | |
|
1295 | |
public static Controller getHandlerController(Handler handler){ |
1296 | 175 | if (handler instanceof SelectorHandler){ |
1297 | 175 | for (Controller controller: controllers){ |
1298 | 7486 | if (controller.getSelectorHandlers().contains(handler)){ |
1299 | 175 | return controller; |
1300 | |
} |
1301 | |
} |
1302 | |
} |
1303 | 0 | return null; |
1304 | |
} |
1305 | |
} |