2 * Copyright (C) 2013 EBay Software Foundation
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 * Authors : Aswin Raveendran
10 package org.opendaylight.ovsdb.lib.jsonrpc;
12 import io.netty.bootstrap.ServerBootstrap;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelHandler;
15 import io.netty.channel.ChannelInitializer;
16 import io.netty.channel.ChannelOption;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.channel.socket.SocketChannel;
20 import io.netty.channel.socket.nio.NioServerSocketChannel;
22 import java.util.concurrent.TimeUnit;
24 public class NettyBootStrapper {
26 EventLoopGroup bossGroup = null;
27 EventLoopGroup workerGroup = null;
28 ChannelFuture f = null;
30 public ChannelFuture startServer(int localPort, final ChannelHandler... handlers) throws Exception {
31 // Configure the server.
32 bossGroup = new NioEventLoopGroup();
33 workerGroup = new NioEventLoopGroup();
34 ServerBootstrap b = new ServerBootstrap();
35 b.group(bossGroup, workerGroup)
36 .channel(NioServerSocketChannel.class)
37 .option(ChannelOption.SO_BACKLOG, 100)
38 .localAddress(localPort)
39 .childOption(ChannelOption.TCP_NODELAY, true)
40 .childHandler(new ChannelInitializer<SocketChannel>() {
43 public void initChannel(SocketChannel ch) throws Exception {
44 for (ChannelHandler handler : handlers) {
45 ch.pipeline().addLast(handler);
55 public void stopServer() throws InterruptedException {
58 ChannelFuture channelFuture = f.channel().closeFuture();
59 channelFuture.get(1000, TimeUnit.MILLISECONDS);
60 if (!channelFuture.isDone()) {
61 f.channel().unsafe().closeForcibly();
64 bossGroup.shutdownGracefully();
65 workerGroup.shutdownGracefully();
67 // Wait until all threads are terminated.
68 bossGroup.terminationFuture().sync();
69 workerGroup.terminationFuture().sync();
70 } catch (Exception e) {