io.github.vastframework.pubsub.PubSubServer.java Source code

Java tutorial

Introduction

Here is the source code for io.github.vastframework.pubsub.PubSubServer.java

Source

/*
 * Copyright 2016 The vast-pubsub authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.vastframework.pubsub;

import io.github.vastframework.pubsub.client.ClientManager;
import io.github.vastframework.pubsub.client.PubSubClientManager;
import io.github.vastframework.pubsub.event.EventManager;
import io.github.vastframework.pubsub.event.PubSubEventManager;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class PubSubServer {

    private static PubSubServer instance;
    private EventManager eventManager;
    private ClientManager clientManager;

    public PubSubServer() {
        instance = this;
        this.eventManager = new PubSubEventManager();
        this.clientManager = new PubSubClientManager();
    }

    public void setup() {
        EventLoopGroup workerGroup;
        EventLoopGroup bossGroup;
        Class<? extends ServerChannel> channelClazz;

        if (Epoll.isAvailable()) {
            workerGroup = new EpollEventLoopGroup();
            bossGroup = new EpollEventLoopGroup();
            channelClazz = EpollServerSocketChannel.class;
        } else {
            workerGroup = new NioEventLoopGroup();
            bossGroup = new NioEventLoopGroup();
            channelClazz = NioServerSocketChannel.class;
        }

        new ServerBootstrap().childOption(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_BACKLOG, 12)
                .group(bossGroup, workerGroup).channel(channelClazz)
                // TODO: Add childHandler
                .bind(4444);
    }

    public static PubSubServer getInstance() {
        return instance;
    }

    public ClientManager getClientManager() {
        return clientManager;
    }

    public EventManager getEventManager() {
        return eventManager;
    }
}