1 /* SelectorImpl.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
40 import java
.nio
.channels
.ClosedSelectorException
;
41 import java
.nio
.channels
.SelectableChannel
;
42 import java
.nio
.channels
.SelectionKey
;
43 import java
.nio
.channels
.Selector
;
44 import java
.nio
.channels
.spi
.AbstractSelectableChannel
;
45 import java
.nio
.channels
.spi
.AbstractSelector
;
46 import java
.nio
.channels
.spi
.SelectorProvider
;
47 import java
.util
.HashSet
;
48 import java
.util
.Iterator
;
51 public class SelectorImpl
extends AbstractSelector
53 boolean closed
= false;
54 Set keys
, selected
, canceled
;
56 public SelectorImpl (SelectorProvider provider
)
60 keys
= new HashSet ();
61 selected
= new HashSet ();
62 canceled
= new HashSet ();
70 public int selectNow ()
80 // A timeout value of -1 means block forever.
81 private static native int java_do_select (int[] read
, int[] write
,
82 int[] except
, long timeout
);
84 private int[] getFDsAsArray (int ops
)
88 Iterator it
= keys
.iterator ();
90 // Count the number of file descriptors needed
93 SelectionKeyImpl key
= (SelectionKeyImpl
) it
.next ();
95 if ((key
.interestOps () & ops
) != 0)
101 result
= new int[counter
];
104 it
= keys
.iterator ();
106 // Fill the array with the file descriptors
107 while (it
.hasNext ())
109 SelectionKeyImpl key
= (SelectionKeyImpl
) it
.next ();
111 if ((key
.interestOps () & ops
) != 0)
113 result
[counter
] = key
.fd
;
121 public int select (long timeout
)
125 throw new ClosedSelectorException ();
135 deregisterCanceledKeys ();
137 // Set only keys with the needed interest ops into the arrays.
138 int[] read
= getFDsAsArray (SelectionKey
.OP_READ
| SelectionKey
.OP_ACCEPT
);
139 int[] write
= getFDsAsArray (SelectionKey
.OP_WRITE
| SelectionKey
.OP_CONNECT
);
140 int[] except
= new int [0]; // FIXME: We dont need to check this yet
142 // Call the native select () on all file descriptors.
143 int anzahl
= read
.length
+ write
.length
+ except
.length
;
144 ret
= java_do_select (read
, write
, except
, timeout
);
146 Iterator it
= keys
.iterator ();
148 while (it
.hasNext ())
151 SelectionKeyImpl key
= (SelectionKeyImpl
) it
.next ();
153 // If key is already selected retrieve old ready ops.
154 if (selected
.contains (key
))
156 ops
= key
.readyOps ();
159 // Set new ready read/accept ops
160 for (int i
= 0; i
< read
.length
; i
++)
162 if (key
.fd
== read
[i
])
164 if (key
.channel () instanceof ServerSocketChannelImpl
)
166 ops
= ops
| SelectionKey
.OP_ACCEPT
;
170 ops
= ops
| SelectionKey
.OP_READ
;
175 // Set new ready write ops
176 for (int i
= 0; i
< write
.length
; i
++)
178 if (key
.fd
== write
[i
])
180 ops
= ops
| SelectionKey
.OP_WRITE
;
182 // if (key.channel ().isConnected ())
184 // ops = ops | SelectionKey.OP_WRITE;
188 // ops = ops | SelectionKey.OP_CONNECT;
193 // FIXME: We dont handle exceptional file descriptors yet.
195 // If key is not yet selected add it.
196 if (!selected
.contains (key
))
202 key
.readyOps (key
.interestOps () & ops
);
205 deregisterCanceledKeys ();
209 public Set
selectedKeys ()
214 public Selector
wakeup ()
219 public void add (SelectionKeyImpl k
)
224 void add_selected (SelectionKeyImpl k
)
229 protected void implCloseSelector ()
234 private void deregisterCanceledKeys ()
236 Iterator it
= canceled
.iterator ();
238 while (it
.hasNext ())
240 keys
.remove ((SelectionKeyImpl
) it
.next ());
245 protected SelectionKey
register (SelectableChannel ch
, int ops
, Object att
)
247 return register ((AbstractSelectableChannel
) ch
, ops
, att
);
250 protected SelectionKey
register (AbstractSelectableChannel ch
, int ops
,
253 // // filechannel is not selectable ?
254 // if (ch instanceof FileChannelImpl)
256 // FileChannelImpl fc = (FileChannelImpl) ch;
257 // SelectionKeyImpl impl = new SelectionKeyImpl (ch, this, fc.fd);
259 // impl.interestOps (ops);
260 // impl.attach (att);
265 if (ch
instanceof SocketChannelImpl
)
267 SocketChannelImpl sc
= (SocketChannelImpl
) ch
;
268 SelectionKeyImpl impl
= new SelectionKeyImpl (ch
, this, sc
.fd
);
270 impl
.interestOps (ops
);
274 else if (ch
instanceof DatagramChannelImpl
)
276 DatagramChannelImpl dc
= (DatagramChannelImpl
) ch
;
277 SelectionKeyImpl impl
= new SelectionKeyImpl (ch
, this, dc
.fd
);
279 impl
.interestOps (ops
);
283 else if (ch
instanceof ServerSocketChannelImpl
)
285 ServerSocketChannelImpl ssc
= (ServerSocketChannelImpl
) ch
;
286 SelectionKeyImpl impl
= new SelectionKeyImpl (ch
, this, ssc
.fd
);
288 impl
.interestOps (ops
);
294 System
.err
.println ("INTERNAL ERROR, no known channel type");