Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/04/19 15:59:40 (5 years ago)
Author:
klichtbe
Message:

#3033 changed machine ID generation to only create intial value once on startup without using MAC addresses

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Clients.Hive.Slave/3.3/Manager/ConfigManager.cs

    r17180 r17316  
    2525using System.Linq;
    2626using System.Management;
    27 using System.Net.NetworkInformation;
    2827using HeuristicLab.Clients.Hive.SlaveCore.Properties;
    2928
     
    133132      try {
    134133        prog = jobManager.GetExecutionTimes();
    135       }
    136       catch (Exception ex) {
     134      } catch (Exception ex) {
    137135        SlaveClientCom.Instance.LogMessage(string.Format("Exception was thrown while trying to get execution times: {0}", ex.Message));
    138136      }
     
    140138    }
    141139
     140    /// <summary>
     141    /// Returns the unique machine id of the slave
     142    /// </summary>
     143    /// <returns><see cref="Guid"/></returns>
    142144    public static Guid GetUniqueMachineId() {
    143       Guid id;
    144       try {
    145         id = GetUniqueMachineIdFromMac();
    146       }
    147       catch {
    148         // fallback if something goes wrong...       
    149         id = new Guid(Environment.MachineName.GetHashCode(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    150       }
    151       return id;
     145      // Due to the fact that repeated calculation of the machine ID can lead to a client registering at
     146      // the Hive server multiple times with different IDs it's better to set the unique machine id only
     147      // once, at first startup, and store it in core settings.
     148      if (Settings.Default.MachineId == Guid.Empty) {
     149        Settings.Default.MachineId = Guid.NewGuid();
     150        Settings.Default.Save();
     151      }
     152
     153      return Settings.Default.MachineId;
    152154    }
    153155
     
    170172    }
    171173
    172     /// <summary>
    173     /// Generate a guid based on mac address of the first found nic (yes, mac addresses are not unique...)
    174     /// and the machine name.
    175     /// Format:
    176     ///
    177     ///  D1      D2  D3  Res.   D4
    178     /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    179     /// |n a m e|0 0|0 0|0 0 mac address|
    180     /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    181     ///
    182     /// The mac address is saved in the last 48 bits of the Data 4 segment
    183     /// of the guid (first 2 bytes of Data 4 are reserved).
    184     /// D1 contains the hash of the machinename.
    185     /// </summary>   
    186     private static Guid GetUniqueMachineIdFromMac() {
    187       //try to get a real network interface, not a virtual one
    188       NetworkInterface validNic = NetworkInterface.GetAllNetworkInterfaces()
    189                       .FirstOrDefault(x =>
    190                                   !x.Name.Contains(vmwareNameString) &&
    191                                   !x.Name.Contains(virtualboxNameString) &&
    192                                   (x.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
    193                                    x.NetworkInterfaceType == NetworkInterfaceType.GigabitEthernet));
    194 
    195       if (validNic == default(NetworkInterface)) {
    196         validNic = NetworkInterface.GetAllNetworkInterfaces().First();
    197       }
    198 
    199       byte[] addr = validNic.GetPhysicalAddress().GetAddressBytes();
    200       if (addr.Length < macLength || addr.Length > macLongLength) {
    201         throw new ArgumentException(string.Format("Error generating slave UID: MAC address has to have a length between {0} and {1} bytes. Actual MAC address is: {2}",
    202               macLength, macLongLength, addr));
    203       }
    204 
    205       if (addr.Length < macLongLength) {
    206         byte[] b = new byte[8];
    207         Array.Copy(addr, 0, b, 2, addr.Length);
    208         addr = b;
    209       }
    210 
    211       // also get machine name and save it to the first 4 bytes               
    212       Guid guid = new Guid(Environment.MachineName.GetHashCode(), 0, 0, addr);
    213       return guid;
    214     }
    215 
    216174    private static long? GetWMIValue(string clazz, string property) {
    217175      ManagementClass mgtClass = new ManagementClass(clazz);
     
    223181            try {
    224182              return long.Parse(prop.Value.ToString());
    225             }
    226             catch {
     183            } catch {
    227184              return null;
    228185            }
     
    241198      try {
    242199        mb = (int)(memCounter.NextValue() / 1024 / 1024);
    243       }
    244       catch { }
     200      } catch { }
    245201      return mb;
    246202    }
     
    251207      try {
    252208        return cpuCounter.NextValue();
    253       }
    254       catch { }
     209      } catch { }
    255210      return cpuVal;
    256211    }
Note: See TracChangeset for help on using the changeset viewer.