-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbc.lua
More file actions
74 lines (63 loc) · 1.68 KB
/
bc.lua
File metadata and controls
74 lines (63 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
local mq = require('mq')
local co = require('co')
local str = require('str')
local bc = {}
local in_flight = {}
local function callback_server(line, from)
local prefix = 'QUERY'
local idx = line:find(prefix)
local q = str.Trim(line:sub(idx + #prefix + 1, -1))
local value = assert(load('return mq.TLO.' .. q .. '()'))() or 'nil'
mq.cmd('/squelch /bct ' .. from .. ' RESPONSE ' .. q .. '|' .. value)
end
local function callback_client(line, from)
local prefix = 'RESPONSE'
local idx = line:find(prefix)
local response = str.Trim(line:sub(idx + #prefix + 1, -1))
local parts = str.Split(response, '|')
in_flight[from .. ':' .. parts[1]] = parts[2]
end
function bc.InitServer()
mq.event('netquery_server', '[#1#(msg)] QUERY #*#', callback_server)
end
function bc.InitClient()
mq.event('netquery_client', '[#1#(msg)] RESPONSE #*#', callback_client)
end
function bc.Query(name, query, timeout)
local key = name .. ':' .. query
in_flight[key] = 'SENT'
mq.cmd('/squelch /bct ' .. name .. ' QUERY ' .. query)
co.delay(
timeout or 20000,
function()
return in_flight[key] ~= 'SENT'
end
)
if in_flight[key] == 'SENT' then
return ''
else
if in_flight[key] == 'nil' then
return nil
end
return in_flight[key]
end
end
function bc.Peers()
return str.Split(mq.TLO.EQBC.Names(), ' ')
end
function bc.PeerIds()
local value = {}
for i, name in ipairs(bc.Peers()) do
value[name] = tonumber(bc.Query(name, 'Me.ID', 5000))
end
return value
end
function bc.IsAPeer(id)
for i, peer_name in ipairs(bc.Peers()) do
if id == mq.TLO.Spawn(peer_name).ID() then
return true
end
end
return false
end
return bc