using Sog; using Sog.ClusterCfg; using System.Collections.Generic; using System.Linq; namespace SMCenter { public class SMProcAppMgr : Singleton { private Dictionary m_appDict = new Dictionary(); private ClusterConfig m_clusterSettings; BusinessConfig m_business; //这个保存正在处理的命令的ClientInfo public ClientInfo m_currHandleClient; private Dictionary m_appNameDict = new Dictionary(); public void ReadClusterConfig() { m_appDict.Clear(); string strSettingsFile = "../cfg/businesscfg/cluster.json"; m_clusterSettings = JsonConfig.parseFile(strSettingsFile); m_business = GetBusinessConfig(SMCenterUtils.Config.businessname); if (m_business == null) { TraceLog.Error("SMProcAppMgr.ReadClusterConfig can not find business {0}", SMCenterUtils.Config.businessname); return; } foreach (var host in m_business.hosts) { ReadHost(host); } } private bool CheckIsDisableAll() { if(m_currHandleClient == null) { return false; } foreach (string disable in m_currHandleClient.m_disableServerID) { string[] splitDisable = disable.Split('.'); if (splitDisable[0] == "*" || splitDisable[1] == "*" || splitDisable[2] == "*") { return true; } } return false; } //这个是获取所有的用于展示 public Dictionary GetAllProc() { return m_appDict; } public void GetAllMatchingProc(string serverId, Dictionary map) { string[] splitSrc = serverId.Split('.'); if (CheckIsDisableAll()) { return; } if (m_currHandleClient != null && m_currHandleClient.m_disableServerID.Contains(serverId)) { return; } foreach (var app in m_appDict) { if(m_currHandleClient != null && m_currHandleClient.m_disableServerID.Contains(app.Value.ServerIDStr)) { continue; } string[] splitDst = app.Value.ServerIDStr.Split('.'); if ((splitSrc[0] == "*" || splitSrc[0] == splitDst[0]) && (splitSrc[1] == "*" || splitSrc[1] == splitDst[1]) && (splitSrc[2] == "*" || splitSrc[2] == splitDst[2]) ) { CmdProcInfo info = new CmdProcInfo(); info.SMApp = app.Value; //只添加开启的服务器 if (info.SMApp.Disable == DisableMode.open) { map.Add(app.Value.AppId, info); } } } } public SMProcApp GetApp(uint appId) { if(m_appDict.ContainsKey(appId)) { return m_appDict[appId]; } return null; } private BusinessConfig GetBusinessConfig(string businessname) { foreach (var bus in m_clusterSettings.bussiness) { if (bus.businessname == businessname) { return bus; } } return null; } private void ReadHost(HostConfig host) { foreach (var group in host.groups) { ReadHostGroup(host, group.groupname); } } private void ReadHostGroup(HostConfig host, string groupname) { GroupDeplayConfig groupconfig = GetGroupDeplayConfig(groupname); if (groupconfig == null) { TraceLog.Error("SMProcAppMgr.ReadHostGroup can not find group {0}", groupname); return; } foreach (var proc in groupconfig.procs) { ReadHostGroupProc(host, proc); } } private GroupDeplayConfig GetGroupDeplayConfig(string groupname) { foreach (var group in m_clusterSettings.groupdeploy) { if (group.groupname == groupname) { return group; } } return null; } public AppConfig GetAppTypeByName(string name, out bool isworldlayer) { isworldlayer = false; name = name.ToLower(); foreach (var app in m_clusterSettings.cluster_layer_apps) { if(app.name.ToLower() == name) { return app; } } foreach (var app in m_clusterSettings.world_layer_apps) { if (app.name.ToLower() == name) { isworldlayer = true; return app; } } return null; } private void ReadHostGroupProc(HostConfig host, GroupProcConfig proc) { bool isworldlayer; AppConfig app = GetAppTypeByName(proc.app, out isworldlayer); if(app == null) { TraceLog.Error("SMProcAppMgr.ReadHostGroup can not find AppConfig {0}", proc.app); return; } SMProcApp smProcApp = SMProcApp.CreateApp(m_business, host, proc, app, isworldlayer); m_appDict.TryAdd(smProcApp.AppId, smProcApp); } } }