Exemplo n.º 1
0
        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);
            }
        }