Free cookie consent management tool by TermsFeed Policy Generator

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

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

added shutdown button (#455)

File size: 11.4 KB
RevLine 
[717]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;
[752]30using System.Diagnostics;
31using System.Threading;
[906]32using ZedGraph;
[973]33using HeuristicLab.Hive.Client.Console.ClientService;
[953]34using System.ServiceModel;
[973]35using System.Net;
[717]36
37namespace HeuristicLab.Hive.Client.Console {
[752]38
[1027]39  #region Delegates
40
[911]41  delegate void UpdateTextDelegate(EventLogEntry ev);
[752]42
[1027]43  #endregion
44
[717]45  public partial class HiveClientConsole : Form {
[752]46
[1027]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
[1002]52    private EventLog HiveClientEventLog;
53    private ClientConsoleCommunicatorClient cccc;
54    private System.Windows.Forms.Timer refreshTimer;
55    private ListViewColumnSorterDate lvwColumnSorter;
[752]56
[1027]57    #endregion
[1002]58
[1027]59    #region Constructor
60
[717]61    public HiveClientConsole() {
62      InitializeComponent();
[1002]63      lvwColumnSorter = new ListViewColumnSorterDate();
64      lvLog.ListViewItemSorter = lvwColumnSorter;
65      lvwColumnSorter.SortColumn = 3;
66      lvwColumnSorter.Order = SortOrder.Descending;
[973]67      InitTimer();
68      ConnectToClient();
69      RefreshGui();
[906]70      GetEventLog();
[973]71    }
[911]72
[1027]73    #endregion
74
75    #region Methods
76
[973]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();
[906]82    }
83
[973]84    private void RefreshGui() {
85      try {
[1002]86        cccc.GetStatusInfosAsync();
[973]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 {
[1027]98        cccc = new ClientConsoleCommunicatorClient(new NetTcpBinding(), new EndpointAddress(ENDPOINTADRESS));
[1002]99        cccc.GetStatusInfosCompleted += new EventHandler<GetStatusInfosCompletedEventArgs>(cccc_GetStatusInfosCompleted);
100        cccc.GetCurrentConnectionCompleted += new EventHandler<GetCurrentConnectionCompletedEventArgs>(cccc_GetCurrentConnectionCompleted);
[973]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
[1027]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
[1080]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();
[1027]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
[1080]139    private void UpdateGraph(JobStatus[] jobs) {
[1027]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
[1080]151      myPane.Fill.Color = this.BackColor;
[1027]152
153      myPane.Chart.Fill.Type = FillType.None;
154      myPane.Fill.Type = FillType.Solid;
155
[1080]156      double allProgress = 0;
157      double done = 0;
[1027]158
[1080]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        }
[1027]165
[1080]166        done = allProgress / jobs.Length;
167
[1087]168        myPane.AddPieSlice(done, Color.Green, 0, "");
169        myPane.AddPieSlice(1-done, Color.Red, 0, "");
[1080]170      }
[1027]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) {
[1002]188      if (e.Error == null) {
189        ConnectionContainer curConnection = e.Result;
190        tbIPAdress.Text = curConnection.IPAdress;
191        tbPort.Text = curConnection.Port.ToString();
[1028]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();
[1002]197      }
198    }
199
[1027]200    private void cccc_GetStatusInfosCompleted(object sender, GetStatusInfosCompletedEventArgs e) {
[1002]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
[1080]229        UpdateGraph(sc.Jobs);
[1002]230
231        if (sc.Status == NetworkEnumWcfConnState.Connected) {
232          btConnect.Enabled = false;
233          btnDisconnect.Enabled = true;
[1016]234          lbCs.Text = sc.ConnectedSince.ToString();
[1002]235          cccc.GetCurrentConnectionAsync();
236        } else if (sc.Status == NetworkEnumWcfConnState.Disconnected) {
237          btConnect.Enabled = true;
238          btnDisconnect.Enabled = false;
[1016]239          lbCs.Text = String.Empty;
[1002]240        } else if (sc.Status == NetworkEnumWcfConnState.Failed) {
241          btConnect.Enabled = true;
242          btnDisconnect.Enabled = false;
[1016]243          lbCs.Text = String.Empty;
[1002]244        }
[1028]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();
[1002]250      }
251    }
252
[906]253    private void HiveClientConsole_Load(object sender, EventArgs e) {
[1027]254      //nothing to do
[906]255    }
256
[911]257    private void UpdateText(EventLogEntry ev) {
[906]258      if (this.lvLog.InvokeRequired) {
259        this.lvLog.Invoke(new
[752]260          UpdateTextDelegate(UpdateText), new object[] { ev });
261      } else {
[1027]262        ListViewItem curEventLogEntry = GenerateEventEntry(ev);
[973]263        lvLog.Items.Add(curEventLogEntry);
[1002]264        lvJobDetail.Sort();
[752]265      }
266    }
267
[906]268    public void OnEntryWritten(object source, EntryWrittenEventArgs e) {
[911]269      UpdateText(e.Entry);
[717]270    }
271
[906]272    private void HiveClientConsole_Resize(object sender, EventArgs e) {
[1027]273      //nothing to do
[752]274    }
[906]275
276    private void lvLog_DoubleClick(object sender, EventArgs e) {
277      ListViewItem lvi = lvLog.SelectedItems[0];
[1002]278      HiveEventEntry hee = new HiveEventEntry(lvi.SubItems[2].Text, lvi.SubItems[3].Text, lvi.SubItems[1].Text);
[1027]279
[906]280      Form EventlogDetails = new EventLogEntryForm(hee);
281      EventlogDetails.Show();
282    }
[973]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;
[1010]291        cccc.SetConnectionAsync(cc);
[973]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) {
[1010]298      cccc.DisconnectAsync();
[973]299    }
[1002]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    }
[1027]319
[1087]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
[1027]328    #endregion
[1087]329
[717]330  }
[752]331}
Note: See TracBrowser for help on using the repository browser.