diff options
author | ubq323 <ubq323@ubq323.website> | 2025-02-27 23:23:57 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2025-02-27 23:23:57 +0000 |
commit | 5dea96ad915692e9abbc0620930756c51c256179 (patch) | |
tree | 881d7680a521474f262bac262f382e778c1fc191 /pylon.lua | |
parent | ec4053dea479c3c85d57c447e262a90addcce5c3 (diff) |
restructure, refactor, a bit
Diffstat (limited to 'pylon.lua')
-rw-r--r-- | pylon.lua | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/pylon.lua b/pylon.lua new file mode 100644 index 0000000..12b3fdb --- /dev/null +++ b/pylon.lua @@ -0,0 +1,55 @@ +local cqueues = require 'cqueues' +local Queue = require 'queue' + +-- commonality between the different pylon classes +-- they can "inherit" from this + +local BasePylon = {} + +function BasePylon.check_config(self, vars) + for x in vars:gmatch"%S+" do + assert(self[x], "missing conf field "..x) + end +end + +function BasePylon.run(self) + local cq = cqueues.new() + self:_connect() + cq:wrap(self.recving, self) + cq:wrap(self.sending, self) + print(self.pylon_type, cq:loop()) +end + +function BasePylon.post(self, dest_channel, message) + self.inbox:enqueue(dest_channel, message) +end + +function BasePylon.log(self, ...) + if self.debug then + print(self.pylon_type, self.name, ...) + end +end + +local function subclass(pylon_type) + local Subclass = {} + setmetatable(Subclass, {__index=BasePylon}) + Subclass.pylon_type = pylon_type + + Subclass.make = function(wilson, conf) + local self = setmetatable(conf, {__index=Subclass}) + for k,v in pairs { + wilson = wilson, + inbox = Queue.make(), + } do self[k] = v end + + self:init(wilson, conf) + return self + end + + return Subclass +end + +return { + BasePylon = BasePylon, + subclass = subclass, +} |