Free cookie consent management tool by TermsFeed Policy Generator

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

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

refactored AddClients with recursive calls of groups, refresh on clients only manual (#600)

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