@@ -2,11 +2,8 @@ local thread = require 'bee.thread'
22local channelMod = require ' bee.channel'
33local selectMod = require ' bee.select'
44
5- local taskPad = channelMod .query (' taskpad' )
6- local waiter = channelMod .query (' waiter' )
7-
8- assert (taskPad , ' taskpad channel not found' )
9- assert (waiter , ' waiter channel not found' )
5+ local reqPad
6+ local resPad
107
118--- @class pub_brave
129local m = {}
@@ -15,17 +12,26 @@ m.ability = {}
1512m .queue = {}
1613
1714--- 注册成为勇者
18- function m .register (id , privatePad )
15+ --- @param id integer
16+ --- @param taskChName string
17+ --- @param replyChName string
18+ function m .register (id , taskChName , replyChName )
1919 m .id = id
2020
21+ reqPad = channelMod .query (taskChName )
22+ resPad = channelMod .query (replyChName )
23+
24+ assert (reqPad , ' task channel not found: ' .. taskChName )
25+ assert (resPad , ' reply channel not found: ' .. replyChName )
26+
2127 if # m .queue > 0 then
2228 for _ , info in ipairs (m .queue ) do
23- waiter :push (m . id , info .name , info .params )
29+ resPad :push (info .name , info .params )
2430 end
2531 end
2632 m .queue = nil
2733
28- m .start (privatePad )
34+ m .start ()
2935end
3036
3137--- 注册能力
3541
3642--- 报告
3743function m .push (name , params )
38- if m .id then
39- waiter :push (m . id , name , params )
44+ if m .id and resPad then
45+ resPad :push (name , params )
4046 else
4147 m .queue [# m .queue + 1 ] = {
4248 name = name ,
@@ -46,9 +52,7 @@ function m.push(name, params)
4652end
4753
4854--- 开始找工作
49- function m .start (privatePad )
50- local reqPad = privatePad and channelMod .query (' req:' .. privatePad ) or taskPad
51- local resPad = privatePad and channelMod .query (' res:' .. privatePad ) or waiter
55+ function m .start ()
5256 local selector = selectMod .create ()
5357 selector :event_add (reqPad :fd (), selectMod .SELECT_READ )
5458
@@ -68,15 +72,15 @@ function m.start(privatePad)
6872 local ability = m .ability [name ]
6973 -- TODO
7074 if not ability then
71- resPad :push (m . id , id )
75+ resPad :push (id )
7276 log .error (' Brave can not handle this work: ' .. name )
7377 goto CONTINUE
7478 end
7579 local ok , res = xpcall (ability , log .error , params )
7680 if ok then
77- resPad :push (m . id , id , res )
81+ resPad :push (id , res )
7882 else
79- resPad :push (m . id , id )
83+ resPad :push (id )
8084 end
8185 m .push (' mem' , collectgarbage ' count' )
8286 :: CONTINUE::
0 commit comments