Free cookie consent management tool by TermsFeed Policy Generator

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

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

added logic to client console (#397)

File size: 7.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    EventLog HiveClientEventLog;
44    ClientConsoleCommunicatorClient cccc;
45    System.Windows.Forms.Timer refreshTimer;
46
47    public HiveClientConsole() {
48      InitializeComponent();
49      InitTimer();
50      ConnectToClient();
51      RefreshGui();
52      GetEventLog();
53    }
54
55    private void InitTimer() {
56      refreshTimer = new System.Windows.Forms.Timer();
57      refreshTimer.Interval = 1000;
58      refreshTimer.Tick += new EventHandler(refreshTimer_Tick);
59      refreshTimer.Start();
60    }
61
62    void refreshTimer_Tick(object sender, EventArgs e) {
63      RefreshGui();
64    }
65   
66    private void RefreshGui() {
67      StatusCommons sc = new StatusCommons();
68     
69      try {
70        sc = cccc.GetStatusInfos();
71      }
72      catch (Exception ex) {
73        refreshTimer.Stop();
74        DialogResult res = MessageBox.Show("Connection Error, check if Hive Client is running!", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
75        if (res == DialogResult.OK)
76          this.Close();
77      }
78
79      lbGuid.Text = sc.ClientGuid.ToString();
80      lbCs.Text = sc.ConnectedSince.ToString();
81      lbConnectionStatus.Text = sc.Status.ToString();
82      lbJobdone.Text = sc.JobsDone.ToString();
83      lbJobsAborted.Text = sc.JobsAborted.ToString();
84      lbJobsFetched.Text = sc.JobsFetched.ToString();
85
86      this.Text = "Client Console (" + sc.Status.ToString() + ")";
87      lbStatus.Text = sc.Status.ToString();
88
89      ListViewItem curJobStatusItem;
90
91      if (sc.Jobs != null) {
92        lvJobDetail.Items.Clear();
93        double progress;
94        foreach (JobStatus curJob in sc.Jobs) {
95          curJobStatusItem = new ListViewItem(curJob.JobId.ToString());
96          curJobStatusItem.SubItems.Add(curJob.Since.ToString());
97          progress = curJob.Progress * 100;
98          curJobStatusItem.SubItems.Add(progress.ToString());
99          lvJobDetail.Items.Add(curJobStatusItem);
100        }
101      }
102
103      UpdateGraph(zGJobs, sc.JobsDone, sc.JobsAborted);
104 
105      if (sc.Status == NetworkEnumWcfConnState.Connected) {
106        btConnect.Enabled = false;
107        btnDisconnect.Enabled = true;
108        ConnectionContainer curConnection = cccc.GetCurrentConnection();
109        tbIPAdress.Text = curConnection.IPAdress;
110        tbPort.Text = curConnection.Port.ToString();
111      } else if (sc.Status == NetworkEnumWcfConnState.Disconnected) {
112        btConnect.Enabled = true;
113        btnDisconnect.Enabled = false;
114      } else if (sc.Status == NetworkEnumWcfConnState.Failed) {
115        btConnect.Enabled = true;
116        btnDisconnect.Enabled = false;
117      }
118    }
119
120    private void ConnectToClient() {
121      try {
122        cccc = new ClientConsoleCommunicatorClient();
123      }
124      catch (Exception) {
125        refreshTimer.Stop();
126        DialogResult res = MessageBox.Show("Connection Error, check if Hive Client is running!", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
127        if (res == DialogResult.OK)
128          this.Close();
129      }
130    }
131
132    private void GetEventLog() {
133      HiveClientEventLog = new EventLog("Hive Client");
134      HiveClientEventLog.Source = "Hive Client";
135      HiveClientEventLog.EntryWritten += new EntryWrittenEventHandler(OnEntryWritten);
136      HiveClientEventLog.EnableRaisingEvents = true;
137
138      ListViewItem curEventLogEntry;
139      foreach (EventLogEntry eve in HiveClientEventLog.Entries) {
140        curEventLogEntry = new ListViewItem("", 0);
141        if(eve.EntryType == EventLogEntryType.Error)
142          curEventLogEntry = new ListViewItem("", 1);
143        curEventLogEntry.SubItems.Add(eve.InstanceId.ToString());
144        curEventLogEntry.SubItems.Add(eve.Message);
145        curEventLogEntry.SubItems.Add(eve.TimeGenerated.Date.ToString());
146        curEventLogEntry.SubItems.Add(eve.TimeGenerated.TimeOfDay.ToString());
147        lvLog.Items.Add(curEventLogEntry);
148      }
149    }
150
151    private void HiveClientConsole_Load(object sender, EventArgs e) {
152      //SetSize();
153    }
154
155    private void UpdateText(EventLogEntry ev) {
156      if (this.lvLog.InvokeRequired) {
157        this.lvLog.Invoke(new
158          UpdateTextDelegate(UpdateText), new object[] { ev });
159      } else {
160        ListViewItem curEventLogEntry;
161        curEventLogEntry = new ListViewItem("", 0);
162        if (ev.EntryType == EventLogEntryType.Error)
163          curEventLogEntry = new ListViewItem("", 1);
164        curEventLogEntry.SubItems.Add(ev.EventID.ToString());
165        curEventLogEntry.SubItems.Add(ev.Message);
166        curEventLogEntry.SubItems.Add(ev.TimeGenerated.Date.ToString());
167        curEventLogEntry.SubItems.Add(ev.TimeGenerated.TimeOfDay.ToString());
168        lvLog.Items.Add(curEventLogEntry);
169      }
170    }
171
172    public void OnEntryWritten(object source, EntryWrittenEventArgs e) {
173      UpdateText(e.Entry);
174    }
175
176    private void UpdateGraph(ZedGraphControl zgc, int jobsDone, int jobsAborted) {
177      zgc.GraphPane.GraphObjList.Clear();
178      GraphPane myPane = zgc.GraphPane;
179
180      // Set the titles and axis labels
181      myPane.Legend.IsVisible = false;
182      myPane.Title.IsVisible = false;
183      myPane.Fill.Type = FillType.None;
184
185      double sum = jobsDone + jobsAborted;
186      double perDone = jobsDone / sum * 100;
187      double perAborted = jobsAborted / sum * 100;
188
189      myPane.AddPieSlice(perAborted, Color.Red, 0, "Jobs aborted");
190      myPane.AddPieSlice(perDone, Color.Green, 0.1, "Jobs done");
191      myPane.AxisChange();
192    }
193
194    private void HiveClientConsole_Resize(object sender, EventArgs e) {
195      //SetSize();
196    }
197
198    private void lvLog_DoubleClick(object sender, EventArgs e) {
199      ListViewItem lvi = lvLog.SelectedItems[0];
200      HiveEventEntry hee = new HiveEventEntry(lvi.SubItems[2].Text, lvi.SubItems[3].Text, lvi.SubItems[4].Text, lvi.SubItems[1].Text);
201     
202      Form EventlogDetails = new EventLogEntryForm(hee);
203      EventlogDetails.Show();
204    }
205
206    private void btConnect_Click(object sender, EventArgs e) {
207      IPAddress ipAdress;
208      int port;
209      ConnectionContainer cc = new ConnectionContainer();
210      //IPAddress.TryParse(tbIPAdress.Text.ToString(), ipAdress);
211      if (IPAddress.TryParse(tbIPAdress.Text, out ipAdress) && int.TryParse(tbPort.Text, out port)) {
212        cc.IPAdress = tbIPAdress.Text;
213        cc.Port = port;
214        cccc.SetConnection(cc);
215      } else {
216        MessageBox.Show("IP Adress and/or Port Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
217      }
218     
219    }
220
221    private void btnDisconnect_Click(object sender, EventArgs e) {
222      cccc.Disconnect();
223    }
224  }
225}
Note: See TracBrowser for help on using the repository browser.