/// <summary> /// Executes the startup sequence of the registered modules with the specified method /// </summary> /// <param name="method">The method used for startup</param> public void ShutdownModules(ModuleShutdownMethod method) { IModuleClientTcp mc; bool result; if ((method == ModuleShutdownMethod.None) || (moduleSequence.Count < 1)) { return; } lock (oLock) { Parent.Log.WriteLine(4, "Executing shutdown sequence..."); for (int i = 0; i < moduleSequence.Count; ++i) { if (!Parent.Modules.Contains(moduleSequence[i])) { Parent.Log.WriteLine(5, "Can not shutdown module '" + moduleSequence[i] + "': The module does not exist."); continue; } mc = Parent.Modules[moduleSequence[i]] as IModuleClientTcp; if (!mc.Enabled) { Parent.Log.WriteLine(5, "Can not shutdown module '" + moduleSequence[i] + "': The module is disabled."); continue; } if (mc == null) { continue; } if (!mc.IsLocal) //if (true) { //Parent.Log.WriteLine(5, "Can not start module '" + moduleSequence[i] + "': unable to start remote process."); result = RemoteShutdown(mc, method); continue; } switch (method) { case ModuleShutdownMethod.CloseOnly: CloseModule(mc); break; case ModuleShutdownMethod.KillOnly: KillModule(mc); break; case ModuleShutdownMethod.CloseThenKill: if (!CloseModule(mc)) { KillModule(mc); } break; } } Parent.Log.WriteLine(4, "Shutdown sequence excecution complete!"); } }
/// <summary> /// Initialzes a new instance of the ShutdownSequenceManager object /// </summary> /// <param name="parent"></param> public ShutdownSequenceManager(Blackboard parent) { this.parent = parent; this.method = ModuleShutdownMethod.CloseThenKill; this.moduleSequence = new List <string>(20); this.oLock = new Object(); this.processManager = new ProcessManager(parent.Log); }
/// <summary> /// Initializes a new instance of ModuleShutdownMethod /// </summary> /// <param name="moduleName">Module name</param> /// <param name="method">The method for shutdown the modules</param> /// <param name="mpi">Data about the program asociated to this module</param> public RemoteShutdownRequest(string moduleName, ModuleShutdownMethod method, IModuleProcessInfo mpi) { this.method = method; this.moduleName = moduleName; if (mpi != null) { this.processInfo = new ModuleProcessInfo(mpi.ProcessName, mpi.ProgramPath, mpi.ProgramArgs); } }
/// <summary> /// Executes the startup sequence of the registered modules with the specified method /// </summary> /// <param name="method">The method used for startup</param> public void ShutdownModulesAsync(ModuleShutdownMethod method) { if ((this.mainThread != null) && this.mainThread.IsAlive) { return; } this.mainThread = new Thread(new ParameterizedThreadStart(ShutdownModules)); this.mainThread.IsBackground = true; this.mainThread.Start(method); }
/// <summary> /// Shutsdown the specified module /// </summary> /// <param name="module">The module to shutdown</param> /// <param name="method">The method used for startup</param> public void ShutdownModule(IModuleClientTcp module, ModuleShutdownMethod method) { bool result; if ((method == ModuleShutdownMethod.None) || (module == null) || (module.ProcessInfo == null)) { return; } lock (oLock) { Log.WriteLine(4, "Executing shutdown sequence..."); if (!module.IsLocal) { result = RemoteShutdown(module, method); return; } switch (method) { case ModuleShutdownMethod.CloseOnly: CloseLocalModule(module); break; case ModuleShutdownMethod.KillOnly: KillLocalModule(module); break; case ModuleShutdownMethod.CloseThenKill: if (!CloseLocalModule(module)) { KillLocalModule(module); } break; } Log.WriteLine(4, "Shutdown sequence excecution complete!"); } }
/// <summary> /// Request to execute the module startup on remote computer /// </summary> /// <param name="mc">The module to start</param> /// <param name="method">The startup sequence method</param> private bool RemoteShutdown(IModuleClientTcp mc, ModuleShutdownMethod method) { RemoteShutdownRequest request; RemoteShutdownResponse response; SocketTcpClient client; AutoResetEvent dataReceivedEvent; string serialized; Log.WriteLine(5, "Shutting down module '" + mc.Name + "': on remote computer."); client = null; foreach (IPAddress ip in mc.ServerAddresses) { client = new SocketTcpClient(ip, 2300); if (client.TryConnect()) { break; } } if ((client == null) || !client.IsConnected) { Log.WriteLine(5, "Can not shutdown module '" + mc.Name + "': unable to connect to remote computer."); return(false); } dataReceivedEvent = new AutoResetEvent(false); client.DataReceived += new TcpDataReceivedEventHandler(delegate(TcpPacket packet) { response = RemoteShutdownResponse.FromXml(packet.DataString); dataReceivedEvent.Set(); }); try { request = new RemoteShutdownRequest(mc.Name, method, mc.ProcessInfo); serialized = RemoteShutdownRequest.ToXml(request); client.Send(serialized); response = null; dataReceivedEvent.WaitOne(10000); if (response == null) { Log.WriteLine(5, "Can not shutdown module '" + mc.Name + "': no response received"); client.Disconnect(); return(false); } if ((response.ModuleName != request.ModuleName) || (response.Method != request.Method)) { Log.WriteLine(5, "Can not shutdown module '" + mc.Name + "': invalid response"); client.Disconnect(); return(false); } if (!response.Success) { Log.WriteLine(5, "Can not shutdown module '" + mc.Name + "': " + response.Message); client.Disconnect(); return(false); } Log.WriteLine(5, "Shutdown module '" + mc.Name + "': Success"); client.Disconnect(); return(true); } catch (Exception ex) { Log.WriteLine(5, "Can not shutdown module '" + mc.Name + "': " + ex.Message); return(false); } finally { if ((client != null) && client.IsConnected) { client.Disconnect(); } if (client.Socket != null) { client.Socket.Close(); } } }
/// <summary> /// Initializes a new instance of RemoteShutdownResponse /// </summary> /// <param name="moduleName">Module name</param> /// <param name="method">The method of startup sequence for modules</param> /// <param name="mpi">Data about the program asociated to this module</param> /// <param name="success">Indicates if the request succedeed or not</param> /// <param name="message">Custom response in case of failure</param> public RemoteShutdownResponse(string moduleName, ModuleShutdownMethod method, ModuleProcessInfo mpi, bool success, string message) : base(moduleName, method, mpi) { this.success = success; this.message = message; }
/// <summary> /// Initializes a new instance of ModuleShutdownMethod /// </summary> public RemoteShutdownRequest() { this.method = ModuleShutdownMethod.None; this.moduleName = null; this.processInfo = new ModuleProcessInfo(); }