annotate rep/channel/SelectorSimulator.java @ 125:9faacdd6c9cb

*** empty log message ***
author pin
date Wed, 27 Aug 2008 17:54:47 +0900
parents 5b1a0574b406
children 785a3e8ea858
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
1 package rep.channel;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
2
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
3 import java.io.IOException;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
4 import java.nio.channels.SelectionKey;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
5 import java.nio.channels.Selector;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
6 import java.nio.channels.spi.SelectorProvider;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
7 import java.util.ArrayList;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
8 import java.util.Set;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
9 import java.util.TreeSet;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
10 //import java.util.Set; //書き直す?
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
11 import java.util.Set;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
12
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
13
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
14
125
9faacdd6c9cb *** empty log message ***
pin
parents: 123
diff changeset
15 public class SelectorSimulator<P> extends REPSelector{
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
16
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
17 private TreeSet<SelectionKey> keyList;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
18 private TreeSet<SelectionKey> selectedKeys;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
19
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
20 public SelectorSimulator() {
125
9faacdd6c9cb *** empty log message ***
pin
parents: 123
diff changeset
21 super(null);
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
22 keyList = new TreeSet<SelectionKey>();
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
23 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
24
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
25 @SuppressWarnings("unchecked")
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
26 public int select() throws IOException {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
27 selectedKeys = new TreeSet<SelectionKey>();
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
28
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
29 synchronized(this) {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
30
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
31 while(selectedKeys.isEmpty()){
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
32 for(SelectionKey key : keyList){
125
9faacdd6c9cb *** empty log message ***
pin
parents: 123
diff changeset
33 if(((SelectionKeySimulator) key).isAble())
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
34 selectedKeys.add(key);
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
35 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
36
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
37 if(selectedKeys.isEmpty())
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
38 try {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
39 this.wait();
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
40 } catch (InterruptedException e) {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
41 throw new IOException("Error, Selector was interrupted!");
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
42 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
43 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
44 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
45 return selectedKeys.size();
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
46 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
47
125
9faacdd6c9cb *** empty log message ***
pin
parents: 123
diff changeset
48 public SelectionKeySimulator register(SelectableChannelSimulator<P> cs, int opt){
9faacdd6c9cb *** empty log message ***
pin
parents: 123
diff changeset
49 SelectionKeySimulator key = new SelectionKeySimulator(cs, opt, this);
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
50 keyList.add(key);
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
51 return key;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
52 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
53
125
9faacdd6c9cb *** empty log message ***
pin
parents: 123
diff changeset
54 public SelectionKeySimulator register(ChannelSimulator<P> cs, int opt, Object handler){
9faacdd6c9cb *** empty log message ***
pin
parents: 123
diff changeset
55 SelectionKeySimulator key = new SelectionKeySimulator(cs, opt, this);
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
56 key.attach(handler);
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
57 keyList.add(key);
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
58 return key;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
59 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
60
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
61 public SelectionKey getKey(ChannelSimulator<P> channel){
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
62 for(SelectionKey key : keyList){
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
63 if(key.channel() == channel)
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
64 return key;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
65 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
66 return null;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
67 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
68
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
69 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
70 public void close() throws IOException {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
71 // TODO Auto-generated method stub
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
72
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
73 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
74
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
75 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
76 public boolean isOpen() {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
77 // TODO Auto-generated method stub
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
78 return false;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
79 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
80
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
81 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
82 public Set<SelectionKey> keys() {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
83 // TODO Auto-generated method stub
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
84 return null;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
85 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
86
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
87 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
88 public SelectorProvider provider() {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
89 // TODO Auto-generated method stub
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
90 return null;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
91 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
92
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
93 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
94 public int select(long timeout) throws IOException {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
95 // TODO Auto-generated method stub
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
96 return 0;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
97 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
98
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
99 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
100 public int selectNow() throws IOException {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
101 // TODO Auto-generated method stub
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
102 return 0;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
103 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
104
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
105 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
106 public Selector wakeup() {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
107 // TODO Auto-generated method stub
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
108 return null;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
109 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
110
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
111 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
112 public Set<SelectionKey> selectedKeys() {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
113 // TODO Auto-generated method stub
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
114 return (Set<SelectionKey>)selectedKeys;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
115 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
116
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
117 }