public int execute(JoinCommand command) { // delay this.delay(); if (this.isFrozen) { this.frozenCommands.Add(command); while (true) { // freeze } return(0); } else { Console.WriteLine("Recieved " + command.getType() + "-" + command.getSequenceNumber() + " command from " + command.getIssuerId()); MeetingProposal meeting = this.getMeetingByTopic(command.getTopic()); if (meeting == null) { Console.WriteLine("Meeting {0} not found, Contacting other servers ...", command.getTopic()); meeting = this.restoreLostMeeting(command.getTopic()); // Console.WriteLine("Meeting {0} not found, Waiting ..."); // Thread.Sleep(2000); if (meeting == null) { Console.WriteLine("Meeting {0} not found", command.getTopic()); return(0); } } if (meeting.isClosed() || meeting.isCancelled()) { // a client can join a closed meeting if it was joined before the closing momment if (command.getTimestamp().CompareTo(meeting.getClosingTimestamp()) < 0) { meeting.open(); this.joinMeeting(command.getIssuerId(), meeting, command.getDesiredSlots()); meeting.close(); } } else { this.joinMeeting(command.getIssuerId(), meeting, command.getDesiredSlots()); } this.informOtherServers(command); return(1); } }
public void unfreeze() { Console.WriteLine("Unfreeze"); this.isFrozen = false; // avoid executing repeated commands SortedSet <Command> executedCommands = new SortedSet <Command>(); for (int i = 0; i < this.frozenCommands.Count; i++) { Command command = this.frozenCommands[i]; if (executedCommands.Contains(command)) { Console.WriteLine(command.commandId() + " is duplicated, will not be executed"); continue; } 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); } executedCommands.Add(command); } this.frozenCommands = new List <Command>(); }
static void Main(string[] args) { try { // Client Information string username = args[0]; string client_url = args[1]; string server_url = args[2]; // Start Connection Regex r = new Regex(@"^(?<protocol>\w+)://[^/]+?:(?<port>\d+)?/", RegexOptions.None, TimeSpan.FromMilliseconds(100)); Match m = r.Match(client_url); int port = Int32.Parse(m.Result("${port}")); BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider(); IDictionary props = new Hashtable(); props["port"] = port; props["timeout"] = 5000; // in milliseconds TcpChannel channel = new TcpChannel(props, null, provider); //TcpChannel channel = new TcpChannel(port); ClientObject client = new ClientObject(); client.setUsername(username); client.setUrl(client_url); client.setPreferedServerUrl(server_url); RemotingServices.Marshal(client, "ClientObject", typeof(ClientObject)); ChannelServices.RegisterChannel(channel, false); // Read Commands int sequeneNumber = 0; InstructsParser parser = new InstructsParser(); string clientScript = args[3]; string[] lines = File.ReadAllLines(clientScript); foreach (string line in lines) { if (args.Length == 5) { Console.WriteLine("Press any key to execute next step: " + line); Console.ReadLine(); } ServerInterface server = client.getServer(server_url); if (server == null) { Console.WriteLine("Server is unreachable"); return; } server.addClient(client_url); char[] delimiter = { ' ' }; string[] instructionParts = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); string myId = username + "-" + client_url; if (instructionParts[0] == "create") { CreateCommand command = parser.parseCreateCommand(instructionParts, myId); command.setIssuerId(myId); command.setSentByClient(true); command.setSequenceNumber(sequeneNumber); command.setTimestamp(DateTime.Now); sequeneNumber++; Console.WriteLine(command.getType() + " - " + command.getSequenceNumber()); int res = -1; while (res == -1) { try { res = server.execute(command); } catch (System.Net.Sockets.SocketException) { Console.WriteLine("Timeout: server " + client.getPreferedServerUrl() + " seems to be frozen, trying another server"); server = client.getServer("ANOTHER_SERVER"); } } Console.WriteLine("Result: {0}", res); // gossip List <string> clients = client.getClientsForGossip(); client.gossip(command.getMeetingProposal(), clients); } else if (instructionParts[0] == "list") { ListCommand command = parser.parseListCommand(instructionParts); command.setIssuerId(myId); command.setSentByClient(true); command.setSequenceNumber(sequeneNumber); command.setTimestamp(DateTime.Now); sequeneNumber++; // lock it Console.WriteLine(command.getType()); List <MeetingProposal> proposals = null; while (proposals == null) { try { proposals = server.execute(command); } catch (System.Net.Sockets.SocketException) { Console.WriteLine("Timeout: server " + client.getPreferedServerUrl() + " seems to be frozen, trying another server"); server = client.getServer("ANOTHER_SERVER"); } } client.listMeetings(proposals); } else if (instructionParts[0] == "join") { JoinCommand command = parser.parseJoinCommand(instructionParts); command.setIssuerId(myId); command.setSentByClient(true); command.setSequenceNumber(sequeneNumber); command.setTimestamp(DateTime.Now); sequeneNumber++; // lock it Console.WriteLine(command.getType()); int res = -1; while (res == -1) { try { res = server.execute(command); } catch (System.Net.Sockets.SocketException) { Console.WriteLine("Timeout: server " + client.getPreferedServerUrl() + " seems to be frozen, trying another server"); server = client.getServer("ANOTHER_SERVER"); } } Console.WriteLine("Result: {0}", res); } else if (instructionParts[0] == "close") { CloseCommand command = parser.parseCloseCommand(instructionParts); command.setIssuerId(myId); command.setSentByClient(true); command.setSequenceNumber(sequeneNumber); command.setTimestamp(DateTime.Now); sequeneNumber++; // lock it Console.WriteLine(command.getType()); int res = -1; while (res == -1) { try { res = server.execute(command); } catch (System.Net.Sockets.SocketException) { Console.WriteLine("Timeout: server " + client.getPreferedServerUrl() + " seems to be frozen, trying another server"); server = client.getServer("ANOTHER_SERVER"); } } Console.WriteLine("Result: {0}", res); } else if (instructionParts[0] == "wait") { WaitCommand command = parser.parseWaitCommand(instructionParts); command.setIssuerId(myId); command.setSentByClient(true); command.setSequenceNumber(sequeneNumber); command.setTimestamp(DateTime.Now); sequeneNumber++; // lock it Console.WriteLine(command.getType()); int res = -1; while (res == -1) { try { res = server.execute(command); } catch (System.Net.Sockets.SocketException) { Console.WriteLine("Timeout: server " + client.getPreferedServerUrl() + " seems to be frozen, trying another server"); server = client.getServer("ANOTHER_SERVER"); } } Console.WriteLine("Result: {0}", res); } else { NotFoundCommand command = new NotFoundCommand(); Console.WriteLine(command.getType()); } } Console.ReadLine(); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.ToString()); Console.WriteLine(e.StackTrace); // diagnostic: to see the error before exiting System.Threading.Thread.Sleep(60000); } }