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