/// <summary> /// 加入一个Remoting宿主 /// </summary> /// <param name="host"></param> public void AddHost(Host host) { if (_HostCollection.ContainsKey(host.Name) == false) { _HostCollection.Add(host.Name, host); } }
//解析Remoting宿主 void ParseHostSection(XmlNode root) { string defaultHostName = base.GetStringAttribute(root, "default"); Host firstHost = null; foreach (XmlNode node in root.ChildNodes) { if (node.NodeType == XmlNodeType.Comment) continue; if (node.Name != "add") continue; if (node.Attributes["name"] == null || node.Attributes["url"] == null || string.IsNullOrEmpty(node.Attributes["name"].Value) || string.IsNullOrEmpty(node.Attributes["url"].Value)) continue; Host host = new Host(); host.Name = node.Attributes["name"].Value; host.Url = node.Attributes["url"].Value; ci.AddHost(host); if (firstHost == null) firstHost = host; } if (ci.HostCollection.Count == 0) { throw new Exception("没有配置Remoting宿主"); } else { if (ci.HostCollection.ContainsKey(defaultHostName)) { ci.DefaultHost = ci.HostCollection[defaultHostName]; } else { ci.DefaultHost = firstHost; } } }
void DoCheck(object obj) { ClientConfig ci = obj as ClientConfig; Host[] hosts = new Host[ci.HostCollection.Count]; ci.HostCollection.Values.CopyTo(hosts, 0); int interval = ci.PollingInterval * 1000; while (true) { StringBuilder sb = new StringBuilder(); foreach (Host host in hosts) { #region Remoting调用方式检测Host try { //若服务不可用,耗时比较长,需改用其他方式检测 string objectUri = "Kalman.Remoting.HostMonitorService"; HostMonitorService srv = RCHelper.Instance.GetWellKnownObject<HostMonitorService>(host.Url, objectUri); //Trace.WriteLine(srv.GetCurrentDate()); //HostMonitorService srv = (HostMonitorService)RemotingServices.Connect(typeof(HostMonitorService), string.Format("{0}/{1}", host.Url, objectUri)); //srv.GetCurrentDate(); sb.Append(host.Url); sb.Append("|"); } catch (Exception ex) { string key = string.Format("{0}|{1}", ci.Name, host.Name); lock (hostStateInfo.SyncRoot) { if (hostStateInfo.ContainsKey(key)) hostStateInfo[key] = ex.Message; else hostStateInfo.Add(key, ex.Message); } //Trace.WriteLine(key+"."+ex.Message); } #endregion #region //string[] ss = host.Url.TrimStart("tcp://".ToCharArray()).Split(':'); //string ip = ss[0]; //int port = int.Parse(ss[1]); //string errMsg = string.Empty; //bool flag = NetUtil.TestIpAndPort(ip, port, out errMsg); //if (flag) //{ // sb.Append(host.Url); // sb.Append("|"); //} //else //{ // string key = string.Format("{0}|{1}", ci.Name, host.Name); // lock (hostStateInfo.SyncRoot) // { // if (hostStateInfo.ContainsKey(key)) // hostStateInfo[key] = errMsg; // else // hostStateInfo.Add(key, errMsg); // } //} #endregion }//foreach string s = sb.ToString().TrimEnd('|'); //更新可用Host列表 lock (ht.SyncRoot) { if (ht.ContainsKey(ci.Name)) { ht[ci.Name] = s; } else { ht.Add(ci.Name, s); } } Thread.Sleep(interval); } }