1 package org.opendaylight.ovsdb.lib.jsonrpc;
3 import io.netty.bootstrap.ServerBootstrap;
4 import io.netty.channel.*;
5 import io.netty.channel.nio.NioEventLoopGroup;
6 import io.netty.channel.socket.SocketChannel;
7 import io.netty.channel.socket.nio.NioServerSocketChannel;
9 import java.util.concurrent.TimeUnit;
11 public class NettyBootStrapper {
13 EventLoopGroup bossGroup = null;
14 EventLoopGroup workerGroup = null;
15 ChannelFuture f = null;
17 public ChannelFuture startServer(int localPort, final ChannelHandler... handlers) throws Exception {
18 // Configure the server.
19 bossGroup = new NioEventLoopGroup();
20 workerGroup = new NioEventLoopGroup();
21 ServerBootstrap b = new ServerBootstrap();
22 b.group(bossGroup, workerGroup)
23 .channel(NioServerSocketChannel.class)
24 .option(ChannelOption.SO_BACKLOG, 100)
25 .localAddress(localPort)
26 .childOption(ChannelOption.TCP_NODELAY, true)
27 .childHandler(new ChannelInitializer<SocketChannel>() {
30 public void initChannel(SocketChannel ch) throws Exception {
31 for (ChannelHandler handler : handlers) {
32 ch.pipeline().addLast(handler);
42 public void stopServer() throws InterruptedException {
45 ChannelFuture channelFuture = f.channel().closeFuture();
46 channelFuture.get(1000, TimeUnit.MILLISECONDS);
47 if (!channelFuture.isDone()) {
48 f.channel().unsafe().closeForcibly();
51 bossGroup.shutdownGracefully();
52 workerGroup.shutdownGracefully();
54 // Wait until all threads are terminated.
55 bossGroup.terminationFuture().sync();
56 workerGroup.terminationFuture().sync();
57 } catch (Exception e) {