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.netconf.tcp.netty;
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.ChannelInboundHandlerAdapter;
16 import io.netty.channel.ChannelInitializer;
17 import io.netty.channel.local.LocalAddress;
18 import io.netty.channel.local.LocalChannel;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class ProxyServerHandler extends ChannelInboundHandlerAdapter {
23 private static final Logger LOG = LoggerFactory.getLogger(ProxyServerHandler.class);
24 private final Bootstrap clientBootstrap;
25 private final LocalAddress localAddress;
27 private Channel clientChannel;
29 public ProxyServerHandler(Bootstrap clientBootstrap, LocalAddress localAddress) {
30 this.clientBootstrap = clientBootstrap;
31 this.localAddress = localAddress;
35 public void channelActive(ChannelHandlerContext remoteCtx) {
36 final ProxyClientHandler clientHandler = new ProxyClientHandler(remoteCtx);
37 clientBootstrap.handler(new ChannelInitializer<LocalChannel>() {
39 public void initChannel(LocalChannel ch) throws Exception {
40 ch.pipeline().addLast(clientHandler);
43 ChannelFuture clientChannelFuture = clientBootstrap.connect(localAddress).awaitUninterruptibly();
44 clientChannel = clientChannelFuture.channel();
48 public void channelInactive(ChannelHandlerContext ctx) {
49 LOG.trace("channelInactive - closing client channel");
50 clientChannel.close();
54 public void channelRead(ChannelHandlerContext ctx, final Object msg) {
55 LOG.trace("Writing to client channel");
56 clientChannel.write(msg);
60 public void channelReadComplete(ChannelHandlerContext ctx) {
61 LOG.trace("Flushing client channel");
62 clientChannel.flush();
66 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
67 // Close the connection when an exception is raised.
68 LOG.warn("Unexpected exception from downstream.", cause);