using com.itac.mes.commonsmt; using com.itac.mes.commonsmt.data; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; namespace com.itac.mes.commonsmt { class FailoverHostList { // this list is cyclically updated as long as a mesService Endpoint is active and not closed private List failoverHosts; // this is the host all current calls are going to private FailoverHost activeHost; public event MyEventHandler OnLog; public FailoverHostList(MyEventHandler log) { this.OnLog = log; } public void log(TraceEventType traceEventType, string message) { if (OnLog == null) { return; } MyEventArgs myEventArgs = new MyEventArgs(); myEventArgs.message = "iTAC.MES.Suite SMT-Plugin:" + message; myEventArgs.traceEventType = traceEventType; OnLog(this, myEventArgs); } public void setFailover(FailoverHost[] newFailoverHosts) { // protocol all changes Boolean newHosts = false; if (newFailoverHosts == null) { activeHost = null; this.failoverHosts = null; return; } if (this.failoverHosts == null) { // initial setting log(TraceEventType.Information, "setting initial failover hosts :"); foreach (FailoverHost item in newFailoverHosts) { log(TraceEventType.Information, ": " + item.getHostname()); } } else { // whats new, whats removed... Dictionary currentHosts = new Dictionary(); foreach (FailoverHost item in this.failoverHosts) { currentHosts.Add(item.getHostname(), item); } foreach (FailoverHost item in newFailoverHosts) { if (currentHosts.ContainsKey(item.getHostname())) { // enthalten, also aus dieser Liste lösche currentHosts.Remove(item.getHostname()); } else { // bisher nicht enthalten, also neu Console.Write("new failover host " + item.getHostname()); newHosts = true; } } if (currentHosts.Count > 0) { log(TraceEventType.Information, "some failover host removed "); foreach (FailoverHost removedHost in currentHosts.Values) { log(TraceEventType.Information, " - " + removedHost.getHostname()); } } else { if (!newHosts) { log(TraceEventType.Information, "failover host list remains unchanged "); foreach (FailoverHost item in newFailoverHosts) { log(TraceEventType.Information, ":" + item.getHostname()); } } } } this.failoverHosts = newFailoverHosts.ToList(); // der erste Host in der Liste ist auch der Host, zu dem die aufrufe ohne Failover gehen!!! if (activeHost == null) { if (newFailoverHosts.Length > 0) { activeHost = newFailoverHosts[0]; } else { // clean default host activeHost = null; } } } /** * Removes a host from the failover list. * * If the host is the currently active host {@link #getActiveHost()} then the current host is set to null
* If the host is not in the host list then nothing happens * * @param host * the host to be removed; do nothing if host is null */ public void remove(FailoverHost host) { if (host == null) { return; } log(TraceEventType.Information, "remove host " + host.getHostname() + " from failover list"); for (int i = 0; i < getList().Count; i++) { FailoverHost item = getList()[i]; if (item.getHostname().Equals(host.getHostname())) { getList().Remove(item); log(TraceEventType.Information, "host " + item.getHostname() + " removed from failover list"); } } if (activeHost != null && activeHost.getHostname().Equals(host.getHostname())) { setActiveHost(null); } } /** * @return the number of hosts in the failover list */ public int size() { return failoverHosts == null ? 0 : failoverHosts.Count; } /** * @return the list of currently known host to fail over */ public List getList() { return failoverHosts == null ? new List() : failoverHosts; } public FailoverHost getActiveHost() { return activeHost; } public void setActiveHost(FailoverHost host) { if (host == null) { log(TraceEventType.Information, "clear active failover hosts"); } else { log(TraceEventType.Information, "set " + host.getHostname() + " as active failover hosts"); } this.activeHost = host; } public FailoverHost getNextFailoverHost() { FailoverHost failoverHost = null; log(TraceEventType.Information, "scanning for other failover hosts. list contains a total of " + size() + " hosts"); foreach (FailoverHost host in getList()) { if (host.Equals(getActiveHost())) { continue; } failoverHost = host; log(TraceEventType.Information, "next failover hosts is " + host.getHostname()); break; } // den nächsten aus der Liste ohne den defaultHost if (size() == 0) { log(TraceEventType.Information, "No more failover host available!"); } return failoverHost; } } }