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