From d6a7dc06706c7e84b5470aa2e1e4da9b913ebc92 Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 19 Dec 2017 15:28:34 +0900 Subject: [PATCH] ing --- .vscode/launch.json | 2 +- .../overflow/probe/container/Container.java | 9 +- .../container/server/ContainerServer.java | 89 ++++++++++++++----- 3 files changed, 75 insertions(+), 25 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 5602362..ca262da 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,7 +13,7 @@ "stopOnEntry": false, "mainClass": "com.loafle.overflow.probe.container.Container", "projectName": "probe_container_general", - "args": "" + "args": "/project/overFlow/probe/pid/general.pid" }, { "type": "java", diff --git a/src/main/java/com/loafle/overflow/probe/container/Container.java b/src/main/java/com/loafle/overflow/probe/container/Container.java index 2d40adb..fdbdfd0 100644 --- a/src/main/java/com/loafle/overflow/probe/container/Container.java +++ b/src/main/java/com/loafle/overflow/probe/container/Container.java @@ -5,8 +5,13 @@ import com.loafle.overflow.probe.container.server.ContainerServer; public class Container { public static void main(String[] args) throws Exception { - ContainerServer server = new ContainerServer(); + if(args.length <= 0) { + System.out.println("first parameter is path of pid file"); + return; + } + String pidPath = args[0]; - server.start(); + ContainerServer server = new ContainerServer(); + server.start(pidPath); } } diff --git a/src/main/java/com/loafle/overflow/probe/container/server/ContainerServer.java b/src/main/java/com/loafle/overflow/probe/container/server/ContainerServer.java index ab9ebd2..abdaea3 100644 --- a/src/main/java/com/loafle/overflow/probe/container/server/ContainerServer.java +++ b/src/main/java/com/loafle/overflow/probe/container/server/ContainerServer.java @@ -5,35 +5,80 @@ import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; -import java.net.InetSocketAddress; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; public class ContainerServer { - private int port = 19390; + private int port; - public ContainerServer() { - } + public ContainerServer() { + } - public void start() throws Exception { - InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", port); + public void start(String pidPath) throws Exception { + EventLoopGroup bossGroup = new NioEventLoopGroup(1); + EventLoopGroup workerGroup = new NioEventLoopGroup(10); + try { + ServerBootstrap b = new ServerBootstrap(); - EventLoopGroup bossGroup = new NioEventLoopGroup(1); - EventLoopGroup workerGroup = new NioEventLoopGroup(10); + b.group(bossGroup, workerGroup) + .channel(NioServerSocketChannel.class) + .childHandler(ContainerConfiguration.channelInitializer()) + .option(ChannelOption.SO_BACKLOG, 128) + .childOption(ChannelOption.SO_KEEPALIVE, true); + + ChannelFuture f = null; + + for (int i = 60000; i < 61000; i++) { try { - ServerBootstrap b = new ServerBootstrap(); - - b.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) - .childHandler(ContainerConfiguration.channelInitializer()) - .option(ChannelOption.SO_BACKLOG, 128) - .childOption(ChannelOption.SO_KEEPALIVE, true); - - ChannelFuture f = b.bind(socketAddress).sync(); - - f.channel().closeFuture().sync(); - } finally { - workerGroup.shutdownGracefully(); - bossGroup.shutdownGracefully(); + f = b.bind("127.0.0.1", i).sync(); + this.port = i; + break; + } catch (Exception e) { + System.out.println(e); + continue; } + } + + if (null == f) { + System.out.println("There is not available port"); + return; + } + + try { + FileOutputStream outputStream = new FileOutputStream(pidPath); + outputStream.write(Integer.toString(this.port).getBytes()); + outputStream.close(); + } catch (IOException e) { + System.out.println(e); + return; + } + + Runtime.getRuntime().addShutdownHook(new Thread() + { + @Override + public void run() + { + ContainerServer.onStop(pidPath); + } + }); + + f.channel().closeFuture().sync(); + } finally { + onStop(pidPath); + + workerGroup.shutdownGracefully(); + bossGroup.shutdownGracefully(); } + } + + private static void onStop(String pidPath) { + try { + File f = new File(pidPath); + f.delete(); + } catch (Exception e) { + System.out.println(e); + } + } }