using System;
using System.Configuration;
using System.Linq;
namespace Plugin.DeviceConfig
{
///
/// V3.0 仲裁域 / Broker 宿主候选枚举。
/// 候选值从宿主配置文件 appSettings 读取(键:V3.AuthorityIds / V3.BrokerIds,分号分隔),
/// 未配置时回退内置默认值,与 InfrastructureBootstrapper 的本地仲裁域 "local-main" 保持一致。
///
public static class V3AuthorityCatalog
{
private const string FallbackAuthorityIds = "local-main;broker-b01";
private const string FallbackBrokerIds = "broker-b01";
/// 全部合法仲裁域标识(Local 域以 local- 前缀标识,Broker 域即各 Broker 自身)
public static string[] AuthorityIds => Read("V3.AuthorityIds", FallbackAuthorityIds);
/// 全部合法 Broker 宿主标识
public static string[] BrokerIds => Read("V3.BrokerIds", FallbackBrokerIds);
/// 本机默认 Local 仲裁域(候选枚举中第一个 local- 前缀项)
public static string LocalAuthorityId =>
AuthorityIds.FirstOrDefault(a => a.StartsWith("local-", StringComparison.OrdinalIgnoreCase))
?? AuthorityIds[0];
/// 判断给定标识是否为 Local 仲裁域
public static bool IsLocalAuthority(string? authorityId) =>
!string.IsNullOrWhiteSpace(authorityId) &&
authorityId!.StartsWith("local-", StringComparison.OrdinalIgnoreCase);
private static string[] Read(string key, string fallback)
{
string? raw = null;
try
{
raw = ConfigurationManager.AppSettings[key];
}
catch
{
// 宿主未提供配置文件时静默回退默认枚举
}
// net472 引用程序集无 NotNullWhen 标注,显式回退后解引用
var value = string.IsNullOrWhiteSpace(raw) ? fallback : raw!;
return value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim())
.Where(s => s.Length > 0)
.ToArray();
}
}
}