public void informOtherServers(Command command) { Console.WriteLine("Inform other servers"); // passive replication strategy if (command.isSentByClient()) { if (command.getType() == "CREATE" || command.getType() == "JOIN" || command.getType() == "CLOSE") { foreach (string serverURL in this.servers) { // TODO Asynchronous calls maybe a better idea if (serverURL != this.url) { try { ServerInterface server = (ServerInterface)Activator.GetObject(typeof(ServerInterface), serverURL); switch (command.getType()) { case "CREATE": command.setSentByClient(false); CreateRemoteAsyncDelegate RemoteDel = new CreateRemoteAsyncDelegate(server.execute); IAsyncResult RemAr = RemoteDel.BeginInvoke((CreateCommand)command, null, null); // server.execute((CreateCommand)command); break; case "JOIN": command.setSentByClient(false); JoinRemoteAsyncDelegate RemoteDel2 = new JoinRemoteAsyncDelegate(server.execute); IAsyncResult RemAr2 = RemoteDel2.BeginInvoke((JoinCommand)command, null, null); // server.execute((JoinCommand)command); break; case "CLOSE": command.setSentByClient(false); CloseRemoteAsyncDelegate RemoteDel3 = new CloseRemoteAsyncDelegate(server.execute); IAsyncResult RemAr3 = RemoteDel3.BeginInvoke((CloseCommand)command, null, null); // server.execute((CloseCommand)command); break; default: break; } } catch (Exception) { Console.WriteLine(serverURL + " FAULT"); // Faults tolerance TODO } } } } } }
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(); }