Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5621


Ignore:
Timestamp:
03/07/11 16:15:55 (14 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
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  
    1919 */
    2020#endregion
     21using System;
    2122using System.ServiceProcess;
    2223using System.Threading;
    23 using HeuristicLab.Clients.Hive.SlaveCore;
    2424
    2525namespace HeuristicLab.Clients.Hive.SlaveCore.SlaveWindowsService {
     
    3030    public SlaveWindowsService() {
    3131      InitializeComponent();
    32       if (!System.Diagnostics.EventLog.SourceExists("HeuristicLab Hive Slave"))
    33         System.Diagnostics.EventLog.CreateEventSource("HeuristicLab Hive Slave",
    34                                                               "HeuristicLab Hive Slave");
    3532
    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) { }
    3841    }
    3942
     
    4245      coreThread = new Thread(core.Start);
    4346      coreThread.Start();
    44       eventLog.WriteEntry("HeuristicLab Hive Slave started!");
     47
     48      try {
     49        eventLog.WriteEntry("HeuristicLab Hive Slave started!");
     50      }
     51      catch (Exception) { }
    4552    }
    4653
    4754    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
    4960      core.Shutdown();
    50       eventLog.WriteEntry("HeuristicLab Hive Slave stopped!");
     61
     62      try {
     63        eventLog.WriteEntry("HeuristicLab Hive Slave stopped!");
     64      }
     65      catch (Exception) { }
    5166    }
    5267  }
  • 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.