Example #1
0
        /// <summary>
        /// Serializes a RemoteShutdownResponse object to xml
        /// </summary>
        /// <param name="rsr">Object to serialize</param>
        /// <returns>string containing serialized xml object</returns>
        public static string ToXml(RemoteCheckResponse 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(RemoteCheckResponse));
            serializer.Serialize(writer, rsr, ns);
            writer.Flush();
            writer.Close();
            return(sb.ToString());
        }
Example #2
0
        /// <summary>
        /// Request the number of running instances of a module on remote computer
        /// </summary>
        /// <param name="mc">The module to check</param>
        /// <returns>The number of running instances of the module</returns>
        private int RemoteCheck(IModuleClientTcp mc)
        {
            RemoteCheckRequest  request;
            RemoteCheckResponse response;
            SocketTcpClient     client;
            AutoResetEvent      dataReceivedEvent;
            string serialized;

            Log.WriteLine(5, "Checking instances of 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 check module '" + mc.Name + "': unable to connect to remote computer.");
                return(-1);
            }

            dataReceivedEvent    = new AutoResetEvent(false);
            client.DataReceived += new TcpDataReceivedEventHandler(delegate(TcpPacket packet)
            {
                response = RemoteCheckResponse.FromXml(packet.DataString);
                dataReceivedEvent.Set();
            });

            try
            {
                request    = new RemoteCheckRequest(mc.Name, mc.ProcessInfo);
                serialized = RemoteCheckRequest.ToXml(request);
                client.Send(serialized);
                response = null;
                dataReceivedEvent.WaitOne(10000);
                if (response == null)
                {
                    Log.WriteLine(5, "Can't check module '" + mc.Name + "': no response received");
                    client.Disconnect();
                    return(-1);
                }
                if (response.ModuleName != request.ModuleName)
                {
                    Log.WriteLine(5, "Can't check module '" + mc.Name + "': invalid response");
                    client.Disconnect();
                    return(-1);
                }
                if (!response.Success)
                {
                    Log.WriteLine(5, "Can't check module '" + mc.Name + "': " + response.Message);
                    client.Disconnect();
                    return(-1);
                }
                Log.WriteLine(5, "Check module '" + mc.Name + "': Success");
                client.Disconnect();
                return(response.Instances);
            }
            catch (Exception ex)
            {
                Log.WriteLine(5, "Can't check module '" + mc.Name + "': " + ex.Message);
                return(-1);
            }
            finally
            {
                if ((client != null) && client.IsConnected)
                {
                    client.Disconnect();
                }
                if (client.Socket != null)
                {
                    client.Socket.Close();
                }
            }
        }
        /// <summary>
        /// Serializes a RemoteShutdownResponse object to xml
        /// </summary>
        /// <param name="rsr">Object to serialize</param>
        /// <returns>string containing serialized xml object</returns>
        public static string ToXml(RemoteCheckResponse 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(RemoteCheckResponse));
            serializer.Serialize(writer, rsr, ns);
            writer.Flush();
            writer.Close();
            return sb.ToString();
        }