// Public Methods public int execute(CreateCommand command) { // delay this.delay(); if (this.isFrozen) { this.frozenCommands.Add(command); while (true) { // freeze } return(0); } else { Console.WriteLine("Recieved " + command.getType() + " command from " + command.getIssuerId()); this.createMeeting(command.getIssuerId(), command.getMeetingProposal()); 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>(); }
public CreateCommand parseCreateCommand(string[] instruction, string issuerId) { string topic = instruction[1]; int min_attendees = Int32.Parse(instruction[2]); int nr_slots = Int32.Parse(instruction[3]); int nr_invitees = Int32.Parse(instruction[4]); List <Slot> slots = new List <Slot>(); for (int i = 0; i < nr_slots; i++) { string slot_info = instruction[i + 5]; char[] delimiter = { ',' }; string[] slot_infos = slot_info.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); string location = slot_infos[0]; DateTime date = DateTime.Parse(slot_infos[1]); slots.Add(new Slot(location, date)); } List <string> invitees = null; if (nr_invitees > 0) { invitees = new List <string>(); for (int i = 0; i < nr_invitees; i++) { invitees.Add(instruction[i + nr_slots + 5]); } } CreateCommand command = new CreateCommand(topic, min_attendees, nr_slots, nr_invitees, slots, invitees); command.getMeetingProposal().setCoordinator(issuerId); return(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); } }