using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using Watchdog.WatchdogLib.Monitor; using Watchdog.WatchdogLib.WinForms; namespace Watchdog { /// /// WCF Operations Contract in order to allow the command-line process /// to communicate with the running Application's instance /// [ServiceContract] public interface IWatchdog { #region Application State Changes /// /// Starts Monitoring /// [OperationContract] void StartMonitoring(); /// /// Pauses Monitoring /// [OperationContract] void PauseMonitoring(); /// /// Restarts Monitoring /// [OperationContract] void RestartMonitoring(); /// /// Restarts All Monitoring /// [OperationContract] void RestartAllMonitoring(); /// /// Stops Monitoring /// [OperationContract] void StopMonitoring(); /// /// Stops All Monitoring /// [OperationContract] void StopAllMonitoring(); /// /// Tell the Running Application to Reload the Configuration /// [OperationContract] void ReloadConfigurationNextInterval(); #endregion #region Ping /// /// To Check if we can communicate with the other side /// /// PING_RETURN_VALUE [OperationContract] int Ping(); #endregion } /// /// Responsible for the WCF Callbacks /// public class WCFHost : IWatchdog { #region IWatchdog Members /// /// Starts Monitoring /// public void StartMonitoring() { HiddenMainWindow.StartMonitoring(true, true); } /// /// Pauses Monitoring /// public void PauseMonitoring() { HiddenMainWindow.PauseMonitoring(true); } /// /// Restarts Monitoring /// public void RestartMonitoring() { HiddenMainWindow.RestartMonitoring(4, true); } /// /// Restarts All Monitoring /// public void RestartAllMonitoring() { HiddenMainWindow.RestartAllMonitoring(4, true); } /// /// Stops Monitoring /// public void StopMonitoring() { HiddenMainWindow.StopMonitoring(true, false); } /// /// Stops All Monitoring /// public void StopAllMonitoring() { HiddenMainWindow.StopAllMonitoring(true); } /// /// Tell the Running Application to Reload the Configuration /// public void ReloadConfigurationNextInterval() { App.xmlfile.ForceRefreshOnNext_ReadData = true; } /// /// Const Value to return on Ping /// public const int PING_RETURN_VALUE = 10; /// /// To Check if we can communicate with the other side /// /// PING_RETURN_VALUE public int Ping() { return PING_RETURN_VALUE; } #endregion #region Configuration Changes / Display Configuration Statics /// /// Adds a Process from the Configuration and saves the xml /// /// Process Exe File name /// Command Line Prms for Process /// Working Directory /// true if successfull, false otherwise public static bool AddProcess(string ProcessExeFileNameNPath, string CommandlinePrms, string WorkingDir) { WatchdogConfiguration config = App.xmlfile.ReadData(false); bool bSuccess = config.MonitoredProcesses.AddProcessExe(ProcessExeFileNameNPath, CommandlinePrms, WorkingDir); if (bSuccess) App.xmlfile.SaveData(); return bSuccess; } /// /// Removes a Process from the Configuration and saves the xml /// /// Process Exe File name /// Command Line Prms for Process /// true if successfully deleted a process with matching ProcessExeFileNameNPath and CommandlinePrms public static bool RemoveProcess(string ProcessExeFileNameNPath, string CommandlinePrms) { WatchdogConfiguration config = App.xmlfile.ReadData(false); bool bSuccess = config.MonitoredProcesses.RemoveProcessExe(ProcessExeFileNameNPath, CommandlinePrms); if (bSuccess) App.xmlfile.SaveData(); return bSuccess; } /// /// Adds a Service from the configuration and saves the xml /// /// Name of Service /// true if sucessfull, false otherwise public static bool AddService(string ServiceName) { WatchdogConfiguration config = App.xmlfile.ReadData(false); bool bSuccess = config.MonitoredServices.AddServiceExe(ServiceName); if (bSuccess) App.xmlfile.SaveData(); return bSuccess; } /// /// Removes a Service from the configuration and saves the xml /// /// Name of Service /// true if sucessfull, false otherwise public static bool RemoveService(string ServiceName) { WatchdogConfiguration config = App.xmlfile.ReadData(false); bool bSuccess = config.MonitoredServices.RemoveServiceExe(ServiceName); if (bSuccess) App.xmlfile.SaveData(); return bSuccess; } /// /// Returns an array of all Processes and their configurations (in a ';' seperated string) /// /// Process List with configuration public static string[] ShowProcesses() { List ProcessList = new List(); WatchdogConfiguration config = App.xmlfile.ReadData(false); string s = String.Empty; foreach (WatchdogConfiguration.ProcessExe p in config.MonitoredProcesses.ProcessExes) { if (String.IsNullOrEmpty(p.CommandLinePrms) && String.IsNullOrEmpty(p.WorkingDirectory)) s = p.ProcessExeFileNameNPath; else if (!String.IsNullOrEmpty(p.CommandLinePrms) && String.IsNullOrEmpty(p.WorkingDirectory)) s = p.ProcessExeFileNameNPath + ";" + p.CommandLinePrms; else if (!String.IsNullOrEmpty(p.CommandLinePrms) && !String.IsNullOrEmpty(p.WorkingDirectory)) s = p.ProcessExeFileNameNPath + ";" + p.CommandLinePrms + ";" + p.WorkingDirectory; else continue; ProcessList.Add(s); } return ProcessList.ToArray(); } /// /// Returns an array of all Services /// /// Services List public static string[] ShowServices() { List ProcessList = new List(); WatchdogConfiguration config = App.xmlfile.ReadData(false); foreach (WatchdogConfiguration.ServiceExe s in config.MonitoredServices.ServiceExes) ProcessList.Add(s.Name); return ProcessList.ToArray(); } #endregion #region Internal Statics private static ServiceHost _host = null; /// /// Start the WCF Pipe Host (Watchdog) /// internal static void StartHost() { // Make sure it is closed StopHost(); _host = new ServiceHost(typeof(WCFHost), new Uri[] { new Uri("net.pipe://localhost") }); _host.AddServiceEndpoint(typeof(IWatchdog), new NetNamedPipeBinding(), AppResx.GetString("APPLICATION_NAME_SHORT")); try { _host.Open(); } catch (Exception e) { App.log.Fatal("Unable to start WCF Pipe Host", e); MsgBox.ShowFatalError("Unable to start WCF Pipe Host", "Pipe Host Failure", System.Windows.Forms.MessageBoxButtons.OK); App.Current.Shutdown(); } } /// /// Stop the WCF Pipe Host /// internal static void StopHost() { if (_host != null) { _host.Close(); _host = null; } } /// /// Called by Client to communicate with the server /// /// a valid IWatchDog Object or Null if error occured internal static IWatchdog GetWatchDogClientInterface() { // We only should be calling this in CommandLine Mode. if (App.State_SpecialMode_IsCommandLineSet()) { try { IWatchdog watchdoginterface = null; ChannelFactory iwatchdogFactory = new ChannelFactory(new NetNamedPipeBinding(), new EndpointAddress(("net.pipe://localhost/" + AppResx.GetString("APPLICATION_NAME_SHORT")))); watchdoginterface = iwatchdogFactory.CreateChannel(); // Try to ping the object! ~if it fails, target object doesn't exist if (watchdoginterface.Ping() == WCFHost.PING_RETURN_VALUE) return watchdoginterface; } catch (Exception) { // * Ignore this error we don't want to log this* //App.log.Error("Failed to obtain WatchDogClientInterface", e); } } return null; } #endregion } }