Browse Source

dsad

feature/MultiClientLog
root 1 month ago
parent
commit
b07806ad37
  1. 40
      LTEMvcApp/Controllers/HomeController.cs
  2. 49
      LTEMvcApp/Services/WebSocketManagerService.cs

40
LTEMvcApp/Controllers/HomeController.cs

@ -170,46 +170,6 @@ public class HomeController : Controller
return View();
}
/// <summary>
/// 添加测试客户端配置
/// </summary>
[HttpPost]
public IActionResult AddTestClient()
{
_logger.LogInformation("添加测试客户端配置");
var config = new ClientConfig
{
Name = "测试客户端",
Address = "192.168.13.12:9001",
Ssl = false,
Enabled = true,
ReconnectDelay = 5000,
Password = "test123",
Logs = new ClientLogsConfig
{
Layers = new Dictionary<string, LogLayerConfig>
{
["PHY"] = new LogLayerConfig { Level = "debug", Filter = "debug", MaxSize = 1000, Payload = false },
["RRC"] = new LogLayerConfig { Level = "debug", Filter = "debug", MaxSize = 1000, Payload = false }
},
Signal = true,
Cch = true
}
};
var success = _webSocketManager.AddClientConfig(config);
if (success)
{
TempData["Message"] = "测试客户端配置已添加";
}
else
{
TempData["Error"] = "添加测试客户端配置失败";
}
return RedirectToAction(nameof(Index));
}
/// <summary>
/// 启动测试客户端
/// </summary>

49
LTEMvcApp/Services/WebSocketManagerService.cs

@ -20,7 +20,6 @@ namespace LTEMvcApp.Services
#region 私有字段
private readonly ConcurrentDictionary<string, LTEClientWebSocket> _clients;
private readonly ConcurrentDictionary<string, ClientConfig> _configs;
private readonly ILogger<WebSocketManagerService> _logger;
private readonly IServiceProvider _serviceProvider;
private List<ClientConfig> _testClientConfigs; // 只保留多个测试配置
@ -70,7 +69,6 @@ namespace LTEMvcApp.Services
public WebSocketManagerService(ILogger<WebSocketManagerService> logger, IServiceProvider serviceProvider)
{
_clients = new ConcurrentDictionary<string, LTEClientWebSocket>();
_configs = new ConcurrentDictionary<string, ClientConfig>();
_logger = logger;
_serviceProvider = serviceProvider;
_testClientConfigs = new List<ClientConfig>(); // 初始化测试配置列表
@ -180,44 +178,6 @@ namespace LTEMvcApp.Services
#region 公共方法
/// <summary>
/// 添加客户端配置
/// </summary>
/// <param name="config">客户端配置</param>
/// <returns>是否成功添加</returns>
public bool AddClientConfig(ClientConfig config)
{
if (string.IsNullOrEmpty(config.Name))
{
_logger.LogWarning("尝试添加空名称客户端配置");
return false;
}
_logger.LogInformation($"添加客户端配置: {config.Name}");
_configs[config.Name] = config;
return true;
}
/// <summary>
/// 移除客户端配置
/// </summary>
/// <param name="clientName">客户端名称</param>
/// <returns>是否成功移除</returns>
public bool RemoveClientConfig(string clientName)
{
_logger.LogInformation($"移除客户端配置: {clientName}");
if (_configs.TryRemove(clientName, out _))
{
// 如果客户端正在运行,停止它
if (_clients.TryGetValue(clientName, out var client))
{
client.Stop();
_clients.TryRemove(clientName, out _);
}
return true;
}
return false;
}
/// <summary>
/// 启动客户端
/// </summary>
@ -234,7 +194,7 @@ namespace LTEMvcApp.Services
config = testConfig;
_logger.LogInformation($"使用测试客户端配置: {config.Address}");
}
else if (!_configs.TryGetValue(address, out config))
else
{
_logger.LogWarning($"客户端配置不存在: {address}");
return false;
@ -308,8 +268,7 @@ namespace LTEMvcApp.Services
/// <returns>客户端配置</returns>
public ClientConfig? GetClientConfig(string clientName)
{
_configs.TryGetValue(clientName, out var config);
return config;
return _testClientConfigs.FirstOrDefault(c => c.Name == clientName);
}
/// <summary>
@ -318,7 +277,7 @@ namespace LTEMvcApp.Services
/// <returns>客户端配置列表</returns>
public List<ClientConfig> GetAllClientConfigs()
{
return _configs.Values.ToList();
return _testClientConfigs.ToList();
}
/// <summary>
@ -432,7 +391,7 @@ namespace LTEMvcApp.Services
/// </summary>
public void StartAllConfiguredClients()
{
foreach (var config in _configs.Values)
foreach (var config in _testClientConfigs)
{
if (config.Enabled)
{

Loading…
Cancel
Save