cc-mek-scada/supervisor/session/svsessions.lua

315 lines
8.6 KiB
Lua
Raw Normal View History

local log = require("scada-common.log")
local mqueue = require("scada-common.mqueue")
2022-05-11 15:31:02 +00:00
local coordinator = require("supervisor.session.coordinator")
local plc = require("supervisor.session.plc")
local rtu = require("supervisor.session.rtu")
2022-04-22 15:07:59 +00:00
-- Supervisor Sessions Handler
local svsessions = {}
local SESSION_TYPE = {
2022-04-22 15:07:59 +00:00
RTU_SESSION = 0,
PLC_SESSION = 1,
COORD_SESSION = 2
}
svsessions.SESSION_TYPE = SESSION_TYPE
2022-04-22 15:07:59 +00:00
local self = {
2022-04-24 17:22:45 +00:00
modem = nil,
2022-04-22 15:07:59 +00:00
num_reactors = 0,
rtu_sessions = {},
plc_sessions = {},
coord_sessions = {},
next_rtu_id = 0,
next_plc_id = 0,
next_coord_id = 0
}
-- PRIVATE FUNCTIONS --
-- iterate all the given sessions
2022-05-11 16:31:19 +00:00
---@param sessions table
local function _iterate(sessions)
for i = 1, #sessions do
2022-05-11 16:31:19 +00:00
local session = sessions[i] ---@type plc_session_struct
if session.open then
local ok = session.instance.iterate()
if ok then
-- send packets in out queue
while session.out_queue.ready() do
local msg = session.out_queue.pop()
2022-05-11 16:31:19 +00:00
if msg ~= nil and msg.qtype == mqueue.TYPE.PACKET then
self.modem.transmit(session.r_port, session.l_port, msg.message.raw_sendable())
end
end
else
session.open = false
end
end
end
end
-- cleanly close a session
---@param session plc_session_struct|rtu_session_struct
local function _shutdown(session)
session.open = false
session.instance.close()
-- send packets in out queue (namely the close packet)
while session.out_queue.ready() do
local msg = session.out_queue.pop()
2022-05-11 16:31:19 +00:00
if msg ~= nil and msg.qtype == mqueue.TYPE.PACKET then
self.modem.transmit(session.r_port, session.l_port, msg.message.raw_sendable())
end
end
log.debug("closed session " .. session.instance.get_id() .. " on remote port " .. session.r_port)
end
-- close connections
2022-05-11 16:31:19 +00:00
---@param sessions table
local function _close(sessions)
for i = 1, #sessions do
2022-05-11 16:31:19 +00:00
local session = sessions[i] ---@type plc_session_struct
if session.open then
_shutdown(session)
end
end
end
-- check if a watchdog timer event matches that of one of the provided sessions
2022-05-11 16:31:19 +00:00
---@param sessions table
---@param timer_event number
local function _check_watchdogs(sessions, timer_event)
for i = 1, #sessions do
2022-05-11 16:31:19 +00:00
local session = sessions[i] ---@type plc_session_struct
if session.open then
local triggered = session.instance.check_wd(timer_event)
if triggered then
log.debug("watchdog closing session " .. session.instance.get_id() .. " on remote port " .. session.r_port .. "...")
_shutdown(session)
end
end
end
end
-- delete any closed sessions
2022-05-11 16:31:19 +00:00
---@param sessions table
local function _free_closed(sessions)
local move_to = 1
for i = 1, #sessions do
2022-05-11 16:31:19 +00:00
local session = sessions[i] ---@type plc_session_struct
if session ~= nil then
2022-05-10 16:01:56 +00:00
if session.open then
if sessions[move_to] == nil then
sessions[move_to] = session
sessions[i] = nil
end
move_to = move_to + 1
else
log.debug("free'ing closed session " .. session.instance.get_id() .. " on remote port " .. session.r_port)
sessions[i] = nil
end
end
end
end
-- PUBLIC FUNCTIONS --
2022-05-11 16:31:19 +00:00
-- link the modem
---@param modem table
svsessions.link_modem = function (modem)
2022-04-23 16:12:33 +00:00
self.modem = modem
end
2022-05-13 15:38:56 +00:00
-- find an RTU session by the remote port
---@param remote_port integer
---@return rtu_session_struct|nil
svsessions.find_rtu_session = function (remote_port)
-- check RTU sessions
for i = 1, #self.rtu_sessions do
if self.rtu_sessions[i].r_port == remote_port then
return self.rtu_sessions[i]
end
end
return nil
end
-- find a PLC session by the remote port
---@param remote_port integer
---@return plc_session_struct|nil
svsessions.find_plc_session = function (remote_port)
-- check PLC sessions
for i = 1, #self.plc_sessions do
if self.plc_sessions[i].r_port == remote_port then
return self.plc_sessions[i]
end
end
return nil
end
-- find a PLC/RTU session by the remote port
2022-05-11 16:31:19 +00:00
---@param remote_port integer
---@return plc_session_struct|rtu_session_struct|nil
2022-05-13 15:38:56 +00:00
svsessions.find_device_session = function (remote_port)
2022-05-02 15:42:24 +00:00
-- check RTU sessions
for i = 1, #self.rtu_sessions do
if self.rtu_sessions[i].r_port == remote_port then
return self.rtu_sessions[i]
end
2022-04-22 15:07:59 +00:00
end
2022-05-02 15:42:24 +00:00
-- check PLC sessions
for i = 1, #self.plc_sessions do
if self.plc_sessions[i].r_port == remote_port then
return self.plc_sessions[i]
2022-04-22 15:07:59 +00:00
end
2022-05-02 15:42:24 +00:00
end
2022-05-13 15:38:56 +00:00
return nil
end
-- find a coordinator session by the remote port
---@param remote_port integer
---@return nil
svsessions.find_coord_session = function (remote_port)
2022-05-02 15:42:24 +00:00
-- check coordinator sessions
for i = 1, #self.coord_sessions do
if self.coord_sessions[i].r_port == remote_port then
return self.coord_sessions[i]
2022-04-22 15:07:59 +00:00
end
end
return nil
end
2022-05-02 15:42:24 +00:00
-- get a session by reactor ID
2022-05-11 16:31:19 +00:00
---@param reactor integer
2022-05-13 15:38:56 +00:00
---@return plc_session_struct|nil session
svsessions.get_reactor_session = function (reactor)
2022-04-22 15:07:59 +00:00
local session = nil
for i = 1, #self.plc_sessions do
if self.plc_sessions[i].reactor == reactor then
session = self.plc_sessions[i]
end
end
return session
end
2022-05-02 15:42:24 +00:00
-- establish a new PLC session
2022-05-11 16:31:19 +00:00
---@param local_port integer
---@param remote_port integer
---@param for_reactor integer
---@return integer|false session_id
svsessions.establish_plc_session = function (local_port, remote_port, for_reactor)
2022-05-11 16:31:19 +00:00
if svsessions.get_reactor_session(for_reactor) == nil then
---@class plc_session_struct
2022-04-22 15:07:59 +00:00
local plc_s = {
open = true,
reactor = for_reactor,
2022-04-23 16:12:33 +00:00
l_port = local_port,
r_port = remote_port,
2022-04-22 15:07:59 +00:00
in_queue = mqueue.new(),
out_queue = mqueue.new(),
instance = nil
}
plc_s.instance = plc.new_session(self.next_plc_id, for_reactor, plc_s.in_queue, plc_s.out_queue)
2022-04-22 15:07:59 +00:00
table.insert(self.plc_sessions, plc_s)
log.debug("established new PLC session to " .. remote_port .. " with ID " .. self.next_plc_id)
self.next_plc_id = self.next_plc_id + 1
2022-04-22 15:07:59 +00:00
-- success
return plc_s.instance.get_id()
else
-- reactor already assigned to a PLC
return false
end
end
-- establish a new RTU session
---@param local_port integer
---@param remote_port integer
---@param advertisement table
---@return integer session_id
svsessions.establish_rtu_session = function (local_port, remote_port, advertisement)
---@class rtu_session_struct
local rtu_s = {
open = true,
l_port = local_port,
r_port = remote_port,
in_queue = mqueue.new(),
out_queue = mqueue.new(),
instance = nil
}
rtu_s.instance = rtu.new_session(self.next_rtu_id, rtu_s.in_queue, rtu_s.out_queue, advertisement)
table.insert(self.rtu_sessions, rtu_s)
log.debug("established new RTU session to " .. remote_port .. " with ID " .. self.next_rtu_id)
self.next_rtu_id = self.next_rtu_id + 1
-- success
return rtu_s.instance.get_id()
end
2022-05-02 15:42:24 +00:00
-- attempt to identify which session's watchdog timer fired
2022-05-11 16:31:19 +00:00
---@param timer_event number
svsessions.check_all_watchdogs = function (timer_event)
2022-04-23 16:12:33 +00:00
-- check RTU session watchdogs
_check_watchdogs(self.rtu_sessions, timer_event)
-- check PLC session watchdogs
_check_watchdogs(self.plc_sessions, timer_event)
-- check coordinator session watchdogs
_check_watchdogs(self.coord_sessions, timer_event)
end
2022-05-02 15:42:24 +00:00
-- iterate all sessions
svsessions.iterate_all = function ()
2022-04-22 15:07:59 +00:00
-- iterate RTU sessions
_iterate(self.rtu_sessions)
-- iterate PLC sessions
_iterate(self.plc_sessions)
-- iterate coordinator sessions
_iterate(self.coord_sessions)
end
2022-05-02 15:42:24 +00:00
-- delete all closed sessions
svsessions.free_all_closed = function ()
2022-04-22 15:07:59 +00:00
-- free closed RTU sessions
_free_closed(self.rtu_sessions)
-- free closed PLC sessions
_free_closed(self.plc_sessions)
-- free closed coordinator sessions
_free_closed(self.coord_sessions)
end
2022-05-02 15:42:24 +00:00
-- close all open connections
svsessions.close_all = function ()
2022-05-02 15:42:24 +00:00
-- close sessions
_close(self.rtu_sessions)
_close(self.plc_sessions)
_close(self.coord_sessions)
-- free sessions
svsessions.free_all_closed()
2022-05-02 15:42:24 +00:00
end
return svsessions