Mercurial > hg > RemoteEditor > REPSessionManager
annotate rep/channel/SelectorSimulator.java @ 314:edb373aa421e
use channel lock instead of selector lock.
remove generic type from NetworkSimulator, which simplifies
singleton pattern.
remove lock from REPLogger.
author | kono |
---|---|
date | Mon, 06 Oct 2008 10:34:37 +0900 |
parents | c5be84d53c7f |
children | ef4afcae0c92 |
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; | |
176 | 8 import java.util.HashSet; |
298 | 9 import java.util.Iterator; |
123 | 10 import java.util.Set; |
11 | |
194 | 12 public class SelectorSimulator<P> extends REPSelector<P>{ |
308 | 13 |
14 // access to these set have to be synchronized | |
176 | 15 private Set<SelectionKey> keyList; |
16 private Set<SelectionKey> selectedKeys; | |
314 | 17 private boolean isOpen=true; |
308 | 18 |
123 | 19 public SelectorSimulator() { |
125 | 20 super(null); |
176 | 21 keyList = new HashSet<SelectionKey>(); |
123 | 22 } |
23 | |
308 | 24 public synchronized int select() throws IOException { |
25 while(true) { | |
26 getSelectedKeys(); | |
27 if(selectedKeys.isEmpty()) { | |
28 try { | |
29 this.wait(); | |
30 } catch (InterruptedException e) { | |
31 throw new IOException("Error, Selector was interrupted!"); | |
123 | 32 } |
308 | 33 } else |
34 break; | |
234
0498425202a4
made ChannelSimulator no extends SelectableChannelSimulator
kent
parents:
210
diff
changeset
|
35 } |
0498425202a4
made ChannelSimulator no extends SelectableChannelSimulator
kent
parents:
210
diff
changeset
|
36 return selectedKeys.size(); |
0498425202a4
made ChannelSimulator no extends SelectableChannelSimulator
kent
parents:
210
diff
changeset
|
37 } |
210 | 38 |
39 @Override | |
308 | 40 public synchronized int select(long timeout) throws IOException { |
41 getSelectedKeys(); | |
42 if(selectedKeys.isEmpty()) { | |
43 try { | |
314 | 44 wait(timeout); |
45 // we cannot know if we time outed or not | |
308 | 46 getSelectedKeys(); |
47 } catch (InterruptedException e) { | |
48 throw new IOException("Error, Selector was interrupted!"); | |
49 } | |
50 } | |
51 return selectedKeys.size(); | |
52 } | |
53 | |
54 private void getSelectedKeys() { | |
210 | 55 selectedKeys = new HashSet<SelectionKey>(); |
308 | 56 for(SelectionKey key : keyList){ |
57 if(((SelectionKeySimulator<?>) key).isAble()) | |
58 selectedKeys.add(key); | |
123 | 59 } |
308 | 60 } |
61 | |
62 @Override | |
63 public synchronized int selectNow() throws IOException { | |
64 getSelectedKeys(); | |
123 | 65 return selectedKeys.size(); |
66 } | |
190 | 67 |
197 | 68 public SelectionKeySimulator<P> register(SelectableChannel cs, int opt){ |
190 | 69 return register(cs, opt, null); |
70 } | |
197 | 71 public SelectionKeySimulator<P> register(SelectableChannel cs, int opt, Object handler){ |
72 SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt, this); | |
174 | 73 key.attach(handler); |
308 | 74 synchronized(this) { |
75 keyList.add(key); | |
76 } | |
174 | 77 return key; |
123 | 78 } |
308 | 79 |
80 public synchronized void deregister(SelectableChannel channel) { | |
298 | 81 for(Iterator<SelectionKey> it = keyList.iterator();it.hasNext();) { |
82 if(it.next().channel() == channel) | |
83 it.remove(); | |
84 } | |
85 } | |
308 | 86 |
194 | 87 |
308 | 88 public synchronized Set<REPSelectionKey<P>> selectedKeys1() { |
89 Set<SelectionKey> keys = keyList; | |
90 Set<REPSelectionKey<P>> newKeys = new HashSet<REPSelectionKey<P>>(); | |
91 for(SelectionKey k: keys) { | |
92 // REPSelectionKeyを生成しないように注意 | |
93 newKeys.add(new SelectionKeySimulator<P>(k)); | |
94 } | |
314 | 95 return newKeys; |
194 | 96 } |
308 | 97 |
98 public synchronized <T> SelectionKey getKey(ChannelSimulator<T> channel){ | |
123 | 99 for(SelectionKey key : keyList){ |
100 if(key.channel() == channel) | |
101 return key; | |
102 } | |
103 return null; | |
104 } | |
105 | |
106 @Override | |
107 public void close() throws IOException { | |
314 | 108 isOpen = false; |
123 | 109 } |
110 | |
111 @Override | |
112 public boolean isOpen() { | |
314 | 113 return isOpen; |
123 | 114 } |
115 | |
116 @Override | |
117 public Set<SelectionKey> keys() { | |
298 | 118 return keyList; |
123 | 119 } |
120 | |
121 @Override | |
122 public SelectorProvider provider() { | |
123 // TODO Auto-generated method stub | |
124 return null; | |
125 } | |
126 | |
127 | |
128 @Override | |
308 | 129 public synchronized Selector wakeup() { |
130 notifyAll(); | |
210 | 131 return this; |
123 | 132 } |
133 | |
134 @Override | |
308 | 135 public synchronized Set<SelectionKey> selectedKeys() { |
123 | 136 return (Set<SelectionKey>)selectedKeys; |
137 } | |
138 | |
139 } |