From 624b0d5d4f9792570111dc34a578dfad68e96c12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 20 Sep 2019 10:06:42 +0800 Subject: =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=BB=BA=E7=AB=8B=E9=85=92=E9=A6=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server-beta/src/boss.lua | 115 ------------------------------------------- server-beta/src/pub/init.lua | 115 +++++++++++++++++++++++++++++++++++++++++++ server-beta/src/service.lua | 10 ++-- 3 files changed, 120 insertions(+), 120 deletions(-) delete mode 100644 server-beta/src/boss.lua create mode 100644 server-beta/src/pub/init.lua (limited to 'server-beta/src') diff --git a/server-beta/src/boss.lua b/server-beta/src/boss.lua deleted file mode 100644 index d9c87f70..00000000 --- a/server-beta/src/boss.lua +++ /dev/null @@ -1,115 +0,0 @@ -local thread = require 'bee.thread' -local utility = require 'utility' -local task = require 'task' - -local errLog = thread.channel 'errlog' -local type = type - -local braveTemplate = [[ -package.path = %q -package.cpath = %q - -log = require 'brave.log' -local brave = require 'brave' -brave.register(%d) -]] - ----@class boss -local m = {} -m.type = 'boss' -m.braves = {} - ---- 招募勇者,勇者会从公告板上领取任务,完成任务后到看板娘处交付任务 ----@param num integer -function m.recruitBraves(num) - for _ = 1, num do - local id = #m.braves + 1 - log.info('Create brave:', id) - thread.newchannel('taskpad' .. id) - thread.newchannel('waiter' .. id) - m.braves[id] = { - id = id, - taskpad = thread.channel('taskpad' .. id), - waiter = thread.channel('waiter' .. id), - thread = thread.thread(braveTemplate:format( - package.path, - package.cpath, - id - )), - taskList = {}, - counter = utility.counter(), - currentTask = nil, - } - end -end - ---- 勇者是否有空 -function m.isIdle(brave) - return next(brave.taskList) == nil -end - ---- 给勇者推送任务 -function m.pushTask(brave, name, params) - local taskID = brave.counter() - brave.taskpad:push(name, taskID, params) - return task.wait(function (waker) - brave.taskList[taskID] = waker - end) -end - ---- 从勇者处接收任务反馈 -function m.popTask(brave, id, result) - local waker = brave.taskList[id] - if not waker then - log.warn(('Brave pushed unknown task result: [%d] => [%d]'):format(brave.id, id)) - return - end - brave.taskList[id] = nil - waker(result) -end - ---- 从勇者处接收报告 -function m.popReport(brave, name, params) -end - ---- 发布任务 ----@parma name string -function m.task(name, params) - for _, brave in ipairs(m.braves) do - if m.isIdle(brave) then - return m.pushTask(brave, name, params) - end - end -end - ---- 接收反馈 ----|返回接收到的反馈数量 ----@return integer -function m.recieve() - for _, brave in ipairs(m.braves) do - local suc, id, result = brave.waiter:pop() - if not suc then - goto CONTINUE - end - if type(id) == 'string' then - m.popReport(brave, id, result) - else - m.popTask(brave, id, result) - end - task.sleep(0) - ::CONTINUE:: - end -end - ---- 检查伤亡情况 -function m.checkDead() - while true do - local suc, err = errLog:pop() - if not suc then - break - end - log.error('Brave is dead!: ' .. err) - end -end - -return m diff --git a/server-beta/src/pub/init.lua b/server-beta/src/pub/init.lua new file mode 100644 index 00000000..a1bac3a8 --- /dev/null +++ b/server-beta/src/pub/init.lua @@ -0,0 +1,115 @@ +local thread = require 'bee.thread' +local utility = require 'utility' +local task = require 'task' + +local errLog = thread.channel 'errlog' +local type = type + +local braveTemplate = [[ +package.path = %q +package.cpath = %q + +log = require 'brave.log' +local brave = require 'brave' +brave.register(%d) +]] + +---@class pub +local m = {} +m.type = 'pub' +m.braves = {} + +--- 招募勇者,勇者会从公告板上领取任务,完成任务后到看板娘处交付任务 +---@param num integer +function m.recruitBraves(num) + for _ = 1, num do + local id = #m.braves + 1 + log.info('Create brave:', id) + thread.newchannel('taskpad' .. id) + thread.newchannel('waiter' .. id) + m.braves[id] = { + id = id, + taskpad = thread.channel('taskpad' .. id), + waiter = thread.channel('waiter' .. id), + thread = thread.thread(braveTemplate:format( + package.path, + package.cpath, + id + )), + taskList = {}, + counter = utility.counter(), + currentTask = nil, + } + end +end + +--- 勇者是否有空 +function m.isIdle(brave) + return next(brave.taskList) == nil +end + +--- 给勇者推送任务 +function m.pushTask(brave, name, params) + local taskID = brave.counter() + brave.taskpad:push(name, taskID, params) + return task.wait(function (waker) + brave.taskList[taskID] = waker + end) +end + +--- 从勇者处接收任务反馈 +function m.popTask(brave, id, result) + local waker = brave.taskList[id] + if not waker then + log.warn(('Brave pushed unknown task result: [%d] => [%d]'):format(brave.id, id)) + return + end + brave.taskList[id] = nil + waker(result) +end + +--- 从勇者处接收报告 +function m.popReport(brave, name, params) +end + +--- 发布任务 +---@parma name string +function m.task(name, params) + for _, brave in ipairs(m.braves) do + if m.isIdle(brave) then + return m.pushTask(brave, name, params) + end + end +end + +--- 接收反馈 +---|返回接收到的反馈数量 +---@return integer +function m.recieve() + for _, brave in ipairs(m.braves) do + local suc, id, result = brave.waiter:pop() + if not suc then + goto CONTINUE + end + if type(id) == 'string' then + m.popReport(brave, id, result) + else + m.popTask(brave, id, result) + end + task.sleep(0) + ::CONTINUE:: + end +end + +--- 检查伤亡情况 +function m.checkDead() + while true do + local suc, err = errLog:pop() + if not suc then + break + end + log.error('Brave is dead!: ' .. err) + end +end + +return m diff --git a/server-beta/src/service.lua b/server-beta/src/service.lua index 122917fa..114ca6d0 100644 --- a/server-beta/src/service.lua +++ b/server-beta/src/service.lua @@ -1,4 +1,4 @@ -local boss = require 'boss' +local pub = require 'pub' local subprocess = require 'bee.subprocess' local thread = require 'bee.thread' local task = require 'task' @@ -15,7 +15,7 @@ function m.listenProto() io.stdout:setvbuf 'no' task.create(function () while true do - local proto = boss.task('loadProto') + local proto = pub.task('loadProto') log.debug('proto:', utility.dump(proto)) end end) @@ -24,8 +24,8 @@ end function m.listenPub() task.create(function () while true do - boss.checkDead() - boss.recieve() + pub.checkDead() + pub.recieve() task.sleep(0) end end) @@ -43,7 +43,7 @@ function m.startTimer() end function m.start() - boss.recruitBraves(4) + pub.recruitBraves(4) task.setErrorHandle(log.error) m.listenProto() m.listenPub() -- cgit v1.2.3