184
|
1 package rep.channel;
|
|
2
|
194
|
3 import java.io.IOException;
|
184
|
4 import java.nio.channels.SelectableChannel;
|
|
5 import java.nio.channels.SelectionKey;
|
|
6 import java.nio.channels.Selector;
|
194
|
7 import java.nio.channels.ServerSocketChannel;
|
203
|
8 import java.nio.channels.SocketChannel;
|
184
|
9
|
|
10
|
194
|
11 public class REPSelectionKey<P> extends SelectionKey {
|
|
12 SelectionKey key;
|
219
|
13 private REPSelector<P> selector;
|
194
|
14
|
221
|
15 public REPSelectionKey() {
|
|
16
|
|
17 }
|
|
18
|
219
|
19 public REPSelectionKey(SelectionKey key,REPSelector<P>s) {
|
194
|
20 this.key = key;
|
219
|
21 this.selector = s;
|
214
|
22 attach(key.attachment());
|
194
|
23 }
|
|
24
|
184
|
25 @Override
|
|
26 public void cancel() {
|
194
|
27 key.cancel();
|
184
|
28 }
|
|
29
|
|
30 @Override
|
|
31 public SelectableChannel channel() {
|
217
|
32 if (REPServerSocketChannel.isSimulation) return key.channel();
|
|
33 if (key.isAcceptable()) {
|
|
34 SelectableChannel sc = key.channel();
|
|
35 SelectableChannel rsc = REPSocketChannel.channels.get(sc);
|
|
36 return rsc;
|
|
37 } else if (key.isReadable()) {
|
|
38 SelectableChannel sc = key.channel();
|
|
39 SelectableChannel rsc = REPSocketChannel.channels.get(sc);
|
|
40 return rsc;
|
|
41 }
|
|
42 return null;
|
|
43
|
194
|
44 }
|
|
45
|
|
46 public SelectableChannel channel(REPPack<P>packer) {
|
|
47 if (REPServerSocketChannel.isSimulation) return key.channel();
|
|
48 if (key.isAcceptable()) {
|
208
|
49 SelectableChannel sc = key.channel();
|
|
50 SelectableChannel rsc = REPSocketChannel.channels.get(sc);
|
|
51 if (rsc!=null) return rsc;
|
194
|
52 return new REPServerSocketChannel<P>(key.channel(),packer);
|
203
|
53 } else if (key.isReadable()) {
|
208
|
54 SelectableChannel sc = key.channel();
|
|
55 SelectableChannel rsc = REPSocketChannel.channels.get(sc);
|
|
56 if (rsc!=null) return rsc;
|
194
|
57 return new REPSocketChannel<P>(key.channel(),packer);
|
|
58 }
|
203
|
59 return null;
|
184
|
60 }
|
|
61
|
|
62 @Override
|
|
63 public int interestOps() {
|
194
|
64 return key.interestOps();
|
184
|
65 }
|
|
66
|
|
67 @Override
|
|
68 public SelectionKey interestOps(int ops) {
|
194
|
69 return key.interestOps(ops);
|
184
|
70 }
|
|
71
|
|
72 @Override
|
|
73 public boolean isValid() {
|
194
|
74 return key.isValid();
|
184
|
75 }
|
|
76
|
|
77 @Override
|
|
78 public int readyOps() {
|
194
|
79 return key.readyOps();
|
184
|
80 }
|
|
81
|
|
82 @Override
|
|
83 public Selector selector() {
|
219
|
84 return selector;
|
184
|
85 }
|
|
86
|
208
|
87 @SuppressWarnings("unchecked")
|
194
|
88 public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException {
|
|
89 assert(!REPServerSocketChannel.isSimulation);
|
203
|
90 if (!key.isAcceptable()) throw new IOException();
|
|
91 ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
|
|
92 if (ssc==null) return null;
|
|
93 SocketChannel ss = (SocketChannel)ssc.accept();
|
|
94 if (ss==null) return null;
|
208
|
95 REPSocketChannel<P> rsc = (REPSocketChannel<P>)REPSocketChannel.channels.get(ss);
|
|
96 if (rsc!=null) return rsc;
|
203
|
97 return new REPSocketChannel<P>(ss,pack);
|
194
|
98 }
|
|
99
|
|
100
|
184
|
101 }
|