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
RevLine 
[794]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;
[831]30using HeuristicLab.Hive.Contracts.Interfaces;
31using HeuristicLab.Hive.Contracts.BusinessObjects;
[904]32using HeuristicLab.Hive.Contracts;
[794]33
[1089]34namespace HeuristicLab.Hive.Server.ServerConsole {
[794]35
[1645]36  /// <summary>
37  /// if form is closed the loginform gets an information
38  /// </summary>
39  /// <param name="cf"></param>
40  /// <param name="error"></param>
[1030]41  public delegate void closeForm(bool cf, bool error);
[794]42
43  public partial class HiveServerManagementConsole : Form {
44
45    public event closeForm closeFormEvent;
46
[1136]47    #region private variables
48    private ResponseList<ClientGroup> clients = null;
49    private ResponseList<ClientInfo> clientInfo = null;
50    private ResponseList<Job> jobs = null;
[844]51
[1645]52    //TODO delete
[1449]53    private Dictionary<Guid, ListViewGroup> clientObjects;
54    private Dictionary<Guid, ListViewItem> clientInfoObjects;
55    private Dictionary<Guid, ListViewItem> jobObjects;
[1261]56
[1136]57    private Job currentJob = null;
58    private ClientInfo currentClient = null;
[1645]59
60    //TODO delete
[1148]61    private string nameCurrentJob = "";
62    private string nameCurrentClient = "";
[1136]63    private bool flagJob = false;
64    private bool flagClient = false;
[1098]65
[1483]66    private List<Changes> changes = new List<Changes>();
67
[1136]68    private ToolTip tt = new ToolTip();
[1645]69
70    private IClientManager ClientManager {
71      get {
72        return ServiceLocator.GetClientManager();
73      }
74
75    }
[1136]76    #endregion
77
[794]78    public HiveServerManagementConsole() {
79      InitializeComponent();
[1598]80      Init();
[1037]81      AddClients();
82      AddJobs();
[1018]83      timerSyncronize.Start();
[844]84    }
[831]85
[1590]86
87    #region Backgroundworker
[1136]88    /// <summary>
89    /// event on Ticker
90    /// </summary>
91    /// <param name="obj"></param>
92    /// <param name="e"></param>
[1267]93    private void TickSync(object obj, EventArgs e) {
[1483]94      if (!updaterWoker.IsBusy) {
95        updaterWoker.RunWorkerAsync();
96      }
[1018]97    }
98
[1581]99    private void updaterWoker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
100      Refresh();
101    }
[1512]102
[1590]103    #endregion
[1512]104
[1628]105    private Guid ConvertStringToGuid(string stringGuid) {
[1645]106      return new Guid(stringGuid);
[1628]107    }
108
109
[1598]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) {
[1645]116          jobManager.AbortJob(((Job)(lvJobControl.SelectedItems[0].Tag)).Id);
[1598]117        }
118      };
119
[1645]120      //adding context menu items for jobs
[1598]121      menuItemGetSnapshot.Click += (s, e) => {
122        IJobManager jobManager = ServiceLocator.GetJobManager();
123        if (lvJobControl.SelectedItems.Count == 1) {
[1645]124          jobManager.RequestSnapshot(((Job)(lvJobControl.SelectedItems[0].Tag)).Id);
[1598]125        }
126      };
127
128    }
129
130    private void lvJobControl_MouseUp(object sender, MouseEventArgs e) {
131      // If the right mouse button was clicked and released,
[1628]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) {
[1645]136        Job selectedJob = (Job)lvJobControl.SelectedItems[0].Tag;
[1628]137
138        if (selectedJob != null && selectedJob.State == State.calculating) {
[1645]139          lvJobControl.ContextMenuStrip.Items.Add(menuItemAbortJob);
140          lvJobControl.ContextMenuStrip.Items.Add(menuItemGetSnapshot);
[1628]141        }
[1598]142      }
[1645]143      lvJobControl.ContextMenuStrip.Show(new Point(e.X, e.Y));
[1598]144    }
145
[1645]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
[1037]236    private void AddClients() {
[1645]237        clients = ClientManager.GetAllClientGroups();
[831]238
[1030]239        foreach (ClientGroup cg in clients.List) {
[1645]240          AddClientOrGroup(cg, null);
241        }
242    }
[1512]243
[1645]244    private void AddClientOrGroup(ClientGroup clientGroup, TreeNode currentNode) {
245      currentNode = CreateTreeNode(clientGroup, currentNode);
246      ListViewGroup lvg = new ListViewGroup(clientGroup.Name, HorizontalAlignment.Left);
[1495]247
[1645]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);
[831]256        }
[1018]257      }
[1645]258      if (lvg.Items.Count > 0) {
259        lvClientControl.Groups.Add(lvg);
[1030]260      }
[1018]261    }
[956]262
[1645]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    }
[1483]278
[1645]279
[1483]280    List<ListViewGroup> jobGroup;
[1136]281    /// <summary>
282    /// Adds jobs to ListView and TreeView
283    /// </summary>
[1037]284    private void AddJobs() {
[1030]285      try {
[1449]286        jobObjects = new Dictionary<Guid, ListViewItem>();
[1030]287        IJobManager jobManager =
288          ServiceLocator.GetJobManager();
289        jobs = jobManager.GetAllJobs();
[1018]290
[1030]291        lvJobControl.Items.Clear();
[1018]292
[1037]293        ListViewGroup lvJobCalculating = new ListViewGroup("calculating", HorizontalAlignment.Left);
[1030]294        ListViewGroup lvJobFinished = new ListViewGroup("finished", HorizontalAlignment.Left);
[1037]295        ListViewGroup lvJobPending = new ListViewGroup("pending", HorizontalAlignment.Left);
[1483]296
297        jobGroup = new List<ListViewGroup>();
298        jobGroup.Add(lvJobCalculating);
299        jobGroup.Add(lvJobFinished);
300        jobGroup.Add(lvJobPending);
[1495]301
[1030]302        foreach (Job job in jobs.List) {
[1037]303          if (job.State == State.calculating) {
[1126]304            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobCalculating);
[1645]305            lvi.Tag = job;
[1261]306            jobObjects.Add(job.Id, lvi);
[1483]307
308            //lvJobControl.Items.Add(lvi);
[1495]309
[1126]310            lvi.ToolTipText = (job.Percentage * 100) + "% of job calculated";
[1037]311          } else if (job.State == State.finished) {
[1126]312            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobFinished);
[1645]313            lvi.Tag = job;
[1261]314            jobObjects.Add(job.Id, lvi);
[1483]315            //lvJobControl.Items.Add(lvi);
[1030]316          } else if (job.State == State.offline) {
[1126]317            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobPending);
[1645]318            lvi.Tag = job;
[1261]319            jobObjects.Add(job.Id, lvi);
[1483]320            //lvJobControl.Items.Add(lvi);
[1030]321          }
322        } // Jobs
[1483]323        lvJobControl.BeginUpdate();
324        foreach (ListViewItem lvi in jobObjects.Values) {
325          lvJobControl.Items.Add(lvi);
326        }
[1037]327        lvJobControl.Groups.Add(lvJobCalculating);
[1030]328        lvJobControl.Groups.Add(lvJobFinished);
[1037]329        lvJobControl.Groups.Add(lvJobPending);
[1483]330        lvJobControl.EndUpdate();
[1136]331        if (flagJob) {
332          JobClicked();
333        }
[1030]334      }
335      catch (Exception ex) {
336        closeFormEvent(true, true);
337        this.Close();
338      }
[1018]339    }
340
[1136]341    /// <summary>
[1581]342    /// if one client is clicked, the details for the clicked client are shown
343    /// in the second panel
[1136]344    /// </summary>
345    private void ClientClicked() {
[1512]346      plClientDetails.Visible = true;
[1148]347      int i = 0;
[1495]348      while (clientInfo.List[i].Id.ToString() != nameCurrentClient) {
[1148]349        i++;
350      }
351      currentClient = clientInfo.List[i];
[1512]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      }
[1645]363      pbClientControl.Image = ilLargeImgClient.Images[usage];
[1136]364      lblClientName.Text = currentClient.Name;
365      lblLogin.Text = currentClient.Login.ToString();
[1164]366      lblState.Text = currentClient.State.ToString();
[1136]367    }
368
369    /// <summary>
[1581]370    /// if one job is clicked, the details for the clicked job are shown
371    /// in the second panel
[1136]372    /// </summary>
373    private void JobClicked() {
[1512]374      plJobDetails.Visible = true;
[1148]375      int i = 0;
376      while (jobs.List[i].Id.ToString() != nameCurrentJob) {
377        i++;
378      }
[1512]379      lvSnapshots.Enabled = true;
[1148]380      currentJob = jobs.List[i];
[1645]381      pbJobControl.Image = ilLargeImgJob.Images[0];
[1136]382      lblJobName.Text = currentJob.Id.ToString();
383      progressJob.Value = (int)(currentJob.Percentage * 100);
384      lblProgress.Text = (int)(currentJob.Percentage * 100) + "% calculated";
[1495]385      lblUserCreatedJob.Text = currentJob.UserId.ToString() + /* currentJob.User.Name + */ " created Job";
[1173]386      lblJobCreated.Text = "Created at " + currentJob.DateCreated;
[1136]387      if (currentJob.ParentJob != null) {
388        lblParentJob.Text = currentJob.ParentJob.Id + " is parent job";
[1495]389      } else {
390        lblParentJob.Text = "";
[1136]391      }
[1173]392      lblPriorityJob.Text = "Priority of job is " + currentJob.Priority;
[1136]393      if (currentJob.Client != null) {
394        lblClientCalculating.Text = currentJob.Client.Name + " calculated Job";
[1495]395        lblJobCalculationBegin.Text = "Startet calculation at " + currentJob.DateCalculated;
396
[1173]397        if (currentJob.State == State.finished) {
398          IJobManager jobManager =
399            ServiceLocator.GetJobManager();
[1509]400          ResponseObject<JobResult> jobRes = jobManager.GetLastJobResultOf(currentJob.Id, false);
[1173]401          lblJobCalculationEnd.Text = "Calculation ended at " + jobRes.Obj.DateFinished;
402        }
[1495]403      } else {
404        lblClientCalculating.Text = "";
405        lblJobCalculationBegin.Text = "";
406        lblJobCalculationEnd.Text = "";
407      }
[1173]408      if (currentJob.State != State.offline) {
[1590]409        lvSnapshots.Items.Clear();
[1628]410        if (currentJob.State == State.finished)
[1590]411          GetSnapshotList();
[1512]412        lvSnapshots.Visible = true;
[1495]413      } else {
[1512]414        lvSnapshots.Visible = false;
[1148]415      }
[1136]416    }
417
[1483]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) {
[1590]423          RefreshClient(change);
[1483]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()) {
[1495]434            if (nameCurrentJob == change.ID.ToString()) {
435              JobClicked();
436            }
[1483]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()) {
[1495]474            if (nameCurrentClient == change.ID.ToString()) {
475              ClientClicked();
476            }
[1483]477            State state = clientInfo.List[change.Position].State;
478            System.Diagnostics.Debug.WriteLine(lvClientControl.Items[i].Text.ToString());
[1595]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);
[1483]489            if ((state == State.offline) || (state == State.nullState)) {
490              lvClientControl.Items[i].ImageIndex = 3;
491            } else {
[1512]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
[1483]500            }
501            lvClientControl.Refresh();
502          }
503        }
504
505
506      } else if (change.ChangeType == Change.Create) {
[1495]507
[1483]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
[1148]524    #region Eventhandlers
[1136]525    /// <summary>
[794]526    /// Send event to Login-GUI when closing
527    /// </summary>
528    /// <param name="sender"></param>
529    /// <param name="e"></param>
[1037]530    private void Close_Click(object sender, EventArgs e) {
[794]531      if (closeFormEvent != null) {
[1030]532        closeFormEvent(true, false);
[794]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) {
[1030]544        closeFormEvent(true, false);
[794]545      }
546    }
[956]547
[1136]548    private void AddJob_Click(object sender, EventArgs e) {
[1018]549      AddJobForm newForm = new AddJobForm();
[956]550      newForm.Show();
[1581]551      newForm.addJobEvent += new addDelegate(updaterWoker.RunWorkerAsync);
[956]552    }
553
[1098]554    private void OnLVClientClicked(object sender, EventArgs e) {
[1495]555      nameCurrentClient = lvClientControl.SelectedItems[0].Tag.ToString();
[1136]556      flagClient = true;
[1098]557      ClientClicked();
558    }
559
[1136]560    private void OnLVJobControlClicked(object sender, EventArgs e) {
[1148]561      nameCurrentJob = lvJobControl.SelectedItems[0].Text;
[1136]562      flagJob = true;
563      JobClicked();
[1098]564    }
565
[1126]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    }
[1261]572
573    private void updaterWoker_DoWork(object sender, DoWorkEventArgs e) {
[1483]574
[1495]575      changes.Clear();
[1261]576      IClientManager clientManager =
577          ServiceLocator.GetClientManager();
[1495]578
[1483]579      #region ClientInfo
580      ResponseList<ClientInfo> clientInfoOld = clientInfo;
[1261]581      clientInfo = clientManager.GetAllClients();
[1483]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;
[1495]592
[1483]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>();
[1495]634      for (int i = 0; i < oldList.List.Count; i++) {
[1483]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
[1590]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
[1483]661    private void GetDelta(IList<ClientInfo> oldClient, IDictionary<int, ClientInfo> helpClients) {
662      bool found = false;
663
[1495]664      for (int i = 0; i < clientInfo.List.Count; i++) {
[1483]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;
[1595]670            if ((ci.State != cio.State) || (ci.NrOfFreeCores != cio.NrOfFreeCores)) {
[1483]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          }
[1261]685        }
[1483]686        if (found == false) {
687          changes.Add(new Changes { Types = Type.Client, ID = ci.Id, ChangeType = Change.Create });
688        }
689        found = false;
[1261]690      }
[1483]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
[1261]695    }
[1483]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;
[1495]736      for (int i = 0; i < jobs.List.Count; i++) {
[1483]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) {
[1590]746              if ((!IsEqual(job.Client, jobold.Client)) || (job.State != jobold.State)
[1512]747                   || (job.Percentage != jobold.Percentage)) {
[1483]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
[1581]780    private void GetSnapshotList() {
[1483]781
[1581]782      lvSnapshots.Items.Clear();
[1590]783      IJobManager jobManager = ServiceLocator.GetJobManager();
784
[1581]785      ResponseObject<JobResult> jobRes = jobManager.GetLastJobResultOf(currentJob.Id, false);
[1590]786
787      // iterate threw all snapshots if method is implemented
788
[1581]789      ListViewItem curSnapshot = new ListViewItem(jobRes.Obj.Client.Name);
790      double percentage = jobRes.Obj.Percentage * 100;
791      curSnapshot.SubItems.Add(percentage.ToString() + " %");
[1626]792      curSnapshot.SubItems.Add(jobRes.Obj.Timestamp.ToString());
[1581]793      lvSnapshots.Items.Add(curSnapshot);
[1483]794    }
795
[1581]796    #endregion
[1483]797
[1628]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    }
[1598]805
[1628]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    }
[1598]813
[1628]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    }
[1598]821
[1628]822
823
824
[794]825  }
[844]826}
Note: See TracBrowser for help on using the repository browser.