From ce575853d5782050cac21bf604f9e81d8c7056c8 Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 24 Apr 2018 02:22:23 +0900 Subject: [PATCH] ing --- .gitignore | 2 +- .../configuration/ContainerConfiguration.java | 64 +++++++++++++++ .../container/server/ContainerServer.java | 79 +++++++++++++++++++ .../{ => handler}/RPCServerHandler.java | 10 ++- 4 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/loafle/overflow/container/configuration/ContainerConfiguration.java create mode 100644 src/main/java/com/loafle/overflow/container/server/ContainerServer.java rename src/main/java/com/loafle/overflow/container/server/{ => handler}/RPCServerHandler.java (86%) diff --git a/.gitignore b/.gitignore index 60af490..ebef548 100644 --- a/.gitignore +++ b/.gitignore @@ -82,7 +82,7 @@ hs_err_pid* .idea/ *.iml -/target/ +target/ .settings/ .classpath .project diff --git a/src/main/java/com/loafle/overflow/container/configuration/ContainerConfiguration.java b/src/main/java/com/loafle/overflow/container/configuration/ContainerConfiguration.java new file mode 100644 index 0000000..86493c9 --- /dev/null +++ b/src/main/java/com/loafle/overflow/container/configuration/ContainerConfiguration.java @@ -0,0 +1,64 @@ +package com.loafle.overflow.container.configuration; + +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.util.Arrays; +import java.util.List; + +import com.loafle.commons.server.Server; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.ServerChannel; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.logging.LogLevel; +import io.netty.handler.logging.LoggingHandler; + +/** + * ContainerConfiguration + */ +@Configuration +public class ContainerConfiguration { + @Autowired + private List pipelineChannelHandlers; + + @Bean + public List> channelClasses() { + return Arrays.asList(NioServerSocketChannel.class); + } + + @Bean + public List> channelOptions() { + return Arrays.asList(new Server.ChannelOptionItem<>(ChannelOption.SO_BACKLOG, 100)); + } + + @Bean + public List channelHandlers() { + return Arrays.asList(new LoggingHandler(LogLevel.INFO)); + } + + @Bean + public ChannelInitializer channelInitializer() { + return new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) throws Exception { + ChannelPipeline cp = ch.pipeline(); + for (ChannelHandler channelHandler : pipelineChannelHandlers) { + cp.addLast(channelHandler); + } + } + }; + } + + @Bean + public SocketAddress address() { + return new InetSocketAddress("127.0.0.1", 60000); + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/container/server/ContainerServer.java b/src/main/java/com/loafle/overflow/container/server/ContainerServer.java new file mode 100644 index 0000000..0b9efa7 --- /dev/null +++ b/src/main/java/com/loafle/overflow/container/server/ContainerServer.java @@ -0,0 +1,79 @@ +package com.loafle.overflow.container.server; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import javax.annotation.Resource; + +import com.loafle.commons.server.Server; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; + +/** + * ContainerServer + */ +public class ContainerServer extends Server { + @Resource + private String pidFilePath; + + private int portNumber; + + public ContainerServer() { + } + + protected ChannelFuture bind(ServerBootstrap serverBootstrap) throws Exception { + ChannelFuture cf = null; + + for (int i = 60000; i < 61000; i++) { + try { + cf = serverBootstrap.bind("127.0.0.1", i).sync(); + this.portNumber = i; + break; + } catch (Exception e) { + System.out.println(e); + continue; + } + } + + if (null == cf) { + throw new Exception("There is not available port"); + } + + try { + FileOutputStream outputStream = new FileOutputStream(this.pidFilePath); + outputStream.write(Integer.toString(this.portNumber).getBytes()); + outputStream.close(); + } catch (IOException e) { + throw e; + } + + return cf; + } + + @Override + protected void init() throws Exception { + + } + + @Override + protected void onStart() throws Exception { + + } + + @Override + protected void onStop() throws Exception { + try { + File f = new File(this.pidFilePath); + f.delete(); + } catch (Exception e) { + throw e; + } + } + + @Override + protected void destroy() throws Exception { + + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/container/server/RPCServerHandler.java b/src/main/java/com/loafle/overflow/container/server/handler/RPCServerHandler.java similarity index 86% rename from src/main/java/com/loafle/overflow/container/server/RPCServerHandler.java rename to src/main/java/com/loafle/overflow/container/server/handler/RPCServerHandler.java index a979214..46897f7 100644 --- a/src/main/java/com/loafle/overflow/container/server/RPCServerHandler.java +++ b/src/main/java/com/loafle/overflow/container/server/handler/RPCServerHandler.java @@ -1,14 +1,15 @@ -package com.loafle.overflow.container.server; +package com.loafle.overflow.container.server.handler; import com.loafle.commons.rpc.RPCException; import com.loafle.commons.rpc.protocol.RPCServerCodec; import com.loafle.commons.rpc.protocol.RPCServerRequestCodec; import com.loafle.commons.rpc.registry.RPCInvoker; -import com.loafle.commons.server.socket.netty.handler.codec.SocketFrame; -import com.loafle.commons.server.socket.netty.handler.codec.TextSocketFrame; +import com.loafle.commons.server.socket.handler.codec.SocketFrame; +import com.loafle.commons.server.socket.handler.codec.TextSocketFrame; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; @@ -18,7 +19,10 @@ import io.netty.channel.SimpleChannelInboundHandler; */ public class RPCServerHandler extends SimpleChannelInboundHandler { private static final Logger logger = LoggerFactory.getLogger(RPCServerHandler.class); + + @Autowired private RPCServerCodec serverCodec; + @Autowired private RPCInvoker rpcInvoker; public RPCServerHandler(RPCServerCodec serverCodec, RPCInvoker rpcInvoker) {