using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CoreAgent.ProtocolClient.Enums;
using CoreAgent.ProtocolClient.Models;
using Microsoft.Extensions.Logging;
namespace CoreAgent.ProtocolClient.Context.UeStateManager
{
///
/// UE标识符管理类
/// 负责管理UE信息、标识符映射、时间戳等状态
/// 优化版本:增强异常处理、性能优化、代码拆分
///
public partial class UeIdentifierManager
{
#region 公共属性 - 映射表
///
/// TMSI到UE ID的映射表
///
public Dictionary TmsiToUeId { get; set; } = new();
///
/// 请求TMSI到UE ID的映射表
///
public Dictionary RequestTmsiToUeId { get; set; } = new();
///
/// IMSI到UE ID的映射表
///
public Dictionary> ImsiToUeId { get; set; } = new();
///
/// PLMN到UE ID的映射表
///
public Dictionary> PlmnToUeId { get; set; } = new();
///
/// Service Request TMSI到UE ID的映射表
///
public Dictionary> SrTmsiToUeId { get; set; } = new();
///
/// RNTI到UE ID的映射表
///
public Dictionary RntiToUeId { get; set; } = new();
///
/// UE信息列表,以UE ID为键
///
public Dictionary UeList { get; set; } = new();
#endregion
#region 公共属性 - 时间戳和小区信息
///
/// 最后处理的时间戳
///
public long LastTimestamp { get; set; }
///
/// 时间戳偏移量
///
public long TimestampOffset { get; set; }
///
/// 最后处理的小区ID
///
public int? LastCell { get; set; }
#endregion
#region 私有字段 - 字符串缓存和性能优化
///
/// 字符串到ID的映射缓存
///
private readonly Dictionary _stringIdCache = new();
///
/// ID到字符串的列表缓存
///
private readonly List _stringList = new();
///
/// 日志记录器
///
private readonly ILogger _logger;
///
/// 缓存统计信息
///
private readonly Dictionary _cacheStats = new();
///
/// 线程锁对象
///
private readonly object _lockObject = new object();
#endregion
#region 构造函数
///
/// 初始化UE标识符管理器
///
/// 日志记录器
public UeIdentifierManager(ILogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
InitializeProtocolTypeStrings();
InitializeCacheStats();
}
#endregion
#region 私有初始化方法
///
/// 初始化协议类型字符串缓存
///
private void InitializeProtocolTypeStrings()
{
try
{
string[] protocolTypeNames = Enum.GetNames(typeof(LogChannelId));
foreach (var protocolTypeName in protocolTypeNames)
{
StringToId(protocolTypeName);
}
_logger.LogDebug("协议类型字符串缓存初始化完成,共 {Count} 个类型", protocolTypeNames.Length);
}
catch (Exception ex)
{
_logger.LogError(ex, "初始化协议类型字符串缓存失败");
throw new CacheException("ProtocolTypeStrings", "初始化协议类型字符串缓存失败", ex);
}
}
///
/// 初始化缓存统计信息
///
private void InitializeCacheStats()
{
_cacheStats["StringToId"] = 0;
_cacheStats["IdToString"] = 0;
_cacheStats["CacheHits"] = 0;
_cacheStats["CacheMisses"] = 0;
}
#endregion
#region 公共方法 - 统计和监控
///
/// 获取缓存统计信息
///
/// 缓存统计信息字典
public Dictionary GetCacheStats()
{
lock (_lockObject)
{
return new Dictionary(_cacheStats);
}
}
///
/// 获取映射表统计信息
///
/// 映射表统计信息
public Dictionary GetMappingStats()
{
return new Dictionary
{
["TmsiToUeId"] = TmsiToUeId.Count,
["RequestTmsiToUeId"] = RequestTmsiToUeId.Count,
["ImsiToUeId"] = ImsiToUeId.Count,
["PlmnToUeId"] = PlmnToUeId.Count,
["SrTmsiToUeId"] = SrTmsiToUeId.Count,
["RntiToUeId"] = RntiToUeId.Count,
["UeList"] = UeList.Count
};
}
///
/// 清理缓存
///
public void ClearCache()
{
lock (_lockObject)
{
_stringIdCache.Clear();
_stringList.Clear();
InitializeCacheStats();
_logger.LogInformation("字符串缓存已清理");
}
}
#endregion
}
}