Free cookie consent management tool by TermsFeed Policy Generator

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

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

refactored AddClients with recursive calls of groups (#600)

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