/// <summary> /// Measure the state of our system /// </summary> private static void MeasureSystemState(object state) { while (true) { long Mem; MM_System_Profiling.MEMORYSTATUSEX MemStatus; SystemInformation.CPUUsage = MM_System_Profiling.GetCPUUsage(out Mem, out MemStatus); SystemInformation.SystemFreeMemory = MemStatus.ullAvailPhys; SystemInformation.SystemTotalMemory = MemStatus.ullTotalPhys; SystemInformation.ApplicationMemoryUse = Mem; SystemInformation.LineCount = LineData.Length; SystemInformation.LoadCount = LoadData.Length; SystemInformation.BusCount = BusData.Count; SystemInformation.LineOutageCount = LineOutageData.Count; SystemInformation.TransformerOutageCount = TransformerOutageData.Count; SystemInformation.UnitOutageCount = UnitOutageData.Count; SystemInformation.UnitCount = UnitData.Length; SystemInformation.UnitGenCount = UnitGenData.Length; SystemInformation.InterfaceCount = InterfaceData.Length; SystemInformation.BasecaseCount = BasecaseViolationData.Count; SystemInformation.ContingencyCount = ContingencyViolationData.Count; SystemInformation.TransformerCount = TransformerData.Length; SystemInformation.PhaseShifterCount = TransformerData.Length; SystemInformation.BreakerSwitchCount = BreakerSwitchData.Length; SystemInformation.TieCount = TieData.Length; SystemInformation.SVCCount = SVCData.Length; SystemInformation.UnitTypeCount = UnitTypeGenerationData.Length; SystemInformation.SystemCount = SystemWideGenerationData.Length; SystemInformation.LMPCount = LmpData.Length; SystemInformation.FlowgateCount = FlowgateData.Length; SystemInformation.AnalogCount = AnalogMeasurementData.Length; SystemInformation.StateCount = StateMeasurementData.Length; SystemInformation.SCADAStatusCount = SCADAStatuses.Length; SystemInformation.SCADAAnalogCount = SCADAAnalogs.Length; SystemInformation.LoadForecastCount = LoadForecastData.Length; SystemInformation.UnitSimulationCount = UnitSimulationData.Length; SystemInformation.SynchroscopeCount = SynchroscopeData.Length; SystemInformation.UnitControlCount = UnitControlStatusData.Count; SystemInformation.ShuntCompensatorCount = ShuntCompensatorData.Length; SystemInformation.ZBRCount = ZBRData.Length; SystemInformation.UserCount = ConnectedUsers.Count; SystemInformation.CommandCount = EMSCommands == null ? -1 : EMSCommands.Count; SystemInformation.OperatorshipUpdateCount = OperatorshipUpdates.Count; SystemInformation.LastTEDeAddress = LastTEDEAddress; SystemInformation.LastTEDeUpdate = LastTEDEUpdate; if (SimulatorTimeData.Length > 0) { SystemInformation.SimulationTime = SimulatorTimeData[0].Simulation_Time; SystemInformation.SimulationStatus = SimulatorTimeData[0].Status; } Thread.Sleep(1000); } }
/// <summary> /// Ping our Macomber Map servers /// </summary> internal static void StartPingMacomberMapServers() { Task.Run(() => { var queryUdp = new UdpClient(); queryUdp.ExclusiveAddressUse = false; queryUdp.Client.ReceiveTimeout = 500; queryUdp.Client.SendTimeout = 500; queryUdp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false); //queryUdp.Client.Bind(new IPEndPoint(IPAddress.Any, Settings.Default.MacomberMapServerQueryPort)); int CurrentIter = 0; //if (Settings.Default.ServersToPing.IndexOf("localhost", StringComparison.CurrentCultureIgnoreCase) < 0) // Settings.Default.ServersToPing += ",localhost"; while (true) { //First, once every 10 seconds, send our commands out for manual servers if (CurrentIter == 0) { foreach (String str in Settings.Default.ServersToPing.Split(',')) { try { foreach (IPAddress Addr in Dns.GetHostEntry(str).AddressList) { if (Addr.AddressFamily == AddressFamily.InterNetwork) { byte[] OutBytes = new UTF8Encoding(false).GetBytes("Macomber Map Client|" + Application.ProductVersion + "|" + Environment.MachineName + "|"); queryUdp.Send(OutBytes, OutBytes.Length, new IPEndPoint(Addr, Settings.Default.MacomberMapServerQueryPort)); Task.Run(async() => { var response = await queryUdp.ReceiveAsync(); HandleUDPMessage(response); }).Wait(queryUdp.Client.ReceiveTimeout); } } } catch (SocketException ex) { if (ex.ErrorCode == 10054) { Debug.WriteLine("Server not available: " + str); } } } } catch { } //Every 5 seconds, ping our clients if (CurrentIter % 5 == 0) { using (Ping p = new Ping()) { foreach (MM_Server_Information Server in MMServers.Values.ToArray()) { try { var reply = p.Send(Server.ServerURI.Host, 25); Server.PingTime = reply.RoundtripTime; } catch { Server.PingTime = -1; } } p.Dispose(); } } //Capture our CPU usage try { long Mem; MM_System_Profiling.MEMORYSTATUSEX MemStatus; ClientStatus.CPUUsage = MM_System_Profiling.GetCPUUsage(out Mem, out MemStatus); ClientStatus.SystemFreeMemory = MemStatus.ullAvailPhys; ClientStatus.SystemTotalMemory = MemStatus.ullTotalPhys; ClientStatus.OpenWindows = -1; ClientStatus.ApplicationMemoryUse = Mem; //Send CPU status every 2 seconds if (CurrentIter % 2 == 0 && Client != null) { Client.ReportUserStatus(ClientStatus); } } catch { } //Wait a second Thread.Sleep(1500); }