forked from haobinaa/DataStructure-DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleReactor.java
101 lines (92 loc) · 3.3 KB
/
SingleReactor.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
package com.haobin.codeBlock.IOModel;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
/**
* @author: HaoBin
* @create: 2019/10/23 8:49
* @description: 单线程 reactor 模式
*
* reactor 和 handler 在一个线程里面
*
*
*#####################################
* reactor 模型各个组件
* reactor: 负责响应事件,将事件分发给绑定了该事件的handler
* handler: 绑定了某类事件的处理器, 负责执行对事件的处理
* Acceptor: 特殊的Handler, 绑定了 connect 事件, 客户端的 connect 事件会分发给 Acceptor
**/
public class SingleReactor implements Runnable{
private Selector selector;
private ServerSocketChannel serverSocket;
public SingleReactor(int port) throws Exception {
selector = Selector.open();
serverSocket= ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(port));
// 设置成非阻塞
serverSocket.configureBlocking(false);
// 只关注 accept 事件
SelectionKey selectionKey = serverSocket.register(selector, SelectionKey.OP_ACCEPT);
selectionKey.attach(new Acceptr());
System.out.println("Listen port:" + port);
}
@Override
public void run() {
try {
while (!Thread.interrupted()) {
selector.select(); // 阻塞至通道就绪
Set<SelectionKey> selectionKeys = selector.selectedKeys(); // 就绪通道 SelectionKey 集合
Iterator<SelectionKey> it = selectionKeys.iterator();
// 遍历就绪事件,分发给对应处理器
while (it.hasNext()) {
SelectionKey selectedKey = it.next();
dispatch(selectedKey);
}
// 清空就绪通道
selectionKeys.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
void dispatch(SelectionKey k) {
// 获取 key 对应的处理器
Runnable r = (Runnable) (k.attachment());
if (r != null) {
r.run();
}
}
class Acceptr implements Runnable {
@Override
public void run() {
try {
// 接收连接,非阻塞模式没有连接直接返回null
SocketChannel socket = serverSocket.accept();
if (socket != null) {
socket.write(ByteBuffer.wrap("single reactor".getBytes()));
System.out.println("Accept and handler - " + socket.socket().getLocalSocketAddress());
// handler 处理
new BasicHandler(selector, socket);
}
} catch (IOException ioex) {
ioex.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
Thread th = new Thread(new SingleReactor(10393));
th.setName("SingleReactor");
th.start();
th.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}