com.barchart.netty.server.base.AbstractStatefulServer.java Source code

Java tutorial

Introduction

Here is the source code for com.barchart.netty.server.base.AbstractStatefulServer.java

Source

/**
 * Copyright (C) 2011-2014 Barchart, Inc. <http://www.barchart.com/>
 *
 * All rights reserved. Licensed under the OSI BSD License.
 *
 * http://www.opensource.org/licenses/bsd-license.php
 */
package com.barchart.netty.server.base;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

import com.barchart.netty.server.util.TimeoutPromiseGroup;

/**
 * Abstract implementation of a stateful server over various protocols, with
 * separate channels for acceptor and child connections.
 */
public abstract class AbstractStatefulServer<T extends AbstractStatefulServer<T>>
        extends AbstractServer<T, ServerBootstrap> {

    protected Class<? extends ServerChannel> channelType = NioServerSocketChannel.class;
    protected EventLoopGroup childGroup = defaultGroup;

    @SuppressWarnings("unchecked")
    protected T channel(final Class<? extends ServerChannel> type) {
        channelType = type;
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public T childGroup(final EventLoopGroup group) {
        childGroup = group;
        return (T) this;
    }

    @Override
    protected ServerBootstrap bootstrap() {

        final ServerBootstrap bootstrap = new ServerBootstrap() //
                .group(defaultGroup, childGroup) //
                .channel(channelType) //
                .childHandler(new ServerChannelInitializer()) //
                .option(ChannelOption.SO_REUSEADDR, true) //
                .option(ChannelOption.SO_SNDBUF, 262144) //
                .option(ChannelOption.SO_RCVBUF, 262144) //
                .childOption(ChannelOption.SO_SNDBUF, 262144) //
                .childOption(ChannelOption.SO_RCVBUF, 262144);

        if (bootstrapInit != null) {
            bootstrapInit.initBootstrap(bootstrap);
        }

        return bootstrap;

    }

    public ChannelFuture listen(final int port) {
        return listen(new InetSocketAddress("0.0.0.0", port));
    }

    @Override
    protected Future<?> shutdownEventLoop() {

        if (childGroup != defaultGroup)
            return new TimeoutPromiseGroup(GlobalEventExecutor.INSTANCE,
                    childGroup.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS), super.shutdownEventLoop());

        return super.shutdownEventLoop();

    }

}