public int execute(WaitCommand command) { // delay this.delay(); if (this.isFrozen) { this.frozenCommands.Add(command); return(0); } else { Console.WriteLine("Recieved " + command.getType() + " command from " + command.getIssuerId()); System.Threading.Thread.Sleep(command.getMilliseconds()); return(0); } }
public void unfreeze() { Console.WriteLine("Unfreeze"); this.isFrozen = false; for (int i = 0; i < this.frozenCommands.Count; i++) { Command command = this.frozenCommands[i]; if (command.getType() == "CREATE") { CreateCommand c = (CreateCommand)command; this.execute(c); } else if (command.getType() == "LIST") { ListCommand c = (ListCommand)command; this.execute(c); } else if (command.getType() == "CLOSE") { CloseCommand c = (CloseCommand)command; this.execute(c); } else if (command.getType() == "JOIN") { JoinCommand c = (JoinCommand)command; this.execute(c); } else if (command.getType() == "WAIT") { WaitCommand c = (WaitCommand)command; this.execute(c); } } this.frozenCommands = new List <Command>(); }
static void Main(string[] args) { string myId = args[0]; string url = args[1]; string server_url = args[2]; Regex r = new Regex(@"^(?<protocol>\w+)://[^/]+?:(?<port>\d+)?/", RegexOptions.None, TimeSpan.FromMilliseconds(100)); Match m = r.Match(url); int port = Int32.Parse(m.Result("${port}")); myId += "-" + url; TcpChannel channel = new TcpChannel(port); ClientObject client = new ClientObject(); RemotingServices.Marshal(client, "ClientObject", typeof(ClientObject)); ChannelServices.RegisterChannel(channel, false); ServerInterface server = (ServerInterface)Activator.GetObject(typeof(ServerInterface), server_url); if (server == null) { System.Console.WriteLine("Could not locate server"); } else { System.Console.WriteLine("Found"); InstructsParser parser = new InstructsParser(); string clientScript = @"C:\Users\cash\MEIC\Development of Distributed Systems\DAD2019\MeetingsScheduleV1\" + args[3]; string[] lines = File.ReadAllLines(clientScript); foreach (string line in lines) { char[] delimiter = { ' ' }; string[] instructionParts = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); if (instructionParts[0] == "create") { CreateCommand command = parser.parseCreateCommand(instructionParts, myId); command.setIssuerId(myId); Console.WriteLine(command.getType()); CreateRemoteAsyncDelegate RemoteDel = new CreateRemoteAsyncDelegate(server.execute); IAsyncResult RemAr = RemoteDel.BeginInvoke(command, null, null); RemAr.AsyncWaitHandle.WaitOne(); Console.WriteLine(RemoteDel.EndInvoke(RemAr)); // server.execute(command); } else if (instructionParts[0] == "list") { ListCommand command = parser.parseListCommand(instructionParts); command.setIssuerId(myId); Console.WriteLine(command.getType()); ListRemoteAsyncDelegate RemoteDel = new ListRemoteAsyncDelegate(server.execute); IAsyncResult RemAr = RemoteDel.BeginInvoke(command, null, null); RemAr.AsyncWaitHandle.WaitOne(); List <MeetingProposal> proposals = RemoteDel.EndInvoke(RemAr); // List<MeetingProposal> proposals = server.execute(command); foreach (MeetingProposal meeting in proposals) { string closed = "Open"; if (meeting.isClosed()) { closed = "Closed"; } if (meeting.isCancelled()) { closed = "Cancelled"; } Console.WriteLine(meeting.getCoordinator() + " " + meeting.getTopic() + " - " + closed); } } else if (instructionParts[0] == "join") { JoinCommand command = parser.parseJoinCommand(instructionParts); command.setIssuerId(myId); Console.WriteLine(command.getType()); JoinRemoteAsyncDelegate RemoteDel = new JoinRemoteAsyncDelegate(server.execute); IAsyncResult RemAr = RemoteDel.BeginInvoke(command, null, null); RemAr.AsyncWaitHandle.WaitOne(); Console.WriteLine(RemoteDel.EndInvoke(RemAr)); // server.execute(command); } else if (instructionParts[0] == "close") { CloseCommand command = parser.parseCloseCommand(instructionParts); command.setIssuerId(myId); Console.WriteLine(command.getType()); CloseRemoteAsyncDelegate RemoteDel = new CloseRemoteAsyncDelegate(server.execute); IAsyncResult RemAr = RemoteDel.BeginInvoke(command, null, null); RemAr.AsyncWaitHandle.WaitOne(); Console.WriteLine(RemoteDel.EndInvoke(RemAr)); //server.execute(command); } else if (instructionParts[0] == "wait") { WaitCommand command = parser.parseWaitCommand(instructionParts); command.setIssuerId(myId); Console.WriteLine(command.getType()); WaitRemoteAsyncDelegate RemoteDel = new WaitRemoteAsyncDelegate(server.execute); IAsyncResult RemAr = RemoteDel.BeginInvoke(command, null, null); RemAr.AsyncWaitHandle.WaitOne(); Console.WriteLine(RemoteDel.EndInvoke(RemAr)); //server.execute(command); } else { NotFoundCommand command = new NotFoundCommand(); Console.WriteLine(command.getType()); } } } System.Console.ReadLine(); }