Archived
0

simple server

This commit is contained in:
2018-04-08 16:37:16 +03:00
parent ef3e158c8f
commit 5fc94d0ab0
8 changed files with 107 additions and 10 deletions

View File

@@ -15,6 +15,7 @@
<slf4j.version>1.7.21</slf4j.version>
<log4j.version>2.5</log4j.version>
<spring.version>4.2.5.RELEASE</spring.version>
<netty.version>4.1.22.Final</netty.version>
</properties>
<dependencies>
@@ -53,6 +54,13 @@
</exclusions>
</dependency>
<!-- Netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
<!-- COMPONENTS -->
<dependency>
<groupId>org.projectlombok</groupId>

View File

@@ -8,4 +8,6 @@ public interface Config {
int getMaxPlayers();
String getDescriptionServer();
String getFaviconBase64();
String getHost();
int getPort();
}

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,9 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-08
*/
package mc.core;
public interface Server {
void start(String host, int port) throws StartServerException;
}

View File

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

View File

@@ -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;
}
}

View File

@@ -0,0 +1,54 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 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<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);
}
}
}

View File

@@ -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">
<bean id="config" class="mc.core.embedded.ConfigFromSpring">
<constructor-arg index="0" type="java.lang.String" value="MC Core"/>
<constructor-arg index="1" type="int" value="100"/>
<constructor-arg index="2" type="java.io.File" value="icon.png"/>
<property name="descriptionServer" value="MC Core"/>
<property name="maxPlayers" value="100"/>
<property name="faviconBase64" value="icon.png"/>
<property name="host" value="127.0.0.1"/>
<property name="port" value="25565"/>
</bean>
<bean id="server" class="mc.core.netty.NettyServer"/>
</beans>