From: <qa...@us...> - 2006-12-15 01:52:01
|
Revision: 2931 http://jnode.svn.sourceforge.net/jnode/?rev=2931&view=rev Author: qades Date: 2006-12-14 17:51:58 -0800 (Thu, 14 Dec 2006) Log Message: ----------- reverted broken changes Modified Paths: -------------- trunk/core/src/classpath/gnu/gnu/java/net/PlainDatagramSocketImpl.java trunk/core/src/classpath/gnu/gnu/java/net/PlainSocketImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/DatagramChannelImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/DatagramChannelSelectionKey.java trunk/core/src/classpath/gnu/gnu/java/nio/FileLockImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/NIOSocket.java trunk/core/src/classpath/gnu/gnu/java/nio/PipeImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/SelectionKeyImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/SelectorImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/SelectorProviderImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/ServerSocketChannelImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/ServerSocketChannelSelectionKey.java trunk/core/src/classpath/gnu/gnu/java/nio/SocketChannelImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/SocketChannelSelectionKey.java trunk/core/src/classpath/gnu/gnu/java/nio/SocketChannelSelectionKeyImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/charset/ByteCharset.java trunk/core/src/classpath/gnu/gnu/java/nio/charset/ISO_8859_1.java trunk/core/src/classpath/gnu/gnu/java/nio/charset/US_ASCII.java trunk/core/src/classpath/gnu/gnu/java/nio/charset/iconv/IconvDecoder.java trunk/core/src/classpath/gnu/gnu/java/nio/charset/iconv/IconvEncoder.java trunk/core/src/classpath/java/java/io/FileDescriptor.java trunk/core/src/classpath/java/java/io/FileInputStream.java trunk/core/src/classpath/java/java/io/FileOutputStream.java trunk/core/src/classpath/java/java/io/RandomAccessFile.java trunk/core/src/classpath/java/java/net/DatagramSocket.java trunk/core/src/classpath/vm/gnu/java/nio/VMPipe.java trunk/core/src/classpath/vm/gnu/java/nio/VMSelector.java trunk/core/src/classpath/vm/java/io/VMOpenMode.java trunk/core/src/classpath/vm/java/nio/channels/VMChannels.java trunk/shell/src/shell/org/jnode/shell/ThreadCommandInvoker.java Added Paths: ----------- trunk/core/src/classpath/gnu/gnu/java/nio/channels/FileChannelImpl.java Removed Paths: ------------- trunk/core/src/classpath/gnu/gnu/java/nio/EpollSelectionKeyImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/EpollSelectorImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/FileChannelImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/KqueueSelectionKeyImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/KqueueSelectorImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/NIOSocketImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/VMChannelOwner.java trunk/core/src/classpath/vm/gnu/java/net/ trunk/core/src/classpath/vm/gnu/java/nio/VMChannel.java trunk/core/src/core/org/jnode/vm/VmChannel.java Modified: trunk/core/src/classpath/gnu/gnu/java/net/PlainDatagramSocketImpl.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/net/PlainDatagramSocketImpl.java 2006-12-14 21:06:58 UTC (rev 2930) +++ trunk/core/src/classpath/gnu/gnu/java/net/PlainDatagramSocketImpl.java 2006-12-15 01:51:58 UTC (rev 2931) @@ -1,5 +1,5 @@ /* PlainDatagramSocketImpl.java -- Default DatagramSocket implementation - Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. + Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -38,10 +38,7 @@ package gnu.java.net; -import gnu.java.nio.VMChannel; - import java.io.IOException; -import java.io.InterruptedIOException; import java.net.DatagramPacket; import java.net.DatagramSocketImpl; import java.net.InetAddress; @@ -49,8 +46,8 @@ import java.net.NetworkInterface; import java.net.SocketAddress; import java.net.SocketException; -import java.net.SocketTimeoutException; -import java.nio.ByteBuffer; +import java.net.SocketOptions; +import gnu.classpath.Configuration; /** * Written using on-line Java Platform 1.2 API Specification, as well @@ -68,12 +65,17 @@ */ public final class PlainDatagramSocketImpl extends DatagramSocketImpl { - private final VMChannel channel; - + // @vm-specific removed System.loadLibrary + /** - * The platform-specific socket implementation. + * Option id for the IP_TTL (time to live) value. */ - private final VMPlainSocketImpl impl; + private static final int IP_TTL = 0x1E61; // 7777 + + /** + * This is the actual underlying file descriptor + */ + int native_fd = -1; /** * Lock object to serialize threads wanting to receive @@ -88,26 +90,24 @@ /** * Default do nothing constructor */ - public PlainDatagramSocketImpl() throws IOException + public PlainDatagramSocketImpl() { - channel = new VMChannel(); - impl = new VMPlainSocketImpl(channel); } - /*protected void finalize() throws Throwable + protected void finalize() throws Throwable { synchronized (this) { - if (channel.getState().isValid()) + if (native_fd != -1) close(); } super.finalize(); - }*/ + } - /*public int getNativeFD() + public int getNativeFD() { return native_fd; - }*/ + } /** * Binds this socket to a particular port and interface @@ -120,20 +120,9 @@ protected synchronized void bind(int port, InetAddress addr) throws SocketException { - try - { - impl.bind(new InetSocketAddress(addr, port)); - } - catch (SocketException se) - { - throw se; - } - catch (IOException ioe) - { - SocketException se = new SocketException(); - se.initCause(ioe); - throw se; - } + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } /** @@ -141,58 +130,13 @@ * * @exception SocketException If an error occurs */ - protected synchronized void create() throws SocketException - { - try - { - channel.initSocket(false); - } - catch (SocketException se) - { - throw se; - } - catch (IOException ioe) - { - SocketException se = new SocketException(); - se.initCause(ioe); - throw se; - } + protected synchronized void create() throws SocketException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } /** - * Connects to the remote address and port specified as arguments. - * - * @param addr The remote address to connect to - * @param port The remote port to connect to - * - * @exception SocketException If an error occurs - */ - protected void connect(InetAddress addr, int port) throws SocketException - { - channel.connect(new InetSocketAddress(addr, port), 0); - } - - /** - * Disconnects the socket. - * - * @since 1.4 - */ - protected void disconnect() - { - synchronized (this) - { - try - { - if (channel.getState().isValid()) - channel.disconnect(); - } - catch (IOException ioe) - { - } - } - } - - /** * Sets the Time to Live value for the socket * * @param ttl The new TTL value @@ -201,7 +145,7 @@ */ protected synchronized void setTimeToLive(int ttl) throws IOException { - impl.setTimeToLive(ttl); + setOption(IP_TTL, new Integer(ttl)); } /** @@ -213,60 +157,48 @@ */ protected synchronized int getTimeToLive() throws IOException { - return impl.getTimeToLive(); - } + Object obj = getOption(IP_TTL); - protected int getLocalPort() - { - if (channel == null) - return -1; + if (! (obj instanceof Integer)) + throw new IOException("Internal Error"); - try - { - InetSocketAddress local = channel.getLocalAddress(); - if (local == null) - return -1; - return local.getPort(); - } - catch (IOException ioe) - { - return -1; - } + return ((Integer) obj).intValue(); } /** * Sends a packet of data to a remote host * + * @param addr The address to send to + * @param port The port to send to + * @param buf The buffer to send + * @param offset The offset of the data in the buffer to send + * @param len The length of the data to send + * + * @exception IOException If an error occurs + */ + private void sendto (InetAddress addr, int port, + byte[] buf, int offset, int len) + throws IOException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); + } + + /** + * Sends a packet of data to a remote host + * * @param packet The packet to send * * @exception IOException If an error occurs */ protected void send(DatagramPacket packet) throws IOException { - synchronized (SEND_LOCK) + synchronized(SEND_LOCK) { - ByteBuffer buf = ByteBuffer.wrap(packet.getData(), - packet.getOffset(), - packet.getLength()); - InetAddress remote = packet.getAddress(); - int port = packet.getPort(); - if (remote == null) - throw new NullPointerException(); - if (port <= 0) - throw new SocketException("invalid port " + port); - while (true) - { - try - { - channel.send(buf, new InetSocketAddress(remote, port)); - break; - } - catch (InterruptedIOException ioe) - { - // Ignore; interrupted system call. - } - } + sendto(packet.getAddress(), packet.getPort(), packet.getData(), + packet.getOffset(), packet.getLength()); } + } /** @@ -279,123 +211,63 @@ protected void receive(DatagramPacket packet) throws IOException { - synchronized(RECEIVE_LOCK) - { - ByteBuffer buf = ByteBuffer.wrap(packet.getData(), - packet.getOffset(), - packet.getLength()); - SocketAddress addr = null; - while (true) - { - try - { - addr = channel.receive(buf); - break; - } - catch (SocketTimeoutException ste) - { - throw ste; - } - catch (InterruptedIOException iioe) - { - // Ignore. Loop. - } - } - if (addr != null) - packet.setSocketAddress(addr); - packet.setLength(buf.position() - packet.getOffset()); - } + synchronized(RECEIVE_LOCK) + { + receive0(packet); + } } + /** + * Native call to receive a UDP packet from the network + * + * @param packet The packet to fill in with the data received + * + * @exception IOException IOException If an error occurs + */ + private void receive0(DatagramPacket packet) throws IOException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); + } /** * Sets the value of an option on the socket * - * @param optionId The identifier of the option to set - * @param value The value of the option to set + * @param option_id The identifier of the option to set + * @param val The value of the option to set * * @exception SocketException If an error occurs */ - public synchronized void setOption(int optionId, Object value) - throws SocketException - { - switch (optionId) - { - case IP_MULTICAST_IF: - case IP_MULTICAST_IF2: - impl.setMulticastInterface(optionId, (InetAddress) value); - break; - - case IP_MULTICAST_LOOP: - case SO_BROADCAST: - case SO_KEEPALIVE: - case SO_OOBINLINE: - case TCP_NODELAY: - case IP_TOS: - case SO_LINGER: - case SO_RCVBUF: - case SO_SNDBUF: - case SO_TIMEOUT: - case SO_REUSEADDR: - impl.setOption(optionId, value); - return; - - default: - throw new SocketException("cannot set option " + optionId); - } + public synchronized void setOption(int option_id, Object val) + throws SocketException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } /** * Retrieves the value of an option on the socket * - * @param optionId The identifier of the option to retrieve + * @param option_id The identifier of the option to retrieve * * @return The value of the option * * @exception SocketException If an error occurs */ - public synchronized Object getOption(int optionId) - throws SocketException - { - if (optionId == SO_BINDADDR) - { - try - { - InetSocketAddress local = channel.getLocalAddress(); - if (local == null) - return null; - return local.getAddress(); - } - catch (SocketException se) - { - throw se; - } - catch (IOException ioe) - { - SocketException se = new SocketException(); - se.initCause(ioe); - throw se; - } - } - if (optionId == IP_MULTICAST_IF || optionId == IP_MULTICAST_IF2) - return impl.getMulticastInterface(optionId); - - return impl.getOption(optionId); + public synchronized Object getOption(int option_id) + throws SocketException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } /** * Closes the socket */ - protected synchronized void close() - { - try - { - if (channel.getState().isValid()) - channel.close(); - } - catch (IOException ioe) - { - } + protected synchronized void close() { + // @vm-specific no natives + //TODO implement me + throw new RuntimeException("Not implemented"); } /** @@ -433,9 +305,9 @@ * * @exception IOException If an error occurs */ - protected synchronized void join(InetAddress addr) throws IOException - { - impl.join(addr); + protected synchronized void join(InetAddress addr) throws IOException { + // @vm-specific no natives + throw new SocketException("Not implemented"); } /** @@ -445,9 +317,10 @@ * * @exception IOException If an error occurs */ - protected synchronized void leave(InetAddress addr) throws IOException - { - impl.leave(addr); + protected synchronized void leave(InetAddress addr) throws IOException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } /** @@ -465,22 +338,14 @@ } public void joinGroup(SocketAddress address, NetworkInterface netIf) - throws IOException { - if (address == null) - throw new NullPointerException(); - if (!(address instanceof InetSocketAddress)) - throw new SocketException("unknown address type"); - impl.joinGroup((InetSocketAddress) address, netIf); + throw new InternalError + ("PlainDatagramSocketImpl::joinGroup is not implemented"); } public void leaveGroup(SocketAddress address, NetworkInterface netIf) - throws IOException { - if (address == null) - throw new NullPointerException(); - if (!(address instanceof InetSocketAddress)) - throw new SocketException("unknown address type"); - impl.leaveGroup((InetSocketAddress) address, netIf); + throw new InternalError + ("PlainDatagramSocketImpl::leaveGroup is not implemented"); } } Modified: trunk/core/src/classpath/gnu/gnu/java/net/PlainSocketImpl.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/net/PlainSocketImpl.java 2006-12-14 21:06:58 UTC (rev 2930) +++ trunk/core/src/classpath/gnu/gnu/java/net/PlainSocketImpl.java 2006-12-15 01:51:58 UTC (rev 2931) @@ -39,20 +39,16 @@ package gnu.java.net; -import gnu.java.nio.SocketChannelImpl; -import gnu.java.nio.VMChannel; - import java.io.InputStream; import java.io.IOException; -import java.io.InterruptedIOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.net.SocketException; import java.net.SocketImpl; -import java.net.SocketTimeoutException; -import java.nio.ByteBuffer; +import java.net.SocketOptions; +import gnu.classpath.Configuration; /** * Written using on-line Java Platform 1.2 API Specification, as well @@ -70,13 +66,25 @@ * @author Nic Ferrier (nfe...@ta...) * @author Aaron M. Renn (ar...@ur...) */ -public class PlainSocketImpl extends SocketImpl +public final class PlainSocketImpl extends SocketImpl { + // Static initializer to load native library. + static + { + if (Configuration.INIT_LOAD_LIBRARY) + { + System.loadLibrary("javanet"); + } + } /** - * The underlying plain socket VM implementation. + * The OS file handle representing the socket. + * This is used for reads and writes to/from the socket and + * to close it. + * + * When the socket is closed this is reset to -1. */ - protected VMPlainSocketImpl impl; + int native_fd = -1; /** * A cached copy of the in stream for reading from the socket. @@ -93,14 +101,8 @@ * is being invoked on this socket. */ private boolean inChannelOperation; - + /** - * The socket channel we use for IO operation. Package-private for - * use by inner classes. - */ - SocketChannelImpl channel; - - /** * Indicates whether we should ignore whether any associated * channel is set to non-blocking mode. Certain operations * throw an <code>IllegalBlockingModeException</code> if the @@ -122,44 +124,48 @@ } /** - * Default do nothing constructor. + * Default do nothing constructor */ public PlainSocketImpl() { - this.impl = new VMPlainSocketImpl(); } + + protected void finalize() throws Throwable + { + synchronized (this) + { + if (native_fd != -1) + try + { + close(); + } + catch (IOException ex) + { + } + } + super.finalize(); + } + public int getNativeFD() + { + return native_fd; + } + /** * Sets the specified option on a socket to the passed in object. For * options that take an integer argument, the passed in object is an * Integer. The option_id parameter is one of the defined constants in * this interface. * - * @param optionId The identifier of the option - * @param value The value to set the option to + * @param option_id The identifier of the option + * @param val The value to set the option to * - * @throws SocketException if an error occurs + * @exception SocketException If an error occurs */ - public void setOption(int optionId, Object value) throws SocketException - { - switch (optionId) - { - case SO_LINGER: - case IP_MULTICAST_LOOP: - case SO_BROADCAST: - case SO_KEEPALIVE: - case SO_OOBINLINE: - case TCP_NODELAY: - case IP_TOS: - case SO_RCVBUF: - case SO_SNDBUF: - case SO_TIMEOUT: - case SO_REUSEADDR: - impl.setOption(optionId, value); - return; - default: - throw new SocketException("Unrecognized TCP option: " + optionId); - } + public void setOption(int optID, Object value) throws SocketException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } /** @@ -167,144 +173,133 @@ * will be an Integer for options that have integer values. The option_id * is one of the defined constants in this interface. * - * @param optionId the option identifier + * @param option_id The option identifier * - * @return the current value of the option + * @return The current value of the option * - * @throws SocketException if an error occurs + * @exception SocketException If an error occurs */ - public Object getOption(int optionId) throws SocketException - { - if (optionId == SO_BINDADDR) - { - try - { - return channel.getVMChannel().getLocalAddress().getAddress(); - } - catch (IOException ioe) - { - SocketException se = new SocketException(); - se.initCause(ioe); - throw se; - } - } - - // This filters options which are invalid for TCP. - switch (optionId) - { - case SO_LINGER: - case IP_MULTICAST_LOOP: - case SO_BROADCAST: - case SO_KEEPALIVE: - case SO_OOBINLINE: - case TCP_NODELAY: - case IP_TOS: - case SO_RCVBUF: - case SO_SNDBUF: - case SO_TIMEOUT: - case SO_REUSEADDR: - return impl.getOption(optionId); - default: - throw new SocketException("Unrecognized TCP option: " + optionId); - } - + public Object getOption(int optID) throws SocketException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } - public void shutdownInput() throws IOException + /** + * Flushes the input stream and closes it. If you read from the input stream + * after calling this method a <code>IOException</code> will be thrown. + * + * @throws IOException if an error occurs + */ + public void shutdownInput() { - impl.shutdownInput(); + // @vm-specific no natives + //TODO implement me + throw new InternalError ("PlainSocketImpl::shutdownInput not implemented"); } + /** + * Flushes the output stream and closes it. If you write to the output stream + * after calling this method a <code>IOException</code> will be thrown. + * + * @throws IOException if an error occurs + */ public void shutdownOutput() throws IOException { - impl.shutdownOutput(); + // @vm-specific no natives + //TODO implement me + throw new InternalError ("PlainSocketImpl::shutdownOutput not implemented"); } /** * Creates a new socket that is not bound to any local address/port and - * is not connected to any remote address/port. The stream parameter will be - * ignored since PlainSocketImpl always is a stream socket. Datagram sockets - * are handled by PlainDatagramSocketImpl. + * is not connected to any remote address/port. This will be created as + * a stream socket if the stream parameter is true, or a datagram socket + * if the stream parameter is false. * - * @param stream <code>true</code> for stream sockets, <code>false</code> for - * datagram sockets + * @param stream true for a stream socket, false for a datagram socket */ - protected synchronized void create(boolean stream) throws IOException - { - channel = new SocketChannelImpl(false); - VMChannel vmchannel = channel.getVMChannel(); - vmchannel.initSocket(stream); - channel.configureBlocking(true); - impl.getState().setChannelFD(vmchannel.getState()); + protected synchronized void create(boolean stream) throws IOException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } /** * Connects to the remote hostname and port specified as arguments. * - * @param hostname the remote hostname to connect to - * @param port the remote port to connect to + * @param hostname The remote hostname to connect to + * @param port The remote port to connect to * - * @throws IOException If an error occurs + * @exception IOException If an error occurs */ - protected synchronized void connect(String hostname, int port) - throws IOException + protected synchronized void connect(String host, int port) throws IOException { - connect(InetAddress.getByName(hostname), port); + connect(InetAddress.getByName(host), port); } /** * Connects to the remote address and port specified as arguments. * - * @param addr the remote address to connect to - * @param port the remote port to connect to + * @param addr The remote address to connect to + * @param port The remote port to connect to * - * @throws IOException If an error occurs + * @exception IOException If an error occurs */ - protected void connect(InetAddress addr, int port) throws IOException - { - connect(new InetSocketAddress(addr, port), 0); + protected void connect(InetAddress addr, int port) throws IOException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } /** * Connects to the remote socket address with a specified timeout. * - * @param address the remote address to connect to - * @param timeout the timeout to use for this connect, 0 means infinite. + * @param timeout The timeout to use for this connect, 0 means infinite. * - * @throws IOException If an error occurs + * @exception IOException If an error occurs */ - protected synchronized void connect(SocketAddress address, int timeout) - throws IOException + protected synchronized void connect(SocketAddress address, int timeout) throws IOException { - if (channel == null) - create(true); - boolean connected = channel.connect(address, timeout); - if (!connected) - throw new SocketTimeoutException("connect timed out"); + InetSocketAddress sockAddr = (InetSocketAddress) address; + InetAddress addr = sockAddr.getAddress(); + + if (addr == null) + throw new IllegalArgumentException("address is unresolved: " + sockAddr); + + int port = sockAddr.getPort(); - // Using the given SocketAddress is important to preserve - // hostnames given by the caller. - InetSocketAddress addr = (InetSocketAddress) address; - this.address = addr.getAddress(); - this.port = addr.getPort(); + if (timeout < 0) + throw new IllegalArgumentException("negative timeout"); + + Object oldTimeoutObj = null; + + try + { + oldTimeoutObj = this.getOption (SocketOptions.SO_TIMEOUT); + this.setOption (SocketOptions.SO_TIMEOUT, new Integer (timeout)); + connect (addr, port); + } + finally + { + if (oldTimeoutObj != null) + this.setOption (SocketOptions.SO_TIMEOUT, oldTimeoutObj); + } } /** * Binds to the specified port on the specified addr. Note that this addr * must represent a local IP address. **** How bind to INADDR_ANY? **** * - * @param addr the address to bind to - * @param port the port number to bind to + * @param addr The address to bind to + * @param port The port number to bind to * - * @throws IOException if an error occurs + * @exception IOException If an error occurs */ protected synchronized void bind(InetAddress addr, int port) - throws IOException - { - if (channel == null) - create(true); - impl.bind(new InetSocketAddress(addr, port)); - localport = channel.getVMChannel().getLocalAddress().getPort(); + throws IOException { + // @vm-specific no natives + throw new SocketException("Not implemented"); } /** @@ -315,12 +310,13 @@ * * @param queuelen The length of the pending connection queue * - * @throws IOException If an error occurs + * @exception IOException If an error occurs */ protected synchronized void listen(int queuelen) - throws IOException - { - impl.listen(queuelen); + throws IOException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } /** @@ -330,64 +326,78 @@ * @param impl The SocketImpl object to accept this connection. */ protected synchronized void accept(SocketImpl impl) - throws IOException - { - if (channel == null) - create(true); - if (!(impl instanceof PlainSocketImpl)) - throw new IOException("incompatible SocketImpl: " - + impl.getClass().getName()); - PlainSocketImpl that = (PlainSocketImpl) impl; - VMChannel c = channel.getVMChannel().accept(); - that.impl.getState().setChannelFD(c.getState()); - that.channel = new SocketChannelImpl(c); - that.setOption(SO_REUSEADDR, Boolean.TRUE); - // Reset the inherited timeout. - that.setOption(SO_TIMEOUT, Integer.valueOf(0)); - + throws IOException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } /** * Returns the number of bytes that the caller can read from this socket * without blocking. * - * @return the number of readable bytes before blocking + * @return The number of readable bytes before blocking * - * @throws IOException if an error occurs + * @exception IOException If an error occurs */ - protected int available() throws IOException - { - if (channel == null) - throw new SocketException("not connected"); - return channel.getVMChannel().available(); + protected int available() throws IOException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } /** * Closes the socket. This will cause any InputStream or OutputStream * objects for this Socket to be closed as well. - * * <p> * Note that if the SO_LINGER option is set on this socket, then the * operation could block. - * </p> * - * @throws IOException if an error occurs + * @exception IOException If an error occurs */ - protected void close() throws IOException - { - if (impl.getState().isValid()) - impl.close(); - - address = null; - port = -1; + protected void close() throws IOException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); } - public void sendUrgentData(int data) throws IOException + public void sendUrgentData(int data) { - impl.sendUrgentData(data); + throw new InternalError ("PlainSocketImpl::sendUrgentData not implemented"); } /** + * Internal method used by SocketInputStream for reading data from + * the connection. Reads up to len bytes of data into the buffer + * buf starting at offset bytes into the buffer. + * + * @return The actual number of bytes read or -1 if end of stream. + * + * @exception IOException If an error occurs + */ + protected int read(byte[] buf, int offset, int len) + throws IOException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); + + } + + /** + * Internal method used by SocketOuputStream for writing data to + * the connection. Writes up to len bytes of data from the buffer + * buf starting at offset bytes into the buffer. + * + * @exception IOException If an error occurs + */ + protected void write(byte[] buf, int offset, int len) + throws IOException { + // @vm-specific no natives + //TODO implement me + throw new SocketException("Not implemented"); + } + + /** * Returns an InputStream object for reading from this socket. This will * be an instance of SocketInputStream. * @@ -399,7 +409,7 @@ { if (in == null) in = new SocketInputStream(); - + return in; } @@ -415,104 +425,15 @@ { if (out == null) out = new SocketOutputStream(); - + return out; } - - public VMChannel getVMChannel() - { - if (channel == null) - return null; - return channel.getVMChannel(); - } - /* (non-Javadoc) - * @see java.net.SocketImpl#getInetAddress() - */ - protected InetAddress getInetAddress() - { - if (channel == null) - return null; - - try - { - InetSocketAddress remote = channel.getVMChannel().getPeerAddress(); - if (remote == null) - return null; - // To mimic behavior of the RI the InetAddress instance which was - // used to establish the connection is returned instead of one that - // was created by the native layer (this preserves exact hostnames). - if (address != null) - return address; - - return remote.getAddress(); - } - catch (IOException ioe) - { - return null; - } - } - - /* (non-Javadoc) - * @see java.net.SocketImpl#getLocalPort() - */ - protected int getLocalPort() - { - if (channel == null) - return -1; - try - { - InetSocketAddress local = channel.getVMChannel().getLocalAddress(); - if (local == null) - return -1; - return local.getPort(); - } - catch (IOException ioe) - { - return -1; - } - } - - public InetSocketAddress getLocalAddress() - { - if (channel == null) - return null; - try - { - return channel.getVMChannel().getLocalAddress(); - } - catch (IOException ioe) - { - return null; - } - } - - /* (non-Javadoc) - * @see java.net.SocketImpl#getPort() - */ - protected int getPort() - { - if (channel == null) - return -1; - - try - { - InetSocketAddress remote = channel.getVMChannel().getPeerAddress(); - if (remote == null) - return -1; - return remote.getPort(); - } - catch (IOException ioe) - { - return -1; - } - } - /** * This class contains an implementation of <code>InputStream</code> for * sockets. It in an internal only class used by <code>PlainSocketImpl</code>. * - * @author Nic Ferrier <nfe...@ta...> + * @author Nic Ferrier (nfe...@ta...) */ final class SocketInputStream extends InputStream @@ -544,23 +465,13 @@ */ public int read() throws IOException { - if (channel == null) - throw new SocketException("not connected"); - while (true) - { - try - { - return channel.getVMChannel().read(); - } - catch (SocketTimeoutException ste) - { - throw ste; - } - catch (InterruptedIOException iioe) - { - // Ignore; NIO may throw this; net io shouldn't - } - } + byte buf[] = new byte [1]; + int bytes_read = read(buf, 0, 1); + + if (bytes_read == -1) + return -1; + + return buf[0] & 0xFF; } /** @@ -577,24 +488,12 @@ */ public int read (byte[] buf, int offset, int len) throws IOException { - if (channel == null) - throw new SocketException("not connected"); - ByteBuffer b = ByteBuffer.wrap(buf, offset, len); - while (true) - { - try - { - return channel.read(b); - } - catch (SocketTimeoutException ste) - { - throw ste; - } - catch (InterruptedIOException iioe) - { - // Ignored; NIO may throw this; net IO not. - } - } + int bytes_read = PlainSocketImpl.this.read (buf, offset, len); + + if (bytes_read == 0) + return -1; + + return bytes_read; } } @@ -604,7 +503,7 @@ * <code>getOutputStream method</code>. It expects only to be used in that * context. * - * @author Nic Ferrier <nfe...@ta...> + * @author Nic Ferrier (nfe...@ta...) */ final class SocketOutputStream extends OutputStream @@ -630,20 +529,8 @@ */ public void write(int b) throws IOException { - if (channel == null) - throw new SocketException("not connected"); - while (true) - { - try - { - channel.getVMChannel().write(b); - return; - } - catch (InterruptedIOException iioe) - { - // Ignored. - } - } + byte buf[] = { (byte) b }; + write(buf, 0, 1); } /** @@ -658,21 +545,7 @@ */ public void write (byte[] buf, int offset, int len) throws IOException { - if (channel == null) - throw new SocketException("not connected"); - ByteBuffer b = ByteBuffer.wrap(buf, offset, len); - while (b.hasRemaining()) - { - try - { - if (channel.write(b) == -1) - throw new IOException("channel has been closed"); - } - catch (InterruptedIOException iioe) - { - // Ignored. - } - } + PlainSocketImpl.this.write (buf, offset, len); } } } Modified: trunk/core/src/classpath/gnu/gnu/java/nio/DatagramChannelImpl.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/nio/DatagramChannelImpl.java 2006-12-14 21:06:58 UTC (rev 2930) +++ trunk/core/src/classpath/gnu/gnu/java/nio/DatagramChannelImpl.java 2006-12-15 01:51:58 UTC (rev 2931) @@ -55,10 +55,8 @@ * @author Michael Koch */ public final class DatagramChannelImpl extends DatagramChannel - implements VMChannelOwner { private NIODatagramSocket socket; - private VMChannel channel; /** * Indicates whether this channel initiated whatever operation @@ -66,16 +64,6 @@ */ private boolean inChannelOperation; - protected DatagramChannelImpl (SelectorProvider provider) - throws IOException - { - super (provider); - socket = new NIODatagramSocket (new PlainDatagramSocketImpl(), this); - channel = new VMChannel(); - channel.initSocket(false); - configureBlocking(true); - } - /** * Indicates whether our datagram socket should ignore whether * we are set to non-blocking mode. Certain operations on our @@ -97,6 +85,14 @@ inChannelOperation = b; } + protected DatagramChannelImpl (SelectorProvider provider) + throws IOException + { + super (provider); + socket = new NIODatagramSocket (new PlainDatagramSocketImpl(), this); + configureBlocking(true); + } + public DatagramSocket socket () { return socket; @@ -105,13 +101,13 @@ protected void implCloseSelectableChannel () throws IOException { - channel.close(); + socket.close (); } protected void implConfigureBlocking (boolean blocking) throws IOException { - channel.setBlocking(blocking); + socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT); } public DatagramChannel connect (SocketAddress remote) @@ -120,43 +116,29 @@ if (!isOpen()) throw new ClosedChannelException(); - try - { - channel.connect((InetSocketAddress) remote, 0); - } - catch (ClassCastException cce) - { - throw new IOException("unsupported socked address type"); - } + socket.connect (remote); return this; } public DatagramChannel disconnect () throws IOException { - channel.disconnect(); + socket.disconnect (); return this; } - public boolean isConnected() + public boolean isConnected () { - try - { - return channel.getPeerAddress() != null; - } - catch (IOException ioe) - { - return false; - } + return socket.isConnected (); } - + public int write (ByteBuffer src) throws IOException { if (!isConnected ()) throw new NotYetConnectedException (); - return channel.write(src); + return send (src, socket.getRemoteSocketAddress()); } public long write (ByteBuffer[] srcs, int offset, int length) @@ -170,11 +152,13 @@ || (length < 0) || (length > (srcs.length - offset))) throw new IndexOutOfBoundsException(); + + long result = 0; - /* We are connected, meaning we will write these bytes to - * the host we connected to, so we don't need to explicitly - * give the host. */ - return channel.writeGathering(srcs, offset, length); + for (int index = offset; index < offset + length; index++) + result += write (srcs [index]); + + return result; } public int read (ByteBuffer dst) @@ -183,7 +167,9 @@ if (!isConnected ()) throw new NotYetConnectedException (); - return channel.read(dst); + int remaining = dst.remaining(); + receive (dst); + return remaining - dst.remaining(); } public long read (ByteBuffer[] dsts, int offset, int length) @@ -198,8 +184,12 @@ || (length > (dsts.length - offset))) throw new IndexOutOfBoundsException(); - /* Likewise, see the comment int write above. */ - return channel.readScattering(dsts, offset, length); + long result = 0; + + for (int index = offset; index < offset + length; index++) + result += read (dsts [index]); + + return result; } public SocketAddress receive (ByteBuffer dst) @@ -210,14 +200,51 @@ try { - begin(); - return channel.receive(dst); + DatagramPacket packet; + int len = dst.remaining(); + + if (dst.hasArray()) + { + packet = new DatagramPacket (dst.array(), + dst.arrayOffset() + dst.position(), + len); + } + else + { + packet = new DatagramPacket (new byte [len], len); + } + + boolean completed = false; + + try + { + begin(); + setInChannelOperation(true); + socket.receive (packet); + completed = true; + } + finally + { + end (completed); + setInChannelOperation(false); + } + + if (!dst.hasArray()) + { + dst.put (packet.getData(), packet.getOffset(), packet.getLength()); + } + else + { + dst.position (dst.position() + packet.getLength()); + } + + return packet.getSocketAddress(); } - finally - { - end(true); - } + catch (SocketTimeoutException e) + { + return null; } + } public int send (ByteBuffer src, SocketAddress target) throws IOException @@ -225,18 +252,46 @@ if (!isOpen()) throw new ClosedChannelException(); - if (!(target instanceof InetSocketAddress)) - throw new IOException("can only send to inet socket addresses"); - - InetSocketAddress dst = (InetSocketAddress) target; - if (dst.isUnresolved()) + if (target instanceof InetSocketAddress + && ((InetSocketAddress) target).isUnresolved()) throw new IOException("Target address not resolved"); - return channel.send(src, dst); + byte[] buffer; + int offset = 0; + int len = src.remaining(); + + if (src.hasArray()) + { + buffer = src.array(); + offset = src.arrayOffset() + src.position(); + } + else + { + buffer = new byte [len]; + src.get (buffer); } - - public VMChannel getVMChannel() + + DatagramPacket packet = new DatagramPacket (buffer, offset, len, target); + + boolean completed = false; + try { - return channel; + begin(); + setInChannelOperation(true); + socket.send(packet); + completed = true; + } + finally + { + end (completed); + setInChannelOperation(false); + } + + if (src.hasArray()) + { + src.position (src.position() + len); + } + + return len; } } Modified: trunk/core/src/classpath/gnu/gnu/java/nio/DatagramChannelSelectionKey.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/nio/DatagramChannelSelectionKey.java 2006-12-14 21:06:58 UTC (rev 2930) +++ trunk/core/src/classpath/gnu/gnu/java/nio/DatagramChannelSelectionKey.java 2006-12-15 01:51:58 UTC (rev 2931) @@ -38,7 +38,6 @@ package gnu.java.nio; -import java.io.IOException; import java.nio.channels.spi.AbstractSelectableChannel; /** @@ -53,16 +52,10 @@ super (channel, selector); } - // FIXME don't use file descriptor integers public int getNativeFD() { - try - { - return ((DatagramChannelImpl) ch).getVMChannel().getState().getNativeFD(); - } - catch (IOException ioe) - { - throw new IllegalStateException(ioe); - } + NIODatagramSocket socket = + (NIODatagramSocket) ((DatagramChannelImpl) ch).socket(); + return socket.getPlainDatagramSocketImpl().getNativeFD(); } } Deleted: trunk/core/src/classpath/gnu/gnu/java/nio/EpollSelectionKeyImpl.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/nio/EpollSelectionKeyImpl.java 2006-12-14 21:06:58 UTC (rev 2930) +++ trunk/core/src/classpath/gnu/gnu/java/nio/EpollSelectionKeyImpl.java 2006-12-15 01:51:58 UTC (rev 2931) @@ -1,122 +0,0 @@ -/* EpollSelectionKeyImpl.java -- selection key for the epoll selector. - Copyright (C) 2006 Free Software Foundation, Inc. - -This file is part of GNU Classpath. - -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ - - -package gnu.java.nio; - -import java.io.IOException; -import java.nio.channels.CancelledKeyException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.AbstractSelectionKey; - -/** - * @author Casey Marshall (cs...@gn...) - */ -public class EpollSelectionKeyImpl extends AbstractSelectionKey -{ - final int fd; - private final EpollSelectorImpl selector; - private final SelectableChannel channel; - int interestOps; - int selectedOps; - int key; - boolean valid; - boolean cancelled; - - EpollSelectionKeyImpl(EpollSelectorImpl selector, - SelectableChannel channel, int fd) - { - this.selector = selector; - this.channel = channel; - this.fd = fd; - } - - /* (non-Javadoc) - * @see java.nio.channels.SelectionKey#channel() - */ - public SelectableChannel channel() - { - return channel; - } - - /* (non-Javadoc) - * @see java.nio.channels.SelectionKey#interestOps() - */ - public int interestOps() - { - return interestOps; - } - - /* (non-Javadoc) - * @see java.nio.channels.SelectionKey#interestOps(int) - */ - public SelectionKey interestOps(int ops) - { - if (cancelled) - throw new CancelledKeyException(); - if ((ops & ~(channel.validOps())) != 0) - throw new IllegalArgumentException("unsupported channel ops"); - try - { - selector.epoll_modify(this, ops); - interestOps = ops; - } - catch (IOException ioe) - { - throw new IllegalArgumentException(ioe); - } - return this; - } - - /* (non-Javadoc) - * @see java.nio.channels.SelectionKey#readyOps() - */ - public int readyOps() - { - return selectedOps; - } - - /* (non-Javadoc) - * @see java.nio.channels.SelectionKey#selector() - */ - public Selector selector() - { - return selector; - } -} Deleted: trunk/core/src/classpath/gnu/gnu/java/nio/EpollSelectorImpl.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/nio/EpollSelectorImpl.java 2006-12-14 21:06:58 UTC (rev 2930) +++ trunk/core/src/classpath/gnu/gnu/java/nio/EpollSelectorImpl.java 2006-12-15 01:51:58 UTC (rev 2931) @@ -1,399 +0,0 @@ -/* EpollSelectorImpl.java -- selector implementation using epoll - Copyright (C) 2006 Free Software Foundation, Inc. - -This file is part of GNU Classpath. - -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ - - -package gnu.java.nio; - -import gnu.classpath.Configuration; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.AbstractSelectableChannel; -import java.nio.channels.spi.AbstractSelector; -import java.nio.channels.spi.SelectorProvider; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; - -/** - * An implementation of {@link Selector} that uses the epoll event - * notification mechanism on GNU/Linux. - * - * @author Casey Marshall (cs...@gn...) - */ -public class EpollSelectorImpl extends AbstractSelector -{ - // XXX is this reasonable? Does it matter? - private static final int DEFAULT_EPOLL_SIZE = 128; - private static final int sizeof_struct_epoll_event; - - private static final int OP_ACCEPT = SelectionKey.OP_ACCEPT; - private static final int OP_CONNECT = SelectionKey.OP_CONNECT; - private static final int OP_READ = SelectionKey.OP_READ; - private static final int OP_WRITE = SelectionKey.OP_WRITE; - - /** our epoll file descriptor. */ - private int epoll_fd; - - private final HashMap keys; - private Set selectedKeys; - private Thread waitingThread; - private ByteBuffer events; - - private static final int INITIAL_CAPACITY; - private static final int MAX_DOUBLING_CAPACITY; - private static final int CAPACITY_INCREMENT; - - static - { - if (Configuration.INIT_LOAD_LIBRARY) - System.loadLibrary("javanio"); - - if (epoll_supported()) - sizeof_struct_epoll_event = sizeof_struct(); - else - sizeof_struct_epoll_event = -1; - - INITIAL_CAPACITY = 64 * sizeof_struct_epoll_event; - MAX_DOUBLING_CAPACITY = 1024 * sizeof_struct_epoll_event; - CAPACITY_INCREMENT = 128 * sizeof_struct_epoll_event; - } - - public EpollSelectorImpl(SelectorProvider provider) - throws IOException - { - super(provider); - epoll_fd = epoll_create(DEFAULT_EPOLL_SIZE); - keys = new HashMap(); - selectedKeys = null; - events = ByteBuffer.allocateDirect(INITIAL_CAPACITY); - } - - /* (non-Javadoc) - * @see java.nio.channels.Selector#keys() - */ - public Set keys() - { - return new HashSet(keys.values()); - } - - /* (non-Javadoc) - * @see java.nio.channels.Selector#select() - */ - public int select() throws IOException - { - return doSelect(-1); - } - - /* (non-Javadoc) - * @see java.nio.channels.Selector#select(long) - */ - public int select(long timeout) throws IOException - { - if (timeout > Integer.MAX_VALUE) - throw new IllegalArgumentException("timeout is too large"); - if (timeout < 0) - throw new IllegalArgumentException("invalid timeout"); - return doSelect((int) timeout); - } - - private int doSelect(int timeout) throws IOException - { - synchronized (keys) - { - Set cancelledKeys = cancelledKeys(); - synchronized (cancelledKeys) - { - for (Iterator it = cancelledKeys.iterator(); it.hasNext(); ) - { - EpollSelectionKeyImpl key = (EpollSelectionKeyImpl) it.next(); - epoll_delete(epoll_fd, key.fd); - key.valid = false; - keys.remove(Integer.valueOf(key.fd)); - it.remove(); - deregister(key); - } - - // Clear out closed channels. The fds are removed from the epoll - // fd when closed, so there is no need to remove them manually. - for (Iterator it = keys.values().iterator(); it.hasNext(); ) - { - EpollSelectionKeyImpl key = (EpollSelectionKeyImpl) it.next(); - SelectableChannel ch = key.channel(); - if (ch instanceof VMChannelOwner) - { - if (!((VMChannelOwner) ch).getVMChannel().getState().isValid()) - it.remove(); - } - } - - // Don't bother if we have nothing to select. - if (keys.isEmpty()) - return 0; - - int ret; - try - { - begin(); - waitingThread = Thread.currentThread(); - ret = epoll_wait(epoll_fd, events, keys.size(), timeout); - } - finally - { - Thread.interrupted(); - waitingThread = null; - end(); - } - - HashSet s = new HashSet(ret); - for (int i = 0; i < ret; i++) - { - events.position(i * sizeof_struct_epoll_event); - ByteBuffer b = events.slice(); - int fd = selected_fd(b); - EpollSelectionKeyImpl key - = (EpollSelectionKeyImpl) keys.get(Integer.valueOf(fd)); - if (key == null) - throw new IOException("fd was selected, but no key found"); - key.selectedOps = selected_ops(b) & key.interestOps; - s.add(key); - } - - reallocateBuffer(); - - selectedKeys = s; - return ret; - } - } - } - - /* (non-Javadoc) - * @see java.nio.channels.Selector#selectedKeys() - */ - public Set selectedKeys() - { - if (selectedKeys == null) - return Collections.EMPTY_SET; - return selectedKeys; - } - - /* (non-Javadoc) - * @see java.nio.channels.Selector#selectNow() - */ - public int selectNow() throws IOException - { - return doSelect(0); - } - - /* (non-Javadoc) - * @see java.nio.channels.Selector#wakeup() - */ - public Selector wakeup() - { - try - { - waitingThread.interrupt(); - } - catch (NullPointerException npe) - { - // Ignored, thrown if we are not in a blocking op. - } - return this; - } - - /* (non-Javadoc) - * @see java.nio.channels.spi.AbstractSelector#implCloseSelector() - */ - protected void implCloseSelector() throws IOException - { - VMChannel.close(epoll_fd); - } - - /* (non-Javadoc) - * @see java.nio.channels.spi.AbstractSelector#register(java.nio.channels.spi.AbstractSelectableChannel, int, java.lang.Object) - */ - protected SelectionKey register(AbstractSelectableChannel ch, int ops, Object att) - { - if (!(ch instanceof VMChannelOwner)) - throw new IllegalArgumentException("unsupported channel type"); - - VMChannel channel = ((VMChannelOwner) ch).getVMChannel(); - try - { - int native_fd = channel.getState().getNativeFD(); - synchronized (keys) - { - if (keys.containsKey(Integer.valueOf(native_fd))) - throw new IllegalArgumentException("channel already registered"); - EpollSelectionKeyImpl result = - new EpollSelectionKeyImpl(this, ch, native_fd); - if ((ops & ~(ch.validOps())) != 0) - throw new IllegalArgumentException("invalid ops for channel"); - result.interestOps = ops; - result.selectedOps = 0; - result.valid = true; - result.attach(att); - result.key = System.identityHashCode(result); - epoll_add(epoll_fd, result.fd, ops); - keys.put(Integer.valueOf(native_fd), result); - reallocateBuffer(); - return result; - } - } - catch (IOException ioe) - { - throw new IllegalArgumentException(ioe); - } - } - - private void reallocateBuffer() - { - // Ensure we have enough space for all potential events that may be - // returned. - if (events.capacity() < keys.size() * sizeof_struct_epoll_event) - { - int cap = events.capacity(); - if (cap < MAX_DOUBLING_CAPACITY) - cap <<= 1; - else - cap += CAPACITY_INCREMENT; - events = ByteBuffer.allocateDirect(cap); - } - // Ensure that the events buffer is not too large, given the number of - // events registered. - else if (events.capacity() > keys.size() * sizeof_struct_epoll_event * 2 + 1 - && events.capacity() > INITIAL_CAPACITY) - { - int cap = events.capacity() >>> 1; - events = ByteBuffer.allocateDirect(cap); - } - } - - void epoll_modify(EpollSelectionKeyImpl key, int ops) throws IOException - { - epoll_modify(epoll_fd, key.fd, ops); - } - - /** - * Tell if epoll is supported by this system, and support was compiled in. - * - * @return True if this system supports event notification with epoll. - */ - public static native boolean epoll_supported(); - - - /** - * Returns the size of `struct epoll_event'. - * - * @return The size of `struct epoll_event'. - */ - private static native int sizeof_struct(); - - - /** - * Open a new epoll file descriptor. - * - * @param size The size... [truncated message content] |