Author: mturk Date: Fri Jun 3 09:56:19 2011 New Revision: 1130968 URL: http://svn.apache.org/viewvc?rev=1130968&view=rev Log: Add SocketType and connect method
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketType.java (with props) 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/LocalStrings.properties commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ProtocolType.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketDescriptor.java commons/sandbox/runtime/trunk/src/main/native/configure 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=1130968&r1=1130967&r2=1130968&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 09:56:19 2011 @@ -35,7 +35,7 @@ import org.apache.commons.runtime.io.Des */ final class LocalDescriptor extends Descriptor { - + private boolean closed = false; private static native int close0(int fd); private static native int sendz0(int fd); @@ -48,15 +48,25 @@ final class LocalDescriptor extends Desc this.fd = fd; } + public void fd(int fd) + { + this.fd = fd; + closed = false; + } + @Override public void close() throws IOException { - if (fd == -1) + if (closed) throw new ClosedDescriptorException(Local.sm.get("socketd.CLOSED")); - int rc = close0(fd); - if (rc != 0) - throw new SocketException(Status.describe(fd)); + closed = true; + if (fd != -1) { + int rc = close0(fd); + fd = -1; + if (rc != 0) + throw new SocketException(Status.describe(rc)); + } } @Override @@ -67,7 +77,7 @@ final class LocalDescriptor extends Desc throw new ClosedDescriptorException(); int rc = sendz0(fd); if (rc != 0) - throw new SocketException(Status.describe(fd)); + throw new SocketException(Status.describe(rc)); } @Override 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=1130968&r1=1130967&r2=1130968&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 09:56:19 2011 @@ -40,9 +40,13 @@ public class LocalEndpoint extends Endpo private final LocalDescriptor 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); + 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. @@ -65,6 +69,18 @@ public class LocalEndpoint extends Endpo this.ea = ea; } + public void connect(LocalEndpointAddress 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())); + int rc = connect0(sd.fd(), endpoint.sockaddr(), timeout); + if (rc != 0) + throw new IOException(Status.describe(rc)); + } + @Override public Descriptor descriptor() { Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties?rev=1130968&r1=1130967&r2=1130968&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties Fri Jun 3 09:56:19 2011 @@ -19,3 +19,4 @@ selector.NULL=Selector can't be null selector.ERANGE=Selector size is outsize allowed range selector.ETYPE=Unknown Selector type endpoint.EBOUND=Endpoint is already bound +endpoint.ECONNECTED=Endpoint is already connected Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ProtocolType.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ProtocolType.java?rev=1130968&r1=1130967&r2=1130968&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ProtocolType.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ProtocolType.java Fri Jun 3 09:56:19 2011 @@ -16,7 +16,7 @@ package org.apache.commons.runtime.net; -/** Represents the protocol family which will be used for communication. +/** Represents the protocol type which will be used for communication. */ public enum ProtocolType { @@ -24,7 +24,7 @@ public enum ProtocolType UNSPEC( 0), /** Transmission Control Protocol (TCP). */ TCP( 1), - /** User Datagrap Protocol */ + /** User Datagram Protocol */ UDP( 2), /** Raw Internet Protocol */ IP( 3); 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=1130968&r1=1130967&r2=1130968&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 09:56:19 2011 @@ -32,6 +32,7 @@ import org.apache.commons.runtime.io.Des final class SocketDescriptor extends Descriptor { + private boolean closed = false; private static native int close0(int fd); private static native int sendz0(int fd); @@ -44,15 +45,25 @@ final class SocketDescriptor extends Des this.fd = fd; } + public void fd(int fd) + { + this.fd = fd; + closed = false; + } + @Override public void close() throws IOException { - if (fd == -1) + if (closed) throw new ClosedDescriptorException(Local.sm.get("socketd.CLOSED")); - int rc = close0(fd); - if (rc != 0) - throw new SocketException(Status.describe(rc)); + closed = true; + if (fd != -1) { + int rc = close0(fd); + fd = -1; + if (rc != 0) + throw new SocketException(Status.describe(rc)); + } } @Override Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketType.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketType.java?rev=1130968&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketType.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketType.java Fri Jun 3 09:56:19 2011 @@ -0,0 +1,61 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.runtime.net; + +/** + * Represents the socket type which will be used for communication. + */ +public enum SocketType +{ + /** Unspecified socket type */ + UNSPEC( 0), + /** + * Provides sequenced, reliable, two-way, connection-based byte streams. + * An out-of-band data transmission mechanism may be supported. + */ + STREAM( 1), + /** + * Supports datagrams. + * Datagram are connectionless, unreliable messages of + * a fixed maximum length. + */ + DATAGRAM( 2), + /** Provides raw network protocol access. */ + RAW( 3); + + + private int value; + private SocketType(int v) + { + value = v; + } + + public int valueOf() + { + return value; + } + + public static SocketType valueOf(int value) + { + for (SocketType e : values()) { + if (e.value == value) + return e; + } + return UNSPEC; + } + +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketType.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/sandbox/runtime/trunk/src/main/native/configure URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/configure?rev=1130968&r1=1130967&r2=1130968&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/configure (original) +++ commons/sandbox/runtime/trunk/src/main/native/configure Fri Jun 3 09:56:19 2011 @@ -1068,9 +1068,9 @@ EOF echo $rc } -have_ipv6() +have_socket() { - do_printf 'Checking for %-32s' "ipv6 support" + do_printf 'Checking for %-32s' "$3" cat > $cccsrc.c << EOF #include <stdio.h> #include <stdlib.h> @@ -1079,7 +1079,7 @@ have_ipv6() #include <sys/socket.h> int rc = 0; int main () { -if(socket(AF_INET6,SOCK_STREAM,0)>= 0)rc = 1;printf("%d", rc);return 0; +if(socket($1,$2,0)>= 0)rc = 1;printf("%d", rc);return 0; } EOF rc=`test_compile z 0` @@ -1378,7 +1378,7 @@ extern "C" { #define HAVE_GETHOSTENT_R `have_function x gethostent_r` #define HAVE_GETSERVBYNAME_R `have_function x getservbyname_r` #define HAVE_GETIFADDRS `have_function x getifaddrs` -#define HAVE_SOCK_CLOEXEC `have_defined SOCK_CLOEXEC` +#define HAVE_SOCK_CLOEXEC `have_socket AF_INET 'SOCK_STREAM|SOCK_CLOEXEC' SOCK_CLOEXEC` #define HAVE_FILE_CLOEXEC `have_defined O_CLOEXEC` #define HAVE_SO_ACCEPTFILTER `have_defined SO_ACCEPTFILTER` #define HAVE_TM_TM_GMTOFF `have_strcut_member time 'struct tm' tm_gmtoff` @@ -1401,7 +1401,7 @@ extern "C" { #define HAVE_FUNC `have_defined __func__` #define HAVE_POSIX_SEMAPHORE $have_posixsem #define HAVE_THREAD_LOCAL `have_thread_local` -#define HAVE_IPV6 `have_ipv6` +#define HAVE_IPV6 `have_socket AF_INET6 SOCK_STREAM 'ipv6 support'` #define HAVE_OPENSSL $have_openssl #define HAVE_OPENSSL_STATIC $have_openssl_static 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=1130968&r1=1130967&r2=1130968&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 09:56:19 2011 @@ -50,6 +50,46 @@ ACR_NET_EXPORT(jint, LocalDescriptor, se return 0; } +ACR_NET_EXPORT(jint, LocalEndpoint, socket0)(JNI_STDARGS, jint stype) +{ + int sd; + int rc = 0; + int type = 0; + + switch (stype) { + case 1: + type = SOCK_STREAM; + break; + case 2: + type = SOCK_DGRAM; + break; + case 3: + type = SOCK_RAW; + break; + + } +#if !HAVE_SOCK_CLOEXEC + sd = socket(AF_LOCAL, type, 0); +#else + sd = socket(AF_LOCAL, type | SOCK_CLOEXEC, 0); +#endif + if (sd == -1) + rc = errno; +#if !HAVE_SOCK_CLOEXEC + else { + rc = AcrCloseOnExec(sd, 1); + if (rc != 0) { + r_close(sd); + sd = -1; + } + } +#endif + if (rc != 0) { + ACR_THROW_NET_ERROR(rc); + } + return sd; +} + ACR_NET_EXPORT(jint, LocalEndpoint, nonblock0)(JNI_STDARGS, jint fd, jboolean on) { return AcrNonblock(fd, on); @@ -66,9 +106,11 @@ ACR_NET_EXPORT(jint, LocalEndpoint, conn rc = connect(fd, (const struct sockaddr *)&ca->sa.sin, ca->salen); } while (rc == -1 && errno == EINTR); - if (rc == -1) { + if (rc == -1) rc = errno; - if (timeout > 0 && (errno == EINPROGRESS || errno == EALREADY)) { + SOCKADDR_RELEASE(cb, ca); + if (rc != 0) { + if (timeout > 0 && (rc == EINPROGRESS || rc == EALREADY)) { rc = AcrWaitIO(fd, timeout, POLLOUT); #if defined(SO_ERROR) if (rc == 0) { @@ -82,7 +124,6 @@ ACR_NET_EXPORT(jint, LocalEndpoint, conn #endif } } - SOCKADDR_RELEASE(cb, ca); return rc; } @@ -93,14 +134,11 @@ ACR_NET_EXPORT(jint, LocalServerEndpoint int rc = 0; acr_sockaddr_t *aa = SOCKADDR_CAST(ba); - if (bind(fd, (const struct sockaddr *)&aa->sa, aa->salen) == -1) { - rc = ACR_GET_NETOS_ERROR(); - SOCKADDR_RELEASE(ba, aa); - return rc; - } - if (listen(fd, backlog) == -1) - rc = ACR_GET_NETOS_ERROR(); + if (bind(fd, (const struct sockaddr *)&aa->sa, aa->salen) == -1) + rc = errno; SOCKADDR_RELEASE(ba, aa); + if (rc == 0 && listen(fd, backlog) == -1) + rc = errno; return rc; } @@ -127,7 +165,14 @@ ACR_NET_EXPORT(jint, LocalServerEndpoint return -1; } #if !HAVE_ACCEPT4 - AcrCloseOnExec(sd, 1); + { + int rc = AcrCloseOnExec(sd, 1); + if (rc != 0) { + r_close(sd); + ACR_THROW_NET_ERROR(rc); + return -1; + } + } #endif #if defined(DEBUG) || defined(_DEBUG) if (aa.sa.unx.sun_family != AF_LOCAL) { @@ -144,6 +189,5 @@ ACR_NET_EXPORT(jint, LocalServerEndpoint * Note that byte array must be of valid size */ (*env)->SetByteArrayRegion(env, ba, 0, ISIZEOF(acr_sockaddr_t), (jbyte *)&aa); - return sd; }