comparison src/rep/channel/SelectorSimulator.java @ 193:3133040ee4f4

(no commit message)
author one
date Wed, 31 Dec 2008 15:06:22 +0900
parents
children
comparison
equal deleted inserted replaced
192:c921022bf494 193:3133040ee4f4
1 package rep.channel;
2
3 import java.io.IOException;
4 import java.nio.channels.SelectableChannel;
5 import java.nio.channels.SelectionKey;
6 import java.nio.channels.Selector;
7 import java.nio.channels.spi.SelectorProvider;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Map;
11 import java.util.Set;
12
13 public class SelectorSimulator<P> extends REPSelector<P>{
14
15 // This selector cannot be shared among threads.
16
17 private Map<SelectableChannel, SelectionKeySimulator<P>> keyList;
18 private Set<SelectionKeySimulator<P>> selectedKeys;
19 private boolean isOpen=true;
20
21 public SelectorSimulator() {
22 super(null);
23 keyList = new HashMap<SelectableChannel,SelectionKeySimulator<P>>();
24 }
25
26 public int select() throws IOException {
27 while(true) {
28 getSelectedKeys();
29 if(selectedKeys.isEmpty()) {
30 try {
31 synchronized(this) {
32 wait();
33 }
34 } catch (InterruptedException e) {
35 throw new IOException();
36 }
37 } else
38 break;
39 }
40 return selectedKeys.size();
41 }
42
43 @Override
44 public int select(long timeout) throws IOException {
45 getSelectedKeys();
46 if(selectedKeys.isEmpty()) {
47 try {
48 synchronized(this) {
49 wait(timeout);
50 }
51 // we cannot know if we time outed or not
52 getSelectedKeys();
53 } catch (InterruptedException e) {
54 throw new IOException();
55 }
56 }
57 return selectedKeys.size();
58 }
59
60 private void getSelectedKeys() {
61 selectedKeys = new HashSet<SelectionKeySimulator<P>>();
62 for(SelectionKeySimulator<P> key : keyList.values()){
63 if(key.isAble())
64 selectedKeys.add(new SelectionKeySimulator<P>(key));
65 }
66 }
67
68 @Override
69 public int selectNow() throws IOException {
70 getSelectedKeys();
71 return selectedKeys.size();
72 }
73
74 public SelectionKeySimulator<P> register(SelectableChannel cs, int opt){
75 return register(cs, opt, null);
76 }
77 public SelectionKeySimulator<P> register(SelectableChannel cs, int opt, Object handler){
78 SelectionKeySimulator<P> key = keyList.get(cs);
79 if (key!=null) {
80 key.attach(handler);
81 key.interestOps(opt);
82 return key;
83 }
84 key = new SelectionKeySimulator<P>(cs, opt, this);
85 key.attach(handler);
86 keyList.put(cs,key);
87 return key;
88 }
89
90 public SelectionKeySimulator<P> deregister(SelectableChannel channel) {
91 SelectionKeySimulator<P> key = keyList.remove(channel);
92 return key;
93 }
94
95
96 public SelectionKey getKey(SelectableChannel channel){
97 return keyList.get(channel);
98 }
99
100 @Override
101 public void close() throws IOException {
102 isOpen = false;
103 }
104
105 @Override
106 public boolean isOpen() {
107 return isOpen;
108 }
109
110 @Override
111 public Set<SelectionKey> keys() {
112 Set<SelectionKey> newKeys = new HashSet<SelectionKey>();
113 for(SelectionKey k: keyList.values()) {
114 newKeys.add(k);
115 }
116 return newKeys;
117 }
118
119 public Set<REPSelectionKey<P>> keys1() {
120 // we cannot solve cast, we need the same method again with different types
121 Set<REPSelectionKey<P>> newKeys = new HashSet<REPSelectionKey<P>>();
122 for(SelectionKeySimulator<P> k: keyList.values()) {
123 newKeys.add(k);
124 }
125 return newKeys;
126 }
127
128 @Override
129 public SelectorProvider provider() {
130 // should return NetworkSimulator?
131 return null;
132 }
133
134
135 @Override
136 public synchronized Selector wakeup() {
137 notifyAll();
138 return this;
139 }
140
141
142 public Set<REPSelectionKey<P>> selectedKeys1() {
143 Set<REPSelectionKey<P>> newKeys = new HashSet<REPSelectionKey<P>>();
144 for(SelectionKeySimulator<P> k: selectedKeys) {
145 newKeys.add(k);
146 }
147 return newKeys;
148 }
149
150 /*
151 * type safe copy of selectedKeys1()
152 */
153 @Override
154 public Set<SelectionKey> selectedKeys() {
155 Set<SelectionKey> newKeys = new HashSet<SelectionKey>();
156 for(SelectionKeySimulator<P> k: selectedKeys) {
157 newKeys.add(k);
158 }
159 return newKeys;
160 }
161
162 }