summaryrefslogtreecommitdiff
path: root/xmake/modules/private/service/remote_cache/server_session.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2022-05-21 00:58:29 +0800
committerruki <[email protected]>2022-05-21 00:58:29 +0800
commitc0a8eb13c2f309ee965d75afac6d7eb05c73fa9f (patch)
tree8e19ecf85ca7bef3ae4b24eb46066405c85c9260 /xmake/modules/private/service/remote_cache/server_session.lua
parent1ebaa37d7fab7fd6a226e1a738709c4fbc020154 (diff)
add remote cache stub
Diffstat (limited to 'xmake/modules/private/service/remote_cache/server_session.lua')
-rw-r--r--xmake/modules/private/service/remote_cache/server_session.lua137
1 files changed, 137 insertions, 0 deletions
diff --git a/xmake/modules/private/service/remote_cache/server_session.lua b/xmake/modules/private/service/remote_cache/server_session.lua
new file mode 100644
index 000000000..5611f73b4
--- /dev/null
+++ b/xmake/modules/private/service/remote_cache/server_session.lua
@@ -0,0 +1,137 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file server_session.lua
+--
+
+-- imports
+import("core.base.pipe")
+import("core.base.bytes")
+import("core.base.object")
+import("core.base.global")
+import("core.base.option")
+import("core.base.hashset")
+import("core.base.scheduler")
+import("private.service.server_config", {alias = "config"})
+import("private.service.message")
+
+-- define module
+local server_session = server_session or object()
+
+-- init server session
+function server_session:init(server, session_id)
+ self._ID = session_id
+ self._SERVER = server
+end
+
+-- get server session id
+function server_session:id()
+ return self._ID
+end
+
+-- get server
+function server_session:server()
+ return self._SERVER
+end
+
+-- open server session
+function server_session:open()
+ if self:is_connected() then
+ return
+ end
+
+ -- ensure source directory
+ self:_ensure_sourcedir()
+
+ -- update status
+ local status = self:status()
+ status.connected = true
+ status.session_id = self:id()
+ self:status_save()
+end
+
+-- close server session
+function server_session:close()
+ if not self:is_connected() then
+ return
+ end
+
+ -- update status
+ local status = self:status()
+ status.connected = false
+ status.session_id = self:id()
+ self:status_save()
+end
+
+-- set stream
+function server_session:stream_set(stream)
+ self._STREAM = stream
+end
+
+-- get stream
+function server_session:stream()
+ return self._STREAM
+end
+
+-- get work directory
+function server_session:workdir()
+ return path.join(self:server():workdir(), "sessons", self:id())
+end
+
+-- is connected?
+function server_session:is_connected()
+ return self:status().connected
+end
+
+-- get the status
+function server_session:status()
+ local status = self._STATUS
+ local statusfile = self: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 server_session:status_save()
+ io.save(self:statusfile(), self:status())
+end
+
+-- get status file
+function server_session:statusfile()
+ return path.join(self:workdir(), "status.txt")
+end
+
+-- get sourcedir directory
+function server_session:sourcedir()
+ return path.join(self:workdir(), "source")
+end
+
+function server_session:__tostring()
+ return string.format("<session %s>", self:id())
+end
+
+function main(server, session_id)
+ local instance = server_session()
+ instance:init(server, session_id)
+ return instance
+end