source: trunk/sources/HeuristicLab.Hive.Client.Console/HiveClientConsole.cs @ 1087

Last change on this file since 1087 was 1087, checked in by whackl, 12 years ago

added shutdown button (#455)

File size: 11.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using System.Diagnostics;
31using System.Threading;
32using ZedGraph;
33using HeuristicLab.Hive.Client.Console.ClientService;
34using System.ServiceModel;
35using System.Net;
36
37namespace HeuristicLab.Hive.Client.Console {
38
39  #region Delegates
40
41  delegate void UpdateTextDelegate(EventLogEntry ev);
42
43  #endregion
44
45  public partial class HiveClientConsole : Form {
46
47    #region Declarations
48
49    private const string ENDPOINTADRESS = "net.tcp://127.0.0.1:8000/ClientConsole/ClientConsoleCommunicator";
50    private const string EVENTLOGNAME = "Hive Client";
51
52    private EventLog HiveClientEventLog;
53    private ClientConsoleCommunicatorClient cccc;
54    private System.Windows.Forms.Timer refreshTimer;
55    private ListViewColumnSorterDate lvwColumnSorter;
56
57    #endregion
58
59    #region Constructor
60
61    public HiveClientConsole() {
62      InitializeComponent();
63      lvwColumnSorter = new ListViewColumnSorterDate();
64      lvLog.ListViewItemSorter = lvwColumnSorter;
65      lvwColumnSorter.SortColumn = 3;
66      lvwColumnSorter.Order = SortOrder.Descending;
67      InitTimer();
68      ConnectToClient();
69      RefreshGui();
70      GetEventLog();
71    }
72
73    #endregion
74
75    #region Methods
76
77    private void InitTimer() {
78      refreshTimer = new System.Windows.Forms.Timer();
79      refreshTimer.Interval = 1000;
80      refreshTimer.Tick += new EventHandler(refreshTimer_Tick);
81      refreshTimer.Start();
82    }
83
84    private void RefreshGui() {
85      try {
86        cccc.GetStatusInfosAsync();
87      }
88      catch (Exception ex) {
89        refreshTimer.Stop();
90        DialogResult res = MessageBox.Show("Connection Error, check if Hive Client is running!", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
91        if (res == DialogResult.OK)
92          this.Close();
93      }
94    }
95
96    private void ConnectToClient() {
97      try {
98        cccc = new ClientConsoleCommunicatorClient(new NetTcpBinding(), new EndpointAddress(ENDPOINTADRESS));
99        cccc.GetStatusInfosCompleted += new EventHandler<GetStatusInfosCompletedEventArgs>(cccc_GetStatusInfosCompleted);
100        cccc.GetCurrentConnectionCompleted += new EventHandler<GetCurrentConnectionCompletedEventArgs>(cccc_GetCurrentConnectionCompleted);
101      }
102      catch (Exception) {
103        refreshTimer.Stop();
104        DialogResult res = MessageBox.Show("Connection Error, check if Hive Client is running!", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
105        if (res == DialogResult.OK)
106          this.Close();
107      }
108    }
109
110    private void GetEventLog() {
111      HiveClientEventLog = new EventLog(EVENTLOGNAME);
112      HiveClientEventLog.Source = EVENTLOGNAME;
113      HiveClientEventLog.EntryWritten += new EntryWrittenEventHandler(OnEntryWritten);
114      HiveClientEventLog.EnableRaisingEvents = true;
115
116      ListViewItem curEventLogEntry;
117
118      //databinding on listview?
119      if (HiveClientEventLog != null && HiveClientEventLog.Entries != null) {
120        foreach (EventLogEntry ele in HiveClientEventLog.Entries) {
121          curEventLogEntry = GenerateEventEntry(ele);
122          lvLog.Items.Add(curEventLogEntry);
123        }
124        lvJobDetail.Sort();
125      }
126    }
127
128    private ListViewItem GenerateEventEntry(EventLogEntry ele) {
129      ListViewItem curEventLogEntry;
130      curEventLogEntry = new ListViewItem("", 0);
131      if (ele.EntryType == EventLogEntryType.Error)
132        curEventLogEntry = new ListViewItem("", 1);
133      curEventLogEntry.SubItems.Add(ele.InstanceId.ToString());
134      curEventLogEntry.SubItems.Add(ele.Message);
135      curEventLogEntry.SubItems.Add(ele.TimeGenerated.ToString());
136      return curEventLogEntry;
137    }
138
139    private void UpdateGraph(JobStatus[] jobs) {
140      ZedGraphControl zgc = new ZedGraphControl();
141      GraphPane myPane = zgc.GraphPane;
142      myPane.GraphObjList.Clear();
143
144      myPane.Title.IsVisible = false;  // no title
145      myPane.Border.IsVisible = false; // no border
146      myPane.Chart.Border.IsVisible = false; // no border around the chart
147      myPane.XAxis.IsVisible = false;  // no x-axis
148      myPane.YAxis.IsVisible = false;  // no y-axis
149      myPane.Legend.IsVisible = false; // no legend
150
151      myPane.Fill.Color = this.BackColor;
152
153      myPane.Chart.Fill.Type = FillType.None;
154      myPane.Fill.Type = FillType.Solid;
155
156      double allProgress = 0;
157      double done = 0;
158
159      if (jobs.Length == 0) {
160        myPane.AddPieSlice(100, Color.Green, 0.1, "");
161      } else {
162        for (int i = 0; i < jobs.Length; i++) {
163          allProgress += jobs[i].Progress;         
164        }
165
166        done = allProgress / jobs.Length;
167
168        myPane.AddPieSlice(done, Color.Green, 0, "");
169        myPane.AddPieSlice(1-done, Color.Red, 0, "");
170      }
171      //Hides the slice labels
172      PieItem.Default.LabelType = PieLabelType.None;
173
174      myPane.AxisChange();
175
176      pbGraph.Image = zgc.GetImage();
177    }
178
179    #endregion
180
181    #region Events
182
183    private void refreshTimer_Tick(object sender, EventArgs e) {
184      RefreshGui();
185    }
186
187    private void cccc_GetCurrentConnectionCompleted(object sender, GetCurrentConnectionCompletedEventArgs e) {
188      if (e.Error == null) {
189        ConnectionContainer curConnection = e.Result;
190        tbIPAdress.Text = curConnection.IPAdress;
191        tbPort.Text = curConnection.Port.ToString();
192      } else {
193               refreshTimer.Stop();
194        DialogResult res = MessageBox.Show("Connection Error, check if Hive Client is running! - " + e.Error.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
195        if (res == DialogResult.OK)
196          this.Close();
197      }
198    }
199
200    private void cccc_GetStatusInfosCompleted(object sender, GetStatusInfosCompletedEventArgs e) {
201
202      if (e.Error == null) {
203        StatusCommons sc = e.Result;
204
205        lbGuid.Text = sc.ClientGuid.ToString();
206        lbConnectionStatus.Text = sc.Status.ToString();
207        lbJobdone.Text = sc.JobsDone.ToString();
208        lbJobsAborted.Text = sc.JobsAborted.ToString();
209        lbJobsFetched.Text = sc.JobsFetched.ToString();
210
211        this.Text = "Client Console (" + sc.Status.ToString() + ")";
212        lbStatus.Text = sc.Status.ToString();
213
214        ListViewItem curJobStatusItem;
215
216        if (sc.Jobs != null) {
217          lvJobDetail.Items.Clear();
218          double progress;
219          foreach (JobStatus curJob in sc.Jobs) {
220            curJobStatusItem = new ListViewItem(curJob.JobId.ToString());
221            curJobStatusItem.SubItems.Add(curJob.Since.ToString());
222            progress = curJob.Progress * 100;
223            curJobStatusItem.SubItems.Add(progress.ToString());
224            lvJobDetail.Items.Add(curJobStatusItem);
225          }
226          lvJobDetail.Sort();
227        }
228
229        UpdateGraph(sc.Jobs);
230
231        if (sc.Status == NetworkEnumWcfConnState.Connected) {
232          btConnect.Enabled = false;
233          btnDisconnect.Enabled = true;
234          lbCs.Text = sc.ConnectedSince.ToString();
235          cccc.GetCurrentConnectionAsync();
236        } else if (sc.Status == NetworkEnumWcfConnState.Disconnected) {
237          btConnect.Enabled = true;
238          btnDisconnect.Enabled = false;
239          lbCs.Text = String.Empty;
240        } else if (sc.Status == NetworkEnumWcfConnState.Failed) {
241          btConnect.Enabled = true;
242          btnDisconnect.Enabled = false;
243          lbCs.Text = String.Empty;
244        }
245      } else {
246        refreshTimer.Stop();
247        DialogResult res = MessageBox.Show("Connection Error, check if Hive Client is running! - " + e.Error.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
248        if (res == DialogResult.OK)
249          this.Close();
250      }
251    }
252
253    private void HiveClientConsole_Load(object sender, EventArgs e) {
254      //nothing to do
255    }
256
257    private void UpdateText(EventLogEntry ev) {
258      if (this.lvLog.InvokeRequired) {
259        this.lvLog.Invoke(new
260          UpdateTextDelegate(UpdateText), new object[] { ev });
261      } else {
262        ListViewItem curEventLogEntry = GenerateEventEntry(ev);
263        lvLog.Items.Add(curEventLogEntry);
264        lvJobDetail.Sort();
265      }
266    }
267
268    public void OnEntryWritten(object source, EntryWrittenEventArgs e) {
269      UpdateText(e.Entry);
270    }
271
272    private void HiveClientConsole_Resize(object sender, EventArgs e) {
273      //nothing to do
274    }
275
276    private void lvLog_DoubleClick(object sender, EventArgs e) {
277      ListViewItem lvi = lvLog.SelectedItems[0];
278      HiveEventEntry hee = new HiveEventEntry(lvi.SubItems[2].Text, lvi.SubItems[3].Text, lvi.SubItems[1].Text);
279
280      Form EventlogDetails = new EventLogEntryForm(hee);
281      EventlogDetails.Show();
282    }
283
284    private void btConnect_Click(object sender, EventArgs e) {
285      IPAddress ipAdress;
286      int port;
287      ConnectionContainer cc = new ConnectionContainer();
288      if (IPAddress.TryParse(tbIPAdress.Text, out ipAdress) && int.TryParse(tbPort.Text, out port)) {
289        cc.IPAdress = tbIPAdress.Text;
290        cc.Port = port;
291        cccc.SetConnectionAsync(cc);
292      } else {
293        MessageBox.Show("IP Adress and/or Port Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
294      }
295    }
296
297    private void btnDisconnect_Click(object sender, EventArgs e) {
298      cccc.DisconnectAsync();
299    }
300
301    private void lvLog_ColumnClick(object sender, ColumnClickEventArgs e) {
302      // Determine if clicked column is already the column that is being sorted.
303      if (e.Column == lvwColumnSorter.SortColumn) {
304        // Reverse the current sort direction for this column.
305        if (lvwColumnSorter.Order == SortOrder.Ascending) {
306          lvwColumnSorter.Order = SortOrder.Descending;
307        } else {
308          lvwColumnSorter.Order = SortOrder.Ascending;
309        }
310      } else {
311        // Set the column number that is to be sorted; default to ascending.
312        lvwColumnSorter.SortColumn = e.Column;
313        lvwColumnSorter.Order = SortOrder.Ascending;
314      }
315
316      // Perform the sort with these new sort options.
317      lvLog.Sort();
318    }
319
320    private void btn_clientShutdown_Click(object sender, EventArgs e) {
321      DialogResult res = MessageBox.Show("Do you really want to shutdown the Hive Client?", "Hive Client Console", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
322      if (res == DialogResult.Yes) {
323        cccc.ShutdownClient();
324        this.Close();
325      }
326    }
327
328    #endregion
329
330  }
331}
Note: See TracBrowser for help on using the repository browser.