private void HandleClientCommand(string[] args) { if (args.Length != 1 + 3) { this.Form.Error("Client: wrong number of arguments"); goto ClientUsage; } string username = args[1]; string url = args[2]; string scriptFile = args[3]; if (Clients.ContainsKey(username)) { this.Form.Error($"Client: client {username} already exists"); return; } if (!url.StartsWith("http://")) { goto InvalidURL; } string[] urlElements = url.Replace("http://", "").Split(":", StringSplitOptions.RemoveEmptyEntries); if (urlElements.Length != 2) { this.Form.Error(urlElements.ToString()); goto InvalidURL; } string host = urlElements[0]; if (!int.TryParse(urlElements[1], out int port)) { goto InvalidPort; } if (port < 1024 || 65535 < port) { goto InvalidPort; } PCSGrpcService.PCSGrpcServiceClient grpcClient; if (PCSClients.ContainsKey(host)) { grpcClient = PCSClients[host]; } else { try { string address = "http://" + host + ":" + PCS_PORT; GrpcChannel channel = GrpcChannel.ForAddress(address); grpcClient = new PCSGrpcService.PCSGrpcServiceClient(channel); } catch (Exception) { this.Form.Error("Client: unable to connect to PCS"); return; } } grpcClient.Ping(new PCSPingRequest()); int clientId; lock (ClientCountLock) { clientId = ++ClientCount; } try { if (grpcClient.LaunchClient(new LaunchClientRequest { ScriptFile = scriptFile, Port = port, Id = clientId }).Ok) { this.Form.Log("Client: successfully launched client at " + host); } else { this.Form.Error("Client: failed launching client"); } } catch (Exception) { this.Form.Error("Client: failed sending request to PCS"); } // register client GrpcChannel clientChannel = GrpcChannel.ForAddress(url); var clientGrpc = new PuppetMasterClientGrpcService.PuppetMasterClientGrpcServiceClient(clientChannel); ClientInfo client = new ClientInfo(username, url, clientGrpc); Clients[username] = client; client.Init(); return; InvalidPort: this.Form.Error("Client: Invalid port number"); goto ClientUsage; InvalidURL: this.Form.Error("Client: Invalid URL"); ClientUsage: this.Form.Error("Client usage: Client username client_URL script_file"); }
private void HandleServerCommand(string[] args) { if (args.Length != 1 + 4) { this.Form.Error("Server: wrong number of arguments"); goto ServerUsage; } string id = args[1]; string url = args[2]; if (!url.StartsWith("http://")) { goto InvalidURL; } string[] urlElements = url.Replace("http://", "").Split(":", StringSplitOptions.RemoveEmptyEntries); if (urlElements.Length != 2) { this.Form.Error(urlElements.ToString()); goto InvalidURL; } string host = urlElements[0]; if (!int.TryParse(urlElements[1], out int port)) { goto InvalidPort; } if (port < 1024 || 65535 < port) { goto InvalidPort; } if (!int.TryParse(args[3], out int min_delay) || !int.TryParse(args[4], out int max_delay) || min_delay < 0 || max_delay < 0) { this.Form.Error("Server: delay arguments must be non negative numbers"); return; } if (min_delay > max_delay) { this.Form.Error("Server: max_delay must be greater or equal than min_delay"); return; } if (Servers.ContainsKey(id)) { this.Form.Error($"Server: server {id} already exists"); return; } PCSGrpcService.PCSGrpcServiceClient grpcClient; if (PCSClients.ContainsKey(host)) { grpcClient = PCSClients[host]; } else { string address = "http://" + host + ":" + PCS_PORT; GrpcChannel channel = GrpcChannel.ForAddress(address); try { grpcClient = new PCSGrpcService.PCSGrpcServiceClient(channel); PCSClients[host] = grpcClient; } catch (Exception) { this.Form.Error("Server: unable to connect to PCS"); return; } } if (grpcClient.LaunchServer(new LaunchServerRequest { Id = id, Port = port, MinDelay = min_delay, MaxDelay = max_delay }).Ok) { this.Form.Log("Server: successfully launched server at " + host + ":" + port); } else { this.Form.Error("Server: failed launching server"); } // register server ServerInfo server = new ServerInfo(id, url); Servers[id] = server; server.Init(); return; InvalidPort: this.Form.Error("Server: Invalid port number"); goto ServerUsage; InvalidURL: this.Form.Error("Server: Invalid URL"); goto ServerUsage; ServerUsage: this.Form.Error("Server usage: Server server_id URL min_delay max_delay"); }