private void AddRoomButton_Click(object sender, EventArgs e) { if (roomLocation.Text.Length == 0 || this.roomCapacity.Text.Length == 0 || roomName.Text.Length == 0) { return; } int roomCapacity = 0; try { roomCapacity = int.Parse(this.roomCapacity.Text); } catch (FormatException) { MessageBox.Show("Room capacity needs to be numeric", "Format Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (OverflowException) { MessageBox.Show("Room capacity overflowed", "Overflow Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } 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(roomLocation.Text, roomCapacity, roomName.Text, 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) { MessageBox.Show($"Could not locate <{server.Key},{server.Value}>", "Socket Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } } roomLocation.Text = ""; this.roomCapacity.Text = ""; roomName.Text = ""; addRoomBox.Enabled = true; }
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"; } }