193
|
1 package rep.channel;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.nio.channels.SelectableChannel;
|
|
5 import java.nio.channels.SelectionKey;
|
|
6 import java.nio.channels.Selector;
|
|
7
|
|
8 public class SelectionKeySimulator<P> extends REPSelectionKey<P>{
|
|
9
|
|
10 private int interestOpt;
|
|
11 private SelectableChannel channel;
|
|
12 private int ready;
|
|
13 public Selector selector;
|
|
14 public boolean canceled = false;
|
|
15
|
|
16 public SelectionKeySimulator(SelectableChannel cs, int opt, Selector _selector) {
|
|
17 channel = cs;
|
|
18 interestOpt = opt;
|
|
19 selector = _selector;
|
|
20 }
|
|
21 public SelectionKeySimulator(SelectionKey k) {
|
|
22 channel = k.channel();
|
|
23 interestOpt = k.interestOps();
|
|
24 selector = k.selector();
|
|
25 attach(k.attachment());
|
|
26 }
|
|
27
|
|
28
|
|
29 public boolean isAble() {
|
|
30 if ( (interestOpt&OP_READ)!=0 && isReadable() )
|
|
31 return true;
|
|
32 else if( (interestOpt&OP_ACCEPT)!=0 && isAcceptable() )
|
|
33 return true;
|
|
34 else if( (interestOpt&OP_WRITE)!=0 && isWritable() )
|
|
35 return true;
|
|
36 else
|
|
37 return false;
|
|
38 }
|
|
39
|
|
40 public void setFlag() {
|
|
41 ChannelSimulator<?> scs = (ChannelSimulator<?>) channel;
|
|
42 ready = 0;
|
|
43 if(scs.isAcceptable()) ready |= OP_ACCEPT;
|
|
44 if(scs.isReadable()) ready |= OP_READ;
|
|
45 if(scs.isWritable()) ready |= OP_WRITE;
|
|
46 }
|
|
47
|
|
48 public SelectableChannel channel() {
|
|
49 return channel;
|
|
50 }
|
|
51
|
|
52 @SuppressWarnings("unchecked")
|
|
53 @Override
|
|
54 public REPSocketChannel<P> channel1() {
|
|
55 return (REPSocketChannel<P>)channel;
|
|
56 }
|
|
57
|
|
58 @SuppressWarnings("unchecked")
|
|
59 @Override
|
|
60 public REPServerSocketChannel<P> serverSocketChannel() {
|
|
61 return (REPServerSocketChannel<P>)channel;
|
|
62 }
|
|
63
|
|
64 public SelectableChannel channel(REPPack<P> packer) {
|
|
65 return channel;
|
|
66 }
|
|
67
|
|
68 @SuppressWarnings("unchecked")
|
|
69 public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException {
|
|
70 assert(channel instanceof ServerChannelSimulator);
|
|
71 ServerChannelSimulator<P> scs = (ServerChannelSimulator<P>) channel;
|
|
72 return scs.accept1();
|
|
73 }
|
|
74
|
|
75 @SuppressWarnings("unchecked")
|
|
76 @Override
|
|
77 public void cancel() {
|
|
78 canceled = true;
|
|
79 SelectorSimulator s = (SelectorSimulator)selector;
|
|
80 s.deregister(channel);
|
|
81 }
|
|
82
|
|
83 @Override
|
|
84 public int interestOps() {
|
|
85 return interestOpt;
|
|
86 }
|
|
87
|
|
88 @Override
|
|
89 public SelectionKey interestOps(int ops) {
|
|
90 interestOpt = ops;
|
|
91 return this;
|
|
92 }
|
|
93
|
|
94 @Override
|
|
95 public boolean isValid() {
|
|
96 return (!canceled) && channel.isOpen() && selector.isOpen();
|
|
97 }
|
|
98
|
|
99
|
|
100 @Override
|
|
101 public Selector selector() {
|
|
102 return selector;
|
|
103 }
|
|
104
|
|
105 @Override
|
|
106 public int readyOps() {
|
|
107 int ops=0;
|
|
108 if ( channel instanceof ServerChannelSimulator ){
|
|
109 ServerChannelSimulator<?> scs = (ServerChannelSimulator<?>) channel;
|
|
110 ops = ( OP_ACCEPT * (scs.isAcceptable()? 1:0) );
|
|
111 } else if ( channel instanceof ChannelSimulator ){
|
|
112 ChannelSimulator<?> scs = (ChannelSimulator<?>) channel;
|
|
113 ops = ( OP_READ * (scs.isReadable()? 1:0) )
|
|
114 | ( OP_WRITE * (scs.isWritable()? 1:0) );
|
|
115 // (OP_READ & true) がつかえないらしい.
|
|
116 }
|
|
117 return ops;
|
|
118 }
|
|
119
|
|
120 }
|