comparison rep/channel/SelectorSimulator.java @ 123:5b1a0574b406 add-simulator

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