Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7534 was 7534, checked in by ascheibe, 13 years ago

#1648 worked on user and client information singletons

File size: 5.5 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.Management;
24using HeuristicLab.Algorithms.Benchmarks;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27
28namespace HeuristicLab.Clients.Access {
29  public static class ClientInformationUtils {
30
31    public static int GetNumberOfCores() {
32      return Environment.ProcessorCount;
33    }
34
35    public static string GetOperatingSystem() {
36      return Environment.OSVersion.VersionString;
37    }
38
39    public static string GetMachineName() {
40      return Environment.MachineName;
41    }
42
43    public static Guid GetUniqueMachineId() {
44      Guid id;
45      try {
46        id = GetUniqueMachineIdFromMac();
47      }
48      catch {
49        // fallback if something goes wrong...       
50        id = new Guid(Environment.MachineName.GetHashCode(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
51      }
52      return id;
53    }
54
55    /// <summary>
56    /// returns total physical memory of the machine in MB
57    /// </summary>
58    public static int? GetPhysicalMemory() {
59      long? res = GetWMIValue("Win32_ComputerSystem", "TotalPhysicalMemory");
60      if (res != null)
61        return (int)(res / 1024 / 1024);
62      else
63        return null;
64    }
65
66    /// <summary>
67    /// returns CPU frequence of the machine in Mhz
68    /// </summary>
69    public static int? GetCpuSpeed() {
70      return (int)GetWMIValue("Win32_Processor", "MaxClockSpeed");
71    }
72
73    /// <summary>
74    /// Generate a guid based on mac address of the first found nic (yes, mac addresses are not unique...)
75    /// and the machine name.
76    /// Format:
77    ///
78    ///  D1      D2  D3  Res.   D4
79    /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80    /// |n a m e|0 0|0 0|0 0 mac address|
81    /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
82    ///
83    /// The mac address is saved in the last 48 bits of the Data 4 segment
84    /// of the guid (first 2 bytes of Data 4 are reserved).
85    /// D1 contains the hash of the machinename.
86    /// </summary>   
87    private static Guid GetUniqueMachineIdFromMac() {
88      ManagementClass mgtClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
89      ManagementObjectCollection mgtCol = mgtClass.GetInstances();
90
91      foreach (ManagementObject mgtObj in mgtCol) {
92        foreach (var prop in mgtObj.Properties) {
93          if (prop.Value != null && prop.Name == "MACAddress") {
94            try {
95              //simply take the first nic
96              string mac = prop.Value.ToString();
97              byte[] b = new byte[8];
98              string[] macParts = mac.Split(':');
99              if (macParts.Length == 6) {
100                for (int i = 0; i < macParts.Length; i++) {
101                  b[i + 2] = (byte)((ParseNybble(macParts[i][0]) << 4) | ParseNybble(macParts[i][1]));
102                }
103
104                // also get machine name and save it to the first 4 bytes               
105                Guid guid = new Guid(Environment.MachineName.GetHashCode(), 0, 0, b);
106                return guid;
107              } else
108                throw new Exception("Error getting mac addresse");
109            }
110            catch {
111              throw new Exception("Error getting mac addresse");
112            }
113          }
114        }
115      }
116      throw new Exception("Error getting mac addresse");
117    }
118
119    /// <summary>
120    /// return numeric value of a single hex-char
121    /// (see: http://stackoverflow.com/questions/854012/how-to-convert-hex-to-a-byte-array)
122    /// </summary>   
123    private static int ParseNybble(char c) {
124      if (c >= '0' && c <= '9') {
125        return c - '0';
126      }
127      if (c >= 'A' && c <= 'F') {
128        return c - 'A' + 10;
129      }
130      if (c >= 'a' && c <= 'f') {
131        return c - 'a' + 10;
132      }
133      throw new ArgumentException("Invalid hex digit: " + c);
134    }
135
136    private static long? GetWMIValue(string clazz, string property) {
137      ManagementClass mgtClass = new ManagementClass(clazz);
138      ManagementObjectCollection mgtCol = mgtClass.GetInstances();
139
140      foreach (ManagementObject mgtObj in mgtCol) {
141        foreach (var prop in mgtObj.Properties) {
142          if (prop.Value != null && prop.Name == property) {
143            try {
144              return long.Parse(prop.Value.ToString());
145            }
146            catch {
147              return null;
148            }
149          }
150        }
151      }
152      return null;
153    }
154
155    public static double RunBenchmark() {
156      Linpack linpack = new Linpack();
157      ResultCollection results = new ResultCollection();
158      linpack.Run(new System.Threading.CancellationToken(), results);
159      DoubleValue mflops = (DoubleValue)results["Mflops/s"].Value;
160      return mflops.Value;
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.