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