diff --git a/src/main/java/mc/core/Main.java b/src/main/java/mc/core/Main.java index d5a749b..b1d58d5 100644 --- a/src/main/java/mc/core/Main.java +++ b/src/main/java/mc/core/Main.java @@ -12,10 +12,8 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; @Slf4j public class Main { - public static ApplicationContext appContext; //FIXME - public static void main(String[] args) { - appContext = new ClassPathXmlApplicationContext("spring.xml"); + ApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml"); Config config = appContext.getBean("config", Config.class); Server server = appContext.getBean("server", Server.class); diff --git a/src/main/java/mc/core/network/proto_125/netty/NettyServer.java b/src/main/java/mc/core/network/proto_125/netty/NettyServer.java index 5297b9d..852f7c5 100644 --- a/src/main/java/mc/core/network/proto_125/netty/NettyServer.java +++ b/src/main/java/mc/core/network/proto_125/netty/NettyServer.java @@ -5,30 +5,38 @@ package mc.core.network.proto_125.netty; import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelInitializer; +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.LoggingHandler; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import mc.core.network.Server; 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 public class NettyServer implements Server { + @Autowired + private ApplicationContext applicationContext; private EventLoopGroup bossGroup, workerGroup; private ChannelInitializer buildChannelInitializer() { return new ChannelInitializer() { @Override protected void initChannel(SocketChannel socketChannel) { - socketChannel.pipeline().addLast( - new LoggingHandler(), - new PacketDecoder(), - new PacketHandler(), - new PacketEncoder() - ); + Map beans = applicationContext.getBeansOfType(ChannelHandler.class); + beans.forEach(socketChannel.pipeline()::addLast); } }; } @@ -47,7 +55,7 @@ public class NettyServer implements Server { public void start(String host, int port) throws StartServerException { log.info("Use protocol 1.2.5"); bossGroup = new NioEventLoopGroup(1); - workerGroup = new NioEventLoopGroup(); + workerGroup = new NioEventLoopGroup(); //TODO сделать изменяемым ServerBootstrap serverBootstrap = buildServerBootstrap(); diff --git a/src/main/java/mc/core/network/proto_125/netty/PacketHandler.java b/src/main/java/mc/core/network/proto_125/netty/PacketHandler.java index 3aa5fa9..47ef38a 100644 --- a/src/main/java/mc/core/network/proto_125/netty/PacketHandler.java +++ b/src/main/java/mc/core/network/proto_125/netty/PacketHandler.java @@ -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.LoginPacket; 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.util.Arrays; @@ -22,7 +25,8 @@ import java.util.Optional; @Slf4j public class PacketHandler extends SimpleChannelInboundHandler { - private static Config config = Main.appContext.getBean("config", Config.class); //FIXME + @Autowired + private Config config; @Override protected void channelRead0(ChannelHandlerContext ctx, CSPacket packet) throws Exception { diff --git a/src/main/resources/spring.xml b/src/main/resources/spring.xml index 7d09237..0d9cd11 100644 --- a/src/main/resources/spring.xml +++ b/src/main/resources/spring.xml @@ -1,7 +1,10 @@ + 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"> + + @@ -10,5 +13,11 @@ + + + + + + \ No newline at end of file