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/07/14 18:38:52 UTC
svn commit: r1146783 - in
/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net:
ConnectedEndpoint.java Endpoint.java LocalEndpoint.java
LocalServerEndpoint.java LocalStrings.properties ServerEndpoint.java
SocketEndpoint.java
Author: mturk
Date: Thu Jul 14 16:38:51 2011
New Revision: 1146783
URL: http://svn.apache.org/viewvc?rev=1146783&view=rev
Log:
Add abstract class for connection type endpoints
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ConnectedEndpoint.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/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/LocalStrings.properties
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ConnectedEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ConnectedEndpoint.java?rev=1146783&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ConnectedEndpoint.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ConnectedEndpoint.java Thu Jul 14 16:38:51 2011
@@ -0,0 +1,72 @@
+/*
+ * 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.IOException;
+import java.net.SocketException;
+import org.apache.commons.runtime.io.ClosedDescriptorException;
+import org.apache.commons.runtime.io.Descriptor;
+import org.apache.commons.runtime.io.Stream;
+
+/**
+ * This class represents a connected network endpoint.
+ */
+public abstract class ConnectedEndpoint extends Endpoint
+{
+ /**
+ * Creates a new ConnectedEndpoint object.
+ */
+ protected ConnectedEndpoint(EndpointType type)
+ {
+ super(type);
+ }
+
+ /**
+ * Shut down part of a full-duplex connection.
+ * Any further data read or sent to this
+ * socket will be discarded depending on the how flag.
+ *
+ * @param how which part of a connection to shut down.
+ * @throws IOException
+ * if an error occurs while closing the socket stream.
+ */
+ public abstract void shutdown(ShutdownHow how)
+ throws IOException;
+
+ /**
+ * Gets a bidirectional stream to read or write data from this connection.
+ *
+ * @return the byte-oriented bidirectional stream.
+ * @throws IOException
+ * if an error occurs while creating the input stream or the
+ * endpoint is in an invalid state.
+ */
+ public abstract Stream getStream()
+ throws IOException;
+
+ /**
+ * Returns whether this endpoint is connected to a remote host.
+ *
+ * @return {@code true} if the endpoint is connected,
+ * {@code false} otherwise.
+ */
+ public abstract boolean connected();
+
+
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ConnectedEndpoint.java
------------------------------------------------------------------------------
svn:eol-style = native
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=1146783&r1=1146782&r2=1146783&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 Thu Jul 14 16:38:51 2011
@@ -235,18 +235,11 @@ public abstract class Endpoint implement
throws IOException;
/**
- * Shutdown a part of full-duplex connection.
+ * Returns whether this endpoint is closed.
*
- * @param how How to shutdown the enpoint
- *
- * @throws IOException
- * if an error occurs while shutting down the endpoint.
+ * @return {@code true} if the endpoint is closed,
+ * {@code false} otherwise.
*/
- public abstract void shutdown(ShutdownHow how)
- throws IOException;
-
- public abstract Stream getStream()
- throws IOException;
+ public abstract boolean closed();
- public abstract boolean connected();
}
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=1146783&r1=1146782&r2=1146783&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 Thu Jul 14 16:38:51 2011
@@ -40,12 +40,14 @@ import org.apache.commons.runtime.Timeou
* depending on the operating system.
* </p>
*/
-public class LocalEndpoint extends Endpoint
+public class LocalEndpoint extends ConnectedEndpoint
{
private final LocalDescriptor sd;
private EndpointAddress ea;
private SelectionKeyImpl key;
private boolean connected = false;
+ private boolean canread = true;
+ private boolean canwrite = true;
private SocketStream stream = null;
private static native int connect0(long fd, byte[] sa, int timeout);
@@ -193,16 +195,35 @@ public class LocalEndpoint extends Endpo
throws IOException
{
sd.shutdown(how);
+ if (how == ShutdownHow.RDWR || how == ShutdownHow.READ)
+ canread = false;
+ if (how == ShutdownHow.RDWR || how == ShutdownHow.WRITE)
+ canwrite = false;
+ }
+
+ @Override
+ public synchronized final boolean closed()
+ {
+ return sd.closed();
}
@Override
public final Stream getStream()
throws IOException
{
- if (stream != null)
+ if (canread || canwrite) {
+ if (stream != null)
+ return stream;
+ stream = new SocketStream(sd);
return stream;
- stream = new SocketStream(sd);
- return stream;
+ }
+ else {
+ /* We only provide bidirectional streams
+ * unlike Java Socket api which has input and
+ * output streams separated.
+ */
+ throw new IOException(Local.sm.get("endpoint.SHUTRDWR"));
+ }
}
@Override
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=1146783&r1=1146782&r2=1146783&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 Thu Jul 14 16:38:51 2011
@@ -208,4 +208,10 @@ public class LocalServerEndpoint extends
blocking = false;
}
+ @Override
+ public synchronized final boolean closed()
+ {
+ return sd.closed();
+ }
+
}
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=1146783&r1=1146782&r2=1146783&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 Thu Jul 14 16:38:51 2011
@@ -21,3 +21,4 @@ selector.ETYPE=Unknown Selector type
endpoint.EBOUND=Endpoint is already bound
endpoint.ECONNECTED=Endpoint is already connected
endpoint.ENOTCONN=Endpoint is not connected
+endpoint.SHUTRDWR=Socket is shutdown
Modified: 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=1146783&r1=1146782&r2=1146783&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ServerEndpoint.java Thu Jul 14 16:38:51 2011
@@ -44,9 +44,31 @@ public abstract class ServerEndpoint<E>
super(type);
}
+ /**
+ * Accept a connection on a endpoint.
+ * This method returns a endpoint object representing the just
+ * opened connection.
+ *
+ * @return the connected endpoint.
+ * @throws IOException
+ * if an error occurs while accepting a new connection.
+ */
public abstract E accept()
throws IOException;
+ /**
+ * Accept a connection on a endpoint and sets the blocking mode
+ * for the new connection.
+ * This method returns a endpoint object representing the just
+ * opened connection and sets the blocking mode for that new
+ * connected endpoint.
+ *
+ * @param blocking {@code true} if the accepted connection
+ * will be in blocking mode.
+ * @return the connected endpoint.
+ * @throws IOException
+ * if an error occurs while accepting a new connection.
+ */
public abstract E accept(boolean blocking)
throws IOException;
@@ -58,29 +80,4 @@ public abstract class ServerEndpoint<E>
{
bind(endpoint, 0);
}
-
- @Override
- public final void shutdown(ShutdownHow how)
- throws IOException
- {
- // Shutdown is valid for connection oriented endpoints
- // Throw an exception.
- throw new IOException(Local.sm.get("endpoint.ENOTCONN"));
- }
-
- @Override
- public final Stream getStream()
- throws IOException
- {
- // Stream can be used only for connection oriented endpoints.
- // Throw an exception.
- throw new IOException(Local.sm.get("endpoint.ENOTCONN"));
- }
-
- @Override
- public boolean connected()
- {
- return false;
- }
-
}
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=1146783&r1=1146782&r2=1146783&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 Thu Jul 14 16:38:51 2011
@@ -32,7 +32,7 @@ import org.apache.commons.runtime.Status
/**
* This class represents a socket endpoint.
*/
-public class SocketEndpoint extends Endpoint
+public class SocketEndpoint extends ConnectedEndpoint
{
private final SocketDescriptor sd;
private SocketAddress ea; // Remote address
@@ -168,6 +168,12 @@ public class SocketEndpoint extends Endp
}
@Override
+ public synchronized final boolean closed()
+ {
+ return sd.closed();
+ }
+
+ @Override
public synchronized void shutdown(ShutdownHow how)
throws IOException
{