diff options
| author | ruki <[email protected]> | 2022-04-14 00:44:06 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-14 00:44:06 +0800 |
| commit | e1f100f3b1da3cdd8592e1c7567f61f4f039a0f7 (patch) | |
| tree | 2c2e4546e2ea7657560d50cc31918c2aa5a73cba | |
| parent | 43acada29cc447c9db2e801410ad6abc20d2276e (diff) | |
impl client runcmd
5 files changed, 79 insertions, 22 deletions
diff --git a/xmake/modules/private/service/message.lua b/xmake/modules/private/service/message.lua index 73f39683d..e8c387681 100644 --- a/xmake/modules/private/service/message.lua +++ b/xmake/modules/private/service/message.lua @@ -29,6 +29,7 @@ message.CODE_CONNECT = 1 message.CODE_DISCONNECT = 2 message.CODE_SYNC = 3 message.CODE_CLEAN = 4 +message.CODE_RUNCMD = 5 -- init message function message:init(body) @@ -65,6 +66,11 @@ function message:is_clean() return self:code() == message.CODE_CLEAN end +-- is run command message? +function message:is_runcmd() + return self:code() == message.CODE_RUNCMD +end + -- is success? function message:success() return self:body().status == true @@ -142,6 +148,16 @@ function new_clean(session_id) }) end +-- new run command message +function new_runcmd(session_id, program, argv) + return _new({ + code = message.CODE_RUNCMD, + session_id = session_id, + program = program, + argv = argv + }) +end + function main(body) return _new(body) end diff --git a/xmake/modules/private/service/remote_build/action.lua b/xmake/modules/private/service/remote_build/action.lua index 2f792ea93..3ac67fd6c 100644 --- a/xmake/modules/private/service/remote_build/action.lua +++ b/xmake/modules/private/service/remote_build/action.lua @@ -29,7 +29,5 @@ end function main() config.load() - local client = remote_build_client() - print("do remote action!") - print(xmake.argv()) + remote_build_client():runcmd("xmake", xmake.argv()) end diff --git a/xmake/modules/private/service/remote_build/client.lua b/xmake/modules/private/service/remote_build/client.lua index f3dde9586..87db928c2 100644 --- a/xmake/modules/private/service/remote_build/client.lua +++ b/xmake/modules/private/service/remote_build/client.lua @@ -69,7 +69,7 @@ function remote_build_client:connect() local port = self:port() local sock = socket.connect(addr, port) local session_id = self:session_id() - local connected = false + local ok = false local errors print("%s: connect %s:%d ..", self, addr, port) if sock then @@ -79,14 +79,14 @@ function remote_build_client:connect() if msg then vprint(msg:body()) if msg:success() then - connected = true + ok = true else errors = msg:errors() end end end end - if connected then + if ok then print("%s: connected!", self) else print("%s: connect %s:%d failed, %s", self, addr, port, errors or "unknown") @@ -96,12 +96,12 @@ function remote_build_client:connect() local status = self:status() status.addr = addr status.port = port - status.connected = connected + status.connected = ok status.session_id = session_id self:status_save() -- sync files - if connected then + if ok then self:sync() end end @@ -117,7 +117,7 @@ function remote_build_client:disconnect() local sock = socket.connect(addr, port) local session_id = self:session_id() local errors - local disconnected = false + local ok = false print("%s: disconnect %s:%d ..", self, addr, port) if sock then local stream = socket_stream(sock) @@ -126,14 +126,14 @@ function remote_build_client:disconnect() if msg then vprint(msg:body()) if msg:success() then - disconnected = true + ok = true else errors = msg:errors() end end end end - if disconnected then + if ok then print("%s: disconnected!", self) else print("%s: disconnect %s:%d failed, %s", self, addr, port, errors or "unknown") @@ -141,7 +141,7 @@ function remote_build_client:disconnect() -- update status local status = self:status() - status.connected = not disconnected + status.connected = not ok self:status_save() end @@ -153,7 +153,7 @@ function remote_build_client:sync() local sock = socket.connect(addr, port) local session_id = self:session_id() local errors - local synced = false + local ok = false print("%s: sync files in %s:%d ..", self, addr, port) if sock then local stream = socket_stream(sock) @@ -165,7 +165,7 @@ function remote_build_client:sync() if stream:send_msg(message.new_sync(session_id, false)) and stream:flush() then msg = stream:recv_msg() if msg and msg:success() then - synced = true + ok = true elseif msg then errors = msg:errors() end @@ -175,10 +175,10 @@ function remote_build_client:sync() end end end - if synced then - print("%s: synced!", self) + if ok then + print("%s: sync files ok!", self) else - print("%s: sync files in %s:%d failed, %s", self, addr, port, errors or "unknown") + print("%s: sync files failed in %s:%d, %s", self, addr, port, errors or "unknown") end end @@ -190,7 +190,7 @@ function remote_build_client:clean() local sock = socket.connect(addr, port) local session_id = self:session_id() local errors - local cleaned = false + local ok = false print("%s: clean files in %s:%d ..", self, addr, port) if sock then local stream = socket_stream(sock) @@ -199,17 +199,49 @@ function remote_build_client:clean() if msg then vprint(msg:body()) if msg:success() then - cleaned = true + ok = true else errors = msg:errors() end end end end - if cleaned then - print("%s: cleaned!", self) + if ok then + print("%s: clean files ok!", self) else - print("%s: clean files in %s:%d failed, %s", self, addr, port, errors or "unknown") + print("%s: clean files failed in %s:%d, %s", self, addr, port, errors or "unknown") + end +end + +-- run command +function remote_build_client:runcmd(program, argv) + assert(self:is_connected(), "%s: has been not connected!", self) + local addr = self:addr() + local port = self:port() + local sock = socket.connect(addr, port) + local session_id = self:session_id() + local errors + local ok = false + local command = os.args(table.join(program, argv)) + cprint("%s: run ${bright}%s${clear} in %s:%d ..", self, command, addr, port) + if sock then + local stream = socket_stream(sock) + if stream:send_msg(message.new_runcmd(session_id, program, argv)) and stream:flush() then + local msg = stream:recv_msg() + if msg then + vprint(msg:body()) + if msg:success() then + ok = true + else + errors = msg:errors() + end + end + end + end + if ok then + print("%s: run command ok!", self) + else + print("%s: run command failed in %s:%d, %s", self, addr, port, errors or "unknown") end end diff --git a/xmake/modules/private/service/remote_build/server.lua b/xmake/modules/private/service/remote_build/server.lua index ae32c869c..3a4de8785 100644 --- a/xmake/modules/private/service/remote_build/server.lua +++ b/xmake/modules/private/service/remote_build/server.lua @@ -87,6 +87,8 @@ function remote_build_server:_on_handle(stream, msg) session:sync(respmsg) elseif msg:is_clean() then session:clean() + elseif msg:is_runcmd() then + session:runcmd(respmsg) end return true end, diff --git a/xmake/modules/private/service/remote_build/session.lua b/xmake/modules/private/service/remote_build/session.lua index 07978e4f8..7826b0bc6 100644 --- a/xmake/modules/private/service/remote_build/session.lua +++ b/xmake/modules/private/service/remote_build/session.lua @@ -73,6 +73,15 @@ function session:clean() vprint("%s: clean files ok", self) end +-- run command +function session:runcmd(respmsg) + local body = respmsg:body() + local program = body.program + local argv = body.argv + vprint("%s: run command(%s) ..", self, os.args(table.join(program, argv))) + vprint("%s: run command ok", self) +end + -- get work directory function session:workdir() local workdir = config.get("remote_build.server.workdir") |
