123
|
1 package rep.channel;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.nio.channels.ClosedChannelException;
|
|
5 import java.nio.channels.SelectableChannel;
|
|
6 import java.nio.channels.SelectionKey;
|
|
7 import java.nio.channels.Selector;
|
|
8 import java.nio.channels.spi.SelectorProvider;
|
|
9
|
|
10
|
|
11 public class ChannelSimulator<P> extends SelectableChannelSimulator<P>{
|
|
12 //private BlockingQueue<P> qread;
|
|
13 //private BlockingQueue<P> qwrite;
|
|
14 //private SelectorSimulator<P> waitingSelector;
|
|
15 protected NetworkSimulator<P> ns;
|
|
16
|
|
17 /** Constructors. */
|
|
18 public ChannelSimulator(NetworkSimulator<P> _ns){
|
|
19 this(_ns, null);
|
|
20 }
|
143
|
21
|
|
22 public ChannelSimulator(NetworkSimulator<P> _ns, SelectorSimulator _selector){
|
123
|
23 super(null);
|
|
24 ns = _ns;
|
|
25 //ns = NetworkSimulator.singleton(); //どっちがいい?
|
|
26 }
|
|
27 public ChannelSimulator<P> createConjugatedChannel() {
|
|
28 ChannelSimulator<P> ret = new ChannelSimulator<P>(ns);
|
|
29 ret.qread=qwrite;
|
|
30 ret.qwrite=qread;
|
|
31 ret.readSelector=writeSelector;
|
|
32 ret.writeSelector=readSelector;
|
|
33 return ret;
|
|
34 }
|
|
35
|
|
36 /** Connecting methods */
|
|
37 // for clients.
|
|
38 public boolean connect(int ip){
|
|
39 return ns.connect(ip, this);
|
|
40 }
|
|
41
|
|
42 public ChannelSimulator<P> accept(){
|
|
43 return null;
|
|
44 }
|
|
45
|
|
46 /* return state of the Queue(debug) */
|
|
47 /*
|
|
48 public boolean readQisEmpty() {
|
|
49 return qread.isEmpty();
|
|
50 }
|
|
51 public boolean writeQisEmpty() {
|
|
52 return qwrite.isEmpty();
|
|
53 }
|
|
54 */
|
|
55
|
|
56 @Override
|
|
57 public boolean isAcceptable() {
|
|
58 return false;
|
|
59 }
|
|
60 @Override
|
|
61 public boolean isReadable() {
|
|
62 synchronized (qread){
|
|
63 return !qread.isEmpty();
|
|
64 }
|
|
65 }
|
|
66 @Override
|
|
67 public boolean isWritable() {
|
|
68 return true;
|
|
69 }
|
|
70
|
|
71 public SelectionKey keyFor(Selector selector2) {
|
143
|
72 return ((SelectorSimulator) selector2).getKey(this);
|
123
|
73 }
|
|
74 @Override
|
|
75 public Object blockingLock() {
|
|
76 // TODO Auto-generated method stub
|
|
77 return null;
|
|
78 }
|
|
79 @Override
|
|
80 public SelectableChannel configureBlocking(boolean block) throws IOException {
|
|
81 // TODO Auto-generated method stub
|
|
82 return null;
|
|
83 }
|
|
84 @Override
|
|
85 public boolean isBlocking() {
|
|
86 // TODO Auto-generated method stub
|
|
87 return false;
|
|
88 }
|
|
89 @Override
|
|
90 public boolean isRegistered() {
|
|
91 // TODO Auto-generated method stub
|
|
92 return false;
|
|
93 }
|
|
94
|
|
95 @Override
|
|
96 public SelectorProvider provider() {
|
|
97 // TODO Auto-generated method stub
|
|
98 return null;
|
|
99 }
|
143
|
100 @SuppressWarnings("unchecked")
|
123
|
101 @Override
|
|
102 public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException {
|
143
|
103 SelectorSimulator selector = (SelectorSimulator) sel;
|
|
104 return selector.register(this, ops, att);
|
123
|
105 }
|
143
|
106 public SelectionKey register(REPSelector sel, int ops, Object att) throws ClosedChannelException {
|
|
107 REPSelector selector = sel;
|
|
108 return selector.register(this, ops, att);
|
|
109 }
|
|
110
|
123
|
111 @Override
|
|
112 public int validOps() {
|
|
113 // TODO Auto-generated method stub
|
|
114 return 0;
|
|
115 }
|
|
116 @Override
|
|
117 protected void implCloseChannel() throws IOException {
|
|
118 // TODO Auto-generated method stub
|
|
119
|
|
120 }
|
|
121
|
|
122 }
|