Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Console/HiveServerManagementConsole.cs @ 1360

Last change on this file since 1360 was 1267, checked in by aleitner, 16 years ago

base for the asynchronous server managment console (#508)

File size: 16.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 HeuristicLab.Hive.Contracts.Interfaces;
31using HeuristicLab.Hive.Contracts.BusinessObjects;
32using HeuristicLab.Hive.Contracts;
33
34namespace HeuristicLab.Hive.Server.ServerConsole {
35
36  public delegate void closeForm(bool cf, bool error);
37
38  public partial class HiveServerManagementConsole : Form {
39
40    public event closeForm closeFormEvent;
41
42    #region private variables
43    private ResponseList<ClientGroup> clients = null;
44    private ResponseList<ClientInfo> clientInfo = null;
45    private ResponseList<Job> jobs = null;
46    private ResponseList<UserGroup> userGroups = null;
47    private ResponseList<User> usersList = null;
48
49    private Dictionary<long, ListViewGroup> clientObjects;
50    private Dictionary<long, ListViewItem> clientInfoObjects;
51    private Dictionary<long, ListViewItem> jobObjects;
52    private Dictionary<long, ListViewGroup> userGroupsObjects;
53    private Dictionary<long, ListViewItem> userListObjects;
54
55    private Job currentJob = null;
56    private ClientInfo currentClient = null;
57    private User currentUser = null;
58    private string nameCurrentJob = "";
59    private string nameCurrentClient = "";
60    private string nameCurrentUser = "";
61    private bool flagJob = false;
62    private bool flagClient = false;
63    private bool flagUser = false;
64
65    private ToolTip tt = new ToolTip();
66    #endregion
67
68    public HiveServerManagementConsole() {
69      InitializeComponent();
70      AddClients();
71      AddJobs();
72      AddUsers();
73      timerSyncronize.Start();
74    }
75
76    /// <summary>
77    /// event on Ticker
78    /// </summary>
79    /// <param name="obj"></param>
80    /// <param name="e"></param>
81    private void TickSync(object obj, EventArgs e) {
82      Refresh();
83      //updaterWoker.RunWorkerAsync();
84    }
85
86    /// <summary>
87    /// Adds clients to ListView and TreeView
88    /// </summary>
89    private void AddClients() {
90      try {
91        clientObjects = new Dictionary<long, ListViewGroup>();
92        clientInfoObjects = new Dictionary<long, ListViewItem>();
93        IClientManager clientManager =
94          ServiceLocator.GetClientManager();
95
96        clients = clientManager.GetAllClientGroups();
97        lvClientControl.Items.Clear();
98        tvClientControl.Nodes.Clear();
99        int count = 0;
100        foreach (ClientGroup cg in clients.List) {
101          tvClientControl.Nodes.Add(cg.Name);
102          ListViewGroup lvg = new ListViewGroup(cg.Name, HorizontalAlignment.Left);
103          foreach (ClientInfo ci in clientManager.GetAllClients().List) {
104            tvClientControl.Nodes[tvClientControl.Nodes.Count - 1].Nodes.Add(ci.Name);
105            ListViewItem item = new ListViewItem(ci.Name, count, lvg);
106            lvClientControl.Items.Add(item);
107            clientInfoObjects.Add(ci.Id, item);
108            count = (count + 1) % 3;
109          }
110          lvClientControl.Groups.Add(lvg);
111          clientObjects.Add(cg.Id, lvg);
112        } // Groups
113
114        clientInfo = clientManager.GetAllClients();
115        ListViewGroup lvunsorted = new ListViewGroup("unsorted", HorizontalAlignment.Left);
116        foreach (ClientInfo ci in clientInfo.List) {
117          tvClientControl.Nodes.Add(ci.Name);
118          lvClientControl.Items.Add(new ListViewItem(ci.Name, count, lvunsorted));
119          count = (count + 1) % 3;
120        }
121        lvClientControl.Groups.Add(lvunsorted);
122        if (flagClient) {
123          ClientClicked();
124        }
125      }
126      catch (Exception ex) {
127        closeFormEvent(true, true);
128        this.Close();
129      }
130    }
131
132    /// <summary>
133    /// Adds jobs to ListView and TreeView
134    /// </summary>
135    private void AddJobs() {
136      try {
137        jobObjects = new Dictionary<long, ListViewItem>();
138        IJobManager jobManager =
139          ServiceLocator.GetJobManager();
140        jobs = jobManager.GetAllJobs();
141
142        lvJobControl.Items.Clear();
143        tvJobControl.Nodes.Clear();
144
145        ListViewGroup lvJobCalculating = new ListViewGroup("calculating", HorizontalAlignment.Left);
146        ListViewGroup lvJobFinished = new ListViewGroup("finished", HorizontalAlignment.Left);
147        ListViewGroup lvJobPending = new ListViewGroup("pending", HorizontalAlignment.Left);
148        tvJobControl.Nodes.Add("calculating");
149        tvJobControl.Nodes.Add("finished");
150        tvJobControl.Nodes.Add("pending");
151        foreach (Job job in jobs.List) {
152          if (job.State == State.calculating) {
153            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobCalculating);
154            jobObjects.Add(job.Id, lvi);
155            tvJobControl.Nodes[0].Nodes.Add(job.Id.ToString());
156            lvJobControl.Items.Add(lvi);
157            lvi.ToolTipText = (job.Percentage * 100) + "% of job calculated";
158          } else if (job.State == State.finished) {
159            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobFinished);
160            jobObjects.Add(job.Id, lvi);
161            tvJobControl.Nodes[1].Nodes.Add(job.Id.ToString());
162            lvJobControl.Items.Add(lvi);
163          } else if (job.State == State.offline) {
164            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobPending);
165            jobObjects.Add(job.Id, lvi);
166            tvJobControl.Nodes[2].Nodes.Add(job.Id.ToString());
167            lvJobControl.Items.Add(lvi);
168          }
169        } // Jobs
170        lvJobControl.Groups.Add(lvJobCalculating);
171        lvJobControl.Groups.Add(lvJobFinished);
172        lvJobControl.Groups.Add(lvJobPending);
173        if (flagJob) {
174          JobClicked();
175        }
176      }
177      catch (Exception ex) {
178        closeFormEvent(true, true);
179        this.Close();
180      }
181    }
182
183    /// <summary>
184    /// Adds users to ListView and TreeView
185    /// </summary>
186    private void AddUsers() {
187      try {
188        userGroupsObjects = new Dictionary<long, ListViewGroup>();
189        userListObjects = new Dictionary<long, ListViewItem>();
190        IUserRoleManager userRoleManager =
191          ServiceLocator.GetUserRoleManager();
192
193        userGroups = userRoleManager.GetAllUserGroups();
194
195        lvUserControl.Items.Clear();
196        tvUserControl.Nodes.Clear();
197
198        foreach (UserGroup ug in userGroups.List) {
199          tvUserControl.Nodes.Add(ug.Name);
200          ListViewGroup lvg = new ListViewGroup(ug.Name, HorizontalAlignment.Left);
201
202          foreach (PermissionOwner permOwner in ug.Members) {
203            if (permOwner is User) {
204              User users = permOwner as User;
205              tvUserControl.Nodes[tvUserControl.Nodes.Count - 1].Nodes.Add(users.Name);
206              ListViewItem item = new ListViewItem(users.Name, 0, lvg);
207              lvUserControl.Items.Add(item);
208              userListObjects.Add(users.Id, item);
209            }
210          }
211          lvUserControl.Groups.Add(lvg);
212          userGroupsObjects.Add(ug.Id, lvg);
213
214        } // Users
215        usersList = userRoleManager.GetAllUsers();
216        ListViewGroup lvunsorted = new ListViewGroup("unsorted", HorizontalAlignment.Left);
217        foreach (User u in usersList.List) {
218          tvUserControl.Nodes.Add(u.Name);
219          lvUserControl.Items.Add(new ListViewItem(u.Name, 0, lvunsorted));
220        }
221        lvUserControl.Groups.Add(lvunsorted);
222        if (flagUser) {
223          UserClicked();
224        }
225      }
226      catch (Exception ex) {
227        closeFormEvent(true, true);
228        this.Close();
229      }
230    }
231
232    /// <summary>
233    /// if one client is clicked, a panel is opened with the details
234    /// </summary>
235    private void ClientClicked() {
236      int i = 0;
237      while (clientInfo.List[i].Name != nameCurrentClient) {
238        i++;
239      }
240      currentClient = clientInfo.List[i];
241      scClientControl.Panel2.Controls.Clear();
242      scClientControl.Panel2.Controls.Add(plClientDetails);
243      pbClientControl.Image = ilClientControl.Images[0];
244      lblClientName.Text = currentClient.Name;
245      lblLogin.Text = currentClient.Login.ToString();
246      lblState.Text = currentClient.State.ToString();
247    }
248
249    /// <summary>
250    /// if one job is clicked, a panel is opened with the details
251    /// </summary>
252    private void JobClicked() {
253      int i = 0;
254      while (jobs.List[i].Id.ToString() != nameCurrentJob) {
255        i++;
256      }
257      lvSnapshots.Enabled = false;
258      int yPos = 0;
259      currentJob = jobs.List[i];
260      scJobControl.Panel2.Controls.Clear();
261      scJobControl.Panel2.Controls.Add(plJobDetails);
262      pbJobControl.Image = ilJobControl.Images[0];
263      lblJobName.Text = currentJob.Id.ToString();
264      progressJob.Value = (int)(currentJob.Percentage * 100);
265      yPos = progressJob.Location.Y;
266      yPos += 20;
267      lblProgress.Location = new Point(
268        lblProgress.Location.X, yPos);
269      lblProgress.Text = (int)(currentJob.Percentage * 100) + "% calculated";
270      yPos += 20;
271      lblUserCreatedJob.Location = new Point(
272        lblUserCreatedJob.Location.X, yPos);
273      lblUserCreatedJob.Text = /* currentJob.User.Name + */ " created Job";
274      yPos += 20;
275      lblJobCreated.Location = new Point(
276        lblJobCreated.Location.X, yPos);
277      lblJobCreated.Text = "Created at " + currentJob.DateCreated;
278      if (currentJob.ParentJob != null) {
279        yPos += 20;
280        lblParentJob.Location = new Point(
281          lblParentJob.Location.X, yPos);
282        lblParentJob.Text = currentJob.ParentJob.Id + " is parent job";
283      }
284      yPos += 20;
285      lblPriorityJob.Location = new Point(
286        lblPriorityJob.Location.X, yPos);
287      lblPriorityJob.Text = "Priority of job is " + currentJob.Priority;
288      if (currentJob.Client != null) {
289        yPos += 20;
290        lblClientCalculating.Location = new Point(
291          lblClientCalculating.Location.X, yPos);
292        lblClientCalculating.Text = currentJob.Client.Name + " calculated Job";
293        yPos += 20;
294        lblJobCalculationBegin.Location = new Point(
295          lblJobCalculationBegin.Location.X, yPos);
296        lblJobCalculationBegin.Text = "Startet calculation at " + currentJob.DateCalculated ;
297       
298        if (currentJob.State == State.finished) {
299          yPos += 20;
300          lblJobCalculationEnd.Location = new Point(
301            lblJobCalculationEnd.Location.X, yPos);
302
303          IJobManager jobManager =
304            ServiceLocator.GetJobManager();
305
306          ResponseObject<JobResult> jobRes = jobManager.GetLastJobResultOf(currentJob.Id);
307
308         
309          lblJobCalculationEnd.Text = "Calculation ended at " + jobRes.Obj.DateFinished;
310        }
311      }                       
312      if (currentJob.State != State.offline) {
313        yPos += 20;
314        lvSnapshots.Location = new Point(
315          lvSnapshots.Location.X, yPos);
316        lvSnapshots.Size = new Size(lvSnapshots.Size.Width,
317          plJobDetails.Size.Height - 10 - yPos);
318        lvSnapshots.Enabled = true;
319      }
320    }
321
322    /// <summary>
323    /// if one user is clicked, a panel is opened with the details
324    /// </summary>
325    private void UserClicked() {
326      int i = 0;
327      while (usersList.List[i].Name != nameCurrentUser) {
328        i++;
329      }
330      currentUser = usersList.List[i];
331      scUserControl.Panel2.Controls.Clear();
332      scUserControl.Panel2.Controls.Add(plUserDetails);
333      pbUserControl.Image = ilUserControl.Images[0];
334      lblUserName.Text = currentUser.Id.ToString();
335    }
336
337    #region Eventhandlers
338    /// <summary>
339    /// Send event to Login-GUI when closing
340    /// </summary>
341    /// <param name="sender"></param>
342    /// <param name="e"></param>
343    private void Close_Click(object sender, EventArgs e) {
344      if (closeFormEvent != null) {
345        closeFormEvent(true, false);
346      }
347      this.Close();
348    }
349
350    /// <summary>
351    /// Send evnt to Login-GUI when closing
352    /// </summary>
353    /// <param name="sender"></param>
354    /// <param name="e"></param>
355    private void HiveServerConsoleInformation_FormClosing(object sender, FormClosingEventArgs e) {
356      if (closeFormEvent != null) {
357        closeFormEvent(true, false);
358      }
359    }
360
361    private void AddJob_Click(object sender, EventArgs e) {
362      AddJobForm newForm = new AddJobForm();
363      newForm.Show();
364      newForm.addJobEvent += new addDelegate(Refresh);
365    }
366
367    private void Refresh() {
368      AddClients();
369      AddJobs();
370      AddUsers();
371    }
372
373    private void AddUser_Click(object sender, EventArgs e) {
374      AddUserForm newForm = new AddUserForm("User", false);
375      newForm.Show();
376      newForm.addUserEvent += new addDelegate(Refresh);
377    }
378
379    private void AddUserGroup_Click(object sender, EventArgs e) {
380      AddUserForm newForm = new AddUserForm("User", true);
381      newForm.Show();
382      newForm.addUserEvent += new addDelegate(Refresh);                                             
383    }
384
385    private void OnLVClientClicked(object sender, EventArgs e) {
386      nameCurrentClient = lvClientControl.SelectedItems[0].Text;
387      flagClient = true;
388      ClientClicked();
389    }
390
391    private void OnTVClientClicked(object sender, TreeViewEventArgs e) {
392      bool clientgroup = false;
393      foreach (ClientGroup cg in clients.List) {
394        if (tvClientControl.SelectedNode.Text == cg.Name) {
395          clientgroup = true;
396        }
397      }
398      if (clientgroup == false) {
399        nameCurrentClient = tvClientControl.SelectedNode.Text;
400        flagClient = true;
401        ClientClicked();
402      }
403
404    }
405
406    private void OnLVJobControlClicked(object sender, EventArgs e) {
407      nameCurrentJob = lvJobControl.SelectedItems[0].Text;
408      flagJob = true;
409      JobClicked();
410    }
411
412    private void OnTVJobControlClicked(object sender, TreeViewEventArgs e) {
413      try {
414        nameCurrentJob = Convert.ToInt32(tvJobControl.SelectedNode.Text).ToString();
415        flagJob = true;
416        JobClicked();
417      }
418      catch (Exception ex) { }
419
420    }
421
422    private void OnLVUserControlClicked(object sender, EventArgs e) {
423      nameCurrentUser = lvUserControl.SelectedItems[0].Text;
424      flagUser = true;
425      UserClicked();
426    }
427
428    private void OnTVUserControlClicked(object sender, TreeViewEventArgs e) {
429      bool usergroup = false;
430      foreach (UserGroup ug in userGroups.List) {
431        if (tvUserControl.SelectedNode.Text == ug.Name) {
432          usergroup = true;
433        }
434      }
435      if (usergroup == false) {
436        nameCurrentUser = tvUserControl.SelectedNode.Text;
437        flagUser = true;
438        UserClicked();
439      }
440
441    }
442
443
444    private void btnClientClose_Click(object sender, EventArgs e) {
445      scClientControl.Panel2.Controls.Clear();
446      scClientControl.Panel2.Controls.Add(lvClientControl);
447      flagClient = false;
448    }
449 
450    private void btnJobDetailClose_Click(object sender, EventArgs e) {
451      scJobControl.Panel2.Controls.Clear();
452      scJobControl.Panel2.Controls.Add(lvJobControl);
453      flagJob = false;
454    }
455
456    private void btnUserControlClose_Click(object sender, EventArgs e) {
457      scUserControl.Panel2.Controls.Clear();
458      scUserControl.Panel2.Controls.Add(lvUserControl);
459      flagUser = false;
460    }
461
462    private void lvJobControl_MouseMove(object sender, MouseEventArgs e) {
463      if ((lvJobControl.GetItemAt(e.X, e.Y) != null) &&
464        (lvJobControl.GetItemAt(e.X, e.Y).ToolTipText != null)) {
465        tt.SetToolTip(lvJobControl, lvJobControl.GetItemAt(e.X, e.Y).ToolTipText);
466      }
467    }
468
469    private void updaterWoker_DoWork(object sender, DoWorkEventArgs e) {
470      ResponseList<ClientInfo> clientInfoOld = clientInfo;
471      IClientManager clientManager =
472          ServiceLocator.GetClientManager();
473      clientInfo = clientManager.GetAllClients();
474      foreach (ClientInfo ci in clientInfo.List) {
475        foreach (ClientInfo cio in clientInfoOld.List) {
476          ci.Id.Equals(cio.Id);
477        }
478      }
479     
480    }
481    #endregion
482  }
483}
Note: See TracBrowser for help on using the repository browser.