[5314] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[5795] | 22 | using System;
|
---|
[5826] | 23 | using System.Security.Principal;
|
---|
| 24 | using System.ServiceProcess;
|
---|
[5795] | 25 | using System.Threading;
|
---|
| 26 | using System.Threading.Tasks;
|
---|
[5314] | 27 | using System.Windows.Forms;
|
---|
[5789] | 28 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
[5314] | 29 | using HeuristicLab.Common;
|
---|
| 30 | using HeuristicLab.Core.Views;
|
---|
| 31 | using HeuristicLab.MainForm;
|
---|
| 32 |
|
---|
| 33 |
|
---|
[5599] | 34 | namespace HeuristicLab.Clients.Hive.SlaveCore.Views {
|
---|
[5314] | 35 |
|
---|
| 36 | [View("HeuristicLab Slave View")]
|
---|
| 37 | [Content(typeof(SlaveItem), IsDefaultView = true)]
|
---|
| 38 | public partial class SlaveView : ItemView {
|
---|
| 39 |
|
---|
| 40 | public new SlaveItem Content {
|
---|
| 41 | get { return (SlaveItem)base.Content; }
|
---|
[5320] | 42 | set {
|
---|
| 43 | if (base.Content != value) {
|
---|
| 44 | base.Content = value;
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
[5314] | 47 | }
|
---|
| 48 |
|
---|
| 49 | public SlaveView() {
|
---|
| 50 | InitializeComponent();
|
---|
[5826] | 51 |
|
---|
| 52 | if (!CheckRunAsAdmin())
|
---|
| 53 | btnRestartService.Enabled = false;
|
---|
| 54 | btnRestart.Enabled = false;
|
---|
| 55 |
|
---|
[5314] | 56 | }
|
---|
| 57 |
|
---|
| 58 | #region Register Content Events
|
---|
| 59 | protected override void DeregisterContentEvents() {
|
---|
| 60 | Content.SlaveMessageLogged -= new System.EventHandler<EventArgs<string>>(Content_SlaveMessageLogged);
|
---|
| 61 | Content.SlaveShutdown -= new System.EventHandler(Content_SlaveShutdown);
|
---|
| 62 | Content.SlaveStatusChanged -= new System.EventHandler<EventArgs<StatusCommons>>(Content_SlaveStatusChanged);
|
---|
[5320] | 63 |
|
---|
| 64 | base.DeregisterContentEvents();
|
---|
[5314] | 65 | }
|
---|
| 66 |
|
---|
| 67 | protected override void RegisterContentEvents() {
|
---|
| 68 | base.RegisterContentEvents();
|
---|
| 69 |
|
---|
| 70 | Content.SlaveMessageLogged += new System.EventHandler<EventArgs<string>>(Content_SlaveMessageLogged);
|
---|
| 71 | Content.SlaveShutdown += new System.EventHandler(Content_SlaveShutdown);
|
---|
| 72 | Content.SlaveStatusChanged += new System.EventHandler<EventArgs<StatusCommons>>(Content_SlaveStatusChanged);
|
---|
| 73 | }
|
---|
| 74 | #endregion
|
---|
| 75 |
|
---|
| 76 | protected override void OnContentChanged() {
|
---|
| 77 | base.OnContentChanged();
|
---|
| 78 | if (Content == null) {
|
---|
[5320] | 79 | //nothing to do...
|
---|
[5314] | 80 | } else {
|
---|
| 81 | //try to establish a connection to the slave service
|
---|
[5795] | 82 | if (base.Content != null) {
|
---|
[5314] | 83 | ((SlaveItem)base.Content).Open();
|
---|
[5795] | 84 | Task.Factory.StartNew(Connector);
|
---|
[5314] | 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[5795] | 89 | private void Connector() {
|
---|
| 90 | bool connected = false;
|
---|
| 91 | while (!connected) {
|
---|
| 92 | this.Invoke(new Func<bool>(() => connected = ((SlaveItem)base.Content).ReconnectToSlaveCore()));
|
---|
| 93 |
|
---|
| 94 | if (!connected) {
|
---|
| 95 | Thread.Sleep(1000);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | this.Invoke(new Action(SetEnabledStateOfControls));
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[5314] | 101 | protected override void SetEnabledStateOfControls() {
|
---|
| 102 | base.SetEnabledStateOfControls();
|
---|
| 103 | //do nothing at the moment, we have nothing editable
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | #region Event Handlers
|
---|
| 107 | void Content_SlaveStatusChanged(object sender, EventArgs<StatusCommons> e) {
|
---|
[5789] | 108 | RenderChart(e.Value);
|
---|
[5314] | 109 | }
|
---|
| 110 |
|
---|
| 111 | void Content_SlaveShutdown(object sender, System.EventArgs e) {
|
---|
[6004] | 112 | txtLog.AppendText("Slave did shutdown" + Environment.NewLine);
|
---|
[5795] | 113 | Task.Factory.StartNew(Connector);
|
---|
[5314] | 114 | }
|
---|
| 115 |
|
---|
| 116 | void Content_SlaveMessageLogged(object sender, EventArgs<string> e) {
|
---|
[6004] | 117 | txtLog.AppendText(e.Value + Environment.NewLine);
|
---|
[5314] | 118 | }
|
---|
| 119 | #endregion
|
---|
[5315] | 120 |
|
---|
[5789] | 121 | private void RenderChart(StatusCommons status) {
|
---|
[5826] | 122 | jobChart.Series[0].Points.Clear();
|
---|
[5789] | 123 |
|
---|
| 124 | DataPoint pJobs = new DataPoint(status.Jobs.Count, status.Jobs.Count);
|
---|
| 125 | DataPoint pJobsAborted = new DataPoint(status.JobsAborted, status.JobsAborted);
|
---|
[6203] | 126 | DataPoint pJobsDone = new DataPoint(status.JobsFinished, status.JobsFinished);
|
---|
[5789] | 127 | DataPoint pJobsFetched = new DataPoint(status.JobsFetched, status.JobsFetched);
|
---|
| 128 |
|
---|
[5826] | 129 | pJobs.LegendText = "Current jobs: " + status.Jobs.Count;
|
---|
| 130 | pJobs.Color = System.Drawing.Color.Orange;
|
---|
| 131 | pJobsAborted.LegendText = "Aborted jobs: " + status.JobsAborted;
|
---|
| 132 | pJobsAborted.Color = System.Drawing.Color.Red;
|
---|
[6203] | 133 | pJobsDone.LegendText = "Finished jobs: " + status.JobsFinished;
|
---|
[5826] | 134 | pJobsDone.Color = System.Drawing.Color.Green;
|
---|
| 135 | pJobsFetched.LegendText = "Fetched jobs: " + status.JobsFetched;
|
---|
| 136 | pJobsFetched.Color = System.Drawing.Color.Blue;
|
---|
| 137 |
|
---|
[5789] | 138 | jobChart.Series[0].Points.Add(pJobs);
|
---|
| 139 | jobChart.Series[0].Points.Add(pJobsAborted);
|
---|
| 140 | jobChart.Series[0].Points.Add(pJobsDone);
|
---|
| 141 | jobChart.Series[0].Points.Add(pJobsFetched);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[5315] | 144 | private void btnSoftPause_Click(object sender, System.EventArgs e) {
|
---|
| 145 | if (Content != null)
|
---|
[5451] | 146 | Content.PauseAll();
|
---|
[5315] | 147 | }
|
---|
| 148 |
|
---|
| 149 | private void btnHardPause_Click(object sender, System.EventArgs e) {
|
---|
| 150 | if (Content != null)
|
---|
[5451] | 151 | Content.StopAll();
|
---|
[5315] | 152 | }
|
---|
| 153 |
|
---|
| 154 | private void btnRestart_Click(object sender, System.EventArgs e) {
|
---|
[5826] | 155 | if (Content != null) {
|
---|
[5315] | 156 | Content.RestartCore();
|
---|
[5826] | 157 | btnRestart.Enabled = false;
|
---|
| 158 | btnSleep.Enabled = true;
|
---|
| 159 | }
|
---|
[5315] | 160 | }
|
---|
[5451] | 161 |
|
---|
| 162 | private void btnSleep_Click(object sender, System.EventArgs e) {
|
---|
[5826] | 163 | if (Content != null) {
|
---|
[5451] | 164 | Content.Sleep();
|
---|
[5826] | 165 | btnRestart.Enabled = true;
|
---|
| 166 | btnSleep.Enabled = false;
|
---|
| 167 | }
|
---|
[5451] | 168 | }
|
---|
[5602] | 169 |
|
---|
| 170 | private void btnAbout_Click(object sender, System.EventArgs e) {
|
---|
| 171 | SlaveAboutDialog diag = new SlaveAboutDialog();
|
---|
| 172 | diag.ShowDialog();
|
---|
| 173 | }
|
---|
[5826] | 174 |
|
---|
| 175 | private void btnRestartService_Click(object sender, EventArgs e) {
|
---|
| 176 | if (CheckRunAsAdmin()) {
|
---|
| 177 | RestartService();
|
---|
| 178 | } else {
|
---|
| 179 | MessageBox.Show("You need to run this application as Administrator to restart the service");
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | private bool CheckRunAsAdmin() {
|
---|
| 184 | bool isRunAsAdmin = false;
|
---|
| 185 | WindowsIdentity user = WindowsIdentity.GetCurrent();
|
---|
| 186 | WindowsPrincipal principal = new WindowsPrincipal(user);
|
---|
| 187 |
|
---|
| 188 | try {
|
---|
| 189 | isRunAsAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
|
---|
| 190 | }
|
---|
| 191 | catch { }
|
---|
| 192 | return isRunAsAdmin;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | private void RestartService() {
|
---|
| 196 | string serviceName = "HeuristicLab.Clients.Hive.SlaveCore.SlaveWindowsService";
|
---|
| 197 | TimeSpan timeout = TimeSpan.FromMilliseconds(5000);
|
---|
| 198 |
|
---|
| 199 | ServiceController service = new ServiceController(serviceName);
|
---|
| 200 | try {
|
---|
| 201 | if (service.Status == ServiceControllerStatus.Running) {
|
---|
| 202 | service.Stop();
|
---|
| 203 | service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | service.Start();
|
---|
| 207 | service.WaitForStatus(ServiceControllerStatus.Running, timeout);
|
---|
| 208 | }
|
---|
| 209 | catch (InvalidOperationException ex) {
|
---|
[6004] | 210 | MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());
|
---|
[5826] | 211 | }
|
---|
| 212 | catch (Exception ex) {
|
---|
[6004] | 213 | MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString());
|
---|
[5826] | 214 | }
|
---|
| 215 | }
|
---|
[5314] | 216 | }
|
---|
| 217 | }
|
---|