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.*;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import io.netty.channel.socket.SocketChannel;
16 import io.netty.channel.socket.nio.NioServerSocketChannel;
18 import java.util.concurrent.TimeUnit;
20 public class NettyBootStrapper {
22 EventLoopGroup bossGroup = null;
23 EventLoopGroup workerGroup = null;
24 ChannelFuture f = null;
26 public ChannelFuture startServer(int localPort, final ChannelHandler... handlers) throws Exception {
27 // Configure the server.
28 bossGroup = new NioEventLoopGroup();
29 workerGroup = new NioEventLoopGroup();
30 ServerBootstrap b = new ServerBootstrap();
31 b.group(bossGroup, workerGroup)
32 .channel(NioServerSocketChannel.class)
33 .option(ChannelOption.SO_BACKLOG, 100)
34 .localAddress(localPort)
35 .childOption(ChannelOption.TCP_NODELAY, true)
36 .childHandler(new ChannelInitializer<SocketChannel>() {
39 public void initChannel(SocketChannel ch) throws Exception {
40 for (ChannelHandler handler : handlers) {
41 ch.pipeline().addLast(handler);
51 public void stopServer() throws InterruptedException {
54 ChannelFuture channelFuture = f.channel().closeFuture();
55 channelFuture.get(1000, TimeUnit.MILLISECONDS);
56 if (!channelFuture.isDone()) {
57 f.channel().unsafe().closeForcibly();
60 bossGroup.shutdownGracefully();
61 workerGroup.shutdownGracefully();
63 // Wait until all threads are terminated.
64 bossGroup.terminationFuture().sync();
65 workerGroup.terminationFuture().sync();
66 } catch (Exception e) {