feat: update forward server

This commit is contained in:
yarnom 2025-07-29 14:56:08 +08:00
parent bd9a6e575e
commit 006979df31

View File

@ -0,0 +1,123 @@
package com.imdroid.sideslope.config;
import com.imdroid.sideslope.server.tcp.DeviceTcpPortManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Component
public class RtcmPortConfigManager {
private static final Logger logger = LoggerFactory.getLogger(RtcmPortConfigManager.class);
@Value("${rtcm.config.file:/root/beidou/config/rtcm_port}")
private String configFilePath;
@Autowired
private DeviceTcpPortManager deviceTcpPortManager;
// 当前配置的设备和端口映射
private final Map<String, Integer> currentConfig = new ConcurrentHashMap<>();
@PostConstruct
public void init() {
// 确保配置目录存在
File configFile = new File(configFilePath);
if (!configFile.getParentFile().exists()) {
configFile.getParentFile().mkdirs();
}
// 初始加载配置
loadConfig();
}
@Scheduled(fixedRate = 60000) // 每60秒执行一次
public void scheduledLoadConfig() {
loadConfig();
}
private void loadConfig() {
Map<String, Integer> newConfig = new HashMap<>();
File configFile = new File(configFilePath);
if (!configFile.exists()) {
logger.info("Config file not found: {}, will create when needed", configFilePath);
return;
}
try (BufferedReader reader = new BufferedReader(new FileReader(configFile))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.isEmpty() || line.startsWith("#")) {
continue; // 跳过空行和注释
}
String[] parts = line.split("\\s+");
if (parts.length >= 2) {
try {
String deviceId = parts[0];
int port = Integer.parseInt(parts[1]);
newConfig.put(deviceId, port);
} catch (NumberFormatException e) {
logger.warn("Invalid port number in config file: {}", line);
}
}
}
} catch (IOException e) {
logger.error("Error reading config file: {}", configFilePath, e);
return;
}
// 找出需要移除的设备
Set<String> devicesToRemove = new HashSet<>(currentConfig.keySet());
devicesToRemove.removeAll(newConfig.keySet());
// 找出新增或更新的设备
Map<String, Integer> devicesToUpdate = new HashMap<>(newConfig);
// 移除不再需要的设备
for (String deviceId : devicesToRemove) {
deviceTcpPortManager.removeDevice(deviceId);
logger.info("Removed device {} from TCP port manager", deviceId);
}
// 更新或添加新的设备配置
for (Map.Entry<String, Integer> entry : devicesToUpdate.entrySet()) {
String deviceId = entry.getKey();
int port = entry.getValue();
// 如果端口发生变化需要重新创建
Integer currentPort = currentConfig.get(deviceId);
if (currentPort == null || !currentPort.equals(port)) {
if (currentPort != null) {
deviceTcpPortManager.removeDevice(deviceId);
}
deviceTcpPortManager.addDevice(deviceId, port);
logger.info("Updated device {} to use port {}", deviceId, port);
}
}
// 更新当前配置
currentConfig.clear();
currentConfig.putAll(newConfig);
logger.info("Config updated, current devices: {}", currentConfig);
}
public boolean isDeviceEnabled(String deviceId) {
return currentConfig.containsKey(deviceId);
}
public Integer getDevicePort(String deviceId) {
return currentConfig.get(deviceId);
}
}