Changeset 5621
- Timestamp:
- 03/07/11 16:15:55 (14 years ago)
- Location:
- branches/HeuristicLab.Hive-3.4/sources
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.SlaveWindowsService/SlaveWindowsService.cs
r5599 r5621 19 19 */ 20 20 #endregion 21 using System; 21 22 using System.ServiceProcess; 22 23 using System.Threading; 23 using HeuristicLab.Clients.Hive.SlaveCore;24 24 25 25 namespace HeuristicLab.Clients.Hive.SlaveCore.SlaveWindowsService { … … 30 30 public SlaveWindowsService() { 31 31 InitializeComponent(); 32 if (!System.Diagnostics.EventLog.SourceExists("HeuristicLab Hive Slave"))33 System.Diagnostics.EventLog.CreateEventSource("HeuristicLab Hive Slave",34 "HeuristicLab Hive Slave");35 32 36 eventLog.Source = "HeuristicLab Hive Slave"; 37 eventLog.Log = "HeuristicLab Hive Slave"; 33 try { 34 if (!System.Diagnostics.EventLog.SourceExists("HLHive")) { 35 System.Diagnostics.EventLog.CreateEventSource("HLHive", "Slave"); 36 } 37 eventLog.Source = "HLHive"; 38 eventLog.Log = "Slave"; 39 } 40 catch (Exception) { } 38 41 } 39 42 … … 42 45 coreThread = new Thread(core.Start); 43 46 coreThread.Start(); 44 eventLog.WriteEntry("HeuristicLab Hive Slave started!"); 47 48 try { 49 eventLog.WriteEntry("HeuristicLab Hive Slave started!"); 50 } 51 catch (Exception) { } 45 52 } 46 53 47 54 protected override void OnStop() { 48 eventLog.WriteEntry("Shutting down HeuristicLab Hive Slave..."); 55 try { 56 eventLog.WriteEntry("Shutting down HeuristicLab Hive Slave..."); 57 } 58 catch (Exception) { } 59 49 60 core.Shutdown(); 50 eventLog.WriteEntry("HeuristicLab Hive Slave stopped!"); 61 62 try { 63 eventLog.WriteEntry("HeuristicLab Hive Slave stopped!"); 64 } 65 catch (Exception) { } 51 66 } 52 67 } -
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/ConfigManager.cs
r5599 r5621 125 125 126 126 public static Guid GetUniqueMachineId() { 127 // todo: instead of creating a new id, generate an ID from hardware IDs which is always the same for one machine128 127 if (Settings.Default.Guid == Guid.Empty) { 129 Guid id = Guid.NewGuid(); 128 Guid id; 129 try { 130 id = GetUniqueMachineIdFromMac(); 131 } 132 catch { 133 // fallback if something goes wrong... 134 id = Guid.NewGuid(); 135 } 136 130 137 Settings.Default.Guid = id; 131 138 Settings.Default.Save(); … … 153 160 } 154 161 162 /// <summary> 163 /// Generate a guid based on mac address of the first found nic (yes, mac addresses are not unique...). 164 /// Format: 165 /// 166 /// D1 D2 D3 Res. D4 167 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 168 /// |0 0 0 0|0 0|0 0|0 0 mac address| 169 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 170 /// 171 /// The mac address is saved in the last 48 bits of the Data 4 segment 172 /// of the guid (first 2 bytes of Data 4 are reserved). 173 /// </summary> 174 private static Guid GetUniqueMachineIdFromMac() { 175 ManagementClass mgtClass = new ManagementClass("Win32_NetworkAdapterConfiguration"); 176 ManagementObjectCollection mgtCol = mgtClass.GetInstances(); 177 178 foreach (ManagementObject mgtObj in mgtCol) { 179 foreach (var prop in mgtObj.Properties) { 180 if (prop.Value != null && prop.Name == "MACAddress") { 181 try { 182 //simply take the first nic 183 string mac = prop.Value.ToString(); 184 byte[] b = new byte[8]; 185 string[] macParts = mac.Split(':'); 186 if (macParts.Length == 6) { 187 for (int i = 0; i < macParts.Length; i++) { 188 b[i + 2] = (byte)((ParseNybble(macParts[i][0]) << 4) | ParseNybble(macParts[i][1])); 189 } 190 191 Guid guid = new Guid(0, 0, 0, b); 192 return guid; 193 } else 194 throw new Exception("Error getting mac addresse"); 195 } 196 catch { 197 throw new Exception("Error getting mac addresse"); 198 } 199 } 200 } 201 } 202 throw new Exception("Error getting mac addresse"); 203 } 204 205 /// <summary> 206 /// return numeric value of a single hex-char 207 /// (see: http://stackoverflow.com/questions/854012/how-to-convert-hex-to-a-byte-array) 208 /// </summary> 209 static int ParseNybble(char c) { 210 if (c >= '0' && c <= '9') { 211 return c - '0'; 212 } 213 if (c >= 'A' && c <= 'F') { 214 return c - 'A' + 10; 215 } 216 if (c >= 'a' && c <= 'f') { 217 return c - 'a' + 10; 218 } 219 throw new ArgumentException("Invalid hex digit: " + c); 220 } 221 155 222 private static long? GetWMIValue(string clazz, string property) { 156 223 ManagementClass mgtClass = new ManagementClass(clazz);
Note: See TracChangeset
for help on using the changeset viewer.