Free cookie consent management tool by TermsFeed Policy Generator

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

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

Bug fixed (#397)

File size: 10.1 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  delegate void UpdateTextDelegate(EventLogEntry ev);
40
41  public partial class HiveClientConsole : Form {
42
43    private EventLog HiveClientEventLog;
44    private ClientConsoleCommunicatorClient cccc;
45    private System.Windows.Forms.Timer refreshTimer;
46    private ListViewColumnSorterDate lvwColumnSorter;
47
48
49    public HiveClientConsole() {
50      InitializeComponent();
51      lvwColumnSorter = new ListViewColumnSorterDate();
52      lvLog.ListViewItemSorter = lvwColumnSorter;
53      lvwColumnSorter.SortColumn = 3;
54      lvwColumnSorter.Order = SortOrder.Descending;
55      InitTimer();
56      ConnectToClient();
57      RefreshGui();
58      GetEventLog();
59    }
60
61    private void InitTimer() {
62      refreshTimer = new System.Windows.Forms.Timer();
63      refreshTimer.Interval = 1000;
64      refreshTimer.Tick += new EventHandler(refreshTimer_Tick);
65      refreshTimer.Start();
66    }
67
68    void refreshTimer_Tick(object sender, EventArgs e) {
69      RefreshGui();
70    }
71   
72    private void RefreshGui() {
73      try {
74        cccc.GetStatusInfosAsync();
75      }
76      catch (Exception ex) {
77        refreshTimer.Stop();
78        DialogResult res = MessageBox.Show("Connection Error, check if Hive Client is running!", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
79        if (res == DialogResult.OK)
80          this.Close();
81      }
82    }
83
84    private void ConnectToClient() {
85      try {
86        cccc = new ClientConsoleCommunicatorClient(new NetTcpBinding(), new EndpointAddress("net.tcp://127.0.0.1:8000/ClientConsole/ClientConsoleCommunicator"));
87        cccc.GetStatusInfosCompleted += new EventHandler<GetStatusInfosCompletedEventArgs>(cccc_GetStatusInfosCompleted);
88        cccc.GetCurrentConnectionCompleted += new EventHandler<GetCurrentConnectionCompletedEventArgs>(cccc_GetCurrentConnectionCompleted);
89      }
90      catch (Exception) {
91        refreshTimer.Stop();
92        DialogResult res = MessageBox.Show("Connection Error, check if Hive Client is running!", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
93        if (res == DialogResult.OK)
94          this.Close();
95      }
96    }
97
98    void cccc_GetCurrentConnectionCompleted(object sender, GetCurrentConnectionCompletedEventArgs e) {
99      if (e.Error == null) {
100        ConnectionContainer curConnection = e.Result;
101        tbIPAdress.Text = curConnection.IPAdress;
102        tbPort.Text = curConnection.Port.ToString();
103      }
104    }
105
106    void cccc_GetStatusInfosCompleted(object sender, GetStatusInfosCompletedEventArgs e) {
107
108      if (e.Error == null) {
109        StatusCommons sc = e.Result;
110
111        lbGuid.Text = sc.ClientGuid.ToString();
112        lbConnectionStatus.Text = sc.Status.ToString();
113        lbJobdone.Text = sc.JobsDone.ToString();
114        lbJobsAborted.Text = sc.JobsAborted.ToString();
115        lbJobsFetched.Text = sc.JobsFetched.ToString();
116
117        this.Text = "Client Console (" + sc.Status.ToString() + ")";
118        lbStatus.Text = sc.Status.ToString();
119
120        ListViewItem curJobStatusItem;
121
122        if (sc.Jobs != null) {
123          lvJobDetail.Items.Clear();
124          double progress;
125          foreach (JobStatus curJob in sc.Jobs) {
126            curJobStatusItem = new ListViewItem(curJob.JobId.ToString());
127            curJobStatusItem.SubItems.Add(curJob.Since.ToString());
128            progress = curJob.Progress * 100;
129            curJobStatusItem.SubItems.Add(progress.ToString());
130            lvJobDetail.Items.Add(curJobStatusItem);
131          }
132          lvJobDetail.Sort();
133        }
134
135        UpdateGraph(sc.JobsDone, sc.JobsAborted);
136
137        if (sc.Status == NetworkEnumWcfConnState.Connected) {
138          btConnect.Enabled = false;
139          btnDisconnect.Enabled = true;
140          lbCs.Text = sc.ConnectedSince.ToString();
141          cccc.GetCurrentConnectionAsync();
142        } else if (sc.Status == NetworkEnumWcfConnState.Disconnected) {
143          btConnect.Enabled = true;
144          btnDisconnect.Enabled = false;
145          lbCs.Text = String.Empty;
146        } else if (sc.Status == NetworkEnumWcfConnState.Failed) {
147          btConnect.Enabled = true;
148          btnDisconnect.Enabled = false;
149          lbCs.Text = String.Empty;
150        }
151      }
152    }
153
154    private void GetEventLog() {
155      HiveClientEventLog = new EventLog("Hive Client");
156      HiveClientEventLog.Source = "Hive Client";
157      HiveClientEventLog.EntryWritten += new EntryWrittenEventHandler(OnEntryWritten);
158      HiveClientEventLog.EnableRaisingEvents = true;
159
160      ListViewItem curEventLogEntry;
161      foreach (EventLogEntry eve in HiveClientEventLog.Entries) {
162        curEventLogEntry = new ListViewItem("", 0);
163        if(eve.EntryType == EventLogEntryType.Error)
164          curEventLogEntry = new ListViewItem("", 1);
165        curEventLogEntry.SubItems.Add(eve.InstanceId.ToString());
166        curEventLogEntry.SubItems.Add(eve.Message);
167        curEventLogEntry.SubItems.Add(eve.TimeGenerated.ToString());
168        lvLog.Items.Add(curEventLogEntry);
169      }
170      lvJobDetail.Sort();
171    }
172
173    private void HiveClientConsole_Load(object sender, EventArgs e) {
174      //SetSize();
175    }
176
177    private void UpdateText(EventLogEntry ev) {
178      if (this.lvLog.InvokeRequired) {
179        this.lvLog.Invoke(new
180          UpdateTextDelegate(UpdateText), new object[] { ev });
181      } else {
182        ListViewItem curEventLogEntry;
183        curEventLogEntry = new ListViewItem("", 0);
184        if (ev.EntryType == EventLogEntryType.Error)
185          curEventLogEntry = new ListViewItem("", 1);
186        curEventLogEntry.SubItems.Add(ev.InstanceId.ToString());
187        curEventLogEntry.SubItems.Add(ev.Message);
188        curEventLogEntry.SubItems.Add(ev.TimeGenerated.ToString());
189        lvLog.Items.Add(curEventLogEntry);
190        lvJobDetail.Sort();
191      }
192    }
193
194    public void OnEntryWritten(object source, EntryWrittenEventArgs e) {
195      UpdateText(e.Entry);
196    }
197
198    private void UpdateGraph(int jobsDone, int jobsAborted) {
199      ZedGraphControl zgc = new ZedGraphControl();
200      GraphPane myPane = zgc.GraphPane;
201      myPane.GraphObjList.Clear();
202
203      myPane.Title.IsVisible = false;  // no title
204      myPane.Border.IsVisible = false; // no border
205      myPane.Chart.Border.IsVisible = false; // no border around the chart
206      myPane.XAxis.IsVisible = false;  // no x-axis
207      myPane.YAxis.IsVisible = false;  // no y-axis
208      myPane.Legend.IsVisible = false; // no legend
209
210      myPane.Fill.Color = Color.FromKnownColor(KnownColor.Control);
211
212      myPane.Chart.Fill.Type = FillType.None;
213      myPane.Fill.Type = FillType.Solid;
214
215      double sum = (double)jobsDone + jobsAborted;
216      double perDone = (double)jobsDone / sum * 100;
217      double perAborted = (double)jobsAborted / sum * 100;
218
219      myPane.AddPieSlice(perDone, Color.Green, 0.1, "");
220      myPane.AddPieSlice(perAborted, Color.Red, 0.1, "");
221
222      //Hides the slice labels
223      PieItem.Default.LabelType = PieLabelType.None;
224     
225      myPane.AxisChange();
226
227      pbGraph.Image = zgc.GetImage();
228    }
229
230    private void HiveClientConsole_Resize(object sender, EventArgs e) {
231      //SetSize();
232    }
233
234    private void lvLog_DoubleClick(object sender, EventArgs e) {
235      ListViewItem lvi = lvLog.SelectedItems[0];
236      HiveEventEntry hee = new HiveEventEntry(lvi.SubItems[2].Text, lvi.SubItems[3].Text, lvi.SubItems[1].Text);
237     
238      Form EventlogDetails = new EventLogEntryForm(hee);
239      EventlogDetails.Show();
240    }
241
242    private void btConnect_Click(object sender, EventArgs e) {
243      IPAddress ipAdress;
244      int port;
245      ConnectionContainer cc = new ConnectionContainer();
246      //IPAddress.TryParse(tbIPAdress.Text.ToString(), ipAdress);
247      if (IPAddress.TryParse(tbIPAdress.Text, out ipAdress) && int.TryParse(tbPort.Text, out port)) {
248        cc.IPAdress = tbIPAdress.Text;
249        cc.Port = port;
250        cccc.SetConnectionAsync(cc);
251      } else {
252        MessageBox.Show("IP Adress and/or Port Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
253      }
254    }
255
256    private void btnDisconnect_Click(object sender, EventArgs e) {
257      cccc.DisconnectAsync();
258    }
259
260    private void lvLog_ColumnClick(object sender, ColumnClickEventArgs e) {
261      // Determine if clicked column is already the column that is being sorted.
262      if (e.Column == lvwColumnSorter.SortColumn) {
263        // Reverse the current sort direction for this column.
264        if (lvwColumnSorter.Order == SortOrder.Ascending) {
265          lvwColumnSorter.Order = SortOrder.Descending;
266        } else {
267          lvwColumnSorter.Order = SortOrder.Ascending;
268        }
269      } else {
270        // Set the column number that is to be sorted; default to ascending.
271        lvwColumnSorter.SortColumn = e.Column;
272        lvwColumnSorter.Order = SortOrder.Ascending;
273      }
274
275      // Perform the sort with these new sort options.
276      lvLog.Sort();
277    }
278  }
279}
Note: See TracBrowser for help on using the repository browser.