From 912ae30ff0aa9d65d9df814f5018d4ee3f264e60 Mon Sep 17 00:00:00 2001 From: chuzhongzai Date: Sun, 31 Aug 2025 14:08:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=94=A4=E9=86=92=E7=AB=AF?= =?UTF-8?q?=E5=8F=A3=E5=85=81=E8=AE=B8=E6=BC=82=E7=A7=BB=EF=BC=8C=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E5=94=A4=E9=86=92=E7=AB=AF=E5=8F=A3=E8=A2=AB=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E7=A8=8B=E5=BA=8F=E5=8D=A0=E7=94=A8=E6=97=B6=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=AE=9E=E7=8E=B0=E5=94=A4=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- src/main/java/lion/CustomUtil.java | 13 +++++++++++++ src/main/java/lion/storageNode.java | 25 ++++++++++++++++++++----- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 840afc1..05b1f4c 100644 --- a/pom.xml +++ b/pom.xml @@ -97,7 +97,7 @@ --gc=G1 --enable-url-protocols=https -H:IncludeResources="simplelogger.properties" - --initialize-at-build-time=org.slf4j.simple.SimpleLogger,org.slf4j.simple.SimpleLoggerFactory + --initialize-at-build-time=org.slf4j.simple.SimpleLogger,org.slf4j.simple.SimpleLoggerFactory,org.slf4j.simple.SimpleLoggerConfiguration -H:ReflectionConfigurationFiles=src/main/resources/reflect-config.json diff --git a/src/main/java/lion/CustomUtil.java b/src/main/java/lion/CustomUtil.java index 6621e72..3c7ca07 100644 --- a/src/main/java/lion/CustomUtil.java +++ b/src/main/java/lion/CustomUtil.java @@ -3,6 +3,8 @@ package lion; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; +import java.io.IOException; +import java.net.ServerSocket; import java.util.concurrent.atomic.AtomicInteger; @@ -12,4 +14,15 @@ public class CustomUtil { public static AtomicInteger counter = new AtomicInteger(); public static ObjectMapper objectMapper = new ObjectMapper(); + + public static int _findIdlePort(int port) { + for(int i=port; i<65535; i++){ + try(ServerSocket ignored = new ServerSocket(i)){ + ignored.close(); + return i; + }catch (IOException ignored) { + } + } + return -1; + } } diff --git a/src/main/java/lion/storageNode.java b/src/main/java/lion/storageNode.java index 77f0efd..db521d1 100644 --- a/src/main/java/lion/storageNode.java +++ b/src/main/java/lion/storageNode.java @@ -54,6 +54,8 @@ public class storageNode { counter = 0; promises = new HashMap<>(); + int real_port = CustomUtil._findIdlePort(26321); + channelFuture = new ServerBootstrap() .channel(NioServerSocketChannel.class) .group(new NioEventLoopGroup()) @@ -64,12 +66,25 @@ public class storageNode { channel.pipeline().addLast(new MessageCodec()); channel.pipeline().addLast(new MyChannelInboundHandlerAdapter(tempQueue)); } - }) - .bind(26321); + }).bind(real_port); + log.info("listening on port {}", real_port); - try(Socket socket = new Socket()){ - socket.connect(new InetSocketAddress("lionwebsite.xyz", 26322)); - } catch (Exception ignored) {} + int i; + for(i=0; i<=20; i++) { + try (Socket socket = new Socket()) { + socket.setSoTimeout(3000); + log.info("wake up main server on port {}", 26322 + i); + socket.connect(new InetSocketAddress("lionwebsite.xyz", 26322 + i)); + byte[] bytes = socket.getInputStream().readAllBytes(); + if(bytes.length > 0 && new String(bytes).equals("lionwebsite")) { + break; + } + } catch (Exception ignored) { + } + } + if (i==20) { + log.info("server connect failed"); + } downloadCheckService = new DownloadCheckService(queue, promises); checkThreadPool = Executors.newScheduledThreadPool(1); checkThreadPool.scheduleAtFixedRate(this::mainThread, 5, 5, TimeUnit.SECONDS);