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
8 package org.opendaylight.netconf.ssh;
10 import com.google.common.base.Splitter;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.channel.ChannelHandler.Sharable;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelInboundHandlerAdapter;
15 import java.nio.charset.StandardCharsets;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * Handler implementation for the echo server.
23 public class EchoServerHandler extends ChannelInboundHandlerAdapter {
25 private static final Logger LOG = LoggerFactory.getLogger(EchoServerHandler.class);
26 private String fromLastNewLine = "";
27 private final Splitter splitter = Splitter.onPattern("\r?\n");
30 public void channelActive(final ChannelHandlerContext ctx) throws Exception {
31 LOG.debug("sleep start");
33 LOG.debug("sleep done");
37 public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
38 ByteBuf byteBuf = (ByteBuf) msg;
39 String message = byteBuf.toString(StandardCharsets.UTF_8);
40 LOG.info("writing back '{}'", message);
42 fromLastNewLine += message;
43 for (String line : splitter.split(fromLastNewLine)) {
44 if ("quit".equals(line)) {
45 LOG.info("closing server ctx");
50 fromLastNewLine = line; // last line should be preserved
53 // do not release byteBuf as it is handled back
57 public void channelReadComplete(final ChannelHandlerContext ctx) {
58 LOG.debug("flushing");