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.bootstrap.ServerBootstrap;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelInitializer;
15 import io.netty.channel.EventLoopGroup;
16 import io.netty.channel.local.LocalAddress;
17 import io.netty.channel.local.LocalChannel;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.channel.socket.SocketChannel;
20 import io.netty.channel.socket.nio.NioServerSocketChannel;
21 import io.netty.handler.logging.LogLevel;
22 import io.netty.handler.logging.LoggingHandler;
23 import java.net.InetSocketAddress;
25 public class ProxyServer implements AutoCloseable {
26 private final EventLoopGroup bossGroup = new NioEventLoopGroup();
27 private final EventLoopGroup workerGroup = new NioEventLoopGroup();
28 private final ChannelFuture channelFuture;
30 public ProxyServer(InetSocketAddress address, final LocalAddress localAddress) {
31 // Configure the server.
32 final Bootstrap clientBootstrap = new Bootstrap();
33 clientBootstrap.group(bossGroup).channel(LocalChannel.class);
35 ServerBootstrap serverBootstrap = new ServerBootstrap();
36 serverBootstrap.group(bossGroup, workerGroup)
37 .channel(NioServerSocketChannel.class)
38 .handler(new LoggingHandler(LogLevel.DEBUG))
39 .childHandler(new ChannelInitializer<SocketChannel>() {
41 public void initChannel(SocketChannel ch) {
42 ch.pipeline().addLast(new ProxyServerHandler(clientBootstrap, localAddress));
47 channelFuture = serverBootstrap.bind(address).syncUninterruptibly();
52 channelFuture.channel().close();
53 bossGroup.shutdownGracefully();
54 workerGroup.shutdownGracefully();