예제 #1
0
        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");
        }
예제 #2
0
 public ClientInfo(string username, string url, PuppetMasterClientGrpcService.PuppetMasterClientGrpcServiceClient grpc)
 {
     Username = username;
     Url      = url;
     Grpc     = grpc;
 }