Skip to content

Commit

Permalink
同步代码
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaobangyu committed Oct 18, 2023
1 parent 88d0b79 commit d035751
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server/autotest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!./quanta
import("kernel.lua")

require("socket.core")
require("LuaPanda").start("127.0.0.1", 8810)
quanta.startup(function()
import("autotest/client.lua")
end)
86 changes: 86 additions & 0 deletions server/autotest/client.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
-- 终端
local log_info = logger.info
local log_err = logger.err
local json_encode = json.encode

local update_mgr = quanta.get("update_mgr")
local http = quanta.get("http_client")

local AUTO_TEST_API = environ.get("AUTO_TEST_API")

local Client = singleton()
local prop = property(Client)
prop:reader("login", false)

function Client:__init()
log_info("[Client][__init]...")
update_mgr:attach_second5(self)
end

-- 获取主机名称
function Client:host_name()
local file = io.popen("hostname")
local hostname = file:read("*a"):gsub("\n", "")
return hostname or ""
end

-- 获取主机地址
function Client:host_ip()
local f = io.popen("ipconfig")
local hostip
for line in f:lines() do
local start_pos, end_pos, ip_addr = line:find(":%s*([^%s]+)")
if start_pos and end_pos and ip_addr then
hostip = ip_addr:match("(%d+%.%d+%.%d+%.%d+)")
if hostip then
break
end
end
end
return hostip or ""
end

-- 获取git版本
function Client:git_ver()
local file = io.popen("git rev-parse --short HEAD")
local git_version = file:read("*a"):gsub("\n", "")
return git_version or ""
end

function Client:on_second5()
self:update()
end

-- 账号逻辑
function Client:account()
local host_name = self:host_name()
if not self.login then
local git_ver = self:git_ver()
local ok, status, res = http:call_post(AUTO_TEST_API .. "/pressclt/login", {}, {},
{ host_name = host_name, gitver = git_ver, host_ip = self:host_ip() })
if not ok or status >= 300 then
log_err("[Client][on_second5] login post failed! code: {}, err: {}", status, res)
return
end
self.login = true
log_info("[Client][on_second5] login success,host_name:{} git_ver:{}", host_name, git_ver)
else
http:call_post(AUTO_TEST_API .. "/pressclt/heart", {}, {}, { host_name = host_name })
end
end

-- 任务逻辑
function Client:task()

end

-- 更新定时器
function Client:update()
-- 账号逻辑
self:account()
-- 任务逻辑
self:task()
end

quanta.client = Client()
return Client
11 changes: 11 additions & 0 deletions server/autotest/robot/robot.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local log_debug = logger.debug
local SessionModule = import("autotest/robot/session.lua")

local Robot = class(nil, SessionModule)
local prop = property(Robot)

prop:accessor("id", 0)
function Robot:__init()
end

return Robot
93 changes: 93 additions & 0 deletions server/autotest/robot/session.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
-- session.lua
local log_warn = logger.warn
local log_debug = logger.debug
local tunpack = table.unpack

local protobuf_mgr = quanta.get("protobuf_mgr")
local NetClient = import("network/net_client.lua")

local SessionModule = mixin()
local prop = property(SessionModule)
prop:reader("client", nil)
prop:reader("cmd_doers", {})

function SessionModule:__init()
end

function SessionModule:disconnect()
if self.client then
self.client:close()
end
end

function SessionModule:connect(ip, port, block)
if self.client then
self.client:close()
end
self.serial = 1
self.client = NetClient(self, ip, port)
return self.client:connect(block)
end

-- 连接成回调
function SessionModule:on_socket_connect(client)
log_debug("[SessionModule][on_socket_connect] %s", self:get_title())
end

-- 连接关闭回调
function SessionModule:on_socket_error(client, token, err)
log_debug("[SessionModule][on_socket_error] %s, err:%s", self:get_title(), err)
end

-- ntf消息回调
function SessionModule:on_socket_rpc(client, cmd_id, body)
self:push_message(cmd_id, body)
local doer = self.cmd_doers[cmd_id]
if not doer then
log_warn("[SessionModule][on_socket_rpc] cmd %s hasn't register doer!, msg=%s", cmd_id, body)
return
end
local module, handler = tunpack(doer)
module[handler](self, body)
end


-- 注册NTF消息处理
function SessionModule:register_doer(pb_name, module, handler)
local cmdid = protobuf_mgr:enum("NCmdId", pb_name)
self.cmd_doers[cmdid] = {module, handler}
end

function SessionModule:conv_type(cmdid)
if type(cmdid) == "string" then
cmdid = protobuf_mgr:msg_id(cmdid)
end
if cmdid < 10000 then
return 0
end
return (cmdid // 1000) % 10
end

function SessionModule:send(cmdid, data)
if self.client then
return self.client:send(cmdid, data, self:conv_type(cmdid))
end
end

function SessionModule:call(cmdid, data)
if self.client then
local ok, resp = self.client:call(cmdid, data, self:conv_type(cmdid))
return ok, resp
end
return false
end

-- 等待NTF命令或者非RPC命令
function SessionModule:wait(cmdid, time)
if self.client then
return self.client:wait(cmdid, time)
end
return false
end

return SessionModule

0 comments on commit d035751

Please sign in to comment.