com.friz.owari.network.client.Client.java Source code

Java tutorial

Introduction

Here is the source code for com.friz.owari.network.client.Client.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.client;

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

import com.friz.owari.network.Bind;
import com.friz.owari.network.client.listener.ExchangeListener;
import com.friz.owari.network.client.listener.HandshakeListener;
import com.friz.owari.network.client.listener.PatchInitListener;
import com.friz.owari.network.client.listener.PatchListener;
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.event.impl.PatchEvent;
import com.friz.owari.network.event.impl.PatchInitEvent;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

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

    private final Bootstrap bootstrap = new Bootstrap();

    private PatchService service = new PatchService(this);

    private ChannelFuture future;

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

    @Override
    public void initialize() throws NoSuchAlgorithmException {
        bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<NioSocketChannel>() {

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

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

        }).option(ChannelOption.TCP_NODELAY, true);

        hub.listen(HandshakeEvent.class, new HandshakeListener());
        hub.listen(ExchangeRecieveEvent.class, new ExchangeListener());
        hub.listen(PatchInitEvent.class, new PatchInitListener());
        hub.listen(PatchEvent.class, new PatchListener());

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

        final KeyPair keyPair = keyPairGenerator.generateKeyPair();

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

    @Override
    public void bind() {
        try {
            future = bootstrap.connect("127.0.0.1", 7432).sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        service.startAsync();
    }

    @Override
    public void stop() {
        try {
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }

    public PatchService getService() {
        return service;
    }

}