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