Free cookie consent management tool by TermsFeed Policy Generator

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

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

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