You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/05/20 11:12:14 UTC

svn commit: r1125287 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/ native/os/unix/

Author: mturk
Date: Fri May 20 09:12:13 2011
New Revision: 1125287

URL: http://svn.apache.org/viewvc?rev=1125287&view=rev
Log:
Add Poll wrapper class

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet4SocketAddress.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet6SocketAddress.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.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/SelectionEvent.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Service.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet4SocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet4SocketAddress.java?rev=1125287&r1=1125286&r2=1125287&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet4SocketAddress.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet4SocketAddress.java Fri May 20 09:12:13 2011
@@ -42,7 +42,7 @@ public final class Inet4SocketAddress ex
     {
         super();
         if (hostname == null || hostname.length() == 0)
-            throw new IllegalArgumentException("invalid hostname");
+            throw new IllegalArgumentException(Local.sm.get("addr.EHOSTNAME"));
         byte[] sa = SocketAddressImpl.getnameinfo(AddressFamily.INET, hostname, null, port);
         SocketAddressImpl.sockaddr(this, sa);        
     }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet6SocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet6SocketAddress.java?rev=1125287&r1=1125286&r2=1125287&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet6SocketAddress.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet6SocketAddress.java Fri May 20 09:12:13 2011
@@ -41,7 +41,7 @@ public final class Inet6SocketAddress ex
         throws OutOfMemoryError, NetworkException
     {
         if (hostname == null || hostname.length() == 0)
-            throw new IllegalArgumentException("invalid hostname");
+            throw new IllegalArgumentException(Local.sm.get("addr.EHOSTNAME"));
         byte[] sa = SocketAddressImpl.getnameinfo(AddressFamily.INET6, hostname, null, port);
         SocketAddressImpl.sockaddr(this, sa);
     }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java?rev=1125287&r1=1125286&r2=1125287&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java Fri May 20 09:12:13 2011
@@ -23,6 +23,7 @@ import java.net.UnknownHostException;
 import org.apache.commons.runtime.Memory;
 import org.apache.commons.runtime.Status;
 import org.apache.commons.runtime.SystemException;
+import org.apache.commons.runtime.InvalidArgumentException;
 
 /**
  * This class represents a Internet Protocol (IP) socket endpoint
@@ -47,9 +48,11 @@ public class InetSocketAddress extends S
     }
 
     public InetSocketAddress(String hostname, int port)
-        throws OutOfMemoryError, NetworkException
+        throws OutOfMemoryError, NetworkException, InvalidArgumentException
     {
         super();
+        if (port < 0 || port > 65535)
+            throw new InvalidArgumentException(Local.sm.get("port.ERANGE"));
         byte[] sa = SocketAddressImpl.getnameinfo(AddressFamily.UNSPEC, hostname, null, port);
         SocketAddressImpl.sockaddr(this, sa);
     }
@@ -61,8 +64,10 @@ public class InetSocketAddress extends S
     }
 
     public static final SocketAddress[] getAll(String hostname, int port)
-        throws OutOfMemoryError, NetworkException
+        throws OutOfMemoryError, NetworkException, InvalidArgumentException
     {        
+        if (port < 0 || port > 65535)
+            throw new InvalidArgumentException(Local.sm.get("port.ERANGE"));
         byte[] sa = SocketAddressImpl.getnameinfo(AddressFamily.UNSPEC, hostname, null, port);
         int n = sa.length / sasize;
         SocketAddress[] a = new SocketAddress[n];

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=1125287&r1=1125286&r2=1125287&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 May 20 09:12:13 2011
@@ -12,3 +12,5 @@
 # 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.
+addr.EHOSTNAME=Invalid hostname
+port.ERANGE=Port is outside allowed range

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java?rev=1125287&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java Fri May 20 09:12:13 2011
@@ -0,0 +1,95 @@
+/*
+ * 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;
+
+import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.InvalidArgumentException;
+import org.apache.commons.runtime.io.InvalidDescriptorException;
+
+/**
+ * Wait for some event on socket descriptor.
+ */
+public final class Poll
+{
+    private Poll()
+    {
+        // No instance
+    }
+
+    /**
+     * There is data to read
+     */
+    public static final short     POLLIN        = 0x0001;
+    /**
+     * There is urgent data to read.
+     * e.g., out-of-band data on TCP socket; pseudo-terminal master in  packet  mode
+     * has seen state change in slave.
+     */
+    public static final short     POLLPRI       = 0x0002;
+    /**
+     * Writing now will not block.
+     */
+    public static final short     POLLOUT       = 0x0004;
+    /**
+     * An error has occurred.
+     */
+    public static final short     POLLERR       = 0x0008;
+    /**
+     * A stream-oriented connection was either diconnected or aborted.
+     */
+    public static final short     POLLHUP       = 0x0010;
+    /**
+     * An invalid descriptor was used.
+     */
+    public static final short     POLLNVAL      = 0x0020;
+    /**
+     * Time-To-Live event occurred.
+     */
+    public static final short     POLLTTL       = 0x0040;
+
+
+    private static native int wait0(int[] fds, short[] events, short[] revents,
+                                    int nelts, int timeout)
+        throws OutOfMemoryError, InvalidArgumentException,
+               InvalidDescriptorException;
+    private static native short wait1(int fd, short events, int timeout);
+
+    public static short wait(int fd, short events, int timeout)
+        throws OutOfMemoryError,
+               InvalidArgumentException,
+               InvalidDescriptorException
+    {
+        if (fd == -1 || (fd >= 0 && fd <= 3))
+            throw new InvalidDescriptorException();
+        if ((events & 0x00ff) == 0)
+            throw new InvalidArgumentException();
+        short se = wait1(fd, events, timeout);
+        if (se == -1)
+            throw new OutOfMemoryError();
+        return se;
+    }
+
+    public static short wait(int fd, short events)
+        throws OutOfMemoryError,
+               InvalidArgumentException,
+               InvalidDescriptorException
+    {
+        return wait(fd, events, -1);
+    }
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionEvent.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionEvent.java?rev=1125287&r1=1125286&r2=1125287&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionEvent.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionEvent.java Fri May 20 09:12:13 2011
@@ -62,7 +62,14 @@ public enum SelectionEvent
      * This is output only flag.
      * </p>
      */
-    ERROR(      0x40);
+    ERROR(      0x40),
+    /** Timeout condition happened on the associated descriptor.
+     * <p>
+     * This is output only flag and indicates that Time-To-Live
+     * event occurred.
+     * </p>
+     */
+    TIMEOUT(    0x80);
     
 
     private int value;

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Service.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Service.java?rev=1125287&r1=1125286&r2=1125287&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Service.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Service.java Fri May 20 09:12:13 2011
@@ -109,7 +109,7 @@ public final class Service
                UnknownServiceException
     {
         if (port < 0 || port > 65535)
-            throw new InvalidArgumentException();
+            throw new InvalidArgumentException(Local.sm.get("port.ERANGE"));
         int rc = getservbyport((char)port, null);
         if (rc != 0)
             throw new UnknownServiceException(Status.describe(rc));
@@ -135,7 +135,7 @@ public final class Service
         if (protocol == null)
             throw new NullPointerException();
         if (port < 0 || port > 65535)
-            throw new InvalidArgumentException();
+            throw new InvalidArgumentException(Local.sm.get("port.ERANGE"));
         int rc = getservbyport((char)port, protocol);
         if (rc != 0)
             throw new UnknownServiceException(Status.describe(rc));

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c?rev=1125287&r1=1125286&r2=1125287&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c Fri May 20 09:12:13 2011
@@ -389,7 +389,7 @@ cleanup:
     return rc;
 }
 
-ACR_UNX_EXPORT(jint, Poll, poll0)(JNI_STDARGS, jintArray fdset,
+ACR_NET_EXPORT(jint, Poll, wait0)(JNI_STDARGS, jintArray fdset,
                                   jshortArray events, jshortArray revents,
                                   jint nevents, jint timeout)
 {
@@ -451,8 +451,8 @@ finally:
     return ns;
 }
 
-ACR_UNX_EXPORT(jint, Poll, poll1)(JNI_STDARGS, jint fd,
-                                  jshort events, jint timeout)
+ACR_NET_EXPORT(jshort, Poll, wait1)(JNI_STDARGS, jint fd,
+                                    jshort events, jint timeout)
 {
     int ns;
     struct pollfd pfd;