annotate src/pathfinder/mergetest/SelectorSimulator.java @ 157:1a2269c820df simulator2008-8-26

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