Archived
0

Channel handlers as beans

This commit is contained in:
2018-04-15 09:29:21 +03:00
parent c4ad074bb3
commit db97d7d446
4 changed files with 31 additions and 12 deletions

View File

@@ -12,10 +12,8 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
@Slf4j @Slf4j
public class Main { public class Main {
public static ApplicationContext appContext; //FIXME
public static void main(String[] args) { public static void main(String[] args) {
appContext = new ClassPathXmlApplicationContext("spring.xml"); ApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");
Config config = appContext.getBean("config", Config.class); Config config = appContext.getBean("config", Config.class);
Server server = appContext.getBean("server", Server.class); Server server = appContext.getBean("server", Server.class);

View File

@@ -5,30 +5,38 @@
package mc.core.network.proto_125.netty; package mc.core.network.proto_125.netty;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup; import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import mc.core.network.Server; import mc.core.network.Server;
import mc.core.network.StartServerException; import mc.core.network.StartServerException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Map;
@Slf4j @Slf4j
public class NettyServer implements Server { public class NettyServer implements Server {
@Autowired
private ApplicationContext applicationContext;
private EventLoopGroup bossGroup, workerGroup; private EventLoopGroup bossGroup, workerGroup;
private ChannelInitializer buildChannelInitializer() { private ChannelInitializer buildChannelInitializer() {
return new ChannelInitializer<SocketChannel>() { return new ChannelInitializer<SocketChannel>() {
@Override @Override
protected void initChannel(SocketChannel socketChannel) { protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline().addLast( Map<String, ChannelHandler> beans = applicationContext.getBeansOfType(ChannelHandler.class);
new LoggingHandler(), beans.forEach(socketChannel.pipeline()::addLast);
new PacketDecoder(),
new PacketHandler(),
new PacketEncoder()
);
} }
}; };
} }
@@ -47,7 +55,7 @@ public class NettyServer implements Server {
public void start(String host, int port) throws StartServerException { public void start(String host, int port) throws StartServerException {
log.info("Use protocol 1.2.5"); log.info("Use protocol 1.2.5");
bossGroup = new NioEventLoopGroup(1); bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); //TODO сделать изменяемым
ServerBootstrap serverBootstrap = buildServerBootstrap(); ServerBootstrap serverBootstrap = buildServerBootstrap();

View File

@@ -15,6 +15,9 @@ import mc.core.network.proto_125.packets.HandshakePacket;
import mc.core.network.proto_125.packets.KickPacket; import mc.core.network.proto_125.packets.KickPacket;
import mc.core.network.proto_125.packets.LoginPacket; import mc.core.network.proto_125.packets.LoginPacket;
import mc.core.network.proto_125.packets.PingPacket; import mc.core.network.proto_125.packets.PingPacket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
@@ -22,7 +25,8 @@ import java.util.Optional;
@Slf4j @Slf4j
public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> { public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
private static Config config = Main.appContext.getBean("config", Config.class); //FIXME @Autowired
private Config config;
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, CSPacket packet) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, CSPacket packet) throws Exception {

View File

@@ -1,7 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="config" class="mc.core.embedded.ConfigFromSpring"> <bean id="config" class="mc.core.embedded.ConfigFromSpring">
<property name="descriptionServer" value="MC Core"/> <property name="descriptionServer" value="MC Core"/>
<property name="maxPlayers" value="100"/> <property name="maxPlayers" value="100"/>
@@ -10,5 +13,11 @@
<property name="port" value="25565"/> <property name="port" value="25565"/>
</bean> </bean>
<!-- Netty Server -->
<!--<bean id="pipeline.log" class="io.netty.handler.logging.LoggingHandler" scope="prototype"/>-->
<bean id="pipeline.decoder" class="mc.core.network.proto_125.netty.PacketDecoder" scope="prototype"/>
<bean id="pipeline.encoder" class="mc.core.network.proto_125.netty.PacketEncoder" scope="prototype"/>
<bean id="pipeline.handler" class="mc.core.network.proto_125.netty.PacketHandler" scope="prototype"/>
<bean id="server" class="mc.core.network.proto_125.netty.NettyServer"/> <bean id="server" class="mc.core.network.proto_125.netty.NettyServer"/>
</beans> </beans>