cc-mek-scada/scada-common/mqueue.lua

60 lines
1.0 KiB
Lua
Raw Normal View History

2022-04-21 16:44:46 +00:00
--
-- Message Queue
--
TYPE = {
COMMAND = 0,
2022-04-27 16:21:10 +00:00
DATA = 1,
PACKET = 2
2022-04-21 16:44:46 +00:00
}
function new()
local queue = {}
local length = function ()
return #queue
end
local empty = function ()
return #queue == 0
end
2022-04-27 16:21:10 +00:00
local ready = function ()
return #queue > 0
end
2022-04-21 16:44:46 +00:00
local _push = function (qtype, message)
table.insert(queue, { qtype = qtype, message = message })
end
local push_command = function (message)
_push(TYPE.COMMAND, message)
2022-04-21 16:44:46 +00:00
end
2022-04-27 16:21:10 +00:00
local push_data = function (message)
_push(TYPE.DATA, message)
end
local push_packet = function (message)
_push(TYPE.PACKET, message)
end
2022-04-21 16:44:46 +00:00
local pop = function ()
if #queue > 0 then
return table.remove(queue)
else
return nil
end
end
return {
length = length,
empty = empty,
2022-04-27 16:21:10 +00:00
ready = ready,
2022-04-21 16:44:46 +00:00
push_packet = push_packet,
2022-04-27 16:21:10 +00:00
push_data = push_data,
2022-04-21 16:44:46 +00:00
push_command = push_command,
pop = pop
}
end