Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1774 was 1774, checked in by aleitner, 15 years ago

Job details in Listview, Refresh-Button on Client refreshing and overlaying form on refresh (#600)

File size: 34.5 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;
33using System.Threading;
34
35namespace HeuristicLab.Hive.Server.ServerConsole {
36
37  /// <summary>
38  /// if form is closed the loginform gets an information
39  /// </summary>
40  /// <param name="cf"></param>
41  /// <param name="error"></param>
42  public delegate void closeForm(bool cf, bool error);
43
44  public partial class HiveServerManagementConsole : Form {
45
46    public event closeForm closeFormEvent;
47
48    #region private variables
49    private ResponseList<ClientGroup> clientGroups = null;
50    private ResponseList<ClientInfo> clients = null;
51    private ResponseList<Job> jobs = null;
52
53    //TODO delete
54    private Dictionary<Guid, ListViewGroup> clientObjects;
55    private Dictionary<Guid, ListViewItem> clientInfoObjects;
56    private Dictionary<Guid, ListViewItem> jobObjects;
57
58    private Job currentJob = null;
59    private ClientInfo currentClient = null;
60   
61    TreeNode currentNode = null;
62
63    //TODO delete
64    private string nameCurrentJob = "";
65    private string nameCurrentClient = "";
66    private bool flagJob = false;
67
68    private List<Changes> changes = new List<Changes>();
69
70    private ToolTip tt = new ToolTip();
71
72    private const string NOGROUP = "No group";
73    //private List<ListViewItem> clientList = new List<ListViewItem>();
74    private Dictionary<Guid, List<ListViewItem>> clientList = new Dictionary<Guid, List<ListViewItem>>();
75
76    private IClientManager ClientManager {
77      get {
78        return ServiceLocator.GetClientManager();
79      }
80
81    }
82    #endregion
83
84    public HiveServerManagementConsole() {
85      InitializeComponent();
86      Init();
87      AddClients();
88      AddJobs();
89      timerSyncronize.Start();
90    }
91
92
93    #region Backgroundworker
94    /// <summary>
95    /// event on Ticker
96    /// </summary>
97    /// <param name="obj"></param>
98    /// <param name="e"></param>
99    private void TickSync(object obj, EventArgs e) {
100      if (!updaterWoker.IsBusy) {
101        updaterWoker.RunWorkerAsync();
102      }
103    }
104
105    private void updaterWoker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
106      Refresh();
107    }
108
109    #endregion
110
111    private Guid ConvertStringToGuid(string stringGuid) {
112      return new Guid(stringGuid);
113    }
114
115
116    private void Init() {
117
118      //adding context menu items for jobs
119      menuItemAbortJob.Click += (s, e) => {
120        IJobManager jobManager = ServiceLocator.GetJobManager();
121        if (lvJobControl.SelectedItems.Count == 1) {
122          jobManager.AbortJob(((Job)(lvJobControl.SelectedItems[0].Tag)).Id);
123        }
124      };
125
126      //adding context menu items for jobs
127      menuItemGetSnapshot.Click += (s, e) => {
128        IJobManager jobManager = ServiceLocator.GetJobManager();
129        if (lvJobControl.SelectedItems.Count == 1) {
130          jobManager.RequestSnapshot(((Job)(lvJobControl.SelectedItems[0].Tag)).Id);
131        }
132      };
133
134    }
135
136    private void lvJobControl_MouseUp(object sender, MouseEventArgs e) {
137      // If the right mouse button was clicked and released,
138      // display the shortcut menu assigned to the ListView.
139      lvJobControl.ContextMenuStrip.Items.Clear();
140      ListViewHitTestInfo hitTestInfo = lvJobControl.HitTest(e.Location);
141      if (e.Button == MouseButtons.Right && hitTestInfo.Item != null && lvJobControl.SelectedItems.Count == 1) {
142        Job selectedJob = (Job)lvJobControl.SelectedItems[0].Tag;
143
144        if (selectedJob != null && selectedJob.State == State.calculating) {
145          lvJobControl.ContextMenuStrip.Items.Add(menuItemAbortJob);
146          lvJobControl.ContextMenuStrip.Items.Add(menuItemGetSnapshot);
147        }
148      }
149      lvJobControl.ContextMenuStrip.Show(new Point(e.X, e.Y));
150    }
151
152    ///// <summary>
153    ///// Adds clients to ListView and TreeView
154    ///// </summary>
155    //private void AddClients() {
156    //  try {
157    //    clientObjects = new Dictionary<Guid, ListViewGroup>();
158    //    clientInfoObjects = new Dictionary<Guid, ListViewItem>();
159
160    //    clients = ClientManager.GetAllClientGroups();
161
162    //    List<Guid> inGroup = new List<Guid>();
163    //    foreach (ClientGroup cg in clients.List) {
164    //      TreeNode tn = new TreeNode(cg.Name);
165    //      tn.Tag = cg;
166    //      tvClientControl.Nodes.Add(tn);
167
168    //      ListViewGroup lvg = new ListViewGroup(cg.Name, HorizontalAlignment.Left);
169
170    //      foreach (ClientInfo ci in cg.Resources) {
171    //        ListViewItem item = null;
172    //        if ((ci.State == State.offline) || (ci.State == State.nullState)) {
173    //          item = new ListViewItem(ci.Name, 3, lvg);
174    //        } else {
175    //          int percentageUsage = CapacityRam(ci.NrOfCores, ci.NrOfFreeCores);
176    //          int usage = 0;
177    //          if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
178    //            usage = 0;
179    //          } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
180    //            usage = 1;
181    //          } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
182    //            usage = 2;
183    //          }
184
185    //          item = new ListViewItem(ci.Name, usage, lvg);
186    //        }
187    //        item.Tag = ci.Id;
188    //        lvClientControl.Items.Add(item);
189    //        clientInfoObjects.Add(ci.Id, item);
190    //        inGroup.Add(ci.Id);
191
192    //      }
193    //      lvClientControl.BeginUpdate();
194    //      lvClientControl.Groups.Add(lvg);
195    //      lvClientControl.EndUpdate();
196    //      clientObjects.Add(cg.Id, lvg);
197    //    } // Groups
198
199    //    clientInfo = ClientManager.GetAllClients();
200    //    ListViewGroup lvunsorted = new ListViewGroup("no group", HorizontalAlignment.Left);
201    //    foreach (ClientInfo ci in clientInfo.List) {
202    //      bool help = false;
203    //      foreach (Guid client in inGroup) {
204    //        if (client == ci.Id) {
205    //          help = true;
206    //          break;
207    //        }
208    //      }
209    //      if (!help) {
210    //        ListViewItem item = null;
211    //        if ((ci.State == State.offline) || (ci.State == State.nullState)) {
212    //          item = new ListViewItem(ci.Name, 3, lvunsorted);
213    //        } else {
214    //          int percentageUsage = CapacityRam(ci.NrOfCores, ci.NrOfFreeCores);
215    //          int usage = 0;
216    //          if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
217    //            usage = 0;
218    //          } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
219    //            usage = 1;
220    //          } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
221    //            usage = 2;
222    //          }
223    //          item = new ListViewItem(ci.Name, usage, lvunsorted);
224    //        }
225    //        item.Tag = ci.Id;
226    //        lvClientControl.Items.Add(item);
227    //      }
228    //    }
229    //    lvClientControl.BeginUpdate();
230    //    lvClientControl.Groups.Add(lvunsorted);
231    //    lvClientControl.EndUpdate();
232    //    if (flagClient) {
233    //      ClientClicked();
234    //    }
235    //  }
236    //  catch (Exception ex) {
237    //    closeFormEvent(true, true);
238    //    this.Close();
239    //  }
240    //}
241
242    private void AddClients() {
243      clientList.Clear();
244      tvClientControl.Nodes.Clear();
245
246      clientGroups = ClientManager.GetAllClientGroups();
247
248      foreach (ClientGroup cg in clientGroups.List) {
249        AddClientOrGroup(cg, null);
250      }
251
252      if (currentNode != null) {
253        lvClientControl.Items.Clear();
254        lvClientControl.Groups.Clear();
255        AddGroupsToListView(currentNode);
256      }
257      tvClientControl.ExpandAll();
258      Thread.Sleep(5000);
259    }
260
261
262    private void AddClientOrGroup(ClientGroup clientGroup, TreeNode currentNode) {
263      currentNode = CreateTreeNode(clientGroup, currentNode);
264      List<ListViewItem> clientGroupList = new List<ListViewItem>();
265      ListViewGroup lvg;
266      if (string.IsNullOrEmpty(clientGroup.Name)) {
267        lvg = new ListViewGroup(NOGROUP, HorizontalAlignment.Left);
268      } else {
269        lvg = new ListViewGroup(clientGroup.Name, HorizontalAlignment.Left);
270      }
271      lvClientControl.Groups.Add(lvg);
272      foreach (Resource resource in clientGroup.Resources) {
273        if (resource is ClientInfo) {
274          int percentageUsage = CapacityRam(((ClientInfo)resource).NrOfCores, ((ClientInfo)resource).NrOfFreeCores);
275          int usage = 3;
276          if ((((ClientInfo)resource).State != State.offline) &&
277            (((ClientInfo)resource).State != State.nullState)) {
278            if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
279              usage = 0;
280            } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
281              usage = 1;
282            } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
283              usage = 2;
284            }
285          }
286          ListViewItem lvi = new ListViewItem(resource.Name, usage, lvg);
287          lvi.Tag = resource as ClientInfo;
288          clientGroupList.Add(lvi);
289        } else if (resource is ClientGroup) {
290          AddClientOrGroup(resource as ClientGroup, currentNode);
291        }
292      }
293      clientList.Add(clientGroup.Id, clientGroupList);
294    }
295
296    private TreeNode CreateTreeNode(ClientGroup clientGroup, TreeNode currentNode) {
297      TreeNode tn;
298      if (string.IsNullOrEmpty(clientGroup.Name)) {
299        tn = new TreeNode(NOGROUP);
300      } else {
301        tn = new TreeNode(clientGroup.Name);
302      }
303      tn.Tag = clientGroup;
304      if (currentNode == null) {
305        tvClientControl.Nodes.Add(tn);
306      } else {
307        currentNode.Nodes.Add(tn);
308      }
309      return tn;
310    }
311
312
313    List<ListViewGroup> jobGroup;
314    /// <summary>
315    /// Adds jobs to ListView and TreeView
316    /// </summary>
317    private void AddJobs() {
318      try {
319        jobObjects = new Dictionary<Guid, ListViewItem>();
320        IJobManager jobManager =
321          ServiceLocator.GetJobManager();
322        jobs = jobManager.GetAllJobs();
323
324        lvJobControl.Items.Clear();
325
326        ListViewGroup lvJobCalculating = new ListViewGroup("calculating", HorizontalAlignment.Left);
327        ListViewGroup lvJobFinished = new ListViewGroup("finished", HorizontalAlignment.Left);
328        ListViewGroup lvJobPending = new ListViewGroup("pending", HorizontalAlignment.Left);
329
330        jobGroup = new List<ListViewGroup>();
331        jobGroup.Add(lvJobCalculating);
332        jobGroup.Add(lvJobFinished);
333        jobGroup.Add(lvJobPending);
334
335        foreach (Job job in jobs.List) {
336          if (job.State == State.calculating) {
337            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 1, lvJobCalculating);
338            lvi.Tag = job;
339            jobObjects.Add(job.Id, lvi);
340
341            //lvJobControl.Items.Add(lvi);
342
343            lvi.ToolTipText = (job.Percentage * 100) + "% of job calculated";
344          } else if (job.State == State.finished) {
345            ListViewItem lvi = new ListViewItem(job.Id.ToString(),0, lvJobFinished);
346            lvi.Tag = job;
347            jobObjects.Add(job.Id, lvi);
348            //lvJobControl.Items.Add(lvi);
349          } else if (job.State == State.offline) {
350            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 2, lvJobPending);
351            lvi.Tag = job;
352            jobObjects.Add(job.Id, lvi);
353            //lvJobControl.Items.Add(lvi);
354          }
355        } // Jobs
356        lvJobControl.BeginUpdate();
357        foreach (ListViewItem lvi in jobObjects.Values) {
358          lvJobControl.Items.Add(lvi);
359        }
360        lvJobControl.Groups.Add(lvJobCalculating);
361        lvJobControl.Groups.Add(lvJobFinished);
362        lvJobControl.Groups.Add(lvJobPending);
363        lvJobControl.EndUpdate();
364        if (flagJob) {
365          JobClicked();
366        }
367      }
368      catch (Exception ex) {
369        closeFormEvent(true, true);
370        this.Close();
371      }
372    }
373
374    /// <summary>
375    /// if one client is clicked, the details for the clicked client are shown
376    /// in the second panel
377    /// </summary>
378    private void ClientClicked() {
379      plClientDetails.Visible = true;
380      //int i = 0;
381      //while (clientInfo.List[i].Id.ToString() != nameCurrentClient) {
382      //  i++;
383      //}
384      if (lvClientControl.SelectedItems != null && lvClientControl.SelectedItems.Count > 0) {
385        currentClient = (ClientInfo)lvClientControl.SelectedItems[0].Tag;
386      }
387      if(currentClient != null) {
388      int percentageUsage = CapacityRam(currentClient.NrOfCores, currentClient.NrOfFreeCores);
389      int usage = 3;
390      if ((currentClient.State != State.offline) && (currentClient.State != State.nullState)) {
391        if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
392          usage = 0;
393        } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
394          usage = 1;
395        } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
396          usage = 2;
397        }
398      }
399      pbClientControl.Image = ilLargeImgClient.Images[usage];
400      lblClientName.Text = currentClient.Name;
401      lblLogin.Text = currentClient.Login.ToString();
402      lblState.Text = currentClient.State.ToString();
403      }
404    }
405
406    /// <summary>
407    /// if one job is clicked, the details for the clicked job are shown
408    /// in the second panel
409    /// </summary>
410    private void JobClicked() {
411      plJobDetails.Visible = true;
412      lvJobDetails.Items.Clear();
413      int i = 0;
414      while (jobs.List[i].Id.ToString() != nameCurrentJob) {
415        i++;
416      }
417      lvSnapshots.Enabled = true;
418      currentJob = jobs.List[i];
419      if (currentJob.State == State.offline) {
420        pbJobControl.Image = ilLargeImgJob.Images[2];
421      } else if (currentJob.State == State.calculating) {
422        pbJobControl.Image = ilLargeImgJob.Images[1];
423      } else if (currentJob.State == State.finished) {
424        pbJobControl.Image = ilLargeImgJob.Images[0];
425      }
426
427
428
429      lblJobName.Text = currentJob.Id.ToString();
430      progressJob.Value = (int)(currentJob.Percentage * 100);
431      lblProgress.Text = (int)(currentJob.Percentage * 100) + "% calculated";
432      lblUserCreatedJob.Text = currentJob.UserId.ToString() + /* currentJob.User.Name + */ " created Job";
433      //lblJobCreated.Text = "Created at " + currentJob.DateCreated;
434      ListViewItem lvi = new ListViewItem();
435      lvi.Text = "User:";
436      lvi.SubItems.Add(currentJob.UserId.ToString());
437      lvJobDetails.Items.Add(lvi);
438
439        lvi = null;
440        lvi = new ListViewItem();
441        lvi.Text = "created at:";
442        lvi.SubItems.Add(currentJob.DateCreated.ToString());
443        lvJobDetails.Items.Add(lvi);
444
445      if (currentJob.ParentJob != null) {
446        lvi = null;
447        lvi = new ListViewItem();
448        lvi.Text = "Parent job:";
449        lvi.SubItems.Add(currentJob.ParentJob.ToString());
450        lvJobDetails.Items.Add(lvi);
451     // lblParentJob.Text = currentJob.ParentJob.Id + " is parent job";
452      }// else {
453      //  lblParentJob.Text = "";
454      //}
455
456      lvi = null;
457      lvi = new ListViewItem();
458      lvi.Text = "Priority:";
459      lvi.SubItems.Add(currentJob.Priority.ToString());
460      lvJobDetails.Items.Add(lvi);
461
462     // lblPriorityJob.Text = "Priority of job is " + currentJob.Priority;
463      if (currentJob.Client != null) {
464        lvi = null;
465        lvi = new ListViewItem();
466        lvi.Text = "Calculation begin:";
467        lvi.SubItems.Add(currentJob.DateCalculated.ToString());
468        lvJobDetails.Items.Add(lvi);
469
470
471        lvi = null;
472        lvi = new ListViewItem();
473        lvi.Text = "Client calculated:";
474        lvi.SubItems.Add(currentJob.Client.Name.ToString());
475        lvJobDetails.Items.Add(lvi);
476
477        //lblClientCalculating.Text = currentJob.Client.Name + " calculated Job";
478        //lblJobCalculationBegin.Text = "Startet calculation at " + currentJob.DateCalculated;
479
480        if (currentJob.State == State.finished) {
481          IJobManager jobManager =
482            ServiceLocator.GetJobManager();
483          ResponseObject<JobResult> jobRes = jobManager.GetLastJobResultOf(currentJob.Id, false);
484         
485          lvi = null;
486          lvi = new ListViewItem();
487          lvi.Text = "Calculation ended:";
488          lvi.SubItems.Add(jobRes.Obj.DateFinished.ToString());
489          lvJobDetails.Items.Add(lvi);
490         // lblJobCalculationEnd.Text = "Calculation ended at " + jobRes.Obj.DateFinished;
491        }
492      } else {
493       // lblClientCalculating.Text = "";
494       // lblJobCalculationBegin.Text = "";
495       // lblJobCalculationEnd.Text = "";
496      }
497      if (currentJob.State != State.offline) {
498        lvSnapshots.Items.Clear();
499        if (currentJob.State == State.finished)
500          GetSnapshotList();
501        lvSnapshots.Visible = true;
502      } else {
503        lvSnapshots.Visible = false;
504      }
505    }
506
507    private void Refresh() {
508      foreach (Changes change in changes) {
509        if (change.Types == Type.Job) {
510          RefreshJob(change);
511        } else if (change.Types == Type.Client) {
512          //RefreshClient(change);
513        } else if (change.Types == Type.ClientGroup) {
514          //RefreshClientGroup(change);
515        }
516      }
517    }
518
519    private void RefreshJob(Changes change) {
520      if (change.ChangeType == Change.Update) {
521        for (int i = 0; i < lvJobControl.Items.Count; i++) {
522          if (lvJobControl.Items[i].Text == change.ID.ToString()) {
523            if (nameCurrentJob == change.ID.ToString()) {
524              JobClicked();
525            }
526            foreach (Job job in jobs.List) {
527              if (job.Id == change.ID) {
528                lvJobControl.Items[i].Tag = job;
529                break;
530              }
531            }
532            State state = jobs.List[change.Position].State;
533            System.Diagnostics.Debug.WriteLine(lvJobControl.Items[i].Text.ToString());
534            if (state == State.finished) {
535              lvJobControl.Items[i].Group = jobGroup[1];
536              lvJobControl.Items[i].ImageIndex = 0;
537              System.Diagnostics.Debug.WriteLine("finished");
538            } else if (state == State.calculating) {
539              lvJobControl.Items[i].Group = jobGroup[0];
540              lvJobControl.Items[i].ImageIndex = 1;
541              System.Diagnostics.Debug.WriteLine("calculating");
542            } else if (state == State.offline) {
543              lvJobControl.Items[i].Group = jobGroup[2];
544              lvJobControl.Items[i].ImageIndex = 2;
545              System.Diagnostics.Debug.WriteLine("offline");
546
547            }
548            lvJobControl.Refresh();
549          }
550        }
551      } else if (change.ChangeType == Change.Create) {
552        ListViewItem lvi = new ListViewItem(
553          change.ID.ToString(), 2, jobGroup[2]);
554        jobObjects.Add(change.ID, lvi);
555        lvJobControl.Items.Add(lvi);
556
557      } else if (change.ChangeType == Change.Delete) {
558        jobObjects.Remove(change.ID);
559        for (int i = 0; i < lvJobControl.Items.Count; i++) {
560          if (change.ID.ToString() == lvJobControl.Items[i].Text.ToString()) {
561            lvJobControl.Items[i].Remove();
562            break;
563          }
564        }
565      }
566    }
567
568    private void RefreshClient(Changes change) {
569      if (change.ChangeType == Change.Update) {
570        for (int i = 0; i < lvClientControl.Items.Count; i++) {
571          if (lvClientControl.Items[i].Tag.ToString() == change.ID.ToString()) {
572            if (nameCurrentClient == change.ID.ToString()) {
573              ClientClicked();
574            }
575            State state = clients.List[change.Position].State;
576            System.Diagnostics.Debug.WriteLine(lvClientControl.Items[i].Text.ToString());
577
578            ClientInfo ci = null;
579
580            foreach (ClientInfo c in clients.List) {
581              if (c.Id == change.ID) {
582                ci = c;
583              }
584            }
585
586            int percentageUsage = CapacityRam(ci.NrOfCores, ci.NrOfFreeCores);
587            if ((state == State.offline) || (state == State.nullState)) {
588              lvClientControl.Items[i].ImageIndex = 3;
589            } else {
590              if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
591                lvClientControl.Items[i].ImageIndex = 0;
592              } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
593                lvClientControl.Items[i].ImageIndex = 1;
594              } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
595                lvClientControl.Items[i].ImageIndex = 2;
596              }
597
598            }
599            lvClientControl.Refresh();
600          }
601        }
602
603
604      } else if (change.ChangeType == Change.Create) {
605
606      } else if (change.ChangeType == Change.Delete) {
607        clientInfoObjects.Remove(change.ID);
608        for (int i = 0; i < lvClientControl.Items.Count; i++) {
609          if (change.ID.ToString() == lvClientControl.Items[i].Text.ToString()) {
610            lvClientControl.Items[i].Remove();
611            break;
612          }
613        }
614
615      }
616    }
617
618    private void RefreshClientGroup(Changes change) {
619
620    }
621
622    #region Eventhandlers
623    /// <summary>
624    /// Send event to Login-GUI when closing
625    /// </summary>
626    /// <param name="sender"></param>
627    /// <param name="e"></param>
628    private void Close_Click(object sender, EventArgs e) {
629      if (closeFormEvent != null) {
630        closeFormEvent(true, false);
631      }
632      this.Close();
633    }
634
635    /// <summary>
636    /// Send evnt to Login-GUI when closing
637    /// </summary>
638    /// <param name="sender"></param>
639    /// <param name="e"></param>
640    private void HiveServerConsoleInformation_FormClosing(object sender, FormClosingEventArgs e) {
641      if (closeFormEvent != null) {
642        closeFormEvent(true, false);
643      }
644    }
645
646    private void AddJob_Click(object sender, EventArgs e) {
647      AddJobForm newForm = new AddJobForm();
648      newForm.Show();
649      newForm.addJobEvent += new addDelegate(updaterWoker.RunWorkerAsync);
650    }
651
652    private void OnLVClientClicked(object sender, EventArgs e) {
653      nameCurrentClient = lvClientControl.SelectedItems[0].Tag.ToString();
654      ClientClicked();
655    }
656
657    private void OnLVJobControlClicked(object sender, EventArgs e) {
658      nameCurrentJob = lvJobControl.SelectedItems[0].Text;
659      flagJob = true;
660      JobClicked();
661    }
662
663    private void lvJobControl_MouseMove(object sender, MouseEventArgs e) {
664      if ((lvJobControl.GetItemAt(e.X, e.Y) != null) &&
665        (lvJobControl.GetItemAt(e.X, e.Y).ToolTipText != null)) {
666        tt.SetToolTip(lvJobControl, lvJobControl.GetItemAt(e.X, e.Y).ToolTipText);
667      }
668    }
669
670    private void updaterWoker_DoWork(object sender, DoWorkEventArgs e) {
671
672      changes.Clear();
673
674      //#region ClientInfo
675      //ResponseList<ClientInfo> clientInfoOld = clientInfo;
676      //clientInfo = ClientManager.GetAllClients();
677
678      //IDictionary<int, ClientInfo> clientInfoOldHelp;
679
680      //CloneList(clientInfoOld, out clientInfoOldHelp);
681
682      //GetDelta(clientInfoOld.List, clientInfoOldHelp);
683      //#endregion
684
685      #region Clients
686      //ResponseList<ClientGroup> clientsOld = clients;
687
688     // newClients = ClientManager.GetAllClientGroups();
689
690      //IDictionary<Guid, ClientGroup> clientsOldHelp;
691
692      //CloneList(clientsOld, out clientsOldHelp);
693
694      //GetDelta(clientsOld.List, clientsOldHelp);
695      //DetermineDelta();
696      #endregion
697
698      #region Job
699      ResponseList<Job> jobsOld = jobs;
700      IJobManager jobManager =
701          ServiceLocator.GetJobManager();
702
703      jobs = jobManager.GetAllJobs();
704
705      IDictionary<int, Job> jobsOldHelp;
706      CloneList(jobsOld, out jobsOldHelp);
707
708      GetDelta(jobsOld.List, jobsOldHelp);
709
710      #endregion
711
712      foreach (Changes change in changes) {
713        System.Diagnostics.Debug.WriteLine(change.ID + " " + change.ChangeType);
714      }
715
716    }
717    #endregion
718
719    #region Helper methods
720
721    private void CloneList(ResponseList<Job> oldList, out IDictionary<int, Job> newList) {
722      newList = new Dictionary<int, Job>();
723      for (int i = 0; i < oldList.List.Count; i++) {
724        newList.Add(i, oldList.List[i]);
725      }
726    }
727
728    //private void CloneList(ResponseList<ClientInfo> oldList, out IDictionary<int, ClientInfo> newList) {
729    //  newList = new Dictionary<int, ClientInfo>();
730    //  for (int i = 0; i < oldList.List.Count; i++) {
731    //    newList.Add(i, oldList.List[i]);
732    //  }
733    //}
734
735    private void CloneList(ResponseList<ClientGroup> oldList, out IDictionary<Guid, ClientGroup> newList) {
736      newList = new Dictionary<Guid, ClientGroup>();
737      foreach (ClientGroup clientGroup in oldList.List) {
738        newList.Add(clientGroup.Id, clientGroup);
739      }
740    }
741
742    private bool IsEqual(ClientInfo ci1, ClientInfo ci2) {
743      if (ci2 == null) {
744        return false;
745      }
746      if (ci1.Id.Equals(ci2.Id)) {
747        return true;
748      } else return false;
749    }
750
751    private int CapacityRam(int noCores, int freeCores) {
752      if (noCores > 0) {
753        int capacity = ((noCores - freeCores) / noCores) * 100;
754        System.Diagnostics.Debug.WriteLine(capacity);
755        return capacity;
756      }
757      return 100;
758    }
759
760    //private void GetDelta(IList<ClientInfo> oldClient, IDictionary<int, ClientInfo> helpClients) {
761    //  bool found = false;
762
763    //  for (int i = 0; i < clientInfo.List.Count; i++) {
764    //    ClientInfo ci = clientInfo.List[i];
765    //    for (int j = 0; j < oldClient.Count; j++) {
766    //      ClientInfo cio = oldClient[j];
767    //      if (ci.Id.Equals(cio.Id)) {
768    //        found = true;
769    //        if ((ci.State != cio.State) || (ci.NrOfFreeCores != cio.NrOfFreeCores)) {
770    //          changes.Add(new Changes { Types = Type.Client, ID = ci.Id, ChangeType = Change.Update, Position = i });
771    //        }
772    //        int removeAt = -1;
773    //        foreach (KeyValuePair<int, ClientInfo> kvp in helpClients) {
774    //          if (cio.Id.Equals(kvp.Value.Id)) {
775    //            removeAt = kvp.Key;
776    //            break;
777    //          }
778    //        }
779    //        if (removeAt >= 0) {
780    //          helpClients.Remove(removeAt);
781    //        }
782    //        break;
783    //      }
784    //    }
785    //    if (found == false) {
786    //      changes.Add(new Changes { Types = Type.Client, ID = ci.Id, ChangeType = Change.Create });
787    //    }
788    //    found = false;
789    //  }
790    //  foreach (KeyValuePair<int, ClientInfo> kvp in helpClients) {
791    //    changes.Add(new Changes { Types = Type.Client, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
792    //  }
793
794    //}
795
796    private void DetermineDelta() {
797     
798
799
800    }
801
802
803    //private void GetDelta(IList<ClientGroup> oldClient, IDictionary<Guid, ClientGroup> helpClients) {
804
805    //  bool found = false;
806    //  for (int i = 0; i < clients.List.Count; i++) {
807    //    ClientGroup cg = clientGroups.List[i];
808    //    for (int j = 0; j < oldClient.Count; i++) {
809    //      ClientGroup cgo = oldClient[j];
810    //      if (cg.Id.Equals(cgo.Id)) {
811    //        found = true;
812    //        foreach (Resource resource in cg.Resources) {
813    //          foreach (Resource resourceold in cgo.Resources) {
814    //            if (resource.Id.Equals(resourceold.Id)) {
815    //              if (resourceold.Name != resource.Name) {
816    //                changes.Add(new Changes { Types = Type.Client, ID = cg.Id, ChangeType = Change.Update, Position = i });
817    //              }
818    //            }
819    //          }
820    //        }
821    //        for (int k = 0; k < helpClients.Count; k++) {
822    //          if (cgo.Id.Equals(helpClients[k].Id)) {
823    //            helpClients.Remove(k);
824    //            break;
825    //          }
826    //        }
827    //        break;
828    //      }
829    //    }
830    //    if (found == false) {
831    //      changes.Add(new Changes { Types = Type.ClientGroup, ID = cg.Id, ChangeType = Change.Create });
832    //    }
833    //    found = false;
834    //  }
835    //  foreach (KeyValuePair<int, ClientGroup> kvp in helpClients) {
836    //    changes.Add(new Changes { Types = Type.ClientGroup, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
837    //  }
838    //}
839
840    private void GetDelta(IList<Job> oldJobs, IDictionary<int, Job> helpJobs) {
841      bool found = false;
842      for (int i = 0; i < jobs.List.Count; i++) {
843        Job job = jobs.List[i];
844        for (int j = 0; j < oldJobs.Count; j++) {
845
846          Job jobold = oldJobs[j];
847
848          if (job.Id.Equals(jobold.Id)) {
849
850            found = true;
851            bool change = false;
852            if (job.State != jobold.State) {
853              change = true;
854            }
855            if (job.State != State.offline) {
856              if ((!IsEqual(job.Client, jobold.Client)) || (job.State != jobold.State)
857                   || (job.Percentage != jobold.Percentage)) {
858                change = true;
859              }
860            } else if (job.DateCalculated != jobold.DateCalculated) {
861              change = true;
862            }
863            if (change) {
864              changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Update, Position = i });
865            }
866
867            int removeAt = -1;
868            foreach (KeyValuePair<int, Job> kvp in helpJobs) {
869              if (job.Id.Equals(kvp.Value.Id)) {
870                removeAt = kvp.Key;
871                break;
872              }
873            }
874            if (removeAt >= 0) {
875              helpJobs.Remove(removeAt);
876            }
877            break;
878          }
879
880        }
881        if (found == false) {
882          changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Create });
883          System.Diagnostics.Debug.WriteLine("new Job: " + job.Id);
884        }
885        found = false;
886      }
887      foreach (KeyValuePair<int, Job> kvp in helpJobs) {
888        changes.Add(new Changes { Types = Type.Job, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
889        System.Diagnostics.Debug.WriteLine("delete Job: " + kvp.Value.Id);
890      }
891    }
892
893    private void GetSnapshotList() {
894
895      lvSnapshots.Items.Clear();
896      IJobManager jobManager = ServiceLocator.GetJobManager();
897
898      ResponseObject<JobResult> jobRes = jobManager.GetLastJobResultOf(currentJob.Id, false);
899
900      // iterate threw all snapshots if method is implemented
901
902      ListViewItem curSnapshot = new ListViewItem(jobRes.Obj.Client.Name);
903      double percentage = jobRes.Obj.Percentage * 100;
904      curSnapshot.SubItems.Add(percentage.ToString() + " %");
905      curSnapshot.SubItems.Add(jobRes.Obj.Timestamp.ToString());
906      lvSnapshots.Items.Add(curSnapshot);
907    }
908
909    #endregion
910
911    private void largeIconsToolStripMenuItem_Click(object sender, EventArgs e) {
912      lvClientControl.View = View.LargeIcon;
913      lvJobControl.View = View.LargeIcon;
914      largeIconsToolStripMenuItem.CheckState = CheckState.Checked;
915      smallIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
916      listToolStripMenuItem.CheckState = CheckState.Unchecked;
917    }
918
919    private void smallIconsToolStripMenuItem_Click(object sender, EventArgs e) {
920      lvClientControl.View = View.SmallIcon;
921      lvJobControl.View = View.SmallIcon;
922      largeIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
923      smallIconsToolStripMenuItem.CheckState = CheckState.Checked;
924      listToolStripMenuItem.CheckState = CheckState.Unchecked;
925    }
926
927    private void listToolStripMenuItem_Click(object sender, EventArgs e) {
928      lvClientControl.View = View.List;
929      lvJobControl.View = View.List;
930      largeIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
931      smallIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
932      listToolStripMenuItem.CheckState = CheckState.Checked;
933    }
934
935    private void tvClientControl_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
936      lvClientControl.Items.Clear();
937      lvClientControl.Groups.Clear();
938      currentNode = e.Node;
939      AddGroupsToListView(e.Node);
940    }
941
942    private void AddGroupsToListView(TreeNode node) {
943      if (node != null) {
944        ListViewGroup lvg = new ListViewGroup(node.Text, HorizontalAlignment.Left);
945        lvClientControl.Groups.Add(lvg);
946        foreach (ListViewItem item in clientList[((ClientGroup)node.Tag).Id]) {
947          item.Group = lvg;
948          lvClientControl.Items.Add(item);
949        }
950
951        if (node.Nodes != null) {
952          foreach (TreeNode curNode in node.Nodes) {
953            AddGroupsToListView(curNode);
954          }
955        }
956      }
957    }
958
959    private void Refresh_Click(object sender, EventArgs e) {
960      Form overlayingForm = new Form();
961      //Label loadingLabel = new Label();
962      //loadingLabel.Text = "Loading";
963      //loadingLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
964      //loadingLabel.Location = new Point(this.Width / 2, this.Height / 2);
965      //overlayingForm.Controls.Add(loadingLabel);
966
967
968      overlayingForm.Show();
969      overlayingForm.FormBorderStyle = FormBorderStyle.None;
970      overlayingForm.BackColor = Color.Gray;
971      overlayingForm.Opacity = 0.4;
972      overlayingForm.Size = this.Size;
973      //overlayingForm.PointToClient(new Point(this.Left, this.Top));
974      overlayingForm.Location = this.Location;
975      Console.WriteLine(this.Left + ", " + this.Top);
976      //overlayingForm.Left = this.Left;
977      //overlayingForm.Top = this.Top;
978
979     
980     
981
982
983      AddClients();
984
985      overlayingForm.Close();
986    }
987  }
988}
Note: See TracBrowser for help on using the repository browser.