Archived
0

simple server

This commit is contained in:
2018-03-25 18:27:47 +03:00
parent dfe250a0c6
commit 4b08137861
6 changed files with 230 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-03-25
*/
package mc.core;
import lombok.extern.slf4j.Slf4j;
import mc.core.netty.NettyServer;
@Slf4j
public class Main {
public static void main(String[] args) {
log.info("Start server");
Server server = new NettyServer();
try {
server.start("127.0.0.1", 25565);
} catch (StartServerException e) {
log.error("Starting server error", e);
server.stop();
}
}
}

View File

@@ -0,0 +1,11 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-03-25
*/
package mc.core;
public interface Server {
void start(String host, int port) throws StartServerException;
void stop();
boolean isRunning();
}

View File

@@ -0,0 +1,11 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-03-25
*/
package mc.core;
public class StartServerException extends Exception {
public StartServerException(Throwable cause) {
super(cause);
}
}

View File

@@ -0,0 +1,67 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-03-25
*/
package mc.core.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
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.LoggingHandler;
import mc.core.Server;
import mc.core.StartServerException;
public class NettyServer implements Server {
private EventLoopGroup bossGroup, workerGroup;
private ChannelInitializer buildChannelInitializer() {
return new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline().addLast(
new LoggingHandler()
);
}
};
}
private ServerBootstrap buildServerBootstrap() {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(buildChannelInitializer());
return bootstrap;
}
@Override
public void start(String host, int port) throws StartServerException {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = buildServerBootstrap();
try {
serverBootstrap.bind(host, port).sync().channel().closeFuture().sync();
} catch (InterruptedException e) {
throw new StartServerException(e);
}
}
@Override
public void stop() {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
@Override
public boolean isRunning() {
return bossGroup != null
&& workerGroup != null
&& !(bossGroup.isShutdown() && workerGroup.isShutdown());
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%-5level] \{%20.20c\:%-4L} %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>