protected override void OnStart() { try { SyncDatas syncDatas = new SyncDatas(); syncDatas.Process(); } catch (Exception ex) { Console.WriteLine("error : " + ex); } }
private void GetCommands() { while (!kill) { if (talker != null) { Command cmd = talker.GetCommand(); if (cmd != null) { lastMessage = DateTime.Now; ServiceActive = true; switch (cmd.CommandType) { case Commands.GetResultsResponse: Results results = (Results)cmd.Data; if (results != null) { counter++; if (results.Count > 0) UpdateDataGridViewWithResults(results.ToEnumerable()); } break; case Commands.GetConfigurationResponse: configurationData = (ConfigurationData)cmd.Data; configurationLoaded = true; if (serverMonitorList.InvokeRequired) { Invoke(new MethodInvoker(() => serverMonitorList.Rows.Clear())); } else serverMonitorList.Rows.Clear(); PopulateGridView(); configurationData.ExportToXml("configuration.xml"); //NOTE: may need to convert this to some sort of overlay window that isn't a modal dialog box so it doesn't block this thread MessageBox.Show("Configuration Retrived from the RemoteMon Service.", "Configuration Retrieved", MessageBoxButtons.OK); break; case Commands.UpdateConfigurationResponse: if ((Boolean)cmd.Data) MessageBox.Show("Configuration Updated successfully.", "Service Configuration Updated", MessageBoxButtons.OK); break; case Commands.ServiceStatus: //Boolean oldValue = serviceActive; ServiceActive = (Boolean)cmd.Data; statusBarLabelServiceStatus.Text = "Service Status: " + (ServiceActive ? "Running" : "Stopped"); if (ServiceActive && MonitorScheduler.Scheduler.Running) { Logger.Instance.Log(this.GetType(), LogType.Info, "Stopping Monitoring"); MonitorScheduler.Scheduler.Kill(); } break; case Commands.GetConfiguration: //if the service requests a configuration, then pop a dialog box asking whether or not to give it. if (!ignoreServiceConfigRequest) { DialogResult dr = MessageBox.Show( "The RemoteMon Service has requested your configuration, because it does not have any. \n\r\n\rDo you want to upload your configuration to the service?", "Service needs a configuration", MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { talker.SendCommand(new Command { CommandType = Commands.GetConfigurationResponse, Data = configurationData, ToNamespace = cmd.FromNamespace, ToIp = cmd.FromIp }); synced = false; ignoreServiceConfigRequest = false; } else ignoreServiceConfigRequest = true; } break; case Commands.ResultsSyncResponse: if (cmd.Data != null && configurationLoaded) { SyncDatas servicedatas = (SyncDatas)cmd.Data; SyncDatas localdatas = new SyncDatas(); counter = servicedatas.Counter; foreach (IMonitor monitor in configurationData.ToEnumerable()) { localdatas.Add(new SyncData { FriendlyName = monitor.FriendlyName, GuidHash = monitor.Hash, IntHash = monitor.GetHashCode() }); } syncDict.Clear(); foreach (SyncData ld in localdatas) { //loop through each and create a dictionary of the guid from service to guid from local configuration SyncData local = ld; SyncData match = GetMatch(servicedatas, local); if (match != null) syncDict.Add(match.GuidHash, ld.GuidHash); //shouldn't be duplicates } //mark synced as true synced = true; } break; } } } Thread.Sleep(250); } }
private SyncData GetMatch(SyncDatas servicedatas, SyncData local) { return servicedatas.Find(service => { if (service.FriendlyName == local.FriendlyName && service.IntHash == local.IntHash) return true; else return false; }); }
private void Check() { while (!kill) { List<Command> commands = new List<Command>(_listener.GetCommands()); foreach (Command cmd in commands) { try { switch (cmd.CommandType) { case Commands.GetResults: Int64 lastcount = Convert.ToInt64(cmd.Data); if (lastcount < ResultChecker.GetLastResultCounter(cmd.FromIp, cmd.FromNamespace)) { Results results = ResultChecker.GetLatestResults(cmd.FromIp, cmd.FromNamespace); if (results != null && results.Count > 0) { Command cmdResults = new Command { CommandType = Commands.GetResultsResponse, Data = results, ToNamespace = cmd.FromNamespace, ToIp = cmd.FromIp }; _listener.SendCommand(cmdResults); ResultChecker.ClearResults(cmd.FromIp, cmd.FromNamespace); } } break; case Commands.StartScheduler: MonitorScheduler.Scheduler.SetMonitors(NetworkMonitor.Configuration); MonitorScheduler.Scheduler.Start(); _listener.SendCommand(new Command { CommandType = Commands.SchedulerStatus, Data = MonitorScheduler.Scheduler.Running, ToNamespace = cmd.FromNamespace, ToIp = cmd.FromIp }); break; case Commands.StopScheduler: MonitorScheduler.Scheduler.Kill(); _listener.SendCommand(new Command { CommandType = Commands.SchedulerStatus, Data = MonitorScheduler.Scheduler.Running, ToNamespace = cmd.FromNamespace, ToIp = cmd.FromIp }); break; case Commands.SchedulerStatus: //need enum to show status better _listener.SendCommand(new Command { CommandType = Commands.SchedulerStatus, Data = MonitorScheduler.Scheduler.Running, ToNamespace = cmd.FromNamespace, ToIp = cmd.FromIp }); break; case Commands.ServiceStatus: //need enum to show status better _listener.SendCommand(new Command { CommandType = Commands.ServiceStatus, Data = true, ToNamespace = cmd.FromNamespace, ToIp = cmd.FromIp }); break; case Commands.UpdateConfiguration: if (cmd.Data != null) { NetworkMonitor.SetConfiguration((ConfigurationData) cmd.Data); //NetworkMonitor.Configuration = (ConfigurationData)cmd.Data; //save it once i get it. NetworkMonitor.Configuration.ExportToXml(_configPath); } _listener.SendCommand(new Command { CommandType = Commands.UpdateConfigurationResponse, Data = true, ToNamespace = cmd.FromNamespace, ToIp = cmd.FromIp }); break; case Commands.GetConfiguration: _listener.SendCommand(new Command { CommandType = Commands.GetConfigurationResponse, Data = NetworkMonitor.Configuration, ToNamespace = cmd.FromNamespace, ToIp = cmd.FromIp }); break; case Commands.GetConfigurationResponse: if (cmd.Data != null) { //NetworkMonitor.Configuration = (ConfigurationData)cmd.Data; NetworkMonitor.SetConfiguration((ConfigurationData) cmd.Data); //save it once i get it. NetworkMonitor.Configuration.ExportToXml(_configPath); } break; case Commands.ResultsSync: if (NetworkMonitor.ConfigurationLoaded) { SyncDatas localdatas = new SyncDatas {Counter = ResultChecker.GetLastResultCounter(cmd.FromIp, cmd.FromNamespace)}; //List<SyncData> localdatas = new List<SyncData>(); foreach (IMonitor monitor in NetworkMonitor.Configuration.ToEnumerable()) { localdatas.Add(new SyncData {FriendlyName = monitor.FriendlyName, GuidHash = monitor.Hash, IntHash = monitor.GetHashCode()}); } _listener.SendCommand(new Command { CommandType = Commands.ResultsSyncResponse, Data = localdatas, ToNamespace = cmd.FromNamespace, ToIp = cmd.FromIp }); } break; case Commands.GetAlertResults: Results alertResults = ResultChecker.GetLatestAlertResults(cmd.FromIp, cmd.FromNamespace); Command cmdAlertResults = new Command { CommandType = Commands.GetAlertResultsResponse, Data = alertResults, ToNamespace = cmd.FromNamespace, ToIp = cmd.FromIp }; _listener.SendCommand(cmdAlertResults); ResultChecker.ClearResults(cmd.FromIp, cmd.FromNamespace); break; } } catch (Exception ex) { Logger.Instance.Log(this.GetType(), LogType.Debug, "Command Type: " + cmd.CommandType); Logger.Instance.LogException(this.GetType(), ex); } } Thread.Sleep(250); } }