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();
|
275
|
33 SelectableChannel sc = key.channel();
|
|
34 SelectableChannel rsc = REPSocketChannel.channels.get(sc);
|
|
35 return rsc;
|
194
|
36 }
|
184
|
37
|
308
|
38 @SuppressWarnings("unchecked")
|
|
39 public REPSocketChannel<P> channel1() {
|
|
40 assert (!REPServerSocketChannel.isSimulation);
|
|
41 SelectableChannel sc = key.channel();
|
|
42 REPSocketChannel<P> rsc = (REPSocketChannel<P>) REPSocketChannel.channels.get(sc);
|
|
43 return rsc;
|
|
44 }
|
|
45
|
|
46 @SuppressWarnings("unchecked")
|
|
47 public REPServerSocketChannel<P> serverSocketChannel() {
|
|
48 assert (!REPServerSocketChannel.isSimulation);
|
|
49 SelectableChannel sc = key.channel();
|
|
50 REPServerSocketChannel<P> rsc = (REPServerSocketChannel<P>) REPSocketChannel.channels.get(sc);
|
|
51 return rsc;
|
|
52 }
|
|
53
|
184
|
54 @Override
|
|
55 public int interestOps() {
|
194
|
56 return key.interestOps();
|
184
|
57 }
|
|
58
|
|
59 @Override
|
|
60 public SelectionKey interestOps(int ops) {
|
194
|
61 return key.interestOps(ops);
|
184
|
62 }
|
|
63
|
|
64 @Override
|
|
65 public boolean isValid() {
|
194
|
66 return key.isValid();
|
184
|
67 }
|
|
68
|
|
69 @Override
|
|
70 public int readyOps() {
|
194
|
71 return key.readyOps();
|
184
|
72 }
|
|
73
|
|
74 @Override
|
|
75 public Selector selector() {
|
219
|
76 return selector;
|
184
|
77 }
|
|
78
|
194
|
79 public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException {
|
|
80 assert(!REPServerSocketChannel.isSimulation);
|
203
|
81 if (!key.isAcceptable()) throw new IOException();
|
|
82 ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
|
|
83 if (ssc==null) return null;
|
|
84 SocketChannel ss = (SocketChannel)ssc.accept();
|
240
|
85 //System.err.println("Accept in SelectionKey "+ss);
|
203
|
86 if (ss==null) return null;
|
437
|
87 return new REPSocketChannel<P>(ss, pack);
|
194
|
88 }
|
|
89
|
|
90
|
184
|
91 }
|