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.AsyncQueueReader; |
42 | |
import com.sun.grizzly.async.AsyncQueueReaderContextTask; |
43 | |
import com.sun.grizzly.async.AsyncQueueWriter; |
44 | |
import com.sun.grizzly.async.TCPAsyncQueueWriter; |
45 | |
import com.sun.grizzly.async.AsyncQueueWriterContextTask; |
46 | |
import com.sun.grizzly.async.TCPAsyncQueueReader; |
47 | |
import com.sun.grizzly.util.CallbackHandlerSelectionKeyAttachment; |
48 | |
import com.sun.grizzly.util.Cloner; |
49 | |
import com.sun.grizzly.util.Copyable; |
50 | |
import com.sun.grizzly.util.SelectionKeyAttachment; |
51 | |
import com.sun.grizzly.util.SelectionKeyOP; |
52 | |
import com.sun.grizzly.util.SelectionKeyOP.ConnectSelectionKeyOP; |
53 | |
import com.sun.grizzly.util.State; |
54 | |
import com.sun.grizzly.util.StateHolder; |
55 | |
import java.io.IOException; |
56 | |
import java.net.BindException; |
57 | |
import java.net.InetAddress; |
58 | |
import java.net.ServerSocket; |
59 | |
import java.net.Socket; |
60 | |
import java.net.InetSocketAddress; |
61 | |
import java.net.SocketAddress; |
62 | |
import java.net.SocketException; |
63 | |
import java.nio.channels.CancelledKeyException; |
64 | |
import java.nio.channels.ClosedChannelException; |
65 | |
import java.nio.channels.ClosedSelectorException; |
66 | |
import java.nio.channels.SelectableChannel; |
67 | |
import java.nio.channels.SelectionKey; |
68 | |
import java.nio.channels.Selector; |
69 | |
import java.nio.channels.ServerSocketChannel; |
70 | |
import java.nio.channels.SocketChannel; |
71 | |
import java.util.ConcurrentModificationException; |
72 | |
import java.util.HashMap; |
73 | |
import java.util.Iterator; |
74 | |
import java.util.Map; |
75 | |
import java.util.Set; |
76 | |
import java.util.concurrent.Callable; |
77 | |
import java.util.concurrent.ConcurrentLinkedQueue; |
78 | |
import java.util.concurrent.atomic.AtomicBoolean; |
79 | |
import java.util.logging.Level; |
80 | |
import java.util.logging.Logger; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public class TCPSelectorHandler implements SelectorHandler { |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
protected ConnectorInstanceHandler connectorInstanceHandler; |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
protected ConcurrentLinkedQueue<SelectionKeyOP> opToRegister; |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | 141 | protected boolean tcpNoDelay = true; |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | 141 | protected boolean reuseAddress = true; |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | 141 | protected int linger = -1; |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | 141 | protected int socketTimeout = -1; |
134 | |
|
135 | |
|
136 | |
protected Logger logger; |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | 141 | protected int serverTimeout = 0; |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
protected InetAddress inet; |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | 141 | protected int port = 18888; |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
protected ServerSocket serverSocket; |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
protected ServerSocketChannel serverSocketChannel; |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
protected Selector selector; |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | 141 | protected long selectTimeout = 1000L; |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | 141 | protected int ssBackLog = 4096; |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | 141 | protected Role role = Role.CLIENT_SERVER; |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
protected SelectionKeyHandler selectionKeyHandler; |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
protected ProtocolChainInstanceHandler instanceHandler; |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
protected Pipeline pipeline; |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
protected AsyncQueueWriter asyncQueueWriter; |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
protected AsyncQueueReader asyncQueueReader; |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
protected Map<String, Object> attributes; |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | 141 | protected StateHolder<State> stateHolder = new StateHolder<State>(true); |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | 141 | protected AtomicBoolean isShutDown = new AtomicBoolean(false); |
240 | |
|
241 | |
public TCPSelectorHandler(){ |
242 | 56 | this(Role.CLIENT_SERVER); |
243 | 56 | } |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
public TCPSelectorHandler(boolean isClient) { |
253 | 79 | this(boolean2Role(isClient)); |
254 | 79 | } |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | 141 | public TCPSelectorHandler(Role role) { |
263 | 141 | this.role = role; |
264 | 141 | logger = Controller.logger(); |
265 | 141 | } |
266 | |
|
267 | |
public void copyTo(Copyable copy) { |
268 | 15 | TCPSelectorHandler copyHandler = (TCPSelectorHandler) copy; |
269 | 15 | copyHandler.selector = selector; |
270 | 15 | if (selectionKeyHandler != null) { |
271 | 4 | copyHandler.setSelectionKeyHandler(Cloner.clone(selectionKeyHandler)); |
272 | |
} |
273 | |
|
274 | 15 | copyHandler.attributes = attributes; |
275 | 15 | copyHandler.selectTimeout = selectTimeout; |
276 | 15 | copyHandler.serverTimeout = serverTimeout; |
277 | 15 | copyHandler.inet = inet; |
278 | 15 | copyHandler.port = port; |
279 | 15 | copyHandler.ssBackLog = ssBackLog; |
280 | 15 | copyHandler.tcpNoDelay = tcpNoDelay; |
281 | 15 | copyHandler.linger = linger; |
282 | 15 | copyHandler.socketTimeout = socketTimeout; |
283 | 15 | copyHandler.logger = logger; |
284 | 15 | copyHandler.reuseAddress = reuseAddress; |
285 | 15 | copyHandler.connectorInstanceHandler = connectorInstanceHandler; |
286 | 15 | copyHandler.stateHolder = stateHolder; |
287 | 15 | } |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
public Set<SelectionKey> keys(){ |
294 | 266289 | if (selector != null){ |
295 | 266289 | return selector.keys(); |
296 | |
} else { |
297 | 0 | throw new IllegalStateException("Selector is not created!"); |
298 | |
} |
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
public boolean isOpen(){ |
306 | 265630 | if (selector != null){ |
307 | 265630 | return selector.isOpen(); |
308 | |
} else { |
309 | 0 | return false; |
310 | |
} |
311 | |
} |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
public void preSelect(Context ctx) throws IOException { |
320 | 215101 | initOpRegistriesIfRequired(); |
321 | |
|
322 | 215101 | if (asyncQueueReader == null) { |
323 | 130 | asyncQueueReader = new TCPAsyncQueueReader(this); |
324 | |
} |
325 | |
|
326 | 215101 | if (asyncQueueWriter == null) { |
327 | 130 | asyncQueueWriter = new TCPAsyncQueueWriter(this); |
328 | |
} |
329 | |
|
330 | 215101 | if (selector == null){ |
331 | |
try { |
332 | 113 | isShutDown.set(false); |
333 | |
|
334 | 113 | connectorInstanceHandler = new ConnectorInstanceHandler. |
335 | |
ConcurrentQueueDelegateCIH( |
336 | |
getConnectorInstanceHandlerDelegate()); |
337 | |
|
338 | |
|
339 | 113 | selector = Selector.open(); |
340 | |
|
341 | 113 | if (role != Role.CLIENT){ |
342 | 34 | serverSocketChannel = ServerSocketChannel.open(); |
343 | 34 | serverSocket = serverSocketChannel.socket(); |
344 | 34 | serverSocket.setReuseAddress(reuseAddress); |
345 | 34 | if ( inet == null){ |
346 | 34 | serverSocket.bind(new InetSocketAddress(port),ssBackLog); |
347 | |
} else { |
348 | 0 | serverSocket.bind(new InetSocketAddress(inet,port),ssBackLog); |
349 | |
} |
350 | |
|
351 | 34 | serverSocketChannel.configureBlocking(false); |
352 | 34 | serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); |
353 | |
} |
354 | 113 | ctx.getController().notifyReady(); |
355 | 0 | } catch (SocketException ex){ |
356 | 0 | throw new BindException(ex.getMessage() + ": " + port + "=" + this); |
357 | 113 | } |
358 | |
|
359 | 113 | if (role != Role.CLIENT){ |
360 | 34 | serverSocket.setSoTimeout(serverTimeout); |
361 | |
} |
362 | |
} else { |
363 | 214988 | processPendingOperations(ctx); |
364 | |
} |
365 | 215101 | } |
366 | |
|
367 | |
|
368 | |
protected void processPendingOperations(Context ctx) throws IOException { |
369 | 266170 | if (!opToRegister.isEmpty()) { |
370 | |
SelectionKeyOP operation; |
371 | 137761 | Iterator<SelectionKeyOP> opIterator = opToRegister.iterator(); |
372 | 677725 | while(opIterator.hasNext()) { |
373 | 539964 | operation = opIterator.next(); |
374 | 539964 | opIterator.remove(); |
375 | |
|
376 | 539964 | if ((operation.getOp() & SelectionKey.OP_CONNECT) != 0) { |
377 | 205 | onConnectOp(ctx, |
378 | |
(SelectionKeyOP.ConnectSelectionKeyOP) operation); |
379 | |
} else { |
380 | 539759 | if ((operation.getOp() & SelectionKey.OP_READ) != 0) { |
381 | 158833 | onReadOp(operation); |
382 | |
} |
383 | |
|
384 | 539759 | if ((operation.getOp() & SelectionKey.OP_WRITE) != 0) { |
385 | 380926 | onWriteOp(operation); |
386 | |
} |
387 | |
} |
388 | |
|
389 | 539964 | SelectionKeyOP.releaseSelectionKeyOP(operation); |
390 | |
} |
391 | |
} |
392 | 266170 | } |
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
protected void onReadOp(SelectionKeyOP selectionKeyOp) |
398 | |
throws ClosedChannelException { |
399 | 158833 | SelectionKey key = selectionKeyOp.getSelectionKey(); |
400 | 158833 | if (key != null) { |
401 | 158810 | selectionKeyHandler.register(key, SelectionKey.OP_READ); |
402 | |
} else { |
403 | 23 | selectionKeyHandler.register(selectionKeyOp.getChannel(), |
404 | |
SelectionKey.OP_READ); |
405 | |
} |
406 | 158833 | } |
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
protected void onWriteOp(SelectionKeyOP selectionKeyOp) |
413 | |
throws ClosedChannelException { |
414 | 380926 | SelectionKey key = selectionKeyOp.getSelectionKey(); |
415 | 380926 | if (key != null) { |
416 | 380926 | selectionKeyHandler.register(key, SelectionKey.OP_WRITE); |
417 | |
} else { |
418 | 0 | selectionKeyHandler.register(selectionKeyOp.getChannel(), |
419 | |
SelectionKey.OP_WRITE); |
420 | |
} |
421 | 380926 | } |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
protected void onConnectOp(Context ctx, |
428 | |
SelectionKeyOP.ConnectSelectionKeyOP selectionKeyOp) throws IOException { |
429 | 174 | SocketAddress remoteAddress = selectionKeyOp.getRemoteAddress(); |
430 | 174 | SocketAddress localAddress = selectionKeyOp.getLocalAddress(); |
431 | 174 | CallbackHandler callbackHandler = selectionKeyOp.getCallbackHandler(); |
432 | |
|
433 | 174 | SocketChannel socketChannel = SocketChannel.open(); |
434 | 174 | socketChannel.socket().setReuseAddress(reuseAddress); |
435 | 174 | if (localAddress != null) { |
436 | 0 | socketChannel.socket().bind(localAddress); |
437 | |
} |
438 | |
|
439 | 174 | socketChannel.configureBlocking(false); |
440 | 174 | SelectionKey key = socketChannel.register(selector, |
441 | |
SelectionKey.OP_CONNECT); |
442 | 174 | key.attach(CallbackHandlerSelectionKeyAttachment.create(key, callbackHandler)); |
443 | |
|
444 | |
boolean isConnected; |
445 | |
try { |
446 | 174 | isConnected = socketChannel.connect(remoteAddress); |
447 | 0 | } catch(Exception e) { |
448 | 0 | if (logger.isLoggable(Level.FINE)) { |
449 | 0 | logger.log(Level.FINE, "Exception occured when tried to connect socket", e); |
450 | |
} |
451 | |
|
452 | |
|
453 | 0 | isConnected = true; |
454 | 174 | } |
455 | |
|
456 | |
|
457 | 174 | if (isConnected) { |
458 | 0 | onConnectInterest(key, ctx); |
459 | |
} |
460 | 174 | } |
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
public Set<SelectionKey> select(Context ctx) throws IOException{ |
469 | 266289 | selector.select(selectTimeout); |
470 | 266289 | return selector.selectedKeys(); |
471 | |
} |
472 | |
|
473 | |
|
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
public void postSelect(Context ctx) { |
479 | 266289 | Set<SelectionKey> readyKeys = keys(); |
480 | 266289 | if (readyKeys.isEmpty()){ |
481 | 659 | return; |
482 | |
} |
483 | |
|
484 | 265629 | if (isOpen()) { |
485 | 265630 | selectionKeyHandler.expire(readyKeys.iterator()); |
486 | |
} |
487 | 265630 | } |
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
|
493 | |
public void register(SelectionKey key, int ops) { |
494 | 539750 | if (key == null) { |
495 | 0 | throw new NullPointerException("SelectionKey parameter is null"); |
496 | |
} |
497 | |
|
498 | 539750 | SelectionKeyOP keyOP = SelectionKeyOP.aquireSelectionKeyOP(ops); |
499 | 539750 | keyOP.setSelectionKey(key); |
500 | 539748 | keyOP.setOp(ops); |
501 | 539749 | initOpRegistriesIfRequired(); |
502 | 539749 | opToRegister.offer(keyOP); |
503 | 539749 | selector.wakeup(); |
504 | 539749 | } |
505 | |
|
506 | |
public void register(SelectableChannel channel, int ops) { |
507 | 23 | if (channel == null) { |
508 | 0 | throw new NullPointerException("SelectableChannel parameter is null"); |
509 | |
} |
510 | |
|
511 | 23 | SelectionKeyOP keyOP = SelectionKeyOP.aquireSelectionKeyOP(ops); |
512 | 23 | keyOP.setChannel(channel); |
513 | 23 | keyOP.setOp(ops); |
514 | 23 | initOpRegistriesIfRequired(); |
515 | 23 | opToRegister.offer(keyOP); |
516 | 23 | selector.wakeup(); |
517 | 23 | } |
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
|
526 | |
|
527 | |
protected void connect(SocketAddress remoteAddress, SocketAddress localAddress, |
528 | |
CallbackHandler callBackHandler) throws IOException { |
529 | |
|
530 | 205 | SelectionKeyOP.ConnectSelectionKeyOP keyOP = |
531 | |
(ConnectSelectionKeyOP) SelectionKeyOP.aquireSelectionKeyOP(SelectionKey.OP_CONNECT); |
532 | |
|
533 | 205 | keyOP.setOp(SelectionKey.OP_CONNECT); |
534 | 205 | keyOP.setRemoteAddress(remoteAddress); |
535 | 205 | keyOP.setLocalAddress(localAddress); |
536 | 205 | keyOP.setCallbackHandler(callBackHandler); |
537 | 205 | initOpRegistriesIfRequired(); |
538 | 205 | opToRegister.offer(keyOP); |
539 | 205 | selector.wakeup(); |
540 | 205 | } |
541 | |
|
542 | |
|
543 | |
|
544 | |
|
545 | |
public void pause() { |
546 | 1 | stateHolder.setState(State.PAUSED); |
547 | 1 | } |
548 | |
|
549 | |
|
550 | |
|
551 | |
|
552 | |
public void resume() { |
553 | 1 | if (!State.PAUSED.equals(stateHolder.getState(false))) { |
554 | 0 | throw new IllegalStateException("SelectorHandler is not in PAUSED state, but: " + |
555 | |
stateHolder.getState(false)); |
556 | |
} |
557 | |
|
558 | 1 | stateHolder.setState(State.STARTED); |
559 | 1 | } |
560 | |
|
561 | |
|
562 | |
|
563 | |
|
564 | |
public StateHolder<State> getStateHolder() { |
565 | 266473 | return stateHolder; |
566 | |
} |
567 | |
|
568 | |
|
569 | |
|
570 | |
|
571 | |
public void shutdown() { |
572 | |
|
573 | 137 | if (isShutDown.getAndSet(true)) return; |
574 | |
|
575 | 136 | stateHolder.setState(State.STOPPED); |
576 | |
|
577 | 136 | if (selector != null) { |
578 | |
try { |
579 | 130 | for (SelectionKey selectionKey : selector.keys()) { |
580 | 76 | selectionKeyHandler.close(selectionKey); |
581 | |
} |
582 | 0 | } catch (ClosedSelectorException e) { |
583 | |
|
584 | 0 | } catch (ConcurrentModificationException e) { |
585 | |
|
586 | 0 | Object[] keys = selector.keys().toArray(); |
587 | 0 | for (Object selectionKey : keys) { |
588 | 0 | selectionKeyHandler.close((SelectionKey) selectionKey); |
589 | |
} |
590 | |
|
591 | 130 | } |
592 | |
} |
593 | |
|
594 | |
try{ |
595 | 136 | if (serverSocket != null) |
596 | 34 | serverSocket.close(); |
597 | 0 | } catch (Throwable ex){ |
598 | 0 | Controller.logger().log(Level.SEVERE, |
599 | |
"serverSocket.close",ex); |
600 | 136 | } |
601 | |
|
602 | |
try{ |
603 | 136 | if (serverSocketChannel != null) |
604 | 34 | serverSocketChannel.close(); |
605 | 0 | } catch (Throwable ex){ |
606 | 0 | Controller.logger().log(Level.SEVERE, |
607 | |
"serverSocketChannel.close",ex); |
608 | 136 | } |
609 | |
|
610 | |
try{ |
611 | 136 | if (selector != null) |
612 | 130 | selector.close(); |
613 | 0 | } catch (Throwable ex){ |
614 | 0 | Controller.logger().log(Level.SEVERE, |
615 | |
"selector.close",ex); |
616 | 136 | } |
617 | |
|
618 | 136 | if (asyncQueueReader != null) { |
619 | 130 | asyncQueueReader.close(); |
620 | 130 | asyncQueueReader = null; |
621 | |
} |
622 | |
|
623 | 136 | if (asyncQueueWriter != null) { |
624 | 130 | asyncQueueWriter.close(); |
625 | 130 | asyncQueueWriter = null; |
626 | |
} |
627 | |
|
628 | 136 | attributes = null; |
629 | 136 | } |
630 | |
|
631 | |
|
632 | |
|
633 | |
|
634 | |
public SelectableChannel acceptWithoutRegistration(SelectionKey key) |
635 | |
throws IOException { |
636 | 199 | ServerSocketChannel server = (ServerSocketChannel) key.channel(); |
637 | 199 | SocketChannel channel = server.accept(); |
638 | 199 | return channel; |
639 | |
} |
640 | |
|
641 | |
|
642 | |
|
643 | |
|
644 | |
|
645 | |
|
646 | |
public boolean onAcceptInterest(SelectionKey key, |
647 | |
Context ctx) throws IOException{ |
648 | 178 | SelectableChannel channel = acceptWithoutRegistration(key); |
649 | |
|
650 | 178 | if (channel != null) { |
651 | 178 | configureChannel(channel); |
652 | 178 | SelectionKey readKey = |
653 | |
channel.register(selector, SelectionKey.OP_READ); |
654 | 178 | readKey.attach(System.currentTimeMillis()); |
655 | |
} |
656 | 178 | return false; |
657 | |
} |
658 | |
|
659 | |
|
660 | |
|
661 | |
|
662 | |
|
663 | |
|
664 | |
|
665 | |
public boolean onReadInterest(final SelectionKey key,final Context ctx) |
666 | |
throws IOException{ |
667 | |
|
668 | 158935 | key.interestOps(key.interestOps() & (~SelectionKey.OP_READ)); |
669 | 158935 | Object attach = SelectionKeyAttachment.getAttachment(key); |
670 | |
|
671 | 158935 | if (asyncQueueReader.isAsyncQueueReaderEnabledFor(key)) { |
672 | 9757 | final Context context = pollContext(ctx, key, Context.OpType.OP_READ); |
673 | 9757 | invokeAsyncQueueReader(context); |
674 | 9757 | return false; |
675 | 149178 | } else if (attach instanceof CallbackHandler){ |
676 | 58379 | final Context context = pollContext(ctx, key, Context.OpType.OP_READ); |
677 | 58379 | invokeCallbackHandler((CallbackHandler) attach, context); |
678 | 58379 | return false; |
679 | |
} else { |
680 | 90799 | return true; |
681 | |
} |
682 | |
} |
683 | |
|
684 | |
|
685 | |
|
686 | |
|
687 | |
|
688 | |
|
689 | |
|
690 | |
|
691 | |
public boolean onWriteInterest(final SelectionKey key,final Context ctx) |
692 | |
throws IOException{ |
693 | |
|
694 | 2241 | key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE)); |
695 | |
|
696 | |
Object attach; |
697 | 2241 | if (asyncQueueWriter.hasReadyAsyncWriteData(key)) { |
698 | 2183 | final Context context = pollContext(ctx, key, Context.OpType.OP_WRITE); |
699 | 2183 | invokeAsyncQueueWriter(context); |
700 | 2183 | return false; |
701 | 58 | } else if ((attach = SelectionKeyAttachment.getAttachment(key)) |
702 | |
instanceof CallbackHandler){ |
703 | 42 | final Context context = pollContext(ctx, key, Context.OpType.OP_WRITE); |
704 | 42 | invokeCallbackHandler((CallbackHandler) attach, context); |
705 | 42 | return false; |
706 | |
} else { |
707 | 16 | return true; |
708 | |
} |
709 | |
} |
710 | |
|
711 | |
|
712 | |
|
713 | |
|
714 | |
|
715 | |
|
716 | |
|
717 | |
public boolean onConnectInterest(final SelectionKey key, Context ctx) |
718 | |
throws IOException{ |
719 | |
try { |
720 | |
|
721 | 205 | key.interestOps(key.interestOps() & (~SelectionKey.OP_CONNECT)); |
722 | |
|
723 | |
|
724 | 205 | key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE)); |
725 | 205 | key.interestOps(key.interestOps() & (~SelectionKey.OP_READ)); |
726 | 0 | } catch(CancelledKeyException e) { |
727 | |
|
728 | 0 | if (logger.isLoggable(Level.FINE)) { |
729 | 0 | logger.log(Level.FINE, "CancelledKeyException occured when tried to change key interests", e); |
730 | |
} |
731 | 205 | } |
732 | |
|
733 | 205 | Object attach = SelectionKeyAttachment.getAttachment(key); |
734 | 205 | if (attach instanceof CallbackHandler){ |
735 | 205 | final Context context = pollContext(ctx, key, Context.OpType.OP_CONNECT); |
736 | 205 | invokeCallbackHandler((CallbackHandler) attach, context); |
737 | |
} |
738 | 205 | return false; |
739 | |
} |
740 | |
|
741 | |
|
742 | |
|
743 | |
|
744 | |
|
745 | |
|
746 | |
|
747 | |
protected void invokeCallbackHandler(CallbackHandler callbackHandler, |
748 | |
Context context) throws IOException { |
749 | 58626 | IOEvent<Context>ioEvent = new IOEvent.DefaultIOEvent<Context>(context); |
750 | 58626 | context.setIOEvent(ioEvent); |
751 | |
|
752 | |
|
753 | 58626 | context.setSelectorHandler(this); |
754 | |
|
755 | |
try { |
756 | 58626 | CallbackHandlerContextTask task = CallbackHandlerContextTask.poll(); |
757 | 58626 | task.setCallBackHandler(callbackHandler); |
758 | 58626 | boolean isRunInSeparateThread = true; |
759 | |
|
760 | 58626 | if (callbackHandler instanceof CallbackHandlerDescriptor) { |
761 | 0 | isRunInSeparateThread = |
762 | |
((CallbackHandlerDescriptor) callbackHandler). |
763 | |
isRunInSeparateThread(context.getCurrentOpType()); |
764 | |
} |
765 | 58626 | context.execute(task, isRunInSeparateThread); |
766 | 0 | } catch (PipelineFullException ex) { |
767 | 0 | throw new IOException(ex.getMessage()); |
768 | 58626 | } |
769 | 58626 | } |
770 | |
|
771 | |
|
772 | |
|
773 | |
|
774 | |
|
775 | |
|
776 | |
|
777 | |
protected void invokeAsyncQueueReader(Context context) throws IOException { |
778 | 9757 | AsyncQueueReaderContextTask task = AsyncQueueReaderContextTask.poll(); |
779 | 9757 | task.setAsyncQueueReader(asyncQueueReader); |
780 | |
try { |
781 | 9757 | context.execute(task); |
782 | 0 | } catch (PipelineFullException ex) { |
783 | 0 | throw new IOException(ex.getMessage()); |
784 | 9757 | } |
785 | 9757 | } |
786 | |
|
787 | |
|
788 | |
|
789 | |
|
790 | |
|
791 | |
|
792 | |
|
793 | |
protected void invokeAsyncQueueWriter(Context context) throws IOException { |
794 | 2183 | AsyncQueueWriterContextTask task = AsyncQueueWriterContextTask.poll(); |
795 | 2183 | task.setAsyncQueueWriter(asyncQueueWriter); |
796 | |
try { |
797 | 2183 | context.execute(task); |
798 | 0 | } catch (PipelineFullException ex) { |
799 | 0 | throw new IOException(ex.getMessage()); |
800 | 2183 | } |
801 | 2183 | } |
802 | |
|
803 | |
|
804 | |
|
805 | |
|
806 | |
|
807 | |
|
808 | |
|
809 | |
public ConnectorHandler acquireConnectorHandler(){ |
810 | 175 | if (selector == null || !selector.isOpen()){ |
811 | 0 | throw new IllegalStateException("SelectorHandler not yet started"); |
812 | |
} |
813 | |
|
814 | 175 | ConnectorHandler connectorHandler = connectorInstanceHandler.acquire(); |
815 | 175 | connectorHandler.setController(Controller.getHandlerController(this)); |
816 | 175 | return connectorHandler; |
817 | |
} |
818 | |
|
819 | |
|
820 | |
|
821 | |
|
822 | |
|
823 | |
public void releaseConnectorHandler(ConnectorHandler connectorHandler){ |
824 | 174 | connectorInstanceHandler.release(connectorHandler); |
825 | 174 | } |
826 | |
|
827 | |
|
828 | |
|
829 | |
|
830 | |
|
831 | |
|
832 | |
public Controller.Protocol protocol(){ |
833 | 49971 | return Controller.Protocol.TCP; |
834 | |
} |
835 | |
|
836 | |
|
837 | |
|
838 | |
|
839 | |
|
840 | |
|
841 | |
protected void initOpRegistriesIfRequired() { |
842 | 806205 | if (opToRegister == null){ |
843 | 135 | opToRegister = new ConcurrentLinkedQueue<SelectionKeyOP>(); |
844 | |
} |
845 | 806267 | } |
846 | |
|
847 | |
|
848 | |
|
849 | |
|
850 | |
public void configureChannel(SelectableChannel channel) throws IOException{ |
851 | 199 | Socket socket = ((SocketChannel) channel).socket(); |
852 | |
|
853 | 199 | channel.configureBlocking(false); |
854 | |
|
855 | 199 | if (!channel.isOpen()){ |
856 | 0 | return; |
857 | |
} |
858 | |
|
859 | |
try{ |
860 | 199 | if(socketTimeout >= 0 ) { |
861 | 0 | socket.setSoTimeout(socketTimeout); |
862 | |
} |
863 | 0 | } catch (SocketException ex){ |
864 | 0 | if (logger.isLoggable(Level.FINE)){ |
865 | 0 | logger.log(Level.FINE, |
866 | |
"setSoTimeout exception ",ex); |
867 | |
} |
868 | 199 | } |
869 | |
|
870 | |
try{ |
871 | 199 | if(linger >= 0 ) { |
872 | 0 | socket.setSoLinger( true, linger); |
873 | |
} |
874 | 0 | } catch (SocketException ex){ |
875 | 0 | if (logger.isLoggable(Level.FINE)){ |
876 | 0 | logger.log(Level.FINE, |
877 | |
"setSoLinger exception ",ex); |
878 | |
} |
879 | 199 | } |
880 | |
|
881 | |
try{ |
882 | 199 | socket.setTcpNoDelay(tcpNoDelay); |
883 | 0 | } catch (SocketException ex){ |
884 | 0 | if (logger.isLoggable(Level.FINE)){ |
885 | 0 | logger.log(Level.FINE, |
886 | |
"setTcpNoDelay exception ",ex); |
887 | |
} |
888 | 199 | } |
889 | |
|
890 | |
try{ |
891 | 199 | socket.setReuseAddress(reuseAddress); |
892 | 0 | } catch (SocketException ex){ |
893 | 0 | if (logger.isLoggable(Level.FINE)){ |
894 | 0 | logger.log(Level.FINE, |
895 | |
"setReuseAddress exception ",ex); |
896 | |
} |
897 | 199 | } |
898 | 199 | } |
899 | |
|
900 | |
|
901 | |
|
902 | |
|
903 | |
public final Selector getSelector() { |
904 | 867232 | return selector; |
905 | |
} |
906 | |
|
907 | |
public final void setSelector(Selector selector) { |
908 | 18 | this.selector = selector; |
909 | 18 | } |
910 | |
|
911 | |
|
912 | |
|
913 | |
|
914 | |
public AsyncQueueReader getAsyncQueueReader() { |
915 | 91104 | return asyncQueueReader; |
916 | |
} |
917 | |
|
918 | |
|
919 | |
|
920 | |
|
921 | |
public AsyncQueueWriter getAsyncQueueWriter() { |
922 | 910802 | return asyncQueueWriter; |
923 | |
} |
924 | |
|
925 | |
public long getSelectTimeout() { |
926 | 0 | return selectTimeout; |
927 | |
} |
928 | |
|
929 | |
public void setSelectTimeout(long selectTimeout) { |
930 | 0 | this.selectTimeout = selectTimeout; |
931 | 0 | } |
932 | |
|
933 | |
public int getServerTimeout() { |
934 | 0 | return serverTimeout; |
935 | |
} |
936 | |
|
937 | |
public void setServerTimeout(int serverTimeout) { |
938 | 0 | this.serverTimeout = serverTimeout; |
939 | 0 | } |
940 | |
|
941 | |
public InetAddress getInet() { |
942 | 0 | return inet; |
943 | |
} |
944 | |
|
945 | |
public void setInet(InetAddress inet) { |
946 | 0 | this.inet = inet; |
947 | 0 | } |
948 | |
|
949 | |
|
950 | |
|
951 | |
|
952 | |
|
953 | |
|
954 | |
|
955 | |
|
956 | |
|
957 | |
public Role getRole() { |
958 | 0 | return role; |
959 | |
} |
960 | |
|
961 | |
|
962 | |
|
963 | |
|
964 | |
|
965 | |
|
966 | |
|
967 | |
|
968 | |
|
969 | |
public void setRole(Role role) { |
970 | 0 | this.role = role; |
971 | 0 | } |
972 | |
|
973 | |
|
974 | |
|
975 | |
|
976 | |
|
977 | |
|
978 | |
|
979 | |
|
980 | |
|
981 | |
|
982 | |
|
983 | |
public int getPortLowLevel() { |
984 | 2 | if (serverSocket != null) { |
985 | 2 | return serverSocket.getLocalPort(); |
986 | |
} |
987 | |
|
988 | 0 | return -1; |
989 | |
} |
990 | |
|
991 | |
public int getPort() { |
992 | 3 | return port; |
993 | |
} |
994 | |
|
995 | |
public void setPort(int port) { |
996 | 39 | this.port = port; |
997 | 39 | } |
998 | |
|
999 | |
public int getSsBackLog() { |
1000 | 0 | return ssBackLog; |
1001 | |
} |
1002 | |
|
1003 | |
public void setSsBackLog(int ssBackLog) { |
1004 | 0 | this.ssBackLog = ssBackLog; |
1005 | 0 | } |
1006 | |
|
1007 | |
|
1008 | |
|
1009 | |
|
1010 | |
|
1011 | |
|
1012 | |
|
1013 | |
public boolean isTcpNoDelay() { |
1014 | 0 | return tcpNoDelay; |
1015 | |
} |
1016 | |
|
1017 | |
|
1018 | |
|
1019 | |
|
1020 | |
|
1021 | |
|
1022 | |
|
1023 | |
|
1024 | |
|
1025 | |
|
1026 | |
|
1027 | |
|
1028 | |
public void setTcpNoDelay(boolean tcpNoDelay) { |
1029 | 0 | this.tcpNoDelay = tcpNoDelay; |
1030 | 0 | } |
1031 | |
|
1032 | |
public int getLinger() { |
1033 | 0 | return linger; |
1034 | |
} |
1035 | |
|
1036 | |
public void setLinger(int linger) { |
1037 | 0 | this.linger = linger; |
1038 | 0 | } |
1039 | |
|
1040 | |
public int getSocketTimeout() { |
1041 | 0 | return socketTimeout; |
1042 | |
} |
1043 | |
|
1044 | |
public void setSocketTimeout(int socketTimeout) { |
1045 | 0 | this.socketTimeout = socketTimeout; |
1046 | 0 | } |
1047 | |
|
1048 | |
public Logger getLogger() { |
1049 | 0 | return logger; |
1050 | |
} |
1051 | |
|
1052 | |
public void setLogger(Logger logger) { |
1053 | 0 | this.logger = logger; |
1054 | 0 | } |
1055 | |
|
1056 | |
public boolean isReuseAddress() { |
1057 | 0 | return reuseAddress; |
1058 | |
} |
1059 | |
|
1060 | |
public void setReuseAddress(boolean reuseAddress) { |
1061 | 0 | this.reuseAddress = reuseAddress; |
1062 | 0 | } |
1063 | |
|
1064 | |
|
1065 | |
|
1066 | |
|
1067 | |
public Pipeline pipeline(){ |
1068 | 90804 | return pipeline; |
1069 | |
} |
1070 | |
|
1071 | |
|
1072 | |
|
1073 | |
|
1074 | |
|
1075 | |
public void setPipeline(Pipeline pipeline){ |
1076 | 0 | this.pipeline = pipeline; |
1077 | 0 | } |
1078 | |
|
1079 | |
|
1080 | |
|
1081 | |
|
1082 | |
|
1083 | |
public Class<? extends SelectionKeyHandler> getPreferredSelectionKeyHandler() { |
1084 | 238 | return DefaultSelectionKeyHandler.class; |
1085 | |
} |
1086 | |
|
1087 | |
|
1088 | |
|
1089 | |
|
1090 | |
|
1091 | |
public SelectionKeyHandler getSelectionKeyHandler() { |
1092 | 565487 | return selectionKeyHandler; |
1093 | |
} |
1094 | |
|
1095 | |
|
1096 | |
|
1097 | |
|
1098 | |
|
1099 | |
public void setSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler) { |
1100 | 135 | this.selectionKeyHandler = selectionKeyHandler; |
1101 | 135 | this.selectionKeyHandler.setSelectorHandler(this); |
1102 | 135 | } |
1103 | |
|
1104 | |
|
1105 | |
|
1106 | |
|
1107 | |
|
1108 | |
|
1109 | |
public void setProtocolChainInstanceHandler(ProtocolChainInstanceHandler |
1110 | |
instanceHandler){ |
1111 | 0 | this.instanceHandler = instanceHandler; |
1112 | 0 | } |
1113 | |
|
1114 | |
|
1115 | |
|
1116 | |
|
1117 | |
|
1118 | |
public ProtocolChainInstanceHandler getProtocolChainInstanceHandler(){ |
1119 | 252174 | return instanceHandler; |
1120 | |
} |
1121 | |
|
1122 | |
|
1123 | |
|
1124 | |
|
1125 | |
public void closeChannel(SelectableChannel channel) { |
1126 | |
|
1127 | 408 | if (channel instanceof SocketChannel) { |
1128 | 374 | Socket socket = ((SocketChannel) channel).socket(); |
1129 | |
|
1130 | |
try { |
1131 | 374 | if (!socket.isInputShutdown()) socket.shutdownInput(); |
1132 | 0 | } catch (IOException ex){ |
1133 | |
; |
1134 | 374 | } |
1135 | |
|
1136 | |
try { |
1137 | 374 | if (!socket.isOutputShutdown()) socket.shutdownOutput(); |
1138 | 0 | } catch (IOException ex){ |
1139 | |
; |
1140 | 374 | } |
1141 | |
|
1142 | |
try{ |
1143 | 374 | socket.close(); |
1144 | 0 | } catch (IOException ex){ |
1145 | |
; |
1146 | 374 | } |
1147 | |
} |
1148 | |
|
1149 | |
try{ |
1150 | 408 | channel.close(); |
1151 | 0 | } catch (IOException ex){ |
1152 | |
; |
1153 | 408 | } |
1154 | |
|
1155 | 408 | if (asyncQueueReader != null) { |
1156 | 408 | asyncQueueReader.onClose(channel); |
1157 | |
} |
1158 | |
|
1159 | 408 | if (asyncQueueWriter != null) { |
1160 | 408 | asyncQueueWriter.onClose(channel); |
1161 | |
} |
1162 | 408 | } |
1163 | |
|
1164 | |
|
1165 | |
|
1166 | |
|
1167 | |
|
1168 | |
|
1169 | |
|
1170 | |
|
1171 | |
protected Context pollContext(final Context serverContext, |
1172 | |
final SelectionKey key, final Context.OpType opType) { |
1173 | 70566 | ProtocolChain protocolChain = instanceHandler != null ? |
1174 | |
instanceHandler.poll() : |
1175 | |
serverContext.getController().getProtocolChainInstanceHandler().poll(); |
1176 | |
|
1177 | 70566 | final Context context = serverContext.getController().pollContext(key, opType); |
1178 | 70566 | context.setSelectionKey(key); |
1179 | 70566 | context.setSelectorHandler(this); |
1180 | 70566 | context.setAsyncQueueReader(asyncQueueReader); |
1181 | 70566 | context.setAsyncQueueWriter(asyncQueueWriter); |
1182 | 70566 | context.setProtocolChain(protocolChain); |
1183 | 70566 | return context; |
1184 | |
} |
1185 | |
|
1186 | |
|
1187 | |
|
1188 | |
|
1189 | |
|
1190 | |
|
1191 | |
|
1192 | |
protected Callable<ConnectorHandler> getConnectorInstanceHandlerDelegate() { |
1193 | 103 | return new Callable<ConnectorHandler>() { |
1194 | |
public ConnectorHandler call() throws Exception { |
1195 | 5 | return new TCPConnectorHandler(); |
1196 | |
} |
1197 | |
}; |
1198 | |
} |
1199 | |
|
1200 | |
|
1201 | |
|
1202 | |
|
1203 | |
|
1204 | |
|
1205 | |
|
1206 | |
|
1207 | |
|
1208 | |
|
1209 | |
public Object removeAttribute(String key) { |
1210 | 0 | if (attributes == null) return null; |
1211 | |
|
1212 | 0 | return attributes.remove(key); |
1213 | |
} |
1214 | |
|
1215 | |
|
1216 | |
|
1217 | |
|
1218 | |
|
1219 | |
|
1220 | |
|
1221 | |
|
1222 | |
public void setAttribute(String key, Object value) { |
1223 | 0 | if (attributes == null) { |
1224 | 0 | attributes = new HashMap<String, Object>(); |
1225 | |
} |
1226 | |
|
1227 | 0 | attributes.put(key, value); |
1228 | 0 | } |
1229 | |
|
1230 | |
|
1231 | |
|
1232 | |
|
1233 | |
|
1234 | |
|
1235 | |
|
1236 | |
|
1237 | |
|
1238 | |
public Object getAttribute(String key) { |
1239 | 0 | if (attributes == null) return null; |
1240 | |
|
1241 | 0 | return attributes.get(key); |
1242 | |
} |
1243 | |
|
1244 | |
|
1245 | |
|
1246 | |
|
1247 | |
|
1248 | |
|
1249 | |
|
1250 | |
|
1251 | |
|
1252 | |
|
1253 | |
public void setAttributes(Map<String, Object> attributes) { |
1254 | 6 | this.attributes = attributes; |
1255 | 6 | } |
1256 | |
|
1257 | |
|
1258 | |
|
1259 | |
|
1260 | |
|
1261 | |
|
1262 | |
|
1263 | |
|
1264 | |
|
1265 | |
public Map<String, Object> getAttributes() { |
1266 | 6 | return attributes; |
1267 | |
} |
1268 | |
|
1269 | |
|
1270 | |
|
1271 | |
|
1272 | |
|
1273 | |
|
1274 | |
|
1275 | |
protected static Role boolean2Role(boolean isClient) { |
1276 | 79 | if (isClient) return Role.CLIENT; |
1277 | |
|
1278 | 0 | return Role.CLIENT_SERVER; |
1279 | |
} |
1280 | |
} |