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