/// <summary> /// Serializes a RemoteShutdownRequest object to xml /// </summary> /// <param name="rsr">Object to serialize</param> /// <returns>string containing serialized xml object</returns> public static string ToXml(RemoteShutdownRequest rsr) { XmlSerializer serializer; XmlWriter writer; XmlWriterSettings settings; XmlSerializerNamespaces ns; System.Text.StringBuilder sb; sb = new System.Text.StringBuilder(); ns = new XmlSerializerNamespaces(); ns.Add(String.Empty, String.Empty); settings = new XmlWriterSettings(); settings.Encoding = System.Text.Encoding.UTF8; settings.Indent = false; writer = XmlWriter.Create(sb, settings); serializer = new XmlSerializer(typeof(RemoteShutdownRequest)); serializer.Serialize(writer, rsr, ns); writer.Flush(); writer.Close(); return(sb.ToString()); }
/// <summary> /// Initializes a new instance of RemoteShutdownResponse /// </summary> /// <param name="request">RemoteShutdownRequest object to create the response from</param> /// <param name="success">Indicates if the request succedeed or not</param> /// <param name="message">Custom response in case of failure</param> public RemoteCheckResponse(RemoteShutdownRequest request, int instances, string message) : base(request.ModuleName, request.ProcessInfo) { this.message = message; }
/// <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="request">RemoteShutdownRequest object to create the response from</param> /// <param name="success">Indicates if the request succedeed or not</param> /// <param name="message">Custom response in case of failure</param> public RemoteShutdownResponse(RemoteShutdownRequest request, bool success, string message) : base(request.ModuleName, request.Method, request.ProcessInfo) { this.success = success; this.message = message; }