Free cookie consent management tool by TermsFeed Policy Generator

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

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

added sort after date (#397)

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