Free cookie consent management tool by TermsFeed Policy Generator

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

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

added some error messages (#397)

File size: 10.8 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      foreach (EventLogEntry ele in HiveClientEventLog.Entries) {
119        curEventLogEntry = GenerateEventEntry(ele);
120        lvLog.Items.Add(curEventLogEntry);
121      }
122      lvJobDetail.Sort();
123    }
124
125    private ListViewItem GenerateEventEntry(EventLogEntry ele) {
126      ListViewItem curEventLogEntry;
127      curEventLogEntry = new ListViewItem("", 0);
128      if (ele.EntryType == EventLogEntryType.Error)
129        curEventLogEntry = new ListViewItem("", 1);
130      curEventLogEntry.SubItems.Add(ele.InstanceId.ToString());
131      curEventLogEntry.SubItems.Add(ele.Message);
132      curEventLogEntry.SubItems.Add(ele.TimeGenerated.ToString());
133      return curEventLogEntry;
134    }
135
136    private void UpdateGraph(int jobsDone, int jobsAborted) {
137      ZedGraphControl zgc = new ZedGraphControl();
138      GraphPane myPane = zgc.GraphPane;
139      myPane.GraphObjList.Clear();
140
141      myPane.Title.IsVisible = false;  // no title
142      myPane.Border.IsVisible = false; // no border
143      myPane.Chart.Border.IsVisible = false; // no border around the chart
144      myPane.XAxis.IsVisible = false;  // no x-axis
145      myPane.YAxis.IsVisible = false;  // no y-axis
146      myPane.Legend.IsVisible = false; // no legend
147
148      myPane.Fill.Color = Color.FromKnownColor(KnownColor.Control);
149
150      myPane.Chart.Fill.Type = FillType.None;
151      myPane.Fill.Type = FillType.Solid;
152
153      double sum = (double)jobsDone + jobsAborted;
154      double perDone = (double)jobsDone / sum * 100;
155      double perAborted = (double)jobsAborted / sum * 100;
156
157      myPane.AddPieSlice(perDone, Color.Green, 0.1, "");
158      myPane.AddPieSlice(perAborted, Color.Red, 0.1, "");
159
160      //Hides the slice labels
161      PieItem.Default.LabelType = PieLabelType.None;
162
163      myPane.AxisChange();
164
165      pbGraph.Image = zgc.GetImage();
166    }
167
168    #endregion
169
170    #region Events
171
172    private void refreshTimer_Tick(object sender, EventArgs e) {
173      RefreshGui();
174    }
175
176    private void cccc_GetCurrentConnectionCompleted(object sender, GetCurrentConnectionCompletedEventArgs e) {
177      if (e.Error == null) {
178        ConnectionContainer curConnection = e.Result;
179        tbIPAdress.Text = curConnection.IPAdress;
180        tbPort.Text = curConnection.Port.ToString();
181      } else {
182               refreshTimer.Stop();
183        DialogResult res = MessageBox.Show("Connection Error, check if Hive Client is running! - " + e.Error.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
184        if (res == DialogResult.OK)
185          this.Close();
186      }
187    }
188
189    private void cccc_GetStatusInfosCompleted(object sender, GetStatusInfosCompletedEventArgs e) {
190
191      if (e.Error == null) {
192        StatusCommons sc = e.Result;
193
194        lbGuid.Text = sc.ClientGuid.ToString();
195        lbConnectionStatus.Text = sc.Status.ToString();
196        lbJobdone.Text = sc.JobsDone.ToString();
197        lbJobsAborted.Text = sc.JobsAborted.ToString();
198        lbJobsFetched.Text = sc.JobsFetched.ToString();
199
200        this.Text = "Client Console (" + sc.Status.ToString() + ")";
201        lbStatus.Text = sc.Status.ToString();
202
203        ListViewItem curJobStatusItem;
204
205        if (sc.Jobs != null) {
206          lvJobDetail.Items.Clear();
207          double progress;
208          foreach (JobStatus curJob in sc.Jobs) {
209            curJobStatusItem = new ListViewItem(curJob.JobId.ToString());
210            curJobStatusItem.SubItems.Add(curJob.Since.ToString());
211            progress = curJob.Progress * 100;
212            curJobStatusItem.SubItems.Add(progress.ToString());
213            lvJobDetail.Items.Add(curJobStatusItem);
214          }
215          lvJobDetail.Sort();
216        }
217
218        UpdateGraph(sc.JobsDone, sc.JobsAborted);
219
220        if (sc.Status == NetworkEnumWcfConnState.Connected) {
221          btConnect.Enabled = false;
222          btnDisconnect.Enabled = true;
223          lbCs.Text = sc.ConnectedSince.ToString();
224          cccc.GetCurrentConnectionAsync();
225        } else if (sc.Status == NetworkEnumWcfConnState.Disconnected) {
226          btConnect.Enabled = true;
227          btnDisconnect.Enabled = false;
228          lbCs.Text = String.Empty;
229        } else if (sc.Status == NetworkEnumWcfConnState.Failed) {
230          btConnect.Enabled = true;
231          btnDisconnect.Enabled = false;
232          lbCs.Text = String.Empty;
233        }
234      } else {
235        refreshTimer.Stop();
236        DialogResult res = MessageBox.Show("Connection Error, check if Hive Client is running! - " + e.Error.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
237        if (res == DialogResult.OK)
238          this.Close();
239      }
240    }
241
242    private void HiveClientConsole_Load(object sender, EventArgs e) {
243      //nothing to do
244    }
245
246    private void UpdateText(EventLogEntry ev) {
247      if (this.lvLog.InvokeRequired) {
248        this.lvLog.Invoke(new
249          UpdateTextDelegate(UpdateText), new object[] { ev });
250      } else {
251        ListViewItem curEventLogEntry = GenerateEventEntry(ev);
252        lvLog.Items.Add(curEventLogEntry);
253        lvJobDetail.Sort();
254      }
255    }
256
257    public void OnEntryWritten(object source, EntryWrittenEventArgs e) {
258      UpdateText(e.Entry);
259    }
260
261    private void HiveClientConsole_Resize(object sender, EventArgs e) {
262      //nothing to do
263    }
264
265    private void lvLog_DoubleClick(object sender, EventArgs e) {
266      ListViewItem lvi = lvLog.SelectedItems[0];
267      HiveEventEntry hee = new HiveEventEntry(lvi.SubItems[2].Text, lvi.SubItems[3].Text, lvi.SubItems[1].Text);
268
269      Form EventlogDetails = new EventLogEntryForm(hee);
270      EventlogDetails.Show();
271    }
272
273    private void btConnect_Click(object sender, EventArgs e) {
274      IPAddress ipAdress;
275      int port;
276      ConnectionContainer cc = new ConnectionContainer();
277      if (IPAddress.TryParse(tbIPAdress.Text, out ipAdress) && int.TryParse(tbPort.Text, out port)) {
278        cc.IPAdress = tbIPAdress.Text;
279        cc.Port = port;
280        cccc.SetConnectionAsync(cc);
281      } else {
282        MessageBox.Show("IP Adress and/or Port Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
283      }
284    }
285
286    private void btnDisconnect_Click(object sender, EventArgs e) {
287      cccc.DisconnectAsync();
288    }
289
290    private void lvLog_ColumnClick(object sender, ColumnClickEventArgs e) {
291      // Determine if clicked column is already the column that is being sorted.
292      if (e.Column == lvwColumnSorter.SortColumn) {
293        // Reverse the current sort direction for this column.
294        if (lvwColumnSorter.Order == SortOrder.Ascending) {
295          lvwColumnSorter.Order = SortOrder.Descending;
296        } else {
297          lvwColumnSorter.Order = SortOrder.Ascending;
298        }
299      } else {
300        // Set the column number that is to be sorted; default to ascending.
301        lvwColumnSorter.SortColumn = e.Column;
302        lvwColumnSorter.Order = SortOrder.Ascending;
303      }
304
305      // Perform the sort with these new sort options.
306      lvLog.Sort();
307    }
308
309    #endregion
310  }
311}
Note: See TracBrowser for help on using the repository browser.