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/06/07 18:12:32 UTC
svn commit: r1133068 - in /commons/sandbox/runtime/trunk/src/main:
java/org/apache/commons/runtime/net/ native/os/unix/
test/org/apache/commons/runtime/
Author: mturk
Date: Tue Jun 7 16:12:32 2011
New Revision: 1133068
URL: http://svn.apache.org/viewvc?rev=1133068&view=rev
Log:
Add ServerEndpoint and selector factories
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSelectorFactory.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java (with props)
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.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/Selector.java
commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h
commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java?rev=1133068&r1=1133067&r2=1133068&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java Tue Jun 7 16:12:32 2011
@@ -50,7 +50,7 @@ public abstract class Endpoint implement
/**
* Gets the endpoint's type.
*/
- public EndpointType type()
+ public final EndpointType type()
{
return type;
}
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSelectorFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSelectorFactory.java?rev=1133068&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSelectorFactory.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSelectorFactory.java Tue Jun 7 16:12:32 2011
@@ -0,0 +1,88 @@
+/*
+ * 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.InvalidRangeException;
+import org.apache.commons.runtime.OverflowException;
+
+/**
+ * Creates a new local endpoint selector.
+ */
+final class LocalSelectorFactory
+{
+ private static final int type;
+ /**
+ * Maximum size of the selector limited by the operating system
+ */
+ public static final int MAX_CAPACITY;
+ private static native int type0();
+ private static native int size0();
+ static {
+ type = type0();
+ MAX_CAPACITY = size0();
+ }
+
+ /**
+ * Creates a new {@code Selector} instance with the given size.
+ *
+ * @param size selector capacity.
+ * @return the new socket selector.
+ *
+ * @throws InvalidRangeException if {@code size} is outside the
+ * valid range.
+ * @throws RuntimeException if the selector capacity cannot be
+ * determined.
+ * @throws OutOfMemoryError if the memory allocation fails.
+ */
+ public static Selector open(int size)
+ throws InvalidRangeException, RuntimeException, OutOfMemoryError
+ {
+ if (MAX_CAPACITY < 1)
+ throw new RuntimeException();
+ if (size < 1 || size > MAX_CAPACITY)
+ throw new InvalidRangeException(Local.sm.get("selector.ERANGE"));
+ switch (type) {
+ case 0:
+ return new PosixSelector(size);
+ default:
+ throw new RuntimeException(Local.sm.get("selector.ETYPE"));
+ }
+ }
+
+ /**
+ * Creates a new {@code Selector} instance.
+ *
+ * @return the new socket selector.
+ *
+ * @throws RuntimeException if the selector capacity cannot be
+ * determined.
+ * @throws OutOfMemoryError if the memory allocation fails.
+ */
+ public static Selector open()
+ throws RuntimeException, OutOfMemoryError
+ {
+ return open(MAX_CAPACITY);
+ }
+
+ private LocalSelectorFactory()
+ {
+ // No instance
+ }
+
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSelectorFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
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=1133068&r1=1133067&r2=1133068&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 Tue Jun 7 16:12:32 2011
@@ -35,12 +35,12 @@ import org.apache.commons.runtime.Status
* depending on the operating system.
* </p>
*/
-public class LocalServerEndpoint extends Endpoint
+public class LocalServerEndpoint extends ServerEndpoint<LocalEndpoint>
{
private static final int LISTEN_BACKLOG = 50;
private final LocalDescriptor sd;
private SelectionKeyImpl key;
- private LocalEndpointAddress sa;
+ private EndpointAddress sa;
private boolean blocking = false;
private boolean bound = false;
@@ -161,7 +161,8 @@ public class LocalServerEndpoint extends
private static native int accept0(int fd, byte[] addr)
throws SocketException;
- public void bind(LocalEndpointAddress endpoint, int backlog)
+ @Override
+ public void bind(EndpointAddress endpoint, int backlog)
throws IOException
{
if (bound)
@@ -179,12 +180,7 @@ public class LocalServerEndpoint extends
sa = endpoint;
}
- public void bind(LocalEndpointAddress endpoint)
- throws IOException
- {
- bind(endpoint, LISTEN_BACKLOG);
- }
-
+ @Override
public LocalEndpoint accept()
throws IOException
{
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java?rev=1133068&r1=1133067&r2=1133068&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java Tue Jun 7 16:12:32 2011
@@ -30,23 +30,16 @@ import org.apache.commons.runtime.Overfl
*/
public abstract class Selector implements Closeable
{
- private int capacity;
- private static final int type;
- /**
- * Maximum size of the selector limited by the operating system
- */
- public static final int MAX_CAPACITY;
- private static native int init0();
- private static native int init1();
- static {
- type = init0();
- MAX_CAPACITY = init1();
- }
-
/**
* Indicates the current auto-cancel mode for this {@code Selector} object.
*/
protected boolean autoCancel;
+ private int capacity;
+
+ private Selector()
+ {
+ // No instance
+ }
/**
* Creates a new {@code Selector} instance with the given size.
@@ -56,19 +49,17 @@ public abstract class Selector implement
*
* @throws InvalidRangeException if {@code size} is outside the
* valid range.
+ * @throws RuntimeException if the selector capacity cannot be
+ * determined.
* @throws OutOfMemoryError if the memory allocation fails.
*/
- public static Selector newInstance(int size)
- throws InvalidRangeException, OutOfMemoryError
+ public static Selector open(EndpointType type, int size)
+ throws InvalidRangeException, RuntimeException, OutOfMemoryError
{
- if (size < 1 || size > MAX_CAPACITY)
- throw new InvalidRangeException(Local.sm.get("selector.ERANGE"));
- switch (type) {
- case 0:
- return new PosixSelector(size);
- default:
- throw new RuntimeException(Local.sm.get("selector.ETYPE"));
- }
+ if (type == EndpointType.LOCAL)
+ return LocalSelectorFactory.open(size);
+ else
+ return SocketSelectorFactory.open(size);
}
/**
@@ -80,17 +71,24 @@ public abstract class Selector implement
* determined.
* @throws OutOfMemoryError if the memory allocation fails.
*/
- public static Selector newInstance()
+ public static Selector open(EndpointType type)
throws RuntimeException, OutOfMemoryError
{
- if (MAX_CAPACITY < 1)
- throw new RuntimeException();
- return newInstance(MAX_CAPACITY);
+ if (type == EndpointType.LOCAL)
+ return LocalSelectorFactory.open();
+ else
+ return SocketSelectorFactory.open();
}
- private Selector()
+ /**
+ * Gets the maximum capacity for the given selector type.
+ */
+ public static int maxCapacity(EndpointType type)
{
- // No instance
+ if (type == EndpointType.LOCAL)
+ return LocalSelectorFactory.MAX_CAPACITY;
+ else
+ return SocketSelectorFactory.MAX_CAPACITY;
}
/**
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java?rev=1133068&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java Tue Jun 7 16:12:32 2011
@@ -0,0 +1,58 @@
+/*
+ * 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 java.io.Closeable;
+import java.io.Flushable;
+import java.io.IOException;
+import java.io.SyncFailedException;
+import java.net.SocketException;
+import org.apache.commons.runtime.io.ClosedDescriptorException;
+import org.apache.commons.runtime.io.Descriptor;
+import org.apache.commons.runtime.OverflowException;
+import org.apache.commons.runtime.Status;
+
+/**
+ * This class represents a server endpoint.
+ */
+public abstract class ServerEndpoint<E> extends Endpoint
+{
+
+ /**
+ * Creates a new local server endpoint from the
+ * given socket descriptor.
+ */
+ protected ServerEndpoint(EndpointType type)
+ {
+ super(type);
+ }
+
+ public abstract E accept()
+ throws IOException;
+
+ public abstract void bind(EndpointAddress endpoint, int backlog)
+ throws IOException;
+
+ public final void bind(EndpointAddress endpoint)
+ throws IOException
+ {
+ bind(endpoint, 0);
+ }
+
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java?rev=1133068&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java Tue Jun 7 16:12:32 2011
@@ -0,0 +1,88 @@
+/*
+ * 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.InvalidRangeException;
+import org.apache.commons.runtime.OverflowException;
+
+/**
+ * Creates a new socket endpoint selector.
+ */
+final class SocketSelectorFactory
+{
+ private static final int type;
+ /**
+ * Maximum size of the selector limited by the operating system
+ */
+ public static final int MAX_CAPACITY;
+ private static native int type0();
+ private static native int size0();
+ static {
+ type = type0();
+ MAX_CAPACITY = size0();
+ }
+
+ /**
+ * Creates a new {@code Selector} instance with the given size.
+ *
+ * @param size selector capacity.
+ * @return the new socket selector.
+ *
+ * @throws InvalidRangeException if {@code size} is outside the
+ * valid range.
+ * @throws RuntimeException if the selector capacity cannot be
+ * determined.
+ * @throws OutOfMemoryError if the memory allocation fails.
+ */
+ public static Selector open(int size)
+ throws InvalidRangeException, RuntimeException, OutOfMemoryError
+ {
+ if (MAX_CAPACITY < 1)
+ throw new RuntimeException();
+ if (size < 1 || size > MAX_CAPACITY)
+ throw new InvalidRangeException(Local.sm.get("selector.ERANGE"));
+ switch (type) {
+ case 0:
+ return new PosixSelector(size);
+ default:
+ throw new RuntimeException(Local.sm.get("selector.ETYPE"));
+ }
+ }
+
+ /**
+ * Creates a new {@code Selector} instance.
+ *
+ * @return the new socket selector.
+ *
+ * @throws RuntimeException if the selector capacity cannot be
+ * determined.
+ * @throws OutOfMemoryError if the memory allocation fails.
+ */
+ public static Selector open()
+ throws RuntimeException, OutOfMemoryError
+ {
+ return open(MAX_CAPACITY);
+ }
+
+ private SocketSelectorFactory()
+ {
+ // No instance
+ }
+
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h?rev=1133068&r1=1133067&r2=1133068&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h Tue Jun 7 16:12:32 2011
@@ -144,5 +144,16 @@ typedef struct stat struct_stat_
#else
#endif
+/**
+ * Figure out the default local socket
+ * selector. On unixes this is always
+ * the same as for standard sockets since
+ * sockets == files
+ */
+#if 1
+# define PS_DEFAULT_LOCAL PS_DEFAULT_TYPE
+#else
+#endif
+
#endif /* _ACR_ARCH_DEFS_H_ */
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c?rev=1133068&r1=1133067&r2=1133068&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c Tue Jun 7 16:12:32 2011
@@ -88,12 +88,17 @@ static short reventt(short event)
return rv;
}
-ACR_NET_EXPORT(jint, Selector, init0)(JNI_STDARGS)
+ACR_NET_EXPORT(jint, SocketSelectorFactory, type0)(JNI_STDARGS)
{
return PS_DEFAULT_TYPE;
}
-ACR_NET_EXPORT(jint, Selector, init1)(JNI_STDARGS)
+ACR_NET_EXPORT(jint, LocalSelectorFactory, type0)(JNI_STDARGS)
+{
+ return PS_DEFAULT_LOCAL;
+}
+
+static int rlimit_nofile(void)
{
int nm = 65536;
#if HAVE_SYS_RESOURCE_H
@@ -111,6 +116,16 @@ ACR_NET_EXPORT(jint, Selector, init1)(JN
return nm;
}
+ACR_NET_EXPORT(jint, SocketSelectorFactory, size0)(JNI_STDARGS)
+{
+ return rlimit_nofile();
+}
+
+ACR_NET_EXPORT(jint, LocalSelectorFactory, size0)(JNI_STDARGS)
+{
+ return rlimit_nofile();
+}
+
ACR_NET_EXPORT(jlong, PosixSelector, create0)(JNI_STDARGS, jint size)
{
int rc;
Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java?rev=1133068&r1=1133067&r2=1133068&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java Tue Jun 7 16:12:32 2011
@@ -32,7 +32,7 @@ public class TestSelectionKey extends As
// Create socket bound to the first free port
//
SocketEndpoint ep = new SocketEndpoint();
- Selector ss = Selector.newInstance();
+ Selector ss = Selector.open(EndpointType.SOCKET);
assertNotNull(ss);
System.out.println("SocketSelector capacity=" + ss.capacity());
ss.close();