Example #1
0
        /// <summary>
        /// Serializes a RemoteStartupRequest object to xml
        /// </summary>
        /// <param name="rsr">Object to serialize</param>
        /// <returns>string containing serialized xml object</returns>
        public static string ToXml(RemoteStartupRequest 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(RemoteStartupRequest));
            serializer.Serialize(writer, rsr, ns);
            writer.Flush();
            writer.Close();
            return(sb.ToString());
        }
Example #2
0
        /// <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>
 /// Initializes a new instance of RemoteStartupRequest
 /// </summary>
 /// <param name="request">RemoteStartupRequest 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 RemoteStartupResponse(RemoteStartupRequest request, bool success, string message)
     : base(request.ModuleName, request.Method, request.ProcessInfo)
 {
     this.success = success;
     this.message = message;
 }
        /// <summary>
        /// Serializes a RemoteStartupRequest object to xml
        /// </summary>
        /// <param name="rsr">Object to serialize</param>
        /// <returns>string containing serialized xml object</returns>
        public static string ToXml(RemoteStartupRequest 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(RemoteStartupRequest));
            serializer.Serialize(writer, rsr, ns);
            writer.Flush();
            writer.Close();
            return sb.ToString();
        }