You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
244 lines
8.0 KiB
244 lines
8.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using CoreAgent.ProtocolClient.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace CoreAgent.ProtocolClient.Context.UeStateManager
|
|
{
|
|
/// <summary>
|
|
/// UeIdentifierManager UE信息管理部分
|
|
/// </summary>
|
|
public partial class UeIdentifierManager
|
|
{
|
|
#region UE信息管理方法
|
|
|
|
/// <summary>
|
|
/// 合并两个UE的信息
|
|
/// 性能优化:减少不必要的对象创建
|
|
/// </summary>
|
|
/// <param name="ueId1">第一个UE ID</param>
|
|
/// <param name="ueId2">第二个UE ID</param>
|
|
public void MergeUeInfo(int ueId1, int ueId2)
|
|
{
|
|
if (ueId1 <= 0 || ueId2 <= 0)
|
|
{
|
|
throw new UeInfoException(Math.Min(ueId1, ueId2), "UE ID必须大于0");
|
|
}
|
|
|
|
try
|
|
{
|
|
lock (_lockObject)
|
|
{
|
|
UeList.TryGetValue(ueId1, out var ue1);
|
|
UeList.TryGetValue(ueId2, out var ue2);
|
|
|
|
if (ReferenceEquals(ue1, ue2))
|
|
{
|
|
if (ue2 == null)
|
|
{
|
|
ue2 = CreateUeInfo(ueId2);
|
|
UeList[ueId1] = ue2;
|
|
}
|
|
}
|
|
else if (ue1 == null)
|
|
{
|
|
UeList[ueId1] = ue2;
|
|
}
|
|
else if (ue2 == null)
|
|
{
|
|
UeList[ueId2] = ue1;
|
|
SyncPlmnFromExistingUe(ueId1, ueId2);
|
|
}
|
|
else
|
|
{
|
|
UeList[ueId2] = ue1;
|
|
SyncPlmnFromExistingUe(ueId1, ueId2);
|
|
}
|
|
|
|
_logger?.LogDebug("合并UE信息: UE {UeId1} 和 UE {UeId2}", ueId1, ueId2);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger?.LogError(ex, "合并UE信息失败: UE {UeId1} 和 UE {UeId2}", ueId1, ueId2);
|
|
throw new UeInfoException(Math.Min(ueId1, ueId2), $"合并UE信息失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建新的UE信息
|
|
/// </summary>
|
|
/// <param name="ueId">UE ID</param>
|
|
/// <returns>创建的UE信息</returns>
|
|
private UEInfo CreateUeInfo(int ueId)
|
|
{
|
|
var ueInfo = new UEInfo
|
|
{
|
|
UeId = ueId,
|
|
Caps = new ProtocolCaps { UeId = ueId }
|
|
};
|
|
|
|
UeList[ueId] = ueInfo;
|
|
_logger?.LogDebug("创建新的UE信息: UE {UeId}", ueId);
|
|
return ueInfo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取UE信息
|
|
/// </summary>
|
|
/// <param name="ueId">UE ID</param>
|
|
/// <returns>UE信息,如果不存在则返回null</returns>
|
|
public UEInfo? GetUeInfo(int ueId)
|
|
{
|
|
if (ueId <= 0)
|
|
{
|
|
_logger?.LogWarning("尝试获取无效UE ID的信息: {UeId}", ueId);
|
|
return null;
|
|
}
|
|
|
|
lock (_lockObject)
|
|
{
|
|
return UeList.TryGetValue(ueId, out var ueInfo) ? ueInfo : null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取UE能力信息,如果不存在则创建
|
|
/// 同时根据ImsiToUeId映射表更新IMSI信息
|
|
/// </summary>
|
|
/// <param name="ueId">UE ID</param>
|
|
/// <returns>UE能力信息</returns>
|
|
public ProtocolCaps GetUeCaps(int ueId)
|
|
{
|
|
if (ueId <= 0)
|
|
{
|
|
throw new UeInfoException(ueId, "UE ID必须大于0");
|
|
}
|
|
|
|
lock (_lockObject)
|
|
{
|
|
if (!UeList.TryGetValue(ueId, out var ueInfo) || ueInfo == null)
|
|
{
|
|
ueInfo = CreateUeInfo(ueId);
|
|
}
|
|
|
|
if (ueInfo.Caps == null)
|
|
{
|
|
ueInfo.Caps = new ProtocolCaps { UeId = ueId };
|
|
}
|
|
|
|
// 根据ImsiToUeId映射表更新IMSI信息
|
|
UpdateImsiFromMapping(ueId, ueInfo);
|
|
|
|
return ueInfo.Caps;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ImsiToUeId映射表更新UE的IMSI信息
|
|
/// 性能优化:使用LINQ提高查找效率
|
|
/// </summary>
|
|
/// <param name="ueId">UE ID</param>
|
|
/// <param name="ueInfo">UE信息对象</param>
|
|
private void UpdateImsiFromMapping(int ueId, UEInfo ueInfo)
|
|
{
|
|
var imsiMapping = ImsiToUeId.FirstOrDefault(kvp => kvp.Value.Contains(ueId));
|
|
if (!string.IsNullOrEmpty(imsiMapping.Key))
|
|
{
|
|
ueInfo.Imsi = imsiMapping.Key;
|
|
_logger?.LogDebug("更新UE {UeId} 的IMSI信息: {Imsi}", ueId, imsiMapping.Key);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从现有UE ID同步IMSI信息到新的UE ID
|
|
/// </summary>
|
|
/// <param name="existingUeId">现有UE ID</param>
|
|
/// <param name="newUeId">新的UE ID</param>
|
|
private void SyncImsiFromExistingUe(int existingUeId, int newUeId)
|
|
{
|
|
var imsiMapping = ImsiToUeId.FirstOrDefault(kvp => kvp.Value.Contains(existingUeId));
|
|
if (!string.IsNullOrEmpty(imsiMapping.Key))
|
|
{
|
|
SetImsi(newUeId, imsiMapping.Key);
|
|
_logger?.LogDebug("同步IMSI信息: 从UE {ExistingUeId} 到 UE {NewUeId}: {Imsi}",
|
|
existingUeId, newUeId, imsiMapping.Key);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从现有UE ID同步PLMN信息到新的UE ID
|
|
/// </summary>
|
|
/// <param name="existingUeId">现有UE ID</param>
|
|
/// <param name="newUeId">新的UE ID</param>
|
|
private void SyncPlmnFromExistingUe(int existingUeId, int newUeId)
|
|
{
|
|
var plmnMapping = PlmnToUeId.FirstOrDefault(kvp => kvp.Value.Contains(existingUeId));
|
|
if (!string.IsNullOrEmpty(plmnMapping.Key))
|
|
{
|
|
SetPlmn(newUeId, plmnMapping.Key);
|
|
_logger?.LogDebug("同步PLMN信息: 从UE {ExistingUeId} 到 UE {NewUeId}: {Plmn}",
|
|
existingUeId, newUeId, plmnMapping.Key);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取UeList中UEInfo.UeId与Caps.UeId组合的字典
|
|
/// </summary>
|
|
/// <returns>包含UE ID和Caps UeId组合的字典,键为UEInfo.UeId,值为Caps.UeId</returns>
|
|
public Dictionary<int, int> GetUeIdToCapsUeIdMapping()
|
|
{
|
|
lock (_lockObject)
|
|
{
|
|
var mapping = new Dictionary<int, int>();
|
|
|
|
foreach (var kvp in UeList)
|
|
{
|
|
var ueId = kvp.Key;
|
|
var ueInfo = kvp.Value;
|
|
|
|
if (ueInfo?.Caps != null)
|
|
{
|
|
mapping[ueId] = ueInfo.Caps.UeId;
|
|
}
|
|
}
|
|
|
|
return mapping;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量获取UE信息
|
|
/// 性能优化:批量处理减少锁开销
|
|
/// </summary>
|
|
/// <param name="ueIds">UE ID列表</param>
|
|
/// <returns>UE信息字典</returns>
|
|
public Dictionary<int, UEInfo> GetUeInfoBatch(IEnumerable<int> ueIds)
|
|
{
|
|
if (ueIds == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(ueIds));
|
|
}
|
|
|
|
var result = new Dictionary<int, UEInfo>();
|
|
lock (_lockObject)
|
|
{
|
|
foreach (var ueId in ueIds)
|
|
{
|
|
if (UeList.TryGetValue(ueId, out var ueInfo))
|
|
{
|
|
result[ueId] = ueInfo;
|
|
}
|
|
}
|
|
}
|
|
|
|
_logger?.LogDebug("批量获取UE信息,共获取 {Count} 个UE的信息", result.Count);
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|