Java tutorial
/** * Copyright (c) 2014 xio4 * Universal bot for lineage-like games (Archeage, Lineage2 etc) * * This file is part of Unibot. * * Unibot 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. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ package com.xiovr.unibot.utils; import java.net.InetSocketAddress; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; /** * Echoes back any received data from a client. */ public final class EchoServer { private InetSocketAddress addr; public EchoServer(InetSocketAddress addr) { this.addr = addr; } private ChannelFuture cf; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; private volatile boolean bStarted; public void startServer() { if (bStarted) { return; } // Configure the server. bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new EchoServerHandler()); } }); // Start the server. cf = b.bind(addr).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { // System.out.println("Echo server started"); EchoServer.this.bStarted = true; } }); } catch (Exception e) { e.printStackTrace(); } } public void close() { // Wait until the server socket is closed. assert cf != null; try { cf.channel().close().sync(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } bStarted = false; } public boolean getStarted() { return bStarted; } }