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 |
|
---|
22 | using System;
|
---|
23 | using System.Security.Principal;
|
---|
24 | using System.ServiceProcess;
|
---|
25 | using System.Threading;
|
---|
26 | using System.Threading.Tasks;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.Core.Views;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 |
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Clients.Hive.SlaveCore.Views {
|
---|
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; }
|
---|
42 | set {
|
---|
43 | if (base.Content != value) {
|
---|
44 | base.Content = value;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public SlaveView() {
|
---|
50 | InitializeComponent();
|
---|
51 |
|
---|
52 | if (!CheckRunAsAdmin())
|
---|
53 | btnRestartService.Enabled = false;
|
---|
54 | btnRestart.Enabled = false;
|
---|
55 |
|
---|
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);
|
---|
63 |
|
---|
64 | base.DeregisterContentEvents();
|
---|
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) {
|
---|
79 | //nothing to do...
|
---|
80 | } else {
|
---|
81 | //try to establish a connection to the slave service
|
---|
82 | if (base.Content != null) {
|
---|
83 | ((SlaveItem)base.Content).Open();
|
---|
84 | Task.Factory.StartNew(Connector);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
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 |
|
---|
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) {
|
---|
108 | RenderChart(e.Value);
|
---|
109 | }
|
---|
110 |
|
---|
111 | void Content_SlaveShutdown(object sender, System.EventArgs e) {
|
---|
112 | txtLog.AppendText("Slave did shutdown" + Environment.NewLine);
|
---|
113 | Task.Factory.StartNew(Connector);
|
---|
114 | }
|
---|
115 |
|
---|
116 | void Content_SlaveMessageLogged(object sender, EventArgs<string> e) {
|
---|
117 | txtLog.AppendText(e.Value + Environment.NewLine);
|
---|
118 | }
|
---|
119 | #endregion
|
---|
120 |
|
---|
121 | private void RenderChart(StatusCommons status) {
|
---|
122 | jobChart.Series[0].Points.Clear();
|
---|
123 |
|
---|
124 | DataPoint pJobs = new DataPoint(status.Jobs.Count, status.Jobs.Count);
|
---|
125 | DataPoint pJobsAborted = new DataPoint(status.JobsAborted, status.JobsAborted);
|
---|
126 | DataPoint pJobsDone = new DataPoint(status.JobsFinished, status.JobsFinished);
|
---|
127 | DataPoint pJobsFetched = new DataPoint(status.JobsFetched, status.JobsFetched);
|
---|
128 |
|
---|
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;
|
---|
133 | pJobsDone.LegendText = "Finished jobs: " + status.JobsFinished;
|
---|
134 | pJobsDone.Color = System.Drawing.Color.Green;
|
---|
135 | pJobsFetched.LegendText = "Fetched jobs: " + status.JobsFetched;
|
---|
136 | pJobsFetched.Color = System.Drawing.Color.Blue;
|
---|
137 |
|
---|
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 |
|
---|
144 | private void btnSoftPause_Click(object sender, System.EventArgs e) {
|
---|
145 | if (Content != null)
|
---|
146 | Content.PauseAll();
|
---|
147 | }
|
---|
148 |
|
---|
149 | private void btnHardPause_Click(object sender, System.EventArgs e) {
|
---|
150 | if (Content != null)
|
---|
151 | Content.StopAll();
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void btnRestart_Click(object sender, System.EventArgs e) {
|
---|
155 | if (Content != null) {
|
---|
156 | Content.RestartCore();
|
---|
157 | btnRestart.Enabled = false;
|
---|
158 | btnSleep.Enabled = true;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | private void btnSleep_Click(object sender, System.EventArgs e) {
|
---|
163 | if (Content != null) {
|
---|
164 | Content.Sleep();
|
---|
165 | btnRestart.Enabled = true;
|
---|
166 | btnSleep.Enabled = false;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | private void btnAbout_Click(object sender, System.EventArgs e) {
|
---|
171 | SlaveAboutDialog diag = new SlaveAboutDialog();
|
---|
172 | diag.ShowDialog();
|
---|
173 | }
|
---|
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) {
|
---|
210 | MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());
|
---|
211 | }
|
---|
212 | catch (Exception ex) {
|
---|
213 | MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString());
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|