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 com.google.common.base.Charsets;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.ChannelHandlerContext;
18 import io.netty.channel.ChannelInboundHandlerAdapter;
19 import io.netty.channel.ChannelInitializer;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.local.LocalAddress;
22 import io.netty.channel.local.LocalChannel;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class ProxyServerHandler extends ChannelInboundHandlerAdapter {
27 private static final Logger logger = LoggerFactory.getLogger(ProxyServerHandler.class.getName());
28 private final Bootstrap clientBootstrap;
29 private final LocalAddress localAddress;
32 private Channel clientChannel;
34 public ProxyServerHandler(EventLoopGroup bossGroup, LocalAddress localAddress) {
35 clientBootstrap = new Bootstrap();
36 clientBootstrap.group(bossGroup).channel(LocalChannel.class);
37 this.localAddress = localAddress;
41 public void channelActive(ChannelHandlerContext remoteCtx) {
42 final ProxyClientHandler clientHandler = new ProxyClientHandler(remoteCtx);
43 clientBootstrap.handler(new ChannelInitializer<LocalChannel>() {
45 public void initChannel(LocalChannel ch) throws Exception {
46 ch.pipeline().addLast(clientHandler);
49 ChannelFuture clientChannelFuture = clientBootstrap.connect(localAddress).awaitUninterruptibly();
50 clientChannel = clientChannelFuture.channel();
51 clientChannel.writeAndFlush(Unpooled.copiedBuffer("connected\n".getBytes()));
55 public void channelInactive(ChannelHandlerContext ctx) {
56 logger.info("channelInactive - closing client connection");
57 clientChannel.close();
61 public void channelRead(ChannelHandlerContext ctx, final Object msg) {
62 logger.debug("Writing to client {}", msg);
63 clientChannel.write(msg);
67 public void channelReadComplete(ChannelHandlerContext ctx) {
68 logger.debug("flushing");
69 clientChannel.flush();
73 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
74 // Close the connection when an exception is raised.
75 logger.warn("Unexpected exception from downstream.", cause);
80 class ProxyClientHandler extends ChannelInboundHandlerAdapter {
81 private static final Logger logger = LoggerFactory.getLogger(ProxyClientHandler.class);
83 private final ChannelHandlerContext remoteCtx;
86 public ProxyClientHandler(ChannelHandlerContext remoteCtx) {
87 this.remoteCtx = remoteCtx;
91 public void channelActive(ChannelHandlerContext ctx) {
92 logger.info("client active");
96 public void channelRead(ChannelHandlerContext ctx, Object msg) {
97 ByteBuf bb = (ByteBuf) msg;
98 logger.info(">{}", bb.toString(Charsets.UTF_8));
103 public void channelReadComplete(ChannelHandlerContext ctx) {
104 logger.debug("Flushing server ctx");
109 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
110 // Close the connection when an exception is raised.
111 logger.warn("Unexpected exception from downstream", cause);
115 // called both when local or remote connection dies
117 public void channelInactive(ChannelHandlerContext ctx) {
118 logger.debug("channelInactive() called, closing remote client ctx");