From: <che...@us...> - 2010-11-19 19:38:02
|
Revision: 3962 http://mp-plugins.svn.sourceforge.net/mp-plugins/?rev=3962&view=rev Author: chef_koch Date: 2010-11-19 19:37:55 +0000 (Fri, 19 Nov 2010) Log Message: ----------- moved restart code to IRShared added hardware monitor to ir tray, only available in app mode Modified Paths: -------------- trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server/IR Server.cs trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Config.cs trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Program.cs trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Tray/Program.cs trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IRServer.Shared/Shared.cs Modified: trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server/IR Server.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server/IR Server.cs 2010-11-19 15:51:30 UTC (rev 3961) +++ trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server/IR Server.cs 2010-11-19 19:37:55 UTC (rev 3962) @@ -207,14 +207,15 @@ #endregion Setup Abstract Remote Model processing #region Setup Hardware Monitoring + + //// Restart is done onlyy in IRTray with AppMode + //if (Settings.RestartOnUSBChanges) + //{ + // _hardwareMonitor = new HardwareMonitor(); + // _hardwareMonitor.DeviceConnected += new HardwareMonitor.HardwareMonitorEvent(OnDeviceConnected); + // _hardwareMonitor.Start(); + //} - if (Settings.RestartOnUSBChanges) - { - _hardwareMonitor = new HardwareMonitor(); - _hardwareMonitor.DeviceConnected += new HardwareMonitor.HardwareMonitorEvent(OnDeviceConnected); - _hardwareMonitor.Start(); - } - #endregion IrssLog.Info("IR Server started"); Modified: trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Config.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Config.cs 2010-11-19 15:51:30 UTC (rev 3961) +++ trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Config.cs 2010-11-19 19:37:55 UTC (rev 3962) @@ -535,9 +535,9 @@ { toolStripServiceButton.Enabled = false; if (Shared._serviceInstalled == true) - Program.ServiceUninstall(); + Shared.ServiceUninstall(); else - Program.ServiceInstall(); + Shared.ServiceInstall(); Shared.getStatus(); setButtons(); @@ -547,17 +547,17 @@ private void toolStripButtonService_Click(object sender, EventArgs e) { if (Shared._irsStatus == IrsStatus.RunningService) - Program.ServiceStop(); + Shared.ServiceStop(); if (Shared._irsStatus == IrsStatus.NotRunning) - Program.ServiceStart(); + Shared.ServiceStart(); } private void toolStripButtonApplication_Click(object sender, EventArgs e) { if (Shared._irsStatus == IrsStatus.RunningApplication) - Program.ApplicationStop(); + Shared.ApplicationStop(); if (Shared._irsStatus == IrsStatus.NotRunning) - Program.ApplicationStart(); + Shared.ApplicationStart(); } #endregion Controls @@ -602,7 +602,7 @@ Settings.SaveSettings(); // Restart IR Server ... - Program.RestartIRS(); + Shared.RestartIRS(); } else { Modified: trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Program.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Program.cs 2010-11-19 15:51:30 UTC (rev 3961) +++ trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Program.cs 2010-11-19 19:37:55 UTC (rev 3962) @@ -41,21 +41,6 @@ { internal static class Program { - #region Constants - - internal static readonly string IRServerFile = Path.Combine(Application.StartupPath, @"IR Server.exe"); - - private static readonly TimeSpan defaultServiceTime = new TimeSpan(0, 0, 30); - - #endregion Constants - - #region Variables - - private static ServiceController serviceController; - private static int waitCount; - - #endregion Variables - /// <summary> /// The main entry point for the application. /// </summary> @@ -76,175 +61,5 @@ IrssLog.Close(); } - - private static ServiceController getServiceController() - { - Shared.serviceControllers = ServiceController.GetServices(); - foreach (ServiceController sc in Shared.serviceControllers) - { - if (sc.ServiceName == Shared.ServerName) - return sc; - } - return null; - } - - internal static void ServiceInstall() - { - try - { - IrssLog.Info("Installing IR Server service"); - Process IRServer = Process.Start(Program.IRServerFile, "/INSTALL"); - IRServer.WaitForExit((int)defaultServiceTime.TotalMilliseconds); - IrssLog.Info("Installing IR Server service - done"); - } - catch (Exception ex) - { - IrssLog.Error("Installing IR Server service - failed (see following...)"); - IrssLog.Error(ex); - } - } - - internal static void ServiceUninstall() - { - try - { - IrssLog.Info("Uninstalling IR Server service"); - Process IRServer = Process.Start(Program.IRServerFile, "/UNINSTALL"); - IRServer.WaitForExit((int)defaultServiceTime.TotalMilliseconds); - IrssLog.Info("Uninstalling IR Server service - done"); - } - catch (Exception ex) - { - IrssLog.Error("Uninstalling IR Server service - failed (see following...)"); - IrssLog.Error(ex); - } - } - - internal static void ServiceStart() - { - try - { - serviceController = getServiceController(); - if (Shared.serviceControllers != null) - { - IrssLog.Info("Starting IR Server (service)"); - serviceController.Start(); - serviceController.WaitForStatus(ServiceControllerStatus.Running, defaultServiceTime); - IrssLog.Info("Starting IR Server (service) - done"); - } - } - catch (TimeoutException ex) - { - IrssLog.Error("Starting IR Server (service) - failed (timeout error)"); - IrssLog.Error(ex); - } - catch (Exception ex) - { - IrssLog.Error("Starting IR Server (service) - failed (see following...)"); - IrssLog.Error(ex); - } - } - - internal static void ServiceStop() - { - try - { - serviceController = getServiceController(); - if (Shared.serviceControllers != null) - { - IrssLog.Info("Stopping IR Server (service)"); - serviceController.Stop(); - serviceController.WaitForStatus(ServiceControllerStatus.Stopped, defaultServiceTime); - IrssLog.Info("Stopping IR Server (service) - done"); - } - } - catch (TimeoutException ex) - { - IrssLog.Error("Stopping IR Server (service) - failed (timeout error)"); - IrssLog.Error(ex); - } - catch (Exception ex) - { - IrssLog.Error("Stopping IR Server (service) - failed (see following...)"); - IrssLog.Error(ex); - } - } - - internal static void ApplicationStart() - { - try - { - IrssLog.Info("Starting IR Server (application)"); - Process IRServer = Process.Start(IRServerFile); - waitCount = 0; - while (Win32.FindWindowByTitle(Shared.ServerWindowName) == IntPtr.Zero) - { - waitCount++; - if (waitCount > 150) throw new TimeoutException(); - Thread.Sleep(200); - } - IrssLog.Info("Starting IR Server (application) - done"); - } - catch (TimeoutException ex) - { - IrssLog.Error("Starting IR Server (application) - failed (timeout error)"); - IrssLog.Error(ex); - } - catch (Exception ex) - { - IrssLog.Error("Starting IR Server (application) - failed (see following...)"); - IrssLog.Error(ex); - } - } - - internal static void ApplicationStop() - { - try - { - IrssLog.Info("Stopping IR Server (application)"); - IntPtr irssWindow = Win32.FindWindowByTitle(Shared.ServerWindowName); - IntPtr result = Win32.SendWindowsMessage(irssWindow, 16, 0, 0); - waitCount = 0; - while (Win32.FindWindowByTitle(Shared.ServerWindowName) != IntPtr.Zero) - { - waitCount++; - if (waitCount > 150) throw new TimeoutException(); - Thread.Sleep(200); - } - IrssLog.Info("Stopping IR Server (application) - done"); - } - catch (TimeoutException ex) - { - IrssLog.Error("Stopping IR Server (application) - failed (timeout error)"); - IrssLog.Error(ex); - } - catch (Exception ex) - { - IrssLog.Error("Stopping IR Server (application) - failed (see following...)"); - IrssLog.Error(ex); - } - } - - internal static void RestartIRS() - { - IrssLog.Info("Restarting IR Server"); - - switch (Shared._irsStatus) - { - case IrsStatus.RunningService: - { - ServiceStop(); - ServiceStart(); - break; - } - case IrsStatus.RunningApplication: - { - ApplicationStop(); - ApplicationStart(); - break; - } - } - IrssLog.Info("Restarting IR Server - done"); - } } } \ No newline at end of file Modified: trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Tray/Program.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Tray/Program.cs 2010-11-19 15:51:30 UTC (rev 3961) +++ trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Tray/Program.cs 2010-11-19 19:37:55 UTC (rev 3962) @@ -18,6 +18,8 @@ private static readonly string _translatorExe = Path.Combine(Application.StartupPath, @"Translator.exe"); private static readonly string _debugClientExe = Path.Combine(Application.StartupPath, @"DebugClient.exe"); + private const int TimeToWaitForRestart = 10000; + #endregion Constants #region Variables @@ -28,6 +30,9 @@ internal static readonly Icon _iconGray = new Icon(Resources.iconGray, new Size(16, 16)); internal static readonly Icon _iconGreen = new Icon(Resources.iconGreen, new Size(16, 16)); + private static HardwareMonitor _hardwareMonitor; + private static DateTime _lastDeviceEvent = DateTime.MinValue; + #endregion Variables /// <summary> @@ -62,6 +67,14 @@ _notifyIcon.Text = Shared.ServerDisplayName; _notifyIcon.Visible = true; + Settings.LoadSettings(); + if (Settings.RestartOnUSBChanges) + { + _hardwareMonitor = new HardwareMonitor(); + _hardwareMonitor.DeviceConnected += new HardwareMonitor.HardwareMonitorEvent(OnDeviceConnected); + _hardwareMonitor.Start(); + } + thread = new Thread(new ThreadStart(UpdateIcon)); thread.IsBackground = true; thread.Start(); @@ -121,5 +134,32 @@ } #endregion + + #region Hardware Monitoring + + private static void OnDeviceConnected() + { + Thread lazyRestartThread = new Thread(LazyRestart); + lazyRestartThread.Start(); + } + + private static void LazyRestart() + { + DateTime tempLastDeviceEvent = DateTime.Now; + _lastDeviceEvent = tempLastDeviceEvent; + + // wait, if new decice events occur + Thread.Sleep(TimeToWaitForRestart); + + // if new device event occured, stop here + if (!tempLastDeviceEvent.Equals(_lastDeviceEvent)) return; + + // restart service + IrssLog.Info("New device event. Restarting Input Service."); + Shared.ApplicationStop(); + Shared.ApplicationStart(); + } + + #endregion } } Modified: trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IRServer.Shared/Shared.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IRServer.Shared/Shared.cs 2010-11-19 15:51:30 UTC (rev 3961) +++ trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IRServer.Shared/Shared.cs 2010-11-19 19:37:55 UTC (rev 3962) @@ -1,8 +1,13 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Reflection; using System.ServiceProcess; +using System.Threading; using IRServer.Plugin; using IrssUtils; +using TimeoutException = System.ServiceProcess.TimeoutException; namespace IRServer { @@ -52,14 +57,22 @@ { #region Constants + public static readonly string IRServerFile = Path.Combine( + Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase), @"IR Server.exe"); + public const string ServerName = "IRServer"; public const string ServerWindowName = "IRSS - " + ServerName; public const string ServerDisplayName = "IR Server"; + private static readonly TimeSpan defaultServiceTime = new TimeSpan(0, 0, 30); + #endregion #region Variables + private static ServiceController serviceController; + private static int waitCount; + public static ServiceController[] serviceControllers; private static IntPtr irsWindow; @@ -68,6 +81,180 @@ #endregion + #region Start / Stop App / Service + + private static ServiceController getServiceController() + { + Shared.serviceControllers = ServiceController.GetServices(); + foreach (ServiceController sc in Shared.serviceControllers) + { + if (sc.ServiceName == Shared.ServerName) + return sc; + } + return null; + } + + public static void ServiceInstall() + { + try + { + IrssLog.Info("Installing IR Server service"); + Process IRServer = Process.Start(Shared.IRServerFile, "/INSTALL"); + IRServer.WaitForExit((int)defaultServiceTime.TotalMilliseconds); + IrssLog.Info("Installing IR Server service - done"); + } + catch (Exception ex) + { + IrssLog.Error("Installing IR Server service - failed (see following...)"); + IrssLog.Error(ex); + } + } + + public static void ServiceUninstall() + { + try + { + IrssLog.Info("Uninstalling IR Server service"); + Process IRServer = Process.Start(Shared.IRServerFile, "/UNINSTALL"); + IRServer.WaitForExit((int)defaultServiceTime.TotalMilliseconds); + IrssLog.Info("Uninstalling IR Server service - done"); + } + catch (Exception ex) + { + IrssLog.Error("Uninstalling IR Server service - failed (see following...)"); + IrssLog.Error(ex); + } + } + + public static void ServiceStart() + { + try + { + serviceController = getServiceController(); + if (Shared.serviceControllers != null) + { + IrssLog.Info("Starting IR Server (service)"); + serviceController.Start(); + serviceController.WaitForStatus(ServiceControllerStatus.Running, defaultServiceTime); + IrssLog.Info("Starting IR Server (service) - done"); + } + } + catch (TimeoutException ex) + { + IrssLog.Error("Starting IR Server (service) - failed (timeout error)"); + IrssLog.Error(ex); + } + catch (Exception ex) + { + IrssLog.Error("Starting IR Server (service) - failed (see following...)"); + IrssLog.Error(ex); + } + } + + public static void ServiceStop() + { + try + { + serviceController = getServiceController(); + if (Shared.serviceControllers != null) + { + IrssLog.Info("Stopping IR Server (service)"); + serviceController.Stop(); + serviceController.WaitForStatus(ServiceControllerStatus.Stopped, defaultServiceTime); + IrssLog.Info("Stopping IR Server (service) - done"); + } + } + catch (TimeoutException ex) + { + IrssLog.Error("Stopping IR Server (service) - failed (timeout error)"); + IrssLog.Error(ex); + } + catch (Exception ex) + { + IrssLog.Error("Stopping IR Server (service) - failed (see following...)"); + IrssLog.Error(ex); + } + } + + public static void RestartIRS() + { + IrssLog.Info("Restarting IR Server"); + + switch (Shared._irsStatus) + { + case IrsStatus.RunningService: + { + ServiceStop(); + ServiceStart(); + break; + } + case IrsStatus.RunningApplication: + { + ApplicationStop(); + ApplicationStart(); + break; + } + } + IrssLog.Info("Restarting IR Server - done"); + } + + public static void ApplicationStart() + { + try + { + IrssLog.Info("Starting IR Server (application)"); + Process IRServer = Process.Start(IRServerFile); + waitCount = 0; + while (Win32.FindWindowByTitle(Shared.ServerWindowName) == IntPtr.Zero) + { + waitCount++; + if (waitCount > 150) throw new TimeoutException(); + Thread.Sleep(200); + } + IrssLog.Info("Starting IR Server (application) - done"); + } + catch (TimeoutException ex) + { + IrssLog.Error("Starting IR Server (application) - failed (timeout error)"); + IrssLog.Error(ex); + } + catch (Exception ex) + { + IrssLog.Error("Starting IR Server (application) - failed (see following...)"); + IrssLog.Error(ex); + } + } + + public static void ApplicationStop() + { + try + { + IrssLog.Info("Stopping IR Server (application)"); + IntPtr irssWindow = Win32.FindWindowByTitle(ServerWindowName); + IntPtr result = Win32.SendWindowsMessage(irssWindow, 16, 0, 0); + waitCount = 0; + while (Win32.FindWindowByTitle(ServerWindowName) != IntPtr.Zero) + { + waitCount++; + if (waitCount > 150) throw new TimeoutException(); + Thread.Sleep(200); + } + IrssLog.Info("Stopping IR Server (application) - done"); + } + catch (TimeoutException ex) + { + IrssLog.Error("Stopping IR Server (application) - failed (timeout error)"); + IrssLog.Error(ex); + } + catch (Exception ex) + { + IrssLog.Error("Stopping IR Server (application) - failed (see following...)"); + IrssLog.Error(ex); + } + } + + #endregion + #region detect hardware /// <summary> @@ -138,7 +325,7 @@ #endregion - #region + #region getstatus public static void getStatus() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |