com.friz.owari.network.server.Server.java Source code

Java tutorial

Introduction

Here is the source code for com.friz.owari.network.server.Server.java

Source

/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Kyle Fricilone
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.friz.owari.network.server;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;

import com.friz.owari.network.Bind;
import com.friz.owari.network.codec.HandshakeDecoder;
import com.friz.owari.network.codec.HandshakeEncoder;
import com.friz.owari.network.event.impl.ExchangeRecieveEvent;
import com.friz.owari.network.event.impl.HandshakeEvent;
import com.friz.owari.network.server.listener.ExchangeListener;
import com.friz.owari.network.server.listener.HandshakeListener;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * @author Kyle Fricilone
 * @date Jun 12, 2016
 */
public class Server extends Bind {

    private ServerBootstrap bootstrap;

    public static void main(String[] args) throws NoSuchAlgorithmException {
        Server s = new Server();
        s.initialize();
        s.bind();
    }

    /* (non-Javadoc)
     * @see com.friz.owari.network.Bind#initialize()
     */
    @Override
    public void initialize() throws NoSuchAlgorithmException {
        bootstrap = new ServerBootstrap();

        bootstrap.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<NioSocketChannel>() {

                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();

                        pipeline.addLast(HandshakeDecoder.class.getName(), new HandshakeDecoder());
                        pipeline.addLast(HandshakeEncoder.class.getName(), new HandshakeEncoder());
                        pipeline.addLast(IdleStateHandler.class.getName(), new IdleStateHandler(15, 0, 0));
                        pipeline.addLast(ServerChannelHandler.class.getName(),
                                new ServerChannelHandler(Server.this));
                    }

                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.TCP_NODELAY, true);

        hub.listen(HandshakeEvent.class, new HandshakeListener());
        hub.listen(ExchangeRecieveEvent.class, new ExchangeListener());

        final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DH");
        keyPairGenerator.initialize(1024);

        final KeyPair keyPair = keyPairGenerator.generateKeyPair();

        privateKey = keyPair.getPrivate();
        publicKey = keyPair.getPublic();
    }

    /* (non-Javadoc)
     * @see com.friz.owari.network.Bind#bind()
     */
    @Override
    public void bind() {
        try {
            future = bootstrap.bind("0.0.0.0", 7432).sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /* (non-Javadoc)
     * @see com.friz.owari.network.Bind#stop()
     */
    @Override
    public void stop() {
        try {
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }

}