ing
This commit is contained in:
		
							parent
							
								
									f578e72c43
								
							
						
					
					
						commit
						d6a7dc0670
					
				
							
								
								
									
										2
									
								
								.vscode/launch.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.vscode/launch.json
									
									
									
									
										vendored
									
									
								
							@ -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",
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user