161
|
1 package pathfinder.mergetest.channels;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.util.ArrayList;
|
|
5 //import java.util.Set; //ζΈγη΄γ?
|
|
6
|
|
7
|
|
8
|
|
9 public class SelectorSimulator<P> {
|
|
10
|
|
11 private ArrayList<SelectionKeySimulator<P>> keyList;
|
|
12 private ArrayList<SelectionKeySimulator<P>> selectedKeys;
|
|
13
|
|
14 public SelectorSimulator() {
|
|
15 // TODO Auto-generated constructor stub
|
|
16 keyList = new ArrayList<SelectionKeySimulator<P>>();
|
|
17 }
|
|
18
|
|
19 public int select() throws IOException {
|
|
20 selectedKeys = new ArrayList<SelectionKeySimulator<P>>();
|
|
21
|
|
22 synchronized(this) {
|
|
23
|
|
24 while(selectedKeys.isEmpty()){
|
|
25 for(SelectionKeySimulator<P> key : keyList){
|
|
26 if(key.isAble())
|
|
27 selectedKeys.add(key);
|
|
28 }
|
|
29
|
|
30 if(selectedKeys.isEmpty())
|
|
31 try {
|
|
32 this.wait();
|
|
33 } catch (InterruptedException e) {
|
|
34 throw new IOException("Error, Selector was interrupted!");
|
|
35 }
|
|
36 }
|
|
37 }
|
|
38 return selectedKeys.size();
|
|
39 }
|
|
40
|
|
41 public SelectionKeySimulator<P> register(SelectableChannelSimulator<P> cs, int opt){
|
|
42 SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt);
|
|
43 keyList.add(key);
|
|
44 return key;
|
|
45 }
|
|
46
|
|
47 public SelectionKeySimulator<P> register(ChannelSimulator<P> cs, int opt, Object handler){
|
|
48 SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt);
|
|
49 key.attach(handler);
|
|
50 keyList.add(key);
|
|
51 return key;
|
|
52 }
|
|
53
|
|
54 public ArrayList<SelectionKeySimulator<P>> selectedKeys() {
|
|
55
|
|
56 return selectedKeys;
|
|
57 }
|
|
58
|
|
59 public SelectionKeySimulator<P> getKey(ChannelSimulator<P> channel){
|
|
60 for(SelectionKeySimulator<P> key : keyList){
|
|
61 if(key.channel() == channel)
|
|
62 return key;
|
|
63 }
|
|
64 return null;
|
|
65 }
|
|
66
|
|
67 }
|