37 |
|
|
38 |
package gnu.java.nio; |
package gnu.java.nio; |
39 |
|
|
40 |
import java.util.*; |
import java.nio.channels.SelectableChannel; |
41 |
import java.nio.channels.*; |
import java.nio.channels.SelectionKey; |
42 |
import java.nio.channels.spi.*; |
import java.nio.channels.Selector; |
43 |
|
import java.nio.channels.spi.AbstractSelectableChannel; |
44 |
|
import java.nio.channels.spi.AbstractSelector; |
45 |
|
import java.nio.channels.spi.SelectorProvider; |
46 |
|
import java.util.HashSet; |
47 |
|
import java.util.Iterator; |
48 |
|
import java.util.Set; |
49 |
|
|
50 |
public class SelectorImpl extends AbstractSelector |
public class SelectorImpl extends AbstractSelector |
51 |
{ |
{ |
52 |
Set keys, selected, canceled; |
Set keys, selected, canceled; |
53 |
|
|
54 |
public SelectorImpl(SelectorProvider provider) |
public SelectorImpl (SelectorProvider provider) |
55 |
{ |
{ |
56 |
super(provider); |
super (provider); |
57 |
} |
} |
58 |
|
|
59 |
public Set keys() |
public Set keys () |
60 |
{ |
{ |
61 |
return keys; |
return keys; |
62 |
} |
} |
63 |
|
|
64 |
public int selectNow() { return select(1); } |
public int selectNow () |
65 |
public int select() { return select(Long.MAX_VALUE); } |
{ |
66 |
|
return select (1); |
67 |
|
} |
68 |
|
public int select () |
69 |
|
{ |
70 |
|
return select (Long.MAX_VALUE); |
71 |
|
} |
72 |
|
|
73 |
private static native int java_do_select(int []read, |
// private static native int java_do_select(int[] read, int[] write, |
74 |
int []write, |
// int[] except, long timeout); |
|
int []except, |
|
|
long timeout); |
|
|
|
|
|
public int select(long timeout) |
|
|
{ |
|
|
if (keys == null) |
|
|
{ |
|
|
return 0; |
|
|
} |
|
75 |
|
|
76 |
int [] read = new int[keys.size()]; |
private static int java_do_select (int[] read, int[] write, |
77 |
int [] write = new int[keys.size()]; |
int[] except, long timeout) |
78 |
int [] except = new int[keys.size()]; |
{ |
79 |
int i = 0; |
return 0; |
80 |
|
} |
81 |
|
|
82 |
Iterator it = keys.iterator(); |
public int select (long timeout) |
83 |
while (it.hasNext()) |
{ |
84 |
|
if (keys == null) |
85 |
{ |
{ |
86 |
SelectionKeyImpl k = (SelectionKeyImpl) it.next(); |
return 0; |
87 |
|
} |
88 |
|
|
89 |
read[i] = k.fd; |
int[] read = new int[keys.size ()]; |
90 |
write[i] = k.fd; |
int[] write = new int[keys.size ()]; |
91 |
except[i] = k.fd; |
int[] except = new int[keys.size ()]; |
92 |
|
int i = 0; |
93 |
|
Iterator it = keys.iterator (); |
94 |
|
|
95 |
i++; |
while (it.hasNext ()) |
96 |
|
{ |
97 |
|
SelectionKeyImpl k = (SelectionKeyImpl) it.next (); |
98 |
|
read[i] = k.fd; |
99 |
|
write[i] = k.fd; |
100 |
|
except[i] = k.fd; |
101 |
|
i++; |
102 |
} |
} |
103 |
|
|
104 |
int ret = java_do_select(read, |
int ret = java_do_select (read, write, except, timeout); |
105 |
write, |
it = keys.iterator (); |
106 |
except, |
|
107 |
timeout); |
while (it.hasNext ()) |
|
it = keys.iterator(); |
|
|
while (it.hasNext()) |
|
108 |
{ |
{ |
109 |
SelectionKeyImpl k = (SelectionKeyImpl) it.next(); |
SelectionKeyImpl k = (SelectionKeyImpl) it.next (); |
110 |
|
|
111 |
if (read[i] != -1 || |
if (read[i] != -1 || |
112 |
write[i] != -1 || |
write[i] != -1 || |
113 |
except[i] != -1) |
except[i] != -1) |
114 |
{ |
{ |
115 |
add_selected(k); |
add_selected (k); |
116 |
} |
} |
117 |
|
|
118 |
i++; |
i++; |
119 |
} |
} |
|
return ret; |
|
|
} |
|
|
|
|
|
|
|
|
public Set selectedKeys() { return selected; } |
|
120 |
|
|
121 |
public Selector wakeup() |
return ret; |
122 |
{ |
} |
123 |
return null; |
|
124 |
} |
public Set selectedKeys () |
125 |
|
{ |
126 |
public void add(SelectionKeyImpl k) |
return selected; |
127 |
{ |
} |
128 |
if (keys == null) |
|
129 |
keys = new HashSet(); |
public Selector wakeup () |
130 |
|
{ |
131 |
keys.add(k); |
return null; |
132 |
} |
} |
133 |
|
|
134 |
void add_selected(SelectionKeyImpl k) |
public void add (SelectionKeyImpl k) |
135 |
{ |
{ |
136 |
if (selected == null) |
if (keys == null) |
137 |
selected = new HashSet(); |
keys = new HashSet (); |
138 |
|
|
139 |
selected.add(k); |
keys.add (k); |
140 |
} |
} |
141 |
|
|
142 |
protected void implCloseSelector() |
void add_selected (SelectionKeyImpl k) |
143 |
{ |
{ |
144 |
} |
if (selected == null) |
145 |
|
selected = new HashSet (); |
146 |
|
|
147 |
|
selected.add(k); |
148 |
|
} |
149 |
|
|
150 |
|
protected void implCloseSelector () |
151 |
|
{ |
152 |
|
} |
153 |
|
|
154 |
protected SelectionKey register(SelectableChannel ch, |
protected SelectionKey register (SelectableChannel ch, int ops, Object att) |
155 |
int ops, |
{ |
156 |
Object att) |
return register ((AbstractSelectableChannel) ch, ops, att); |
157 |
{ |
} |
158 |
return register((AbstractSelectableChannel) ch, |
|
159 |
ops, |
protected SelectionKey register (AbstractSelectableChannel ch, int ops, |
160 |
att); |
Object att) |
161 |
} |
{ |
162 |
|
/* |
|
protected SelectionKey register(AbstractSelectableChannel ch, |
|
|
int ops, |
|
|
Object att) |
|
|
{ |
|
|
/* |
|
163 |
// filechannel is not selectable ? |
// filechannel is not selectable ? |
164 |
if (ch instanceof gnu.java.nio.FileChannelImpl) |
if (ch instanceof FileChannelImpl) |
165 |
{ |
{ |
166 |
FileChannelImpl fc = (FileChannelImpl) ch; |
FileChannelImpl fc = (FileChannelImpl) ch; |
167 |
|
SelectionKeyImpl impl = new SelectionKeyImpl (ch, this, fc.fd); |
168 |
SelectionKeyImpl impl = new SelectionKeyImpl(ch, |
keys.add (impl); |
169 |
this, |
return impl; |
170 |
fc.fd); |
} |
171 |
|
else |
172 |
keys.add(impl); |
*/ |
|
|
|
|
return impl; |
|
|
} |
|
|
else |
|
|
*/ |
|
173 |
|
|
174 |
if (ch instanceof gnu.java.nio.SocketChannelImpl) |
if (ch instanceof SocketChannelImpl) |
175 |
{ |
{ |
176 |
SocketChannelImpl fc = (SocketChannelImpl) ch; |
SocketChannelImpl fc = (SocketChannelImpl) ch; |
177 |
|
SelectionKeyImpl impl = new SelectionKeyImpl (ch, this, fc.fd); |
178 |
SelectionKeyImpl impl = new SelectionKeyImpl(ch, |
add (impl); |
179 |
this, |
return impl; |
|
fc.fd); |
|
|
add(impl); |
|
|
|
|
|
return impl; |
|
180 |
} |
} |
181 |
else |
else |
182 |
{ |
{ |
183 |
System.err.println("INTERNAL ERROR, no known channel type"); |
System.err.println ("INTERNAL ERROR, no known channel type"); |
184 |
} |
} |
185 |
|
|
186 |
return null; |
return null; |
187 |
} |
} |
|
|
|
188 |
} |
} |