Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/09/11 20:19:43 (13 years ago)
Author:
ascheibe
Message:

#1233 added Hive Slave HL App client

File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Views/3.3/SlaveStats.cs

    r6727 r6730  
    2121
    2222using System;
    23 using System.Diagnostics;
    24 using System.Runtime.InteropServices;
    25 using System.Security.Principal;
    26 using System.ServiceProcess;
    27 using System.Threading;
    28 using System.Threading.Tasks;
    2923using System.Windows.Forms;
    3024using System.Windows.Forms.DataVisualization.Charting;
    31 using HeuristicLab.Clients.Hive.SlaveCore.Views.Properties;
    3225using HeuristicLab.Common;
    3326using HeuristicLab.Core.Views;
     
    3730namespace HeuristicLab.Clients.Hive.SlaveCore.Views {
    3831
    39   [View("HeuristicLab Slave View")]
    40   [Content(typeof(SlaveItem), IsDefaultView = true)]
    41   public partial class SlaveView : ItemView {
     32  [View("HeuristicLab Slave Stats View")]
     33  [Content(typeof(SlaveItem), IsDefaultView = false)]
     34  public partial class SlaveStats : ItemView {
    4235    private SlaveDisplayStat lastSlaveDisplayStat;
    43     private const string serviceName = "HeuristicLab.Clients.Hive.SlaveCore.SlaveWindowsService";
    4436
    45     private const UInt32 BCM_SETSHIELD = 0x160C;
    46     [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
    47     static extern int SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, IntPtr lParam);
    4837
    4938    public new SlaveItem Content {
     
    5645    }
    5746
    58     public SlaveView() {
     47    public SlaveStats() {
    5948      InitializeComponent();
    60 
    61       if (CheckRunAsAdmin()) {
    62         btnKill.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
    63         btnKill.Image = HeuristicLab.Common.Resources.VSImageLibrary.BreakpointActive;
    64       } else {
    65         this.btnKill.FlatStyle = FlatStyle.System;
    66         SendMessage(btnKill.Handle, BCM_SETSHIELD, 0, (IntPtr)1);
    67       }
    68 
    6949      lblSlaveState.Text = SlaveDisplayStat.NoService.ToString();
    7050      lastSlaveDisplayStat = SlaveDisplayStat.NoService;
     
    7454    #region Register Content Events
    7555    protected override void DeregisterContentEvents() {
    76       Content.SlaveShutdown -= new System.EventHandler(Content_SlaveShutdown);
    7756      Content.SlaveStatusChanged -= new System.EventHandler<EventArgs<StatusCommons>>(Content_SlaveStatusChanged);
    7857      Content.SlaveDisplayStateChanged -= new EventHandler<EventArgs<SlaveDisplayStat>>(Content_SlaveDisplayStateChanged);
     
    8564      base.RegisterContentEvents();
    8665
    87       Content.SlaveShutdown += new System.EventHandler(Content_SlaveShutdown);
    8866      Content.SlaveStatusChanged += new System.EventHandler<EventArgs<StatusCommons>>(Content_SlaveStatusChanged);
    8967      Content.SlaveDisplayStateChanged += new EventHandler<EventArgs<SlaveDisplayStat>>(Content_SlaveDisplayStateChanged);
     
    9472    protected override void OnContentChanged() {
    9573      base.OnContentChanged();
    96       btnKill.Enabled = false;
    97       btnStart.Enabled = false;
    98       btnStop.Enabled = false;
    99 
    100       if (Content == null) {
    101         //nothing to do...         
    102       } else {
    103         //try to establish a connection to the slave service
    104         if (base.Content != null) {
    105           ((SlaveItem)base.Content).Open();
    106           Task.Factory.StartNew(Connector);
    107         }
    108       }
    109     }
    110 
    111     private void Connector() {
    112       Content_SlaveDisplayStateChanged(this, new EventArgs<SlaveDisplayStat>(SlaveDisplayStat.NoService));
    113       bool connected = false;
    114       while (!connected) {
    115         this.Invoke(new Func<bool>(() => connected = ((SlaveItem)base.Content).ReconnectToSlaveCore()));
    116 
    117         if (!connected) {
    118           Thread.Sleep(1000);
    119         }
    120       }
    121       this.Invoke(new Action(SetEnabledStateOfControls));
    12274    }
    12375
     
    13385    }
    13486
    135     void Content_SlaveShutdown(object sender, System.EventArgs e) {
    136       Task.Factory.StartNew(Connector);
    137     }
    138 
    13987    void Content_SlaveDisplayStateChanged(object sender, EventArgs<SlaveDisplayStat> e) {
    14088      lblSlaveState.Text = e.Value.ToString();
    14189      lastSlaveDisplayStat = e.Value;
    142 
    143       if (e.Value == SlaveDisplayStat.Asleep || e.Value == SlaveDisplayStat.NoService) {
    144         btnKill.Enabled = false;
    145         btnStart.Enabled = true;
    146         btnStop.Enabled = false;
    147       }
    148 
    149       if (e.Value == SlaveDisplayStat.Busy || e.Value == SlaveDisplayStat.Idle || e.Value == SlaveDisplayStat.Offline) {
    150         btnKill.Enabled = true;
    151         btnStart.Enabled = false;
    152         btnStop.Enabled = true;
    153       }
    15490    }
    15591
    15692    void Content_CoreConnectionChanged(object sender, EventArgs<CoreConnection> e) {
    157       if (e.Value == CoreConnection.Offline) {
    158         btnKill.Enabled = false;
    159         btnStart.Enabled = false;
    160         btnStop.Enabled = false;
    161       }
     93
    16294    }
    16395    #endregion
     
    228160      coresChart.Series[0].Points.Add(pUsedCores);
    229161    }
    230 
    231     private bool CheckRunAsAdmin() {
    232       bool isRunAsAdmin = false;
    233       WindowsIdentity user = WindowsIdentity.GetCurrent();
    234       WindowsPrincipal principal = new WindowsPrincipal(user);
    235 
    236       try {
    237         isRunAsAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
    238       }
    239       catch { }
    240       return isRunAsAdmin;
    241     }
    242 
    243     /// <summary>
    244     /// Shows the windows UAC dialog and restarts tray icon app
    245     /// </summary>
    246     private void ElevateApplication() {
    247       // launch itself as administrator
    248       ProcessStartInfo proc = new ProcessStartInfo(Application.ExecutablePath, Settings.Default.ShowUICmd);
    249       proc.UseShellExecute = true;
    250       proc.WorkingDirectory = Environment.CurrentDirectory;
    251       proc.FileName = Application.ExecutablePath;
    252       proc.Verb = "runas";
    253 
    254       try {
    255         Process.Start(proc);
    256       }
    257       catch {
    258         // user refused to allow privileges elevation       
    259         return;
    260       }
    261       Application.Exit();
    262     }
    263 
    264     private void StartService() {
    265       TimeSpan timeout = TimeSpan.FromMilliseconds(5000);
    266 
    267       ServiceController service = new ServiceController(serviceName);
    268       try {
    269         if (service.Status == ServiceControllerStatus.Running) {
    270           service.Stop();
    271           service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
    272         }
    273 
    274         service.Start();
    275         service.WaitForStatus(ServiceControllerStatus.Running, timeout);
    276       }
    277       catch (InvalidOperationException ex) {
    278         MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());
    279       }
    280       catch (Exception ex) {
    281         MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString());
    282       }
    283     }
    284 
    285     private void StopService() {
    286       TimeSpan timeout = TimeSpan.FromMilliseconds(7000);
    287 
    288       ServiceController service = new ServiceController(serviceName);
    289       try {
    290         if (service.Status == ServiceControllerStatus.Running) {
    291           service.Stop();
    292           service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
    293         }
    294       }
    295       catch (InvalidOperationException ex) {
    296         MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());
    297       }
    298       catch (Exception ex) {
    299         MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString());
    300       }
    301     }
    302 
    303     private void btnKill_Click(object sender, EventArgs e) {
    304       if (CheckRunAsAdmin()) {
    305         StopService();
    306       } else {
    307         ElevateApplication();
    308       }
    309     }
    310 
    311     private void btnStop_Click(object sender, EventArgs e) {
    312       if (Content != null) {
    313         Content.Sleep();
    314       }
    315     }
    316 
    317     private void btnStart_Click(object sender, EventArgs e) {
    318       if (Content != null) {
    319         if (lastSlaveDisplayStat == SlaveDisplayStat.Asleep) {
    320           Content.RestartCore();
    321         } else if (lastSlaveDisplayStat == SlaveDisplayStat.NoService) {
    322           if (CheckRunAsAdmin()) {
    323             StartService();
    324           } else {
    325             ElevateApplication();
    326           }
    327         }
    328       }
    329     }
    330162  }
    331163}
Note: See TracChangeset for help on using the changeset viewer.