/// <summary> /// Initializes a new instance of RemoteStartupRequest /// </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> public RemoteStartupRequest(string moduleName, ModuleStartupMethod method, IModuleProcessInfo mpi) { this.method = method; this.moduleName = moduleName; if (mpi != null) this.processInfo = new ModuleProcessInfo(mpi.ProcessName, mpi.ProgramPath, mpi.ProgramArgs); }
/// <summary> /// Initialzes a new instance of the StartupSequenceExecuter object /// </summary> /// <param name="parent"></param> public StartupSequenceManager(Blackboard parent) { this.parent = parent; this.method = ModuleStartupMethod.KillThenLaunch; this.moduleSequence = new List <string>(20); this.oLock = new Object(); this.processManager = new ProcessManager(parent.Log); }
/// <summary> /// Initializes a new instance of RemoteStartupRequest /// </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> public RemoteStartupRequest(string moduleName, ModuleStartupMethod 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 StartModulesAsync(ModuleStartupMethod method) { if ((this.mainThread != null) && this.mainThread.IsAlive) { return; } this.mainThread = new Thread(new ParameterizedThreadStart(StartModules)); this.mainThread.IsBackground = true; this.mainThread.Start(method); }
/// <summary> /// Starts the specified module /// </summary> /// <param name="module">The module to start</param> /// <param name="method">The method used for startup</param> public void LaunchModule(IModuleClientTcp module, ModuleStartupMethod method) { bool result; if ((method == ModuleStartupMethod.None) || (module == null) || (module.ProcessInfo == null)) { return; } lock (oLock) { if (!module.IsLocal) { result = RemoteStartup(module, method); //if (result && (method == ModuleStartupMethod.LaunchAndWaitReady)) // WaitModuleReady(module); return; } switch (method) { case ModuleStartupMethod.LaunchAlways: LaunchLocalModule(module); break; case ModuleStartupMethod.LaunchAndWaitReady: //if (KillModule(module) && LaunchModule(module)) // WaitModuleReady(module); KillLocalModule(module); LaunchLocalModule(module); break; case ModuleStartupMethod.LaunchIfNotRunning: LaunchLocalModuleIfNotRunning(module); break; case ModuleStartupMethod.KillThenLaunch: if (KillLocalModule(module)) { LaunchLocalModule(module); } break; case ModuleStartupMethod.KillOnly: KillLocalModule(module); break; } } }
/// <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 RemoteStartup(IModuleClientTcp mc, ModuleStartupMethod method) { RemoteStartupRequest request; RemoteStartupResponse response; SocketTcpClient client; AutoResetEvent dataReceivedEvent; string serialized; Log.WriteLine(5, "Starting 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 start module '" + mc.Name + "': unable to connect to remote computer."); return(false); } dataReceivedEvent = new AutoResetEvent(false); client.DataReceived += new TcpDataReceivedEventHandler(delegate(TcpPacket packet) { response = RemoteStartupResponse.FromXml(packet.DataString); dataReceivedEvent.Set(); }); try { request = new RemoteStartupRequest(mc.Name, method, mc.ProcessInfo); serialized = RemoteStartupRequest.ToXml(request); client.Send(serialized); response = null; dataReceivedEvent.WaitOne(10000); if (response == null) { Log.WriteLine(5, "Can not start module '" + mc.Name + "': no response received"); client.Disconnect(); return(false); } if ((response.ModuleName != request.ModuleName) || (response.Method != request.Method)) { Log.WriteLine(5, "Can not start module '" + mc.Name + "': invalid response"); client.Disconnect(); return(false); } if (!response.Success) { Log.WriteLine(5, "Can not start module '" + mc.Name + "': " + response.Message); client.Disconnect(); return(false); } Log.WriteLine(5, "Start module '" + mc.Name + "': Success"); client.Disconnect(); return(true); } catch (Exception ex) { Log.WriteLine(5, "Can not start module '" + mc.Name + "': " + ex.Message); return(false); } finally { if ((client != null) && client.IsConnected) { client.Disconnect(); } if (client.Socket != null) { client.Socket.Close(); } } }
/// <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 StartModules(ModuleStartupMethod method) { IModuleClientTcp mc; bool result; if ((method == ModuleStartupMethod.None) || (moduleSequence.Count < 1)) { return; } lock (oLock) { Parent.Log.WriteLine(4, "Executing startup sequence..."); for (int i = 0; i < moduleSequence.Count && parent.IsRunning; ++i) { if (!Parent.Modules.Contains(moduleSequence[i])) { Parent.Log.WriteLine(5, "Can not start 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 start 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 = RemoteStartup(mc, method); if (result && (method == ModuleStartupMethod.LaunchAndWaitReady)) { WaitModuleReady(mc); } continue; } switch (method) { case ModuleStartupMethod.LaunchAlways: LaunchModule(mc); break; case ModuleStartupMethod.LaunchAndWaitReady: if (KillModule(mc) && LaunchModule(mc)) { WaitModuleReady(mc); } break; case ModuleStartupMethod.LaunchIfNotRunning: LaunchIfNotRunning(mc); break; case ModuleStartupMethod.KillThenLaunch: if (KillModule(mc)) { LaunchModule(mc); } break; case ModuleStartupMethod.KillOnly: KillModule(mc); break; } } Parent.Log.WriteLine(4, "Startup sequence excecution complete!"); } }
/// <summary> /// Initializes a new instance of RemoteStartupRequest /// </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 RemoteStartupResponse(string moduleName, ModuleStartupMethod method, ModuleProcessInfo mpi, bool success, string message) : base(moduleName, method, mpi) { this.success = success; this.message = message; }
/// <summary> /// Initializes a new instance of RemoteStartupRequest /// </summary> public RemoteStartupRequest() { this.method = ModuleStartupMethod.None; this.moduleName = null; this.processInfo = new ModuleProcessInfo(); }
/// <summary> /// Initializes a new instance of RemoteStartupRequest /// </summary> public RemoteStartupRequest() { this.method = ModuleStartupMethod.None; this.moduleName = null; this.processInfo = new ModuleProcessInfo(); }