diff options
| author | ruki <[email protected]> | 2022-04-12 22:36:31 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-12 22:36:31 +0800 |
| commit | 8f1db2b9b2df2b7223fb2878a1dc7d8cf7b1adfd (patch) | |
| tree | 20f8932c61ec6ddda56b512bab64722c6c080cbb | |
| parent | eb7105bbfb5e5f5b1f88441692bd36a5f3238519 (diff) | |
improve session id
| -rw-r--r-- | xmake/modules/private/service/remote_build/client.lua | 47 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_build/session.lua | 16 |
2 files changed, 37 insertions, 26 deletions
diff --git a/xmake/modules/private/service/remote_build/client.lua b/xmake/modules/private/service/remote_build/client.lua index 9b7b38dd2..c50282979 100644 --- a/xmake/modules/private/service/remote_build/client.lua +++ b/xmake/modules/private/service/remote_build/client.lua @@ -57,15 +57,14 @@ end -- connect to the remote server function remote_build_client:connect() - local statusfile = self:statusfile() - if os.isfile(statusfile) then + if self:is_connected() then print("%s: has been connected!", self) return end local addr = self:addr() local port = self:port() local sock = socket.connect(addr, port) - local session_id = hash.uuid():split("-", {plain = true})[1]:lower() + local session_id = self:session_id() local connected = false print("%s: connect %s:%d ..", self, addr, port) if sock then @@ -84,27 +83,30 @@ function remote_build_client:connect() end if connected then self:_syncfiles() - io.save(statusfile, { - addr = addr, - port = port, - session_id = session_id}) print("%s: connected!", self) else print("%s: connect %s:%d failed", self, addr, port) end + + -- update status + local status = self:status() + status.addr = addr + status.port = port + status.connected = connected + status.session_id = session_id + self:status_save() end -- disconnect server function remote_build_client:disconnect() - local statusfile = self:statusfile() - if not os.isfile(statusfile) then + if not self:is_connected() then print("%s: has been disconnected!", self) return end local addr = self:addr() local port = self:port() local sock = socket.connect(addr, port) - local session_id = assert(self:session_id(), "session id not found!") + local session_id = self:session_id() local disconnected = false print("%s: disconnect %s:%d ..", self, addr, port) if sock then @@ -122,29 +124,41 @@ function remote_build_client:disconnect() end end if disconnected then - os.rm(statusfile) print("%s: disconnected!", self) else print("%s: disconnect %s:%d failed", self, addr, port) end + + -- update status + local status = self:status() + status.connected = not disconnected + self:status_save() end -- is connected? function remote_build_client:is_connected() - return os.isfile(self:statusfile()) + return self:status().connected end -- get the status function remote_build_client:status() local status = self._STATUS local statusfile = self:statusfile() - if not status and os.isfile(statusfile) then - status = io.load(statusfile) + if not status then + if os.isfile(statusfile) then + status = io.load(statusfile) + end + status = status or {} self._STATUS = status end return status end +-- save status +function remote_build_client:status_save() + io.save(self:statusfile(), self:status()) +end + -- get the status file function remote_build_client:statusfile() return path.join(self:workdir(), "status.txt") @@ -162,10 +176,7 @@ end -- get the session id, only for unique project function remote_build_client:session_id() - local status = self:status() - if status then - return status.session_id - end + return self:status().session_id or hash.uuid():split("-", {plain = true})[1]:lower() end -- sync files diff --git a/xmake/modules/private/service/remote_build/session.lua b/xmake/modules/private/service/remote_build/session.lua index e8001adf7..527d49723 100644 --- a/xmake/modules/private/service/remote_build/session.lua +++ b/xmake/modules/private/service/remote_build/session.lua @@ -38,16 +38,12 @@ end -- open session function session:open() + self:_reset_sourcedir() end -- close session function session:close() - - -- remove the session caches - local workdir = self:workdir() - if workdir then - os.tryrm(workdir) - end + self:_reset_sourcedir() end -- syncfiles @@ -63,11 +59,15 @@ function session:workdir() return path.join(workdir, "sessons", self:id()) end --- get project directory -function session:projectdir() +-- get sourcedir directory +function session:sourcedir() return path.join(self:workdir(), "source") end +-- reset sourcedir +function session:_reset_sourcedir() +end + function main(session_id) local instance = session() instance:init(session_id) |
