Exemplo n.º 1
0
        public void Client(string client_id, string URL, string script_file)
        {
            if (processNames.ContainsKey(client_id))
            {
                Console.WriteLine("Client ID already exists");
                return;
            }
            string urlPcs = URL.Split(':')[0] + ":" + URL.Split(':')[1] + ":10000/pcs";

            IPCS pcs = null;

            pcs = (IPCS)Activator.GetObject(typeof(IPCS), urlPcs);
            string type = pcs.Client(URL, script_file, XLFEcounter);

            if (type == "SMR")
            {
                processNames.Add(client_id, new Process(URL, Process.Type.CLIENT_SMR));
            }
            else if (type == "XL")
            {
                processNames.Add(client_id, new Process(URL, Process.Type.CLIENT_XL));
                XLFEcounter++;
            }
        }
Exemplo n.º 2
0
        private void RunScriptButton_Click(object sender, EventArgs e)
        {
            if (scriptBox.TextLength == 0)
            {
                return;
            }

            outputBox.Text += "Reading " + scriptBox.Text + "...\r\n";
            try
            {
                IPCS     pcs       = null;
                string[] fileLines = File.ReadAllLines(scriptBox.Text);
                foreach (string line in fileLines)
                {
                    string[] commandLine = line.Split(' ');
                    if (commandLine.Length <= 0)
                    {
                        continue;
                    }

                    outputBox.Text += "Running command " + commandLine[0] + "\r\n";
                    switch (commandLine[0])
                    {
                    case "PCS":
                        if (commandLine.Length == 2)
                        {
                            pcs = (IPCS)Activator.GetObject(typeof(IPCS), commandLine[1]);
                        }
                        else
                        {
                            outputBox.Text += "ERROR - PCS usage: PCS <pcs_url>\r\n";
                            return;
                        }
                        break;

                    case "Server":
                        if (commandLine.Length == 6)
                        {
                            // find the pcs with a matching hostname
                            pcs = GetPCSFromHostname(commandLine[2]);
                            try
                            {
                                pcs.Server(commandLine[1], commandLine[2], Int32.Parse(commandLine[3]),
                                           Int32.Parse(commandLine[4]), Int32.Parse(commandLine[5]));
                                servers.Add(commandLine[1], commandLine[2]);
                            }
                            catch (Exception)
                            {
                                outputBox.Text += "ERROR - must be connected to PCS\r\n";
                            }
                        }
                        else
                        {
                            outputBox.Text +=
                                "ERROR - Server usage: Server <server_id> <URL> <max_faults> <min_delay> <max_delay>\r\n";
                            return;
                        }
                        break;

                    case "Client":
                        if (commandLine.Length == 5)
                        {
                            // find the pcs with a matching hostname
                            pcs = GetPCSFromHostname(commandLine[2]);
                            try
                            {
                                pcs.Client(commandLine[1], commandLine[2], commandLine[3], commandLine[4]);
                                clients.Add(commandLine[1], commandLine[2]);
                            }
                            catch (Exception)
                            {
                                outputBox.Text += "ERROR - must be connected to PCS\r\n";
                            }
                        }
                        else
                        {
                            outputBox.Text +=
                                "ERROR - Client usage: Client <username> <client_URL> <server_URL> <script_file>\r\n";
                            return;
                        }
                        break;

                    case "AddRoom":
                        if (commandLine.Length == 4)
                        {
                            foreach (var server in servers)
                            {
                                // server.Key -> id
                                // server.Value -> url

                                IServer serverObj = (IServer)Activator.GetObject(typeof(IServer), server.Value);

                                try
                                {
                                    AddRoomAsync async    = new AddRoomAsync(serverObj.AddRoom);
                                    IAsyncResult asyncRes = async.BeginInvoke(commandLine[1], Int32.Parse(commandLine[2]), commandLine[3], null, null);
                                    asyncRes.AsyncWaitHandle.WaitOne();
                                    async.EndInvoke(asyncRes);
                                    outputBox.Text += $"Added room <{roomLocation.Text},{roomName.Text}> to server <{server.Key},{server.Value}> \r\n";
                                }
                                catch (SocketException)
                                {
                                    outputBox.Text += $"[Socket Exception] Could not locate <{ server.Key},{ server.Value}> \r\n";
                                }
                            }
                        }
                        else
                        {
                            outputBox.Text +=
                                "ERROR - AddRoom usage: AddRoom <location> <capacity> <room_name>\r\n";
                            return;
                        }
                        break;

                    case "Status":
                        int i = 0;
                        EventWaitHandle[] handles = new EventWaitHandle[servers.Count + clients.Count];
                        foreach (string server_url in servers.Values)
                        {
                            Thread task = new Thread(() =>
                            {
                                ((IServer)Activator.GetObject(typeof(IServer), server_url)).Status();
                                handles[i++].Set();
                            });
                        }

                        foreach (string client_url in clients.Values)
                        {
                            Thread task = new Thread(() =>
                            {
                                ((IServer)Activator.GetObject(typeof(IServer), client_url)).Status();
                                handles[i++].Set();
                            });
                        }

                        //WaitHandle.WaitAll(handles);
                        break;

                    case "Crash":
                        if (commandLine.Length == 2)
                        {
                            if (servers.TryGetValue(commandLine[1], out string server_url))
                            {
                                try
                                {
                                    ((IServer)Activator.GetObject(typeof(IServer), server_url)).Crash();
                                }
                                catch (SocketException)
                                {
                                    outputBox.Text += $"[Socket Exception] Could not locate <{commandLine[1]},{server_url}>\r\n";
                                }
                            }
                            else
                            {
                                outputBox.Text += "ERROR - server does not exist\r\n";
                                return;
                            }
                        }
                        else
                        {
                            outputBox.Text +=
                                "ERROR - Crash usage: Crash <server_id>\r\n";
                            return;
                        }
                        break;

                    case "Freeze":
                        if (commandLine.Length == 2)
                        {
                            if (servers.TryGetValue(commandLine[1], out string server_url))
                            {
                                try
                                {
                                    ((IServer)Activator.GetObject(typeof(IServer), server_url)).Freeze();
                                }
                                catch (SocketException)
                                {
                                    outputBox.Text += $"[Socket Exception] Could not locate <{commandLine[1]},{server_url}>\r\n";
                                }
                            }
                            else
                            {
                                outputBox.Text += "ERROR - server does not exist\r\n";
                                return;
                            }
                        }
                        else
                        {
                            outputBox.Text +=
                                "ERROR - Freeze usage: Freeze <server_id>\r\n";
                            return;
                        }
                        break;

                    case "Unfreeze":
                        if (commandLine.Length == 2)
                        {
                            if (servers.TryGetValue(commandLine[1], out string server_url))
                            {
                                try
                                {
                                    ((IServer)Activator.GetObject(typeof(IServer), server_url)).Unfreeze();
                                }
                                catch (SocketException)
                                {
                                    outputBox.Text += $"[Socket Exception] Could not locate <{commandLine[1]},{server_url}>\r\n";
                                }
                            }
                            else
                            {
                                outputBox.Text += "ERROR - server does not exist\r\n";
                                return;
                            }
                        }
                        else
                        {
                            outputBox.Text +=
                                "ERROR - Unfreeze usage: Unfreeze <server_id>\r\n";
                            return;
                        }
                        break;

                    case "Wait":
                        if (commandLine.Length == 2)
                        {
                            Thread.Sleep(Int32.Parse(commandLine[1]));
                        }
                        else
                        {
                            outputBox.Text += "ERROR - Wait usage: Wait <ms>\r\n";
                            return;
                        }
                        break;

                    default:
                        outputBox.Text += "Command not recognized\r\n";
                        break;
                    }
                }
            }
            catch (IOException)
            {
                outputBox.Text += "ERROR - Could not read file " + scriptBox.Text + "\r\n";
            }
            catch (FormatException formatException)
            {
                outputBox.Text += $"ERROR - Could not parse value: ${formatException}\r\n";
            }
            catch (SocketException)
            {
                outputBox.Text += "ERROR - Could not connecto to PCS\r\n";
            }
        }