private static bool TryParseCreateServerCommand(string[] arguments, out ICommand command) { command = null; if (arguments.Length != 4) { return(false); } string serverId = arguments[0]; string URL = arguments[1]; if (!int.TryParse(arguments[2], out int minDelay)) { return(false); } if (!int.TryParse(arguments[3], out int maxDelay)) { return(false); } if (!HttpURLs.TryParseHostAndPort(URL, out string host, out int port)) { return(false); } command = new CreateServerCommand { ServerId = serverId, Host = host, Port = port, MinDelay = minDelay, MaxDelay = maxDelay }; return(true); }
private static bool TryParseCreateClientCommand(string[] arguments, out ICommand command) { command = null; if (arguments.Length != 3) { return(false); } string username = arguments[0]; string URL = arguments[1]; string scriptFile = arguments[2]; if (!HttpURLs.TryParseHostAndPort(URL, out string host, out int port)) { return(false); } command = new CreateClientCommand { Username = username, Host = host, Port = port, ScriptFile = scriptFile }; return(true); }
private static void RunAdvancedVersion(ServerConfiguration serverConfig, bool file) { AdvancedGrpcMessageLayer.SetContext(serverConfig); AdvancedPartitionsDB partitionsDB = new AdvancedPartitionsDB( serverConfig.ServerId, HttpURLs.FromHostAndPort(serverConfig.Host, serverConfig.Port)); if (file) { if (!partitionsDB.ConfigurePartitions(serverConfig.Filename)) { Console.Error.WriteLine("Couldn't configure server from file."); DisplayAdvacnedFileSyntax(); } } AdvancedReplicationService service = new AdvancedReplicationService(partitionsDB, serverConfig); service.Bind(); ReliableBroadcastLayer.Instance.Start(); Console.WriteLine( "[{0}] Server {1} running at {2} with advanced version", DateTime.Now.ToString("HH:mm:ss"), serverConfig.ServerId, serverConfig.Url); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); ReliableBroadcastLayer.Instance.Shutdown(); }
private static void RunSimpleVersion(ServerConfiguration serverConfig, bool file) { SimpleGrpcMessageLayer.SetContext(serverConfig); // Add self id so that server knows itself SimplePartitionsDB partitionsDB = new SimplePartitionsDB( serverConfig.ServerId, HttpURLs.FromHostAndPort(serverConfig.Host, serverConfig.Port)); if (file) { if (!partitionsDB.ConfigurePartitions(serverConfig.Filename)) { Console.Error.WriteLine("Couldn't configure server from file."); DisplaySimpleFileSyntax(); } } ReplicationService service = new ReplicationService(partitionsDB, serverConfig); // Bind handlers for messages service.Bind(); FailureDetectionLayer.Instance.Start(); Console.WriteLine( "[{0}] Server {1} running at {2} with simple version", DateTime.Now.ToString("HH:mm:ss"), serverConfig.ServerId, serverConfig.Url); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); FailureDetectionLayer.Instance.Shutdown(); }
public void OnCreateClientCommand(CreateClientCommand command) { CreatePartitionsIfRequired(); string username = command.Username; string url = HttpURLs.FromHostAndPort(command.Host, command.Port); // Check if client already exists if (!nameServiceDB.TryAddClient(username, url)) { Console.Error.WriteLine("Client with username {0} already exists", username); return; } PCSConnection connection = new PCSConnection(command.Host); connection.CreateClientAsync( username, command.Port, command.ScriptFile, nameServiceDB.ListServers(), config.Version) .ContinueWith(antecedent => { if (antecedent.Result) { Console.WriteLine( "[{0}] Client started at {1}:{2}", DateTime.Now.ToString("HH:mm:ss"), command.Host, command.Port); } else { // Remove inserted username if operation failed nameServiceDB.RemoveClient(username); } }); }
public void OnCreateServerCommand(CreateServerCommand command) { string serverId = command.ServerId; string url = HttpURLs.FromHostAndPort(command.Host, command.Port); // Check if server already exists if (!nameServiceDB.TryAddServer(serverId, url)) { Console.Error.WriteLine("Server with id {0} already exists", serverId); return; } PCSConnection connection = new PCSConnection(command.Host); connection.CreateServerAsync( serverId, command.Port, command.MinDelay, command.MaxDelay, config.Version) .ContinueWith(antecedent => { if (antecedent.Result) { Console.WriteLine( "[{0}] Server started at {1}:{2}", DateTime.Now.ToString("HH:mm:ss"), command.Host, command.Port); } else { // Remove inserted id if operation failed nameServiceDB.RemoveServer(serverId); } }); }