Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/07/11 16:15:55 (13 years ago)
Author:
ascheibe
Message:

#1233

  • use mac address as Guid for Slaves (for now...)
  • Slave Windows Service now also works if it can't write to an event source
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/ConfigManager.cs

    r5599 r5621  
    125125
    126126    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 machine
    128127      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
    130137        Settings.Default.Guid = id;
    131138        Settings.Default.Save();
     
    153160    }
    154161
     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
    155222    private static long? GetWMIValue(string clazz, string property) {
    156223      ManagementClass mgtClass = new ManagementClass(clazz);
Note: See TracChangeset for help on using the changeset viewer.