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