-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reactor.java
93 lines (84 loc) · 3.17 KB
/
Reactor.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
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Selector;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class Reactor extends Thread {
final Selector selector;
final ServerSocketChannel serverChannel;
static final int WORKER_POOL_SIZE = 10;
static ExecutorService workerPool;
Map<String,SocketChannel> myMap = new ConcurrentHashMap<String,SocketChannel>();
Reactor(int port) throws IOException {
selector = Selector.open();
serverChannel = ServerSocketChannel.open();
serverChannel.socket().bind(new InetSocketAddress(port));
serverChannel.configureBlocking(false);
SelectionKey sk = serverChannel.register(selector, SelectionKey.OP_ACCEPT);
sk.attach(new Acceptor());
}
public void run() {
try {
while (true) {
selector.select();
Iterator it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey sk = (SelectionKey) it.next();
it.remove();
Runnable r = (Runnable) sk.attachment();
if (r != null)
r.run();
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
class Acceptor implements Runnable {
Map<String,SocketChannel> myMap = new ConcurrentHashMap<String,SocketChannel>();
public void run() {
try {
int nbytes = 0;
ByteBuffer nameBuffer = ByteBuffer.allocate(1024);
SocketChannel channel = serverChannel.accept();
nbytes = channel.read(nameBuffer);
String name = bytesBufferToString(nameBuffer);
if (channel != null && nbytes != -1)
if (myMap.containsKey(name)){
myMap.remove(name);
}
myMap.put(name, channel);
new Handler(selector, channel, name, myMap);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public String bytesBufferToString(ByteBuffer buffer){
byte[] bytes;
buffer.flip();
bytes = new byte[buffer.remaining()];
buffer.get(bytes, 0, bytes.length);
return new String(bytes, Charset.forName("ISO-8859-1"));
}
}
public static void main(String[] args) {
workerPool = Executors.newFixedThreadPool(WORKER_POOL_SIZE);
try {
new Thread(new Reactor(Integer.parseInt(args[0]))).start();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}