123
|
1 package rep.channel;
|
|
2
|
|
3 import java.io.IOException;
|
174
|
4 import java.nio.channels.SelectableChannel;
|
123
|
5 import java.nio.channels.SelectionKey;
|
|
6 import java.nio.channels.Selector;
|
|
7 import java.nio.channels.spi.SelectorProvider;
|
176
|
8 import java.util.HashSet;
|
123
|
9 import java.util.Set;
|
|
10
|
194
|
11 public class SelectorSimulator<P> extends REPSelector<P>{
|
123
|
12
|
176
|
13 private Set<SelectionKey> keyList;
|
|
14 private Set<SelectionKey> selectedKeys;
|
123
|
15
|
|
16 public SelectorSimulator() {
|
125
|
17 super(null);
|
176
|
18 keyList = new HashSet<SelectionKey>();
|
123
|
19 }
|
|
20
|
|
21 public int select() throws IOException {
|
176
|
22 selectedKeys = new HashSet<SelectionKey>();
|
123
|
23
|
|
24 synchronized(this) {
|
|
25
|
|
26 while(selectedKeys.isEmpty()){
|
|
27 for(SelectionKey key : keyList){
|
203
|
28 if(((SelectionKeySimulator<?>) key).isAble())
|
123
|
29 selectedKeys.add(key);
|
|
30 }
|
|
31
|
|
32 if(selectedKeys.isEmpty())
|
|
33 try {
|
|
34 this.wait();
|
|
35 } catch (InterruptedException e) {
|
|
36 throw new IOException("Error, Selector was interrupted!");
|
|
37 }
|
|
38 }
|
|
39 }
|
|
40 return selectedKeys.size();
|
|
41 }
|
190
|
42
|
197
|
43 public SelectionKeySimulator<P> register(SelectableChannel cs, int opt){
|
190
|
44 return register(cs, opt, null);
|
|
45 }
|
197
|
46 public SelectionKeySimulator<P> register(SelectableChannel cs, int opt, Object handler){
|
|
47 SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt, this);
|
174
|
48 key.attach(handler);
|
|
49 keyList.add(key);
|
|
50 return key;
|
123
|
51 }
|
194
|
52
|
|
53
|
|
54 public Set<REPSelectionKey<P>> selectedKeys1() {
|
|
55 Set<SelectionKey> keys = keyList;
|
197
|
56 Set<REPSelectionKey<P>> newKeys = new HashSet<REPSelectionKey<P>>();
|
194
|
57 for(SelectionKey k: keys) {
|
197
|
58 // REPSelectionKeyを生成しないように注意
|
|
59 newKeys.add(new SelectionKeySimulator<P>(k));
|
194
|
60 }
|
197
|
61 return newKeys;//(Set<REPSelectionKey<P>>)newKeys;
|
194
|
62 }
|
|
63
|
143
|
64 public <T> SelectionKey getKey(ChannelSimulator<T> channel){
|
123
|
65 for(SelectionKey key : keyList){
|
|
66 if(key.channel() == channel)
|
|
67 return key;
|
|
68 }
|
|
69 return null;
|
|
70 }
|
|
71
|
|
72 @Override
|
|
73 public void close() throws IOException {
|
|
74 // TODO Auto-generated method stub
|
|
75
|
|
76 }
|
|
77
|
|
78 @Override
|
|
79 public boolean isOpen() {
|
|
80 // TODO Auto-generated method stub
|
|
81 return false;
|
|
82 }
|
|
83
|
|
84 @Override
|
|
85 public Set<SelectionKey> keys() {
|
|
86 // TODO Auto-generated method stub
|
|
87 return null;
|
|
88 }
|
|
89
|
|
90 @Override
|
|
91 public SelectorProvider provider() {
|
|
92 // TODO Auto-generated method stub
|
|
93 return null;
|
|
94 }
|
|
95
|
|
96 @Override
|
|
97 public int select(long timeout) throws IOException {
|
|
98 // TODO Auto-generated method stub
|
|
99 return 0;
|
|
100 }
|
|
101
|
|
102 @Override
|
|
103 public int selectNow() throws IOException {
|
|
104 // TODO Auto-generated method stub
|
|
105 return 0;
|
|
106 }
|
|
107
|
|
108 @Override
|
|
109 public Selector wakeup() {
|
|
110 // TODO Auto-generated method stub
|
|
111 return null;
|
|
112 }
|
|
113
|
|
114 @Override
|
|
115 public Set<SelectionKey> selectedKeys() {
|
|
116 // TODO Auto-generated method stub
|
|
117 return (Set<SelectionKey>)selectedKeys;
|
|
118 }
|
|
119
|
|
120 }
|