Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12297 for stable


Ignore:
Timestamp:
04/08/15 16:53:00 (9 years ago)
Author:
ascheibe
Message:

#2356 merged r12236,r12273 into stable

Location:
stable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Clients.Hive.Slave/3.3/Manager/ConfigManager.cs

    r12009 r12297  
    2525using System.Linq;
    2626using System.Management;
     27using System.Net.NetworkInformation;
    2728using HeuristicLab.Clients.Hive.SlaveCore.Properties;
    2829
     
    3435  public class ConfigManager {
    3536    private static ConfigManager instance = null;
     37    private const string vmwareNameString = "VMware";
     38    private const string virtualboxNameString = "VirtualBox";
     39    private const int macLength = 6;
     40    private const int macLongLength = 8;
     41
    3642    public static ConfigManager Instance {
    3743      get { return instance; }
     
    179185    /// </summary>   
    180186    private static Guid GetUniqueMachineIdFromMac() {
    181       ManagementClass mgtClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
    182       ManagementObjectCollection mgtCol = mgtClass.GetInstances();
    183 
    184       foreach (ManagementObject mgtObj in mgtCol) {
    185         foreach (var prop in mgtObj.Properties) {
    186           if (prop.Value != null && prop.Name == "MACAddress") {
    187             try {
    188               //simply take the first nic
    189               string mac = prop.Value.ToString();
    190               byte[] b = new byte[8];
    191               string[] macParts = mac.Split(':');
    192               if (macParts.Length == 6) {
    193                 for (int i = 0; i < macParts.Length; i++) {
    194                   b[i + 2] = (byte)((ParseNybble(macParts[i][0]) << 4) | ParseNybble(macParts[i][1]));
    195                 }
    196 
    197                 // also get machine name and save it to the first 4 bytes               
    198                 Guid guid = new Guid(Environment.MachineName.GetHashCode(), 0, 0, b);
    199                 return guid;
    200               } else
    201                 throw new Exception("Error getting mac addresse");
    202             }
    203             catch {
    204               throw new Exception("Error getting mac addresse");
    205             }
    206           }
    207         }
    208       }
    209       throw new Exception("Error getting mac addresse");
    210     }
    211 
    212     /// <summary>
    213     /// return numeric value of a single hex-char
    214     /// (see: http://stackoverflow.com/questions/854012/how-to-convert-hex-to-a-byte-array)
    215     /// </summary>   
    216     static int ParseNybble(char c) {
    217       if (c >= '0' && c <= '9') {
    218         return c - '0';
    219       }
    220       if (c >= 'A' && c <= 'F') {
    221         return c - 'A' + 10;
    222       }
    223       if (c >= 'a' && c <= 'f') {
    224         return c - 'a' + 10;
    225       }
    226       throw new ArgumentException("Invalid hex digit: " + c);
     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;
    227214    }
    228215
Note: See TracChangeset for help on using the changeset viewer.