Author: mturk Date: Fri Jun 3 19:09:20 2011 New Revision: 1131146 URL: http://svn.apache.org/viewvc?rev=1131146&view=rev Log: Add connect methods
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalDescriptor.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalDescriptor.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalDescriptor.java?rev=1131146&r1=1131145&r2=1131146&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalDescriptor.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalDescriptor.java Fri Jun 3 19:09:20 2011 @@ -23,6 +23,7 @@ import java.io.SyncFailedException; import java.net.SocketException; import org.apache.commons.runtime.Status; import org.apache.commons.runtime.io.ClosedDescriptorException; +import org.apache.commons.runtime.io.InvalidDescriptorException; import org.apache.commons.runtime.io.Descriptor; /** @@ -35,9 +36,11 @@ import org.apache.commons.runtime.io.Des */ final class LocalDescriptor extends Descriptor { - private boolean closed = false; + private boolean xclosed = false; private static native int close0(int fd); private static native int sendz0(int fd); + private static native int socket0(int type) + throws IOException; public LocalDescriptor() { @@ -48,20 +51,23 @@ final class LocalDescriptor extends Desc this.fd = fd; } - public void fd(int fd) + public void create(SocketType type) + throws IOException { - this.fd = fd; - closed = false; + this.fd = socket0(type.valueOf()); + xclosed = false; } @Override public void close() throws IOException { - if (closed) + if (xclosed) throw new ClosedDescriptorException(Local.sm.get("socketd.CLOSED")); - closed = true; - if (fd != -1) { + // Guard against multiple method invocations + // Closing an empty socket is not an error + xclosed = true; + if (valid()) { int rc = close0(fd); fd = -1; if (rc != 0) @@ -73,8 +79,12 @@ final class LocalDescriptor extends Desc public void sync() throws SyncFailedException, IOException { - if (fd == -1) - throw new ClosedDescriptorException(); + if (closed()) { + if (xclosed) + throw new ClosedDescriptorException(); + else + throw new InvalidDescriptorException(); + } int rc = sendz0(fd); if (rc != 0) throw new SocketException(Status.describe(rc)); @@ -84,8 +94,12 @@ final class LocalDescriptor extends Desc public void flush() throws SyncFailedException, IOException { - if (fd == -1) - throw new ClosedDescriptorException(); + if (closed()) { + if (xclosed) + throw new ClosedDescriptorException(); + else + throw new InvalidDescriptorException(); + } } /** Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java?rev=1131146&r1=1131145&r2=1131146&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpoint.java Fri Jun 3 19:09:20 2011 @@ -45,8 +45,6 @@ public class LocalEndpoint extends Endpo private static native int nonblock0(int fd, boolean block); private static native int connect0(int fd, byte[] sa, int timeout); - private static native int socket0(int type) - throws IOException; /** * Creates a new unconnected socket object. @@ -58,29 +56,37 @@ public class LocalEndpoint extends Endpo } /** - * Creates a new socket object from socket descriptor. + * Creates a new connected local endpoint from a local descriptor + * and address. */ public LocalEndpoint(LocalDescriptor sd, EndpointAddress ea) { super(EndpointType.LOCAL); - if (sd == null) + if (sd == null || ea == null) throw new NullPointerException(); - this.sd = sd; - this.ea = ea; + this.sd = sd; + this.ea = ea; + connected = true; } - public void connect(LocalEndpointAddress endpoint, int timeout) + public void connect(EndpointAddress endpoint, int timeout) throws IOException { if (connected) throw new IOException(Local.sm.get("endpoint.ECONNECTED")); - if (sd.fd() == -1) - sd.fd(socket0(SocketType.STREAM.valueOf())); + if (sd.closed()) + sd.create(SocketType.STREAM); int rc = connect0(sd.fd(), endpoint.sockaddr(), timeout); if (rc != 0) throw new IOException(Status.describe(rc)); } + public final void connect(EndpointAddress endpoint) + throws IOException + { + connect(endpoint, -1); + } + @Override public Descriptor descriptor() { Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java?rev=1131146&r1=1131145&r2=1131146&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java Fri Jun 3 19:09:20 2011 @@ -163,6 +163,8 @@ public class LocalServerEndpoint extends { if (bound) throw new IOException(Local.sm.get("endpoint.EBOUND")); + if (sd.closed()) + throw new ClosedDescriptorException(); if (backlog < LISTEN_BACKLOG) backlog = LISTEN_BACKLOG; int rc = bind0(sd.fd(), sa.sockaddr(), backlog); @@ -173,6 +175,8 @@ public class LocalServerEndpoint extends public LocalEndpoint accept() throws IOException { + if (sd.closed()) + throw new ClosedDescriptorException(); byte[] addr = new byte[AbstractAddress.SALEN]; int fd = accept0(sd.fd(), addr); Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java?rev=1131146&r1=1131145&r2=1131146&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java Fri Jun 3 19:09:20 2011 @@ -23,6 +23,7 @@ import java.io.SyncFailedException; import java.net.SocketException; import org.apache.commons.runtime.Status; import org.apache.commons.runtime.io.ClosedDescriptorException; +import org.apache.commons.runtime.io.InvalidDescriptorException; import org.apache.commons.runtime.io.Descriptor; /** @@ -32,9 +33,11 @@ import org.apache.commons.runtime.io.Des final class SocketDescriptor extends Descriptor { - private boolean closed = false; + private boolean xclosed = false; private static native int close0(int fd); private static native int sendz0(int fd); + private static native int socket0(int type) + throws IOException; public SocketDescriptor() { @@ -45,20 +48,23 @@ final class SocketDescriptor extends Des this.fd = fd; } - public void fd(int fd) + public void create(SocketType type) + throws IOException { - this.fd = fd; - closed = false; + this.fd = socket0(type.valueOf()); + xclosed = false; } @Override public void close() throws IOException { - if (closed) + if (xclosed) throw new ClosedDescriptorException(Local.sm.get("socketd.CLOSED")); - closed = true; - if (fd != -1) { + // Guard against multiple method invocations + // Closing an empty socket is not an error + xclosed = true; + if (valid()) { int rc = close0(fd); fd = -1; if (rc != 0) @@ -70,8 +76,12 @@ final class SocketDescriptor extends Des public void sync() throws SyncFailedException, IOException { - if (fd == -1) - throw new ClosedDescriptorException(); + if (closed()) { + if (xclosed) + throw new ClosedDescriptorException(); + else + throw new InvalidDescriptorException(); + } int rc = sendz0(fd); if (rc != 0) throw new SocketException(Status.describe(rc)); @@ -81,8 +91,12 @@ final class SocketDescriptor extends Des public void flush() throws SyncFailedException, IOException { - if (fd == -1) - throw new ClosedDescriptorException(); + if (closed()) { + if (xclosed) + throw new ClosedDescriptorException(); + else + throw new InvalidDescriptorException(); + } } /** Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java?rev=1131146&r1=1131145&r2=1131146&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java Fri Jun 3 19:09:20 2011 @@ -36,12 +36,13 @@ public class SocketEndpoint extends Endp private final SocketDescriptor sd; private EndpointAddress ea; private SelectionKeyImpl key; - private boolean blocking = false; + private boolean blocking = false; + private boolean connected = false; private static native int nonblock0(int fd, boolean block); /** - * Creates a new unconnected socket object. + * Creates a new unconnected socket endpoint. */ public SocketEndpoint() { @@ -50,15 +51,30 @@ public class SocketEndpoint extends Endp } /** - * Creates a new socket object from socket descriptor. + * Creates a new connected socket endpoint from a socket descriptor + * and local address. */ public SocketEndpoint(SocketDescriptor sd, EndpointAddress ea) { super(EndpointType.SOCKET); - if (sd == null) + if (sd == null || ea == null) throw new NullPointerException(); - this.sd = sd; - this.ea = ea; + this.sd = sd; + this.ea = ea; + connected = true; + } + + public void connect(EndpointAddress endpoint, int timeout) + throws IOException + { + if (connected) + throw new IOException(Local.sm.get("endpoint.ECONNECTED")); + } + + public final void connect(EndpointAddress endpoint) + throws IOException + { + connect(endpoint, -1); } @Override Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c?rev=1131146&r1=1131145&r2=1131146&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c Fri Jun 3 19:09:20 2011 @@ -50,7 +50,7 @@ ACR_NET_EXPORT(jint, LocalDescriptor, se return 0; } -ACR_NET_EXPORT(jint, LocalEndpoint, socket0)(JNI_STDARGS, jint stype) +ACR_NET_EXPORT(jint, LocalDescriptor, socket0)(JNI_STDARGS, jint stype) { int sd; int rc = 0;