监听唤醒端口允许漂移,防止唤醒端口被其他程序占用时无法实现唤醒

This commit is contained in:
chuzhongzai 2025-08-31 14:08:04 +08:00
parent 95329a5603
commit 912ae30ff0
3 changed files with 34 additions and 6 deletions

View File

@ -97,7 +97,7 @@
<arg>--gc=G1</arg> <arg>--gc=G1</arg>
<arg>--enable-url-protocols=https</arg> <arg>--enable-url-protocols=https</arg>
<arg>-H:IncludeResources="simplelogger.properties"</arg> <arg>-H:IncludeResources="simplelogger.properties"</arg>
<arg>--initialize-at-build-time=org.slf4j.simple.SimpleLogger,org.slf4j.simple.SimpleLoggerFactory</arg> <arg>--initialize-at-build-time=org.slf4j.simple.SimpleLogger,org.slf4j.simple.SimpleLoggerFactory,org.slf4j.simple.SimpleLoggerConfiguration</arg>
<arg>-H:ReflectionConfigurationFiles=src/main/resources/reflect-config.json</arg> <arg>-H:ReflectionConfigurationFiles=src/main/resources/reflect-config.json</arg>
</buildArgs> </buildArgs>
<metadataRepository> <metadataRepository>

View File

@ -3,6 +3,8 @@ package lion;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data; import lombok.Data;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -12,4 +14,15 @@ public class CustomUtil {
public static AtomicInteger counter = new AtomicInteger(); public static AtomicInteger counter = new AtomicInteger();
public static ObjectMapper objectMapper = new ObjectMapper(); 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;
}
} }

View File

@ -54,6 +54,8 @@ public class storageNode {
counter = 0; counter = 0;
promises = new HashMap<>(); promises = new HashMap<>();
int real_port = CustomUtil._findIdlePort(26321);
channelFuture = new ServerBootstrap() channelFuture = new ServerBootstrap()
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.group(new NioEventLoopGroup()) .group(new NioEventLoopGroup())
@ -64,12 +66,25 @@ public class storageNode {
channel.pipeline().addLast(new MessageCodec()); channel.pipeline().addLast(new MessageCodec());
channel.pipeline().addLast(new MyChannelInboundHandlerAdapter(tempQueue)); channel.pipeline().addLast(new MyChannelInboundHandlerAdapter(tempQueue));
} }
}) }).bind(real_port);
.bind(26321); log.info("listening on port {}", real_port);
int i;
for(i=0; i<=20; i++) {
try (Socket socket = new Socket()) { try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress("lionwebsite.xyz", 26322)); socket.setSoTimeout(3000);
} catch (Exception ignored) {} 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); downloadCheckService = new DownloadCheckService(queue, promises);
checkThreadPool = Executors.newScheduledThreadPool(1); checkThreadPool = Executors.newScheduledThreadPool(1);
checkThreadPool.scheduleAtFixedRate(this::mainThread, 5, 5, TimeUnit.SECONDS); checkThreadPool.scheduleAtFixedRate(this::mainThread, 5, 5, TimeUnit.SECONDS);