X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-ssh%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fnetty%2FEchoClientHandler.java;h=1d3cd5325cb99f77c40a270f8963abd2ed2e3a12;hp=81182a580eff12b59a77872d416c19f6feba9a30;hb=a2563a94253f9c2603e0ab25b8f412ea07fcf51d;hpb=999c87c5aa34530297757b653ffb0dea80bf4ff2 diff --git a/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/EchoClientHandler.java b/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/EchoClientHandler.java index 81182a580e..1d3cd5325c 100644 --- a/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/EchoClientHandler.java +++ b/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/EchoClientHandler.java @@ -13,6 +13,8 @@ import static com.google.common.base.Preconditions.checkState; import com.google.common.base.Charsets; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import org.slf4j.Logger; @@ -23,40 +25,72 @@ import org.slf4j.LoggerFactory; * traffic between the echo client and server by sending the first message to * the server. */ -public class EchoClientHandler extends ChannelInboundHandlerAdapter { - private static final Logger logger = LoggerFactory.getLogger(EchoClientHandler.class); +public class EchoClientHandler extends ChannelInboundHandlerAdapter implements ChannelFutureListener { + private static final Logger LOG = LoggerFactory.getLogger(EchoClientHandler.class); private ChannelHandlerContext ctx; + private final StringBuilder fromServer = new StringBuilder(); + + public static enum State {CONNECTING, CONNECTED, FAILED_TO_CONNECT, CONNECTION_CLOSED} + + + private State state = State.CONNECTING; @Override - public void channelActive(ChannelHandlerContext ctx) { + public synchronized void channelActive(ChannelHandlerContext ctx) { checkState(this.ctx == null); - logger.info("client active"); + LOG.info("channelActive"); this.ctx = ctx; + state = State.CONNECTED; } @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - ByteBuf bb = (ByteBuf) msg; - logger.info(">{}", bb.toString(Charsets.UTF_8)); - bb.release(); + public synchronized void channelInactive(ChannelHandlerContext ctx) throws Exception { + state = State.CONNECTION_CLOSED; } @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + ByteBuf bb = (ByteBuf) msg; + String string = bb.toString(Charsets.UTF_8); + fromServer.append(string); + LOG.info(">{}", string); + bb.release(); } @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + public synchronized void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // Close the connection when an exception is raised. - logger.warn("Unexpected exception from downstream.", cause); + LOG.warn("Unexpected exception from downstream.", cause); checkState(this.ctx.equals(ctx)); ctx.close(); this.ctx = null; } - public void write(String message) { + public synchronized void write(String message) { ByteBuf byteBuf = Unpooled.copiedBuffer(message.getBytes()); ctx.writeAndFlush(byteBuf); } + + public synchronized boolean isConnected() { + return state == State.CONNECTED; + } + + public synchronized String read() { + return fromServer.toString(); + } + + @Override + public synchronized void operationComplete(ChannelFuture future) throws Exception { + checkState(state == State.CONNECTING); + if (future.isSuccess()) { + LOG.trace("Successfully connected, state will be switched in channelActive"); + } else { + state = State.FAILED_TO_CONNECT; + } + } + + public State getState() { + return state; + } }