Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClientUserManagement/HeuristicLab.Clients.Access/3.3/ClientInformationUtils.cs @ 7536

Last change on this file since 7536 was 7536, checked in by ascheibe, 12 years ago

#1648 added client registration ui

File size: 7.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Diagnostics;
24using System.Management;
25using System.Reflection;
26using HeuristicLab.Algorithms.Benchmarks;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29
30namespace HeuristicLab.Clients.Access {
31  public static class ClientInformationUtils {
32
33    public static Client CollectClientInformation() {
34      Client client = new Client();
35      OperatingSystem os = new OperatingSystem();
36      ClientType cType = new ClientType();
37      cType.Name = "HLClient";
38
39      client.Id = GetUniqueMachineId();
40      client.HeuristicLabVersion = GetHLVersion();
41      client.Name = GetMachineName();
42      client.MemorySize = GetPhysicalMemory().GetValueOrDefault();
43      client.NumberOfCores = GetNumberOfCores();
44      os.Name = GetOperatingSystem();
45      client.OperatingSystem = os;
46      client.ProcessorType = GetCpuInfo();
47      client.ClientType = cType;
48      //client.ClientConfiguration = GetClientConfiguration();
49      client.Timestamp = DateTime.Now;
50      client.PerformanceValue = RunBenchmark();
51
52      return client;
53    }
54
55    public static string GetClientConfiguration() {
56      //TODO: does it make sense to send the client configuration to the server? for what do we need this?
57      return string.Empty;
58    }
59
60    public static string GetHLVersion() {
61      FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
62      return versionInfo.FileVersion;
63    }
64
65    public static int GetNumberOfCores() {
66      return Environment.ProcessorCount;
67    }
68
69    public static string GetOperatingSystem() {
70      return Environment.OSVersion.VersionString;
71    }
72
73    public static string GetMachineName() {
74      return Environment.MachineName;
75    }
76
77    public static Guid GetUniqueMachineId() {
78      Guid id;
79      try {
80        id = GetUniqueMachineIdFromMac();
81      }
82      catch {
83        // fallback if something goes wrong...       
84        id = new Guid(Environment.MachineName.GetHashCode(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
85      }
86      return id;
87    }
88
89    /// <summary>
90    /// returns total physical memory of the machine in MB
91    /// </summary>
92    public static int? GetPhysicalMemory() {
93      long? res = GetWMIValue("Win32_ComputerSystem", "TotalPhysicalMemory");
94      if (res != null)
95        return (int)(res / 1024 / 1024);
96      else
97        return null;
98    }
99
100    /// <summary>
101    /// returns CPU frequence of the machine in Mhz
102    /// </summary>
103    public static string GetCpuInfo() {
104      string name = GetWMIString("Win32_Processor", "Name");
105      string manufacturer = GetWMIString("Win32_Processor", "Manufacturer");
106      return manufacturer + " " + name;
107    }
108
109    /// <summary>
110    /// Generate a guid based on mac address of the first found nic (yes, mac addresses are not unique...)
111    /// and the machine name.
112    /// Format:
113    ///
114    ///  D1      D2  D3  Res.   D4
115    /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116    /// |n a m e|0 0|0 0|0 0 mac address|
117    /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118    ///
119    /// The mac address is saved in the last 48 bits of the Data 4 segment
120    /// of the guid (first 2 bytes of Data 4 are reserved).
121    /// D1 contains the hash of the machinename.
122    /// </summary>   
123    private static Guid GetUniqueMachineIdFromMac() {
124      ManagementClass mgtClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
125      ManagementObjectCollection mgtCol = mgtClass.GetInstances();
126
127      foreach (ManagementObject mgtObj in mgtCol) {
128        foreach (var prop in mgtObj.Properties) {
129          if (prop.Value != null && prop.Name == "MACAddress") {
130            try {
131              //simply take the first nic
132              string mac = prop.Value.ToString();
133              byte[] b = new byte[8];
134              string[] macParts = mac.Split(':');
135              if (macParts.Length == 6) {
136                for (int i = 0; i < macParts.Length; i++) {
137                  b[i + 2] = (byte)((ParseNybble(macParts[i][0]) << 4) | ParseNybble(macParts[i][1]));
138                }
139
140                // also get machine name and save it to the first 4 bytes               
141                Guid guid = new Guid(Environment.MachineName.GetHashCode(), 0, 0, b);
142                return guid;
143              } else
144                throw new Exception("Error getting mac addresse");
145            }
146            catch {
147              throw new Exception("Error getting mac addresse");
148            }
149          }
150        }
151      }
152      throw new Exception("Error getting mac addresse");
153    }
154
155    /// <summary>
156    /// return numeric value of a single hex-char
157    /// (see: http://stackoverflow.com/questions/854012/how-to-convert-hex-to-a-byte-array)
158    /// </summary>   
159    private static int ParseNybble(char c) {
160      if (c >= '0' && c <= '9') {
161        return c - '0';
162      }
163      if (c >= 'A' && c <= 'F') {
164        return c - 'A' + 10;
165      }
166      if (c >= 'a' && c <= 'f') {
167        return c - 'a' + 10;
168      }
169      throw new ArgumentException("Invalid hex digit: " + c);
170    }
171
172    private static long? GetWMIValue(string clazz, string property) {
173      ManagementClass mgtClass = new ManagementClass(clazz);
174      ManagementObjectCollection mgtCol = mgtClass.GetInstances();
175
176      foreach (ManagementObject mgtObj in mgtCol) {
177        foreach (var prop in mgtObj.Properties) {
178          if (prop.Value != null && prop.Name == property) {
179            try {
180              return long.Parse(prop.Value.ToString());
181            }
182            catch {
183              return null;
184            }
185          }
186        }
187      }
188      return null;
189    }
190
191    private static string GetWMIString(string clazz, string property) {
192      ManagementClass mgtClass = new ManagementClass(clazz);
193      ManagementObjectCollection mgtCol = mgtClass.GetInstances();
194
195      foreach (ManagementObject mgtObj in mgtCol) {
196        foreach (var prop in mgtObj.Properties) {
197          if (prop.Value != null && prop.Name == property) {
198            try {
199              return prop.Value.ToString();
200            }
201            catch {
202              return string.Empty;
203            }
204          }
205        }
206      }
207      return string.Empty;
208    }
209
210    public static double RunBenchmark() {
211      Linpack linpack = new Linpack();
212      ResultCollection results = new ResultCollection();
213      linpack.Run(new System.Threading.CancellationToken(), results);
214      DoubleValue mflops = (DoubleValue)results["Mflops/s"].Value;
215      return mflops.Value;
216    }
217  }
218}
Note: See TracBrowser for help on using the repository browser.