# HG changeset patch # User Jon VanAlten # Date 1348004309 14400 # Node ID dacf6c5ea49ff912f37aa91b2d50fa07c7a0f6f8 # Parent bd6dab2e930087b33fc0acdd0e5ebf5c4de71791 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. diff -r bd6dab2e9300 -r dacf6c5ea49f client/command/src/main/java/com/redhat/thermostat/client/command/internal/RequestEncoder.java --- 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 diff -r bd6dab2e9300 -r dacf6c5ea49f client/command/src/main/java/com/redhat/thermostat/client/command/internal/RequestQueueImpl.java --- 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); + } + } } } }