diff --git a/pom.xml b/pom.xml index 1e5e435..768b838 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ 1.7.21 2.5 4.2.5.RELEASE + 4.1.22.Final @@ -53,6 +54,13 @@ + + + io.netty + netty-all + ${netty.version} + + org.projectlombok diff --git a/src/main/java/mc/core/Config.java b/src/main/java/mc/core/Config.java index a772dd1..08b4969 100644 --- a/src/main/java/mc/core/Config.java +++ b/src/main/java/mc/core/Config.java @@ -8,4 +8,6 @@ public interface Config { int getMaxPlayers(); String getDescriptionServer(); String getFaviconBase64(); + String getHost(); + int getPort(); } diff --git a/src/main/java/mc/core/Main.java b/src/main/java/mc/core/Main.java index 6d4275f..f079a2a 100644 --- a/src/main/java/mc/core/Main.java +++ b/src/main/java/mc/core/Main.java @@ -14,8 +14,11 @@ public class Main { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Config config = context.getBean("config", Config.class); - log.info("Description: {}", config.getDescriptionServer()); - log.info("Max online: {}", config.getMaxPlayers()); - log.info("Favicon (base64): {}", config.getFaviconBase64()); + Server server = context.getBean("server", Server.class); + try { + server.start(config.getHost(), config.getPort()); + } catch (StartServerException e) { + log.error("Can't start server", e); + } } } diff --git a/src/main/java/mc/core/Server.java b/src/main/java/mc/core/Server.java new file mode 100644 index 0000000..eefcb65 --- /dev/null +++ b/src/main/java/mc/core/Server.java @@ -0,0 +1,9 @@ +/* + * DmitriyMX + * 2018-04-08 + */ +package mc.core; + +public interface Server { + void start(String host, int port) throws StartServerException; +} diff --git a/src/main/java/mc/core/StartServerException.java b/src/main/java/mc/core/StartServerException.java new file mode 100644 index 0000000..2cd4cbd --- /dev/null +++ b/src/main/java/mc/core/StartServerException.java @@ -0,0 +1,11 @@ +/* + * DmitriyMX + * 2018-04-08 + */ +package mc.core; + +public class StartServerException extends Exception { + public StartServerException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/mc/core/embedded/ConfigFromSpring.java b/src/main/java/mc/core/embedded/ConfigFromSpring.java index 5c4841e..d5cc215 100644 --- a/src/main/java/mc/core/embedded/ConfigFromSpring.java +++ b/src/main/java/mc/core/embedded/ConfigFromSpring.java @@ -5,6 +5,7 @@ package mc.core.embedded; import lombok.Getter; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import mc.core.Config; import org.apache.commons.io.FileUtils; @@ -16,13 +17,18 @@ import java.util.Base64; @Slf4j @Getter public class ConfigFromSpring implements Config { + @Setter private String descriptionServer; private String faviconBase64; + @Setter private int maxPlayers; + @Setter + private String host; + @Setter + private int port; - public ConfigFromSpring(String descriptionServer, int maxPlayers, File faviconImageFile) { - this.descriptionServer = descriptionServer; - this.maxPlayers = maxPlayers; + public void setFaviconBase64(File faviconImageFile) { + log.debug("faviconImageFile: {}", faviconImageFile.getAbsolutePath()); try { faviconBase64 = new String( Base64.getEncoder().encode( @@ -30,7 +36,7 @@ public class ConfigFromSpring implements Config { ) ); } catch (IOException e) { - log.warn("Con't load favicon", e); + log.warn("Can't load favicon", e); faviconBase64 = null; } } diff --git a/src/main/java/mc/core/netty/NettyServer.java b/src/main/java/mc/core/netty/NettyServer.java new file mode 100644 index 0000000..730eb90 --- /dev/null +++ b/src/main/java/mc/core/netty/NettyServer.java @@ -0,0 +1,54 @@ +/* + * DmitriyMX + * 2018-04-08 + */ +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() { + @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); + } + } +} diff --git a/src/main/resources/spring.xml b/src/main/resources/spring.xml index d71d9fa..e435841 100644 --- a/src/main/resources/spring.xml +++ b/src/main/resources/spring.xml @@ -3,8 +3,12 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> - - - + + + + + + + \ No newline at end of file