Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.Core/3.3/ConfigurationManager/ConfigManager.cs @ 4253

Last change on this file since 4253 was 4253, checked in by cneumuel, 14 years ago

Rename "Client" to "Slave" #1157

File size: 5.2 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.Linq;
25using System.Text;
26using HeuristicLab.Hive.Contracts.BusinessObjects;
27using HeuristicLab.Hive.Slave.ExecutionEngine;
28using HeuristicLab.Hive.Slave.Core.ClientConsoleService;
29using HeuristicLab.Hive.Slave.Communication;
30using HeuristicLab.Hive.Slave.Core.Properties;
31using HeuristicLab.Hive.Contracts;
32
33namespace HeuristicLab.Hive.Slave.Core.ConfigurationManager {
34  /// <summary>
35  /// accesses the Server and sends his data (uuid, uptimes, hardware config)
36  /// </summary>
37  public class ConfigManager {
38    private static ConfigManager instance = null;
39    public static ConfigManager Instance {
40      get {
41        if (instance == null) {
42          instance = new ConfigManager();
43        }
44        return instance;
45      }
46    }
47
48    public Core Core { get; set; }   
49    private ClientDto hardwareInfo;       
50
51    /// <summary>
52    /// Constructor for the singleton, must recover Guid, Calendar, ...
53    /// </summary>
54    private ConfigManager() {     
55      hardwareInfo = new ClientDto();
56
57      if (Settings.Default.Guid == Guid.Empty) {
58        hardwareInfo.Id = Guid.NewGuid();
59        Settings.Default.Guid = hardwareInfo.Id;
60        Settings.Default.Save();
61      } else
62        hardwareInfo.Id = Settings.Default.Guid;
63     
64      hardwareInfo.NrOfCores = Environment.ProcessorCount;
65      hardwareInfo.Memory = 1024;
66      hardwareInfo.Name = Environment.MachineName;
67    }
68
69    /// <summary>
70    /// Get all the Information about the client
71    /// </summary>
72    /// <returns>the ClientInfo object</returns>
73    public ClientDto GetClientInfo() {
74      hardwareInfo.Login = WcfService.Instance.ConnectedSince;
75      return hardwareInfo;         
76    }
77
78    /// <summary>
79    /// Returns the connectioncontainer with the IP and Port, storred in the Settings framework
80    /// </summary>
81    /// <returns>The IP and Port of the server</returns>
82    public ConnectionContainer GetServerIPAndPort() {
83      ConnectionContainer cc = new ConnectionContainer();
84      cc.IPAdress = Settings.Default.ServerIP;
85      cc.Port = Settings.Default.ServerPort;
86      return cc;
87    }
88
89    /// <summary>
90    /// Sets and saves IP and Port in the Settings framework
91    /// </summary>
92    /// <param name="cc"></param>
93    public void SetServerIPAndPort(ConnectionContainer cc) {
94      Settings.Default.ServerIP = cc.IPAdress;
95      Settings.Default.ServerPort = cc.Port;
96      Settings.Default.Save();
97    }
98    /// <summary>
99    /// collects and returns information that get displayed by the Client Console
100    /// </summary>
101    /// <returns></returns>
102    public StatusCommons GetStatusForClientConsole() {
103      //Todo: Locking
104      StatusCommons st = new StatusCommons();
105      st.ClientGuid = hardwareInfo.Id;
106     
107      st.Status = WcfService.Instance.ConnState;
108      st.ConnectedSince = WcfService.Instance.ConnectedSince;
109
110      st.TotalCores = hardwareInfo.NrOfCores;
111      st.FreeCores = hardwareInfo.NrOfCores - GetUsedCores();
112
113      st.JobsAborted = SlaveStatusInfo.JobsAborted;
114      st.JobsDone = SlaveStatusInfo.JobsProcessed;
115      st.JobsFetched = SlaveStatusInfo.JobsFetched;     
116
117      Dictionary<Guid, Executor> engines = Core.GetExecutionEngines();
118      st.Jobs = new List<JobStatus>();
119
120      lock (engines) {
121        foreach (KeyValuePair<Guid, Executor> kvp in engines) {
122          Executor e = kvp.Value;
123          st.Jobs.Add(new JobStatus { JobId = e.JobId, Progress = e.Progress, Since = e.CreationTime });
124        }
125      }
126      return st;     
127    }
128
129    public Dictionary<Guid, double> GetProgressOfAllJobs() {
130      Dictionary<Guid, double> prog = new Dictionary<Guid, double>();
131      Dictionary<Guid, Executor> engines = Core.GetExecutionEngines();
132      lock (engines) {
133        foreach (KeyValuePair<Guid, Executor> kvp in engines) {
134          Executor e = kvp.Value;
135          //if (!e.JobIsFinished)
136          prog[e.JobId] = e.Progress;
137        }
138      }
139      return prog;
140    }
141
142    public int GetUsedCores() {
143      Dictionary<Guid, Executor> engines = Core.GetExecutionEngines();
144      Dictionary<Guid, JobDto> jobs = Core.GetJobs();
145      int usedCores = 0;
146      lock (engines) {       
147        foreach (KeyValuePair<Guid, JobDto> kvp in jobs)
148          usedCores += kvp.Value.CoresNeeded;
149      }
150      return usedCores;
151    }
152
153  }
154}
Note: See TracBrowser for help on using the repository browser.