#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Security.Principal; using System.ServiceProcess; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; using HeuristicLab.Common; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; namespace HeuristicLab.Clients.Hive.SlaveCore.Views { [View("HeuristicLab Slave View")] [Content(typeof(SlaveItem), IsDefaultView = true)] public partial class SlaveView : ItemView { public new SlaveItem Content { get { return (SlaveItem)base.Content; } set { if (base.Content != value) { base.Content = value; } } } public SlaveView() { InitializeComponent(); if (!CheckRunAsAdmin()) btnRestartService.Enabled = false; btnRestart.Enabled = false; } #region Register Content Events protected override void DeregisterContentEvents() { Content.SlaveMessageLogged -= new System.EventHandler>(Content_SlaveMessageLogged); Content.SlaveShutdown -= new System.EventHandler(Content_SlaveShutdown); Content.SlaveStatusChanged -= new System.EventHandler>(Content_SlaveStatusChanged); base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.SlaveMessageLogged += new System.EventHandler>(Content_SlaveMessageLogged); Content.SlaveShutdown += new System.EventHandler(Content_SlaveShutdown); Content.SlaveStatusChanged += new System.EventHandler>(Content_SlaveStatusChanged); } #endregion protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { //nothing to do... } else { //try to establish a connection to the slave service if (base.Content != null) { ((SlaveItem)base.Content).Open(); Task.Factory.StartNew(Connector); } } } private void Connector() { bool connected = false; while (!connected) { this.Invoke(new Func(() => connected = ((SlaveItem)base.Content).ReconnectToSlaveCore())); if (!connected) { Thread.Sleep(1000); } } this.Invoke(new Action(SetEnabledStateOfControls)); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); //do nothing at the moment, we have nothing editable } #region Event Handlers void Content_SlaveStatusChanged(object sender, EventArgs e) { RenderChart(e.Value); } void Content_SlaveShutdown(object sender, System.EventArgs e) { txtLog.AppendText("Slave did shutdown" + Environment.NewLine); Task.Factory.StartNew(Connector); } void Content_SlaveMessageLogged(object sender, EventArgs e) { txtLog.AppendText(e.Value + Environment.NewLine); } #endregion private void RenderChart(StatusCommons status) { jobChart.Series[0].Points.Clear(); DataPoint pJobs = new DataPoint(status.Jobs.Count, status.Jobs.Count); DataPoint pJobsAborted = new DataPoint(status.JobsAborted, status.JobsAborted); DataPoint pJobsDone = new DataPoint(status.JobsFinished, status.JobsFinished); DataPoint pJobsFetched = new DataPoint(status.JobsFetched, status.JobsFetched); pJobs.LegendText = "Current jobs: " + status.Jobs.Count; pJobs.Color = System.Drawing.Color.Orange; pJobsAborted.LegendText = "Aborted jobs: " + status.JobsAborted; pJobsAborted.Color = System.Drawing.Color.Red; pJobsDone.LegendText = "Finished jobs: " + status.JobsFinished; pJobsDone.Color = System.Drawing.Color.Green; pJobsFetched.LegendText = "Fetched jobs: " + status.JobsFetched; pJobsFetched.Color = System.Drawing.Color.Blue; jobChart.Series[0].Points.Add(pJobs); jobChart.Series[0].Points.Add(pJobsAborted); jobChart.Series[0].Points.Add(pJobsDone); jobChart.Series[0].Points.Add(pJobsFetched); } private void btnSoftPause_Click(object sender, System.EventArgs e) { if (Content != null) Content.PauseAll(); } private void btnHardPause_Click(object sender, System.EventArgs e) { if (Content != null) Content.StopAll(); } private void btnRestart_Click(object sender, System.EventArgs e) { if (Content != null) { Content.RestartCore(); btnRestart.Enabled = false; btnSleep.Enabled = true; } } private void btnSleep_Click(object sender, System.EventArgs e) { if (Content != null) { Content.Sleep(); btnRestart.Enabled = true; btnSleep.Enabled = false; } } private void btnAbout_Click(object sender, System.EventArgs e) { SlaveAboutDialog diag = new SlaveAboutDialog(); diag.ShowDialog(); } private void btnRestartService_Click(object sender, EventArgs e) { if (CheckRunAsAdmin()) { RestartService(); } else { MessageBox.Show("You need to run this application as Administrator to restart the service"); } } private bool CheckRunAsAdmin() { bool isRunAsAdmin = false; WindowsIdentity user = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(user); try { isRunAsAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator); } catch { } return isRunAsAdmin; } private void RestartService() { string serviceName = "HeuristicLab.Clients.Hive.SlaveCore.SlaveWindowsService"; TimeSpan timeout = TimeSpan.FromMilliseconds(5000); ServiceController service = new ServiceController(serviceName); try { if (service.Status == ServiceControllerStatus.Running) { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch (InvalidOperationException ex) { MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString()); } catch (Exception ex) { MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString()); } } } }