151
|
1 package pathfinder.mergetest;
|
|
2
|
|
3 import java.util.ArrayList;
|
|
4 import java.util.Set;
|
|
5
|
|
6 public class SelectorSimulator<P> {
|
|
7
|
|
8 private ArrayList<SelectionKeySimulator<P>> keyList;
|
|
9 private NetworkSimulator<P> ns;
|
|
10 private ArrayList<SelectionKeySimulator<P>> selectedKeys;
|
|
11
|
|
12 public SelectorSimulator(NetworkSimulator<P> _ns) {
|
|
13 // TODO Auto-generated constructor stub
|
|
14 ns = _ns;
|
|
15 keyList = new ArrayList<SelectionKeySimulator<P>>();
|
|
16 }
|
|
17
|
|
18 public int select(){
|
|
19 selectedKeys = new ArrayList<SelectionKeySimulator<P>>();
|
|
20
|
|
21 synchronized(ns){
|
155
|
22 boolean empty = true;
|
154
|
23 //while(empty){
|
153
|
24 for(SelectionKeySimulator<P> key : keyList){
|
|
25 ChannelSimulator<P> channel = key.channel();
|
155
|
26 // if(channel.readQisEmpty()){
|
|
27 // empty = true;
|
|
28 // }else{
|
|
29 // empty = false;
|
|
30 // selectedKeys.add(key);
|
|
31 // break;
|
|
32 // }
|
|
33 if(!channel.readQisEmpty()){
|
153
|
34 empty = false;
|
|
35 selectedKeys.add(key);
|
|
36 }
|
151
|
37 }
|
153
|
38
|
|
39 try {
|
|
40 if(empty) ns.wait();
|
|
41 } catch (InterruptedException e) {
|
|
42 //e.printStackTrace();
|
|
43 }
|
154
|
44 //}
|
151
|
45 }
|
|
46 return selectedKeys.size();
|
|
47 }
|
|
48
|
|
49 public SelectionKeySimulator<P> register(ChannelSimulator<P> cs){
|
|
50 SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs);
|
|
51 keyList.add(new SelectionKeySimulator<P>(cs));
|
|
52 return key;
|
|
53 }
|
157
|
54
|
|
55 public SelectionKeySimulator<P> register(ChannelSimulator<P> cs, int opt, Object handler){
|
|
56 SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs);
|
|
57 key.attach(handler);
|
|
58 keyList.add(key);
|
|
59 return key;
|
|
60 }
|
151
|
61
|
|
62 public ArrayList<SelectionKeySimulator<P>> selectedKeys() {
|
|
63
|
|
64 return selectedKeys;
|
|
65 }
|
157
|
66
|
|
67 public SelectionKeySimulator getKey(ChannelSimulator channel){
|
|
68 for(SelectionKeySimulator key : keyList){
|
|
69 if(key.channel() == channel)
|
|
70 return key;
|
|
71 }
|
|
72 return null;
|
|
73 }
|
151
|
74
|
|
75 }
|