diff options
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, +} |