summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-05-21 09:39:31 +0800
committerruki <[email protected]>2022-05-21 09:39:31 +0800
commita391526b3c1ffdcad34c2095980ee046e161016e (patch)
tree3f33404a0267b9a5edb7835adde842f080552f9b
parent9b07378b1e3bea7d02c3d9d556a09f71ae53d610 (diff)
pull and push file
-rw-r--r--xmake/modules/private/service/remote_cache/client.lua69
-rw-r--r--xmake/modules/private/service/remote_cache/server_session.lua24
2 files changed, 93 insertions, 0 deletions
diff --git a/xmake/modules/private/service/remote_cache/client.lua b/xmake/modules/private/service/remote_cache/client.lua
index e3b785762..13a20d65c 100644
--- a/xmake/modules/private/service/remote_cache/client.lua
+++ b/xmake/modules/private/service/remote_cache/client.lua
@@ -163,6 +163,75 @@ function remote_cache_client:disconnect()
self:status_save()
end
+-- pull cache file
+function remote_cache_client:pull(cachekey, cachefile)
+ assert(self:is_connected(), "%s: has been not connected!", self)
+ local addr = self:addr()
+ local port = self:port()
+ local sock = assert(socket.connect(addr, port), "%s: server unreachable!", self)
+ local session_id = self:session_id()
+ local errors
+ local ok = false
+ local exists = false
+ dprint("%s: pull cache(%s) in %s:%d ..", self, cachekey, addr, port)
+ local stream = socket_stream(sock)
+ if stream:send_msg(message.new_pull(session_id, cachekey, {token = self:token()})) and stream:flush() then
+ if stream:recv_file(cachefile) then
+ local msg = stream:recv_msg()
+ if msg then
+ dprint(msg:body())
+ if msg:success() then
+ ok = true
+ exists = msg:body().exists
+ else
+ errors = msg:errors()
+ end
+ end
+ else
+ errors = "recv cache file failed"
+ end
+ end
+ if ok then
+ dprint("%s: pull cache(%s) ok!", self, cachekey)
+ else
+ dprint("%s: pull cache(%s) failed in %s:%d, %s", self, cachekey, addr, port, errors or "unknown")
+ end
+ return exists
+end
+
+-- push cache file
+function remote_cache_client:push(cachekey, cachefile)
+ assert(self:is_connected(), "%s: has been not connected!", self)
+ local addr = self:addr()
+ local port = self:port()
+ local sock = assert(socket.connect(addr, port), "%s: server unreachable!", self)
+ local session_id = self:session_id()
+ local errors
+ local ok = false
+ dprint("%s: push cache(%s) in %s:%d ..", self, cachekey, addr, port)
+ local stream = socket_stream(sock)
+ if stream:send_msg(message.new_push(session_id, cachekey, {token = self:token()})) and stream:flush() then
+ if stream:send_file(cachefile, {compress = os.filesize(cachefile) > 4096}) then
+ local msg = stream:recv_msg()
+ if msg then
+ dprint(msg:body())
+ if msg:success() then
+ ok = true
+ else
+ errors = msg:errors()
+ end
+ end
+ else
+ errors = "send cache file failed"
+ end
+ end
+ if ok then
+ dprint("%s: push cache(%s) ok!", self, cachekey)
+ else
+ dprint("%s: push cache(%s) failed in %s:%d, %s", self, cachekey, addr, port, errors or "unknown")
+ end
+end
+
-- clean server files
function remote_cache_client:clean()
assert(self:is_connected(), "%s: has been not connected!", self)
diff --git a/xmake/modules/private/service/remote_cache/server_session.lua b/xmake/modules/private/service/remote_cache/server_session.lua
index a90589d39..53845a189 100644
--- a/xmake/modules/private/service/remote_cache/server_session.lua
+++ b/xmake/modules/private/service/remote_cache/server_session.lua
@@ -76,10 +76,34 @@ end
-- pull file
function server_session:pull(respmsg)
+ local body = respmsg:body()
+ local stream = self:stream()
+ local cachekey = body.filename
+ local cachefile = path.join(self:cachedir(), cachekey:sub(1, 2), cachekey)
+
+ -- send cache file
+ if os.isfile(cachefile) then
+ body.exists = true
+ if not stream:send_file(cachefile, {compress = os.filesize(cachefile) > 4096}) then
+ raise("send %s failed!", cachefile)
+ end
+ else
+ body.exists = false
+ if not stream:send_emptydata() then
+ raise("send empty data failed!")
+ end
+ end
end
-- push file
function server_session:push(respmsg)
+ local body = respmsg:body()
+ local stream = self:stream()
+ local cachekey = body.filename
+ local cachefile = path.join(self:cachedir(), cachekey:sub(1, 2), cachekey)
+ if not stream:recv_file(cachefile) then
+ raise("recv %s failed!", cachefile)
+ end
end
-- clean files