com.agrn.senqm.network.connection.Connection.java Source code

Java tutorial

Introduction

Here is the source code for com.agrn.senqm.network.connection.Connection.java

Source

/*
 *      Copyright  2014 Alban GRUIN
 *
 *      This file is part of SenqmAndroid.
 *
 *      SenqmAndroid is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      SenqmAndroid is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with SenqmAndroid.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.agrn.senqm.network.connection;

import com.agrn.senqm.network.Message;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Connection {
    private final String host;
    private final static int PORT = 23487;

    private ChannelFuture channelFuture;
    private Bootstrap bootstrap;
    private EventLoopGroup workerGroup;

    public Connection(String host) {
        this.host = host;

        workerGroup = new NioEventLoopGroup();
        bootstrap = getBootstrap(workerGroup);

        connect();
    }

    public void close() {
        try {
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
        } finally {
            workerGroup.shutdownGracefully();
        }
    }

    public void connect() {
        try {
            channelFuture = bootstrap.connect(host, PORT).sync();
            channelFuture.channel().closeFuture().addListener(new CloseListener());
        } catch (InterruptedException e) {
        }
    }

    public void messageReceived(Message message) {
    }

    public String getHostAddress() {
        return host;
    }

    public void sendMessage(Message message) {
        sendMessage(message.toString());
    }

    public void sendMessage(String message) {
        Channel channel = channelFuture.channel();

        ByteBuf buffer = channel.alloc().buffer(message.length());
        for (char character : message.toCharArray())
            buffer.writeChar(character);

        channel.writeAndFlush(buffer);
    }

    private Bootstrap getBootstrap(EventLoopGroup workerGroup) {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(workerGroup);
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
        bootstrap.handler(new ConnectionInitializer());

        return bootstrap;
    }

    private class ConnectionInitializer extends ChannelInitializer<SocketChannel> {
        @Override
        protected void initChannel(SocketChannel socketChannel) throws Exception {
            socketChannel.pipeline().addLast(new ConnectionHandler(Connection.this));
        }
    }

    private class CloseListener implements ChannelFutureListener {
        @Override
        public void operationComplete(ChannelFuture channelFuture) throws Exception {
            Connection.this.close();
        }
    }
}