Example #1
0
        /// <summary>
        /// Suspend standby by SetThreadExecutionSTate because of a given CheckSuspendResult.
        /// </summary>
        /// <param name="result">The result why the standby should be suspended.</param>
        private void SuspendStandbyThreadExecutionState(CheckSuspendResult result)
        {
            try
            {
                lock (lockObj)
                {
                    var success = NativeMethods.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);

                    if (success == 0)
                    {
                        this.standbySuspended = false;
                        var ex = Win32ExceptionManager.GetWin32Exception();
                        throw new WsapmException(Resources.Wsapm_Core.StandbyManager_SuspendStandbyFailed, ex);
                    }
                    else
                    {
                        this.standbySuspended = true;

                        if (!String.IsNullOrEmpty(result.Reason))
                        {
                            WsapmLog.Log.WriteLine(String.Format(Resources.Wsapm_Core.StandbyManager_StandbySuspendedWithReason, result.Reason), LogMode.Normal);
                        }
                        else
                        {
                            WsapmLog.Log.WriteLine(Resources.Wsapm_Core.StandbyManager_StandbySuspendedWithoutReason, LogMode.Verbose);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WsapmLog.Log.WriteError(ex);
            }
        }
Example #2
0
        /// <summary>
        /// Suspend standby by PowerAvailabilityRequest because of a given CheckSuspendResult.
        /// </summary>
        /// <param name="result">The result why the standby should be suspended.</param>
        private void SuspendStandbyPowerAvailabilityRequest(CheckSuspendResult result)
        {
            try
            {
                lock (lockObj)
                {
                    // Clear current power request if there is any.
                    if (this.currentPowerRequest != IntPtr.Zero)
                    {
                        NativeMethods.PowerClearRequest(this.currentPowerRequest, PowerRequestType.PowerRequestSystemRequired);
                        this.currentPowerRequest = IntPtr.Zero;
                        this.standbySuspended    = false;
                    }

                    POWER_REQUEST_CONTEXT pContext;
                    pContext.Flags              = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
                    pContext.Version            = POWER_REQUEST_CONTEXT_VERSION;
                    pContext.SimpleReasonString = Resources.Wsapm_Core.StandbyManager_StandbySuspended;

                    this.currentPowerRequest = NativeMethods.PowerCreateRequest(ref pContext);

                    if (this.currentPowerRequest == IntPtr.Zero)
                    {
                        this.standbySuspended = false;
                        var ex = Win32ExceptionManager.GetWin32Exception();
                        throw new WsapmException(Resources.Wsapm_Core.StandbyManager_CreatePowerRequestFail, ex);
                    }

                    bool success = NativeMethods.PowerSetRequest(this.currentPowerRequest, PowerRequestType.PowerRequestSystemRequired);

                    if (!success)
                    {
                        this.standbySuspended    = false;
                        this.currentPowerRequest = IntPtr.Zero;
                        var ex = Win32ExceptionManager.GetWin32Exception();
                        throw new WsapmException(Resources.Wsapm_Core.StandbyManager_SuspendStandbyFailed, ex);
                    }
                    else
                    {
                        this.standbySuspended = true;

                        if (!String.IsNullOrEmpty(result.Reason))
                        {
                            WsapmLog.Log.WriteLine(String.Format(Resources.Wsapm_Core.StandbyManager_StandbySuspendedWithReason, result.Reason), LogMode.Normal);
                        }
                        else
                        {
                            WsapmLog.Log.WriteLine(Resources.Wsapm_Core.StandbyManager_StandbySuspendedWithoutReason, LogMode.Verbose);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WsapmLog.Log.WriteError(ex);
            }
        }
Example #3
0
        /// <summary>
        /// Suspends standby according to the given CheckSuspendResult.
        /// </summary>
        /// <param name="result"></param>
        private void SuspendStandby(CheckSuspendResult result)
        {
            if (this.standbyManager == null)
            {
                return;
            }

            this.standbyManager.SuspendStandby(result);
        }
Example #4
0
 /// <summary>
 /// Suspends standby because of the given CheckSuspendResult.
 /// </summary>
 /// <param name="result">The result why the standby should be suspended.</param>
 public void SuspendStandby(CheckSuspendResult result)
 {
     if (this.powerRequestsSupported)
     {
         SuspendStandbyPowerAvailabilityRequest(result);
     }
     else
     {
         SuspendStandbyThreadExecutionState(result);
     }
 }
Example #5
0
        /// <summary>
        /// Gets a CheckSuspendResult from a FromPluginCheckSuspendResult.
        /// </summary>
        /// <param name="pluginCheckSuspendResult">The PluginCheckSuspendResult from which to get a CheckSuspendResult.</param>
        /// <param name="pluginSource">The plugin which is the source of the PluginCheckSuspendResult.</param>
        /// <returns>A CheckSuspendResult representing a FromPluginCheckSuspendResult.</returns>
        public static CheckSuspendResult FromPluginCheckSuspendResult(PluginCheckSuspendResult pluginCheckSuspendResult, WsapmPluginBase pluginSource)
        {
            var result = new CheckSuspendResult(false, string.Empty);

            if (pluginCheckSuspendResult == null)
            {
                return(result);
            }

            result.SuspendStandby = pluginCheckSuspendResult.SuspendStandby;

            if (pluginSource != null)
            {
                result.Reason = pluginCheckSuspendResult.Reason + " [" + pluginSource.PluginAttribute.Name + "]";
            }

            return(result);
        }
Example #6
0
        /// <summary>
        /// Prepares, checks the policies and tears down all loaded plugins.
        /// </summary>
        /// <param name="activatedPlugins">The list of activated plugins.</param>
        /// <returns>The result of the check as PluginCheckSuspendResult.</returns>
        public CheckSuspendResult CheckPluginPolicies(List <Guid> activatedPlugins)
        {
            var checkResult = new CheckSuspendResult(false, String.Empty);

            if (activatedPlugins == null || activatedPlugins.Count == 0 || this.pluginLoader.Plugins == null || this.pluginLoader.Plugins.Length == 0)
            {
                return(checkResult);
            }

            foreach (var plugin in this.pluginLoader.Plugins)
            {
                // Skip non activated plugins.
                if (!activatedPlugins.Contains(plugin.PluginAttribute.PluginGuid))
                {
                    continue;
                }

                // Skip plugins which are not initialized.
                if (!plugin.IsInitialized)
                {
                    continue;
                }

                // First load current settings (if any), then prepare, then check - plugin by plugin.
                try
                {
                    // Load settings only if it is an advanced plugin.
                    WsapmPluginAdvancedBase pluginAdvanced = plugin as WsapmPluginAdvancedBase;

                    if (pluginAdvanced != null)
                    {
                        pluginAdvanced.LoadSettings();
                    }

                    // Prepare.
                    plugin.PreparePlugin();
                    WsapmLog.Log.WriteLine(String.Format(Resources.Wsapm_Core.PluginManager_PluginPrepared, plugin.PluginAttribute.Name), LogMode.Verbose);
                }
                catch (Exception ex)
                {
                    WsapmLog.Log.WriteError(String.Format(Resources.Wsapm_Core.PluginManager_ErrorPluginPrepare, plugin.PluginAttribute.Name), ex);
                    continue;
                }

                // Skip plugins which were not prepared.
                if (!plugin.IsPrepared)
                {
                    continue;
                }

                try
                {
                    WsapmLog.Log.WriteLine(String.Format(Resources.Wsapm_Core.PluginManager_PluginCheckPolicy, plugin.PluginAttribute.Name), LogMode.Normal);
                    var checkResultPlugin = plugin.CheckPluginPolicyForPlugin();

                    if (checkResultPlugin.SuspendStandby)
                    {
                        checkResult = CheckSuspendResult.FromPluginCheckSuspendResult(checkResultPlugin, plugin);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    WsapmLog.Log.WriteError(String.Format(Resources.Wsapm_Core.PluginManager_ErrorPluginCheckPolicy, plugin.PluginAttribute.Name), ex);
                }
                finally
                {
                    // Make sure that the plugin gets teared down.
                    try
                    {
                        WsapmLog.Log.WriteLine(String.Format(Resources.Wsapm_Core.PluginManager_PluginTearedDown, plugin.PluginAttribute.Name), LogMode.Verbose);
                        plugin.TearDownPlugin();
                    }
                    catch (Exception ex)
                    {
                        WsapmLog.Log.WriteError(String.Format(Resources.Wsapm_Core.PluginManager_ErrorPluginTearDown, plugin.PluginAttribute.Name), ex);
                    }
                }
            }

            return(checkResult);
        }
Example #7
0
        /// <summary>
        /// Checks all available monitoring policies.
        /// </summary>
        /// <returns>The check result as CheckSuspendResult.</returns>
        private CheckSuspendResult CheckPolicies()
        {
            try
            {
                // Check all sources.
                // From these checks that last a shorter time to these that take a longer timer.
                // When standby is suspended, cancel the ResumeAutomaticTimer, so that it can't enable standby when it elapses.
                CheckSuspendResult result = CheckTemporaryUptime();

                if (result.SuspendStandby)
                {
                    // Maybe not needed
                    //ExecuteActionsWhenAtLeastOnePolicySatisfied();
                    return(result);
                }

                result = CheckScheduledUptime();

                if (result.SuspendStandby)
                {
                    // Maybe not needed
                    //ExecuteActionsWhenAtLeastOnePolicySatisfied();
                    return(result);
                }

                result = CheckProcesses();

                if (result.SuspendStandby)
                {
                    ExecuteActionsWhenAtLeastOnePolicySatisfied();
                    return(result);
                }

                result = CheckNetworkAccess();

                if (result.SuspendStandby)
                {
                    ExecuteActionsWhenAtLeastOnePolicySatisfied();
                    return(result);
                }

                result = CheckCpuLoad();

                if (result.SuspendStandby)
                {
                    ExecuteActionsWhenAtLeastOnePolicySatisfied();
                    return(result);
                }

                result = CheckMemoryLoad();

                if (result.SuspendStandby)
                {
                    ExecuteActionsWhenAtLeastOnePolicySatisfied();
                    return(result);
                }

                result = CheckPing();

                if (result.SuspendStandby)
                {
                    ExecuteActionsWhenAtLeastOnePolicySatisfied();
                    return(result);
                }

                result = CheckNetworkLoad();

                if (result.SuspendStandby)
                {
                    ExecuteActionsWhenAtLeastOnePolicySatisfied();
                    return(result);
                }

                result = CheckHddLoad();

                if (result.SuspendStandby)
                {
                    ExecuteActionsWhenAtLeastOnePolicySatisfied();
                    return(result);
                }

                result = CheckPluginPloicies();

                if (result.SuspendStandby)
                {
                    ExecuteActionsWhenAtLeastOnePolicySatisfied();
                    return(result);
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }

            ExecuteActionsWhenNoPolicySatisfied();
            return(new CheckSuspendResult(false, String.Empty));
        }