2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.netty;
11 import static com.google.common.base.Preconditions.checkState;
13 import com.google.common.base.Charsets;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.ChannelFutureListener;
18 import io.netty.channel.ChannelHandlerContext;
19 import io.netty.channel.ChannelInboundHandlerAdapter;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Handler implementation for the echo client. It initiates the ping-pong
25 * traffic between the echo client and server by sending the first message to
28 public class EchoClientHandler extends ChannelInboundHandlerAdapter implements ChannelFutureListener {
29 private static final Logger LOG = LoggerFactory.getLogger(EchoClientHandler.class);
31 private ChannelHandlerContext ctx;
32 private final StringBuilder fromServer = new StringBuilder();
34 public static enum State {CONNECTING, CONNECTED, FAILED_TO_CONNECT, CONNECTION_CLOSED}
37 private State state = State.CONNECTING;
40 public synchronized void channelActive(ChannelHandlerContext ctx) {
41 checkState(this.ctx == null);
42 LOG.info("channelActive");
44 state = State.CONNECTED;
48 public synchronized void channelInactive(ChannelHandlerContext ctx) throws Exception {
49 state = State.CONNECTION_CLOSED;
53 public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
54 ByteBuf bb = (ByteBuf) msg;
55 String string = bb.toString(Charsets.UTF_8);
56 fromServer.append(string);
57 LOG.info(">{}", string);
62 public synchronized void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
63 // Close the connection when an exception is raised.
64 LOG.warn("Unexpected exception from downstream.", cause);
65 checkState(this.ctx.equals(ctx));
70 public synchronized void write(String message) {
71 ByteBuf byteBuf = Unpooled.copiedBuffer(message.getBytes());
72 ctx.writeAndFlush(byteBuf);
75 public synchronized boolean isConnected() {
76 return state == State.CONNECTED;
79 public synchronized String read() {
80 return fromServer.toString();
84 public synchronized void operationComplete(ChannelFuture future) throws Exception {
85 checkState(state == State.CONNECTING);
86 if (future.isSuccess()) {
87 LOG.trace("Successfully connected, state will be switched in channelActive");
89 state = State.FAILED_TO_CONNECT;
93 public State getState() {