mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
60 lines
1.0 KiB
Lua
60 lines
1.0 KiB
Lua
--
|
|
-- Message Queue
|
|
--
|
|
|
|
TYPE = {
|
|
COMMAND = 0,
|
|
DATA = 1,
|
|
PACKET = 2
|
|
}
|
|
|
|
function new()
|
|
local queue = {}
|
|
|
|
local length = function ()
|
|
return #queue
|
|
end
|
|
|
|
local empty = function ()
|
|
return #queue == 0
|
|
end
|
|
|
|
local ready = function ()
|
|
return #queue > 0
|
|
end
|
|
|
|
local _push = function (qtype, message)
|
|
table.insert(queue, { qtype = qtype, message = message })
|
|
end
|
|
|
|
local push_command = function (message)
|
|
_push(TYPE.COMMAND, message)
|
|
end
|
|
|
|
local push_data = function (message)
|
|
_push(TYPE.DATA, message)
|
|
end
|
|
|
|
local push_packet = function (message)
|
|
_push(TYPE.PACKET, message)
|
|
end
|
|
|
|
local pop = function ()
|
|
if #queue > 0 then
|
|
return table.remove(queue)
|
|
else
|
|
return nil
|
|
end
|
|
end
|
|
|
|
return {
|
|
length = length,
|
|
empty = empty,
|
|
ready = ready,
|
|
push_packet = push_packet,
|
|
push_data = push_data,
|
|
push_command = push_command,
|
|
pop = pop
|
|
}
|
|
end
|