You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by tr...@apache.org on 2007/09/17 15:25:57 UTC
svn commit: r576419 - in /mina/trunk/core/src/main/java/org/apache/mina:
common/AbstractIoAcceptor.java common/AbstractIoService.java
transport/socket/nio/DatagramAcceptor.java
transport/socket/nio/SocketAcceptor.java
Author: trustin
Date: Mon Sep 17 06:25:56 2007
New Revision: 576419
URL: http://svn.apache.org/viewvc?rev=576419&view=rev
Log:
Removed code duplication by adding AbstractIoService.ServiceOperationFuture.
Modified:
mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoAcceptor.java
mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoService.java
mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java
mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java
Modified: mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoAcceptor.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoAcceptor.java?rev=576419&r1=576418&r2=576419&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoAcceptor.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoAcceptor.java Mon Sep 17 06:25:56 2007
@@ -90,7 +90,16 @@
throw new IllegalStateException("handler is not set.");
}
- doBind();
+ try {
+ doBind();
+ } catch (IOException e) {
+ throw e;
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Throwable e) {
+ throw new RuntimeIOException(
+ "Failed to bind to: " + getLocalAddress(), e);
+ }
bound = true;
}
}
@@ -101,7 +110,14 @@
return;
}
- doUnbind();
+ try {
+ doUnbind();
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Throwable e) {
+ throw new RuntimeIOException(
+ "Failed to unbind from: " + getLocalAddress(), e);
+ }
bound = false;
}
}
@@ -115,10 +131,10 @@
/**
* Implement this method to perform the actual bind operation.
*/
- protected abstract void doBind() throws IOException;
+ protected abstract void doBind() throws Exception;
/**
* Implement this method to perform the actual unbind operation.
*/
- protected abstract void doUnbind();
+ protected abstract void doUnbind() throws Exception;
}
Modified: mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoService.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoService.java?rev=576419&r1=576418&r2=576419&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoService.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoService.java Mon Sep 17 06:25:56 2007
@@ -116,4 +116,30 @@
public IoSessionConfig getSessionConfig() {
return sessionConfig;
}
+
+ protected static class ServiceOperationFuture extends DefaultIoFuture {
+ public ServiceOperationFuture() {
+ super(null);
+ }
+
+ public boolean isDone() {
+ return (getValue() == Boolean.TRUE);
+ }
+
+ public void setDone() {
+ setValue(Boolean.TRUE);
+ }
+
+ public Exception getException() {
+ if (getValue() instanceof Exception) {
+ return (Exception) getValue();
+ } else {
+ return null;
+ }
+ }
+
+ public void setException(Exception cause) {
+ setValue(cause);
+ }
+ }
}
Modified: mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java?rev=576419&r1=576418&r2=576419&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java Mon Sep 17 06:25:56 2007
@@ -68,11 +68,14 @@
private DatagramChannel channel;
- private final Queue<RegistrationRequest> registerQueue = new ConcurrentLinkedQueue<RegistrationRequest>();
+ private final Queue<ServiceOperationFuture> registerQueue =
+ new ConcurrentLinkedQueue<ServiceOperationFuture>();
- private final Queue<CancellationRequest> cancelQueue = new ConcurrentLinkedQueue<CancellationRequest>();
+ private final Queue<ServiceOperationFuture> cancelQueue =
+ new ConcurrentLinkedQueue<ServiceOperationFuture>();
- private final ConcurrentMap<SocketAddress, Object> cache = new ConcurrentHashMap<SocketAddress, Object>();
+ private final ConcurrentMap<SocketAddress, Object> cache =
+ new ConcurrentHashMap<SocketAddress, Object>();
private Worker worker;
@@ -151,49 +154,33 @@
}
@Override
- protected void doBind() throws IOException {
- RegistrationRequest request = new RegistrationRequest();
+ protected void doBind() throws Exception {
+ ServiceOperationFuture future = new ServiceOperationFuture();
- registerQueue.add(request);
+ registerQueue.add(future);
startupWorker();
selector.wakeup();
- synchronized (request) {
- while (!request.done) {
- try {
- request.wait();
- } catch (InterruptedException e) {
- }
- }
- }
+ future.awaitUninterruptibly();
- if (request.exception != null) {
- throw (IOException) new IOException("Failed to bind")
- .initCause(request.exception);
- } else {
- setLocalAddress(channel.socket().getLocalSocketAddress());
+ if (future.getException() != null) {
+ throw future.getException();
}
+
+ setLocalAddress(channel.socket().getLocalSocketAddress());
}
@Override
- protected void doUnbind() {
- CancellationRequest request = new CancellationRequest();
+ protected void doUnbind() throws Exception {
+ ServiceOperationFuture future = new ServiceOperationFuture();
- cancelQueue.add(request);
+ cancelQueue.add(future);
startupWorker();
selector.wakeup();
- synchronized (request) {
- while (!request.done) {
- try {
- request.wait();
- } catch (InterruptedException e) {
- }
- }
- }
-
- if (request.exception != null) {
- throw new RuntimeException("Failed to unbind", request.exception);
+ future.awaitUninterruptibly();
+ if (future.getException() != null) {
+ throw future.getException();
}
}
@@ -352,8 +339,8 @@
}
for (; ;) {
- RegistrationRequest req = registerQueue.poll();
- if (req == null) {
+ ServiceOperationFuture future = registerQueue.poll();
+ if (future == null) {
break;
}
@@ -372,19 +359,15 @@
ch.configureBlocking(false);
ch.socket().bind(getLocalAddress());
- ch.register(selector, SelectionKey.OP_READ, req);
+ ch.register(selector, SelectionKey.OP_READ, future);
this.channel = ch;
getListeners().fireServiceActivated();
- } catch (Throwable t) {
- req.exception = t;
+ future.setDone();
+ } catch (Exception e) {
+ future.setException(e);
} finally {
- synchronized (req) {
- req.done = true;
- req.notify();
- }
-
- if (ch != null && req.exception != null) {
+ if (ch != null && future.getException() != null) {
try {
ch.disconnect();
ch.close();
@@ -398,8 +381,8 @@
private void cancelKeys() {
for (; ;) {
- CancellationRequest request = cancelQueue.poll();
- if (request == null) {
+ ServiceOperationFuture future = cancelQueue.poll();
+ if (future == null) {
break;
}
@@ -416,27 +399,9 @@
} catch (Throwable t) {
ExceptionMonitor.getInstance().exceptionCaught(t);
} finally {
- synchronized (request) {
- request.done = true;
- request.notify();
- }
-
- if (request.exception == null) {
- getListeners().fireServiceDeactivated();
- }
+ future.setDone();
+ getListeners().fireServiceDeactivated();
}
}
- }
-
- private static class RegistrationRequest {
- private Throwable exception;
-
- private boolean done;
- }
-
- private static class CancellationRequest {
- private boolean done;
-
- private RuntimeException exception;
}
}
Modified: mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java?rev=576419&r1=576418&r2=576419&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java Mon Sep 17 06:25:56 2007
@@ -70,9 +70,11 @@
private ServerSocketChannel serverSocketChannel;
- private final Queue<RegistrationRequest> registerQueue = new ConcurrentLinkedQueue<RegistrationRequest>();
+ private final Queue<ServiceOperationFuture> registerQueue =
+ new ConcurrentLinkedQueue<ServiceOperationFuture>();
- private final Queue<CancellationRequest> cancelQueue = new ConcurrentLinkedQueue<CancellationRequest>();
+ private final Queue<ServiceOperationFuture> cancelQueue =
+ new ConcurrentLinkedQueue<ServiceOperationFuture>();
private final NIOProcessor[] ioProcessors;
@@ -247,8 +249,8 @@
}
@Override
- protected void doBind() throws IOException {
- RegistrationRequest request = new RegistrationRequest();
+ protected void doBind() throws Exception {
+ ServiceOperationFuture request = new ServiceOperationFuture();
// adds the Registration request to the queue for the Workers
// to handle
@@ -260,31 +262,17 @@
selector.wakeup();
- synchronized (request) {
- while (!request.done) {
- try {
- request.wait();
- } catch (InterruptedException e) {
- ExceptionMonitor.getInstance().exceptionCaught(e);
- }
- }
- }
+ request.awaitUninterruptibly();
- if (request.exception != null) {
- if (request.exception instanceof RuntimeException) {
- throw (RuntimeException) request.exception;
- } else if (request.exception instanceof IOException) {
- throw (IOException) request.exception;
- } else {
- throw new RuntimeIOException(request.exception);
- }
- } else {
- // Update the local address.
- // setLocalAddress() shouldn't be called from the worker thread
- // because of deadlock.
- setLocalAddress(serverSocketChannel.socket()
- .getLocalSocketAddress());
- }
+ if (request.getException() != null) {
+ throw request.getException();
+ }
+
+ // Update the local address.
+ // setLocalAddress() shouldn't be called from the worker thread
+ // because of deadlock.
+ setLocalAddress(serverSocketChannel.socket()
+ .getLocalSocketAddress());
}
/**
@@ -307,27 +295,16 @@
}
@Override
- protected void doUnbind() {
- CancellationRequest request = new CancellationRequest();
+ protected void doUnbind() throws Exception {
+ ServiceOperationFuture future = new ServiceOperationFuture();
- cancelQueue.add(request);
+ cancelQueue.add(future);
startupWorker();
selector.wakeup();
- synchronized (request) {
- while (!request.done) {
- try {
- request.wait();
- } catch (InterruptedException e) {
- ExceptionMonitor.getInstance().exceptionCaught(e);
- }
- }
- }
-
- if (request.exception != null) {
- request.exception.fillInStackTrace();
-
- throw request.exception;
+ future.awaitUninterruptibly();
+ if (future.getException() != null) {
+ throw future.getException();
}
}
@@ -447,8 +424,8 @@
*/
private void registerNew() {
for (; ;) {
- RegistrationRequest req = registerQueue.poll();
- if (req == null) {
+ ServiceOperationFuture future = registerQueue.poll();
+ if (future == null) {
break;
}
@@ -465,22 +442,17 @@
// and bind.
ssc.socket().bind(getLocalAddress(), getBacklog());
- ssc.register(selector, SelectionKey.OP_ACCEPT, req);
+ ssc.register(selector, SelectionKey.OP_ACCEPT, future);
serverSocketChannel = ssc;
// and notify.
getListeners().fireServiceActivated();
- } catch (Throwable e) {
- req.exception = e;
+ future.setDone();
+ } catch (Exception e) {
+ future.setException(e);
} finally {
- synchronized (req) {
- req.done = true;
-
- req.notifyAll();
- }
-
- if (ssc != null && req.exception != null) {
+ if (ssc != null && future.getException() != null) {
try {
ssc.close();
} catch (IOException e) {
@@ -499,8 +471,8 @@
*/
private void cancelKeys() {
for (; ;) {
- CancellationRequest request = cancelQueue.poll();
- if (request == null) {
+ ServiceOperationFuture future = cancelQueue.poll();
+ if (future == null) {
break;
}
@@ -516,34 +488,10 @@
} catch (IOException e) {
ExceptionMonitor.getInstance().exceptionCaught(e);
} finally {
- synchronized (request) {
- request.done = true;
- request.notifyAll();
- }
-
- if (request.exception == null) {
- getListeners().fireServiceDeactivated();
- }
+ future.setDone();
+ getListeners().fireServiceDeactivated();
}
}
- }
-
- /**
- * Class that triggers registration, or startup, of this class
- */
- private static class RegistrationRequest {
- private Throwable exception;
-
- private boolean done;
- }
-
- /**
- * Class that triggers a signal to unbind.
- */
- private static class CancellationRequest {
- private boolean done;
-
- private RuntimeException exception;
}
/**