Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/23/09 17:55:47 (15 years ago)
Author:
aleitner
Message:

refactored AddClients with recursive calls of groups (#600)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Hive.Server.Console/3.2/HiveServerManagementConsole.cs

    r1628 r1645  
    3434namespace HeuristicLab.Hive.Server.ServerConsole {
    3535
     36  /// <summary>
     37  /// if form is closed the loginform gets an information
     38  /// </summary>
     39  /// <param name="cf"></param>
     40  /// <param name="error"></param>
    3641  public delegate void closeForm(bool cf, bool error);
    3742
     
    4550    private ResponseList<Job> jobs = null;
    4651
     52    //TODO delete
    4753    private Dictionary<Guid, ListViewGroup> clientObjects;
    4854    private Dictionary<Guid, ListViewItem> clientInfoObjects;
     
    5157    private Job currentJob = null;
    5258    private ClientInfo currentClient = null;
     59
     60    //TODO delete
    5361    private string nameCurrentJob = "";
    5462    private string nameCurrentClient = "";
     
    5967
    6068    private ToolTip tt = new ToolTip();
     69
     70    private IClientManager ClientManager {
     71      get {
     72        return ServiceLocator.GetClientManager();
     73      }
     74
     75    }
    6176    #endregion
    6277
     
    89104
    90105    private Guid ConvertStringToGuid(string stringGuid) {
    91       Guid guid = Guid.Empty;
    92       return (Guid)TypeDescriptor.GetConverter(guid).ConvertFrom(stringGuid);
     106      return new Guid(stringGuid);
    93107    }
    94108
     
    100114        IJobManager jobManager = ServiceLocator.GetJobManager();
    101115        if (lvJobControl.SelectedItems.Count == 1) {
    102           jobManager.AbortJob(ConvertStringToGuid(lvJobControl.SelectedItems[0].Text));
     116          jobManager.AbortJob(((Job)(lvJobControl.SelectedItems[0].Tag)).Id);
    103117        }
    104118      };
    105119
     120      //adding context menu items for jobs
    106121      menuItemGetSnapshot.Click += (s, e) => {
    107122        IJobManager jobManager = ServiceLocator.GetJobManager();
    108123        if (lvJobControl.SelectedItems.Count == 1) {
    109           jobManager.RequestSnapshot(ConvertStringToGuid(lvJobControl.SelectedItems[0].Text));
     124          jobManager.RequestSnapshot(((Job)(lvJobControl.SelectedItems[0].Tag)).Id);
    110125        }
    111126      };
     
    119134      ListViewHitTestInfo hitTestInfo = lvJobControl.HitTest(e.Location);
    120135      if (e.Button == MouseButtons.Right && hitTestInfo.Item != null && lvJobControl.SelectedItems.Count == 1) {
    121         Guid selectedJobGuid = ConvertStringToGuid(lvJobControl.SelectedItems[0].Text);
    122         Job selectedJob = jobs.List.FirstOrDefault(x => x.Id == selectedJobGuid);
     136        Job selectedJob = (Job)lvJobControl.SelectedItems[0].Tag;
    123137
    124138        if (selectedJob != null && selectedJob.State == State.calculating) {
    125         lvJobControl.ContextMenuStrip.Items.Add(menuItemAbortJob);
    126         lvJobControl.ContextMenuStrip.Items.Add(menuItemGetSnapshot);
    127         }
    128       }
    129        lvJobControl.ContextMenuStrip.Show(new Point(e.X, e.Y));
    130     }
    131 
    132     /// <summary>
    133     /// Adds clients to ListView and TreeView
    134     /// </summary>
     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
    135236    private void AddClients() {
    136       try {
    137         clientObjects = new Dictionary<Guid, ListViewGroup>();
    138         clientInfoObjects = new Dictionary<Guid, ListViewItem>();
    139         IClientManager clientManager =
    140           ServiceLocator.GetClientManager();
    141 
    142         clients = clientManager.GetAllClientGroups();
    143         lvClientControl.Items.Clear();
    144         List<Guid> inGroup = new List<Guid>();
     237        clients = ClientManager.GetAllClientGroups();
     238
    145239        foreach (ClientGroup cg in clients.List) {
    146           ListViewGroup lvg = new ListViewGroup(cg.Name, HorizontalAlignment.Left);
    147           foreach (ClientInfo ci in cg.Resources) {
    148             ListViewItem item = null;
    149             if ((ci.State == State.offline) || (ci.State == State.nullState)) {
    150               item = new ListViewItem(ci.Name, 3, lvg);
    151             } else {
    152               int percentageUsage = CapacityRam(ci.NrOfCores, ci.NrOfFreeCores);
    153               int usage = 0;
    154               if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
    155                 usage = 0;
    156               } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
    157                 usage = 1;
    158               } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
    159                 usage = 2;
    160               }
    161 
    162               item = new ListViewItem(ci.Name, usage, lvg);
    163             }
    164             item.Tag = ci.Id;
    165             lvClientControl.Items.Add(item);
    166             clientInfoObjects.Add(ci.Id, item);
    167             inGroup.Add(ci.Id);
    168 
    169           }
    170           lvClientControl.BeginUpdate();
    171           lvClientControl.Groups.Add(lvg);
    172           lvClientControl.EndUpdate();
    173           clientObjects.Add(cg.Id, lvg);
    174         } // Groups
    175 
    176         clientInfo = clientManager.GetAllClients();
    177         ListViewGroup lvunsorted = new ListViewGroup("no group", HorizontalAlignment.Left);
    178         foreach (ClientInfo ci in clientInfo.List) {
    179           bool help = false;
    180           foreach (Guid client in inGroup) {
    181             if (client == ci.Id) {
    182               help = true;
    183               break;
    184             }
    185           }
    186           if (!help) {
    187             ListViewItem item = null;
    188             if ((ci.State == State.offline) || (ci.State == State.nullState)) {
    189               item = new ListViewItem(ci.Name, 3, lvunsorted);
    190             } else {
    191               int percentageUsage = CapacityRam(ci.NrOfCores, ci.NrOfFreeCores);
    192               int usage = 0;
    193               if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
    194                 usage = 0;
    195               } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
    196                 usage = 1;
    197               } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
    198                 usage = 2;
    199               }
    200               item = new ListViewItem(ci.Name, usage, lvunsorted);
    201             }
    202             item.Tag = ci.Id;
    203             lvClientControl.Items.Add(item);
    204           }
    205         }
    206         lvClientControl.BeginUpdate();
    207         lvClientControl.Groups.Add(lvunsorted);
    208         lvClientControl.EndUpdate();
    209         if (flagClient) {
    210           ClientClicked();
    211         }
    212       }
    213       catch (Exception ex) {
    214         closeFormEvent(true, true);
    215         this.Close();
    216       }
     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;
    217277    }
    218278
     
    243303          if (job.State == State.calculating) {
    244304            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobCalculating);
     305            lvi.Tag = job;
    245306            jobObjects.Add(job.Id, lvi);
    246307
     
    250311          } else if (job.State == State.finished) {
    251312            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobFinished);
     313            lvi.Tag = job;
    252314            jobObjects.Add(job.Id, lvi);
    253315            //lvJobControl.Items.Add(lvi);
    254316          } else if (job.State == State.offline) {
    255317            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobPending);
     318            lvi.Tag = job;
    256319            jobObjects.Add(job.Id, lvi);
    257320            //lvJobControl.Items.Add(lvi);
     
    298361        }
    299362      }
    300       pbClientControl.Image = ilLargeImgJob.Images[usage];
     363      pbClientControl.Image = ilLargeImgClient.Images[usage];
    301364      lblClientName.Text = currentClient.Name;
    302365      lblLogin.Text = currentClient.Login.ToString();
     
    316379      lvSnapshots.Enabled = true;
    317380      currentJob = jobs.List[i];
    318       pbJobControl.Image = ilLargeImgClient.Images[0];
     381      pbJobControl.Image = ilLargeImgJob.Images[0];
    319382      lblJobName.Text = currentJob.Id.ToString();
    320383      progressJob.Value = (int)(currentJob.Percentage * 100);
Note: See TracChangeset for help on using the changeset viewer.