changeset 626:dacf6c5ea49f

prevent double notification when command channel is misconfigured reviewed-by: rkennke review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-September/003280.html Avoids writing to an unreachable channel, which was causing netty to throw multiple exceptions.
author Jon VanAlten <vanaltj@gmail.com>
date Tue, 18 Sep 2012 17:38:29 -0400
parents bd6dab2e9300
children 7252a5f21644
files client/command/src/main/java/com/redhat/thermostat/client/command/internal/RequestEncoder.java client/command/src/main/java/com/redhat/thermostat/client/command/internal/RequestQueueImpl.java
diffstat 2 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/client/command/src/main/java/com/redhat/thermostat/client/command/internal/RequestEncoder.java	Tue Sep 18 22:52:53 2012 +0200
+++ b/client/command/src/main/java/com/redhat/thermostat/client/command/internal/RequestEncoder.java	Tue Sep 18 17:38:29 2012 -0400
@@ -41,6 +41,7 @@
 import org.jboss.netty.buffer.ChannelBuffer;
 import org.jboss.netty.channel.ChannelHandlerContext;
 import org.jboss.netty.channel.Channels;
+import org.jboss.netty.channel.ExceptionEvent;
 import org.jboss.netty.channel.MessageEvent;
 
 import com.redhat.thermostat.common.command.EncodingHelper;
@@ -76,4 +77,10 @@
         Channels.write(ctx, e.getFuture(), buf);
     }
 
+    // This must be implemented, even though we are simply passing on the exception.  If
+    // not implemented, this exception ends up going uncaught which causes problems.
+    @Override
+    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
+    	Channels.fireExceptionCaught(ctx, e.getCause());
+    }
 }
\ No newline at end of file
--- a/client/command/src/main/java/com/redhat/thermostat/client/command/internal/RequestQueueImpl.java	Tue Sep 18 22:52:53 2012 +0200
+++ b/client/command/src/main/java/com/redhat/thermostat/client/command/internal/RequestQueueImpl.java	Tue Sep 18 17:38:29 2012 -0400
@@ -45,6 +45,9 @@
 
 import com.redhat.thermostat.client.command.RequestQueue;
 import com.redhat.thermostat.common.command.Request;
+import com.redhat.thermostat.common.command.RequestResponseListener;
+import com.redhat.thermostat.common.command.Response;
+import com.redhat.thermostat.common.command.Response.ResponseType;
 
 class RequestQueueImpl implements RequestQueue {
 
@@ -103,9 +106,17 @@
                 }
                 ChannelFuture f = ((ClientBootstrap) ctx.getBootstrap()).connect(request.getTarget());
                 f.awaitUninterruptibly();
-                Channel c = f.getChannel();
-                c.getPipeline().addLast("responseHandler", new ResponseHandler(request));
-                c.write(request);
+                if (f.isSuccess()) {
+                	Channel c = f.getChannel();
+                	c.getPipeline().addLast("responseHandler", new ResponseHandler(request));
+                	c.write(request);
+                } else {
+                	Response response  = new Response(ResponseType.ERROR);
+                	// TODO add more information once Response supports parameters.
+                	for (RequestResponseListener listener : request.getListeners()) {
+                		listener.fireComplete(request, response);
+                	}
+                }
             }
         }
     }