diff options
| author | rebecca <ubq323@ubq323.website> | 2026-09-12 18:58:49 +0100 |
|---|---|---|
| committer | rebecca <ubq323@ubq323.website> | 2026-09-12 18:58:49 +0100 |
| commit | 90856a67fa2fa8061254006974b8921b4e97bc1a (patch) | |
| tree | cce28902a826d511b6bd38755c886746fe5cd18e | |
| parent | 085040f49cc1386b294857aa68651329b0044420 (diff) | |
more oop refactoring
| -rw-r--r-- | channel.lua | 9 | ||||
| -rw-r--r-- | discord/pylon.lua | 4 | ||||
| -rw-r--r-- | irc/pylon.lua | 11 | ||||
| -rw-r--r-- | main.lua | 23 | ||||
| -rw-r--r-- | nanochat_send/pylon.lua | 24 | ||||
| -rw-r--r-- | pylon.lua | 11 | ||||
| -rw-r--r-- | queue.lua | 8 | ||||
| -rw-r--r-- | xmpp/pylon.lua | 1 |
8 files changed, 45 insertions, 46 deletions
diff --git a/channel.lua b/channel.lua index e908069..f921e36 100644 --- a/channel.lua +++ b/channel.lua @@ -1,15 +1,14 @@ +local class = require'r.class' -- channel descriptor -local Channel = {} -function Channel.make(pylon, descriptor) - return setmetatable({pylon=pylon, descriptor=descriptor}, Channel) +local Channel = class() +function Channel.make(cls, pylon, descriptor) + return setmetatable({pylon=pylon, descriptor=descriptor}, cls) end function Channel.__eq(self, other) return self.pylon == other.pylon and self.descriptor == other.descriptor end -function Channel.__call(_, ...) return Channel.make(...) end function Channel.__tostring(self) return self.pylon.name .. ':' .. self.descriptor end -setmetatable(Channel, Channel) return Channel diff --git a/discord/pylon.lua b/discord/pylon.lua index 0b50b84..4a3e832 100644 --- a/discord/pylon.lua +++ b/discord/pylon.lua @@ -15,7 +15,7 @@ local API_BASE = "https://discord.com/api/v10/" local Discord = class.extend(BasePylon) function Discord.init(self) - self:check_config "token" + self:_check_fields "token" end local function identify_payload(token) @@ -63,7 +63,7 @@ function Discord.recving(self) for packet in self.ws:each() do local event = json.decode(packet) print(event.s, event.op, event.t) - if true or event.op ~= opcodes.dispatch then + if event.op ~= opcodes.dispatch then pprint(event.d) end diff --git a/irc/pylon.lua b/irc/pylon.lua index 0cee9aa..db88c6c 100644 --- a/irc/pylon.lua +++ b/irc/pylon.lua @@ -1,11 +1,12 @@ local cqueues = require'cqueues' local socket = require'cqueues.socket' -local rirc = require'irc.rirc' -local Queue = require'queue' +local class = require'r.class' + local Channel = require'channel' -local pylon = require'pylon' +local BasePylon = require'pylon' +local rirc = require'irc.rirc' -local Irc = pylon.subclass 'irc' +local Irc = class.extend(BasePylon) function Irc._send(self, args) args.source = args.source or self.nodename @@ -14,7 +15,7 @@ function Irc._send(self, args) end function Irc.init(self) - self:check_config "host port password nodename" + self:_check_fields "host port password nodename" end function Irc._connect(self) @@ -1,4 +1,6 @@ local cqueues = require'cqueues' +local class = require'r.class' + local config = require'config' local Channel = require'channel' @@ -9,18 +11,18 @@ local pylon_classes = { nanochat_send = require'nanochat_send.pylon', } -local Wilson = {} -function Wilson.make(conf) - local self = { +local Wilson = class() +function Wilson.make(cls, conf) + local self = setmetatable({ pylons={}, busses={}, cq = cqueues.new(), - } + }, Wilson) for name, pylon_conf in pairs(conf.pylon) do pylon_conf.name = name local pylon_class = pylon_classes[pylon_conf.type] assert(pylon_class, "no such pylon type "..pylon_conf.type) - self.pylons[name] = pylon_class.make(self, pylon_conf) + self.pylons[name] = pylon_class:make(self, pylon_conf) print("constructed pylon",name,pylon_conf.type) end for name, bus_conf in pairs(conf.bus) do @@ -34,8 +36,7 @@ function Wilson.make(conf) print("constructed bus ",name,'with '..#bus_conf..' channels') self.busses[name] = bus end - return setmetatable(self, {__index=Wilson}) -end + return self end -- find bus containing given channel function Wilson._find_bus(self, channel) @@ -59,16 +60,12 @@ function Wilson.run(self) self.cq:wrap(pylon.run, pylon) print("now running pylon", pylonname) end - for busname, bus in pairs(self.busses) do - for _, channel in ipairs(bus) do - channel.pylon:add_channel(channel) - end - end print('toplevel',self.cq:loop()) + cqueues.sleep(2) end local config_file = assert(io.open("wilson.ini","r")) local conf = config.parse(config_file) config_file:close() -local wilson = Wilson.make(conf) +local wilson = Wilson:make(conf) wilson:run() diff --git a/nanochat_send/pylon.lua b/nanochat_send/pylon.lua index 0e57763..f7cd60f 100644 --- a/nanochat_send/pylon.lua +++ b/nanochat_send/pylon.lua @@ -1,25 +1,23 @@ -local Queue = require 'queue' local cqueues = require 'cqueues' -local pylon = require 'pylon' -local pprint = require 'pprint' -local Channel = require 'channel' local socket = require 'cqueues.socket' +local pprint = require 'pprint' +local class = require 'r.class' -local NanochatSend = pylon.subclass "nanochat_send" +local BasePylon = require 'pylon' +local Channel = require 'channel' +local NanochatSend = class.extend(BasePylon) function NanochatSend:init() - self:check_config "server port poll" + self:_check_fields "server port poll" end function NanochatSend:_connect() - if not self.sock then - self.sock = socket.connect(self.server, tonumber(self.port)) - self.sock:write("LAST 0\n") - self.sock:read("*l") -- discard count - self.lastid = tonumber(assert(self.sock:read("*l"))) - self.usids = {} - end + self.sock = socket.connect(self.server, tonumber(self.port)) + self.sock:write("LAST 0\n") + self.sock:read("*l") -- discard count + self.lastid = tonumber(assert(self.sock:read("*l"))) + self.usids = {} end function NanochatSend:add_channel(channel) @@ -20,9 +20,12 @@ function BasePylon.log(self, ...) if self.debug then print(self.name, ...) end end function BasePylon.run(self) self.cq = cqueues.new() - self:_connect() - self.cq:wrap(self.recving, self) - self.cq:wrap(self.sending, self) - print(self.name, self.cq:loop()) end + self.cq:wrap(function() + self:_connect() + self.cq:wrap(self.recving, self) + self.cq:wrap(self.sending, self) + end) + print(self.name, self.cq:loop()) +end return BasePylon @@ -1,15 +1,15 @@ local cqueues = require'cqueues' local condition = require'cqueues.condition' local cqaux = require'cqueues.auxlib' +local class = require 'r.class' -local Queue = {} +local Queue = class() -function Queue.make() +function Queue.make(cls) return setmetatable({ items = {}, cv = condition.new(), - }, {__index=Queue}) -end + }, cls) end function Queue.enqueue(self, ...) local item = table.pack(...) diff --git a/xmpp/pylon.lua b/xmpp/pylon.lua index b3b6bc7..302b787 100644 --- a/xmpp/pylon.lua +++ b/xmpp/pylon.lua @@ -40,6 +40,7 @@ function Xmpp._connect_component(self) return sock end +Xmpp._connect = Xmpp._connect_component local THE_MUC = 'd@conference.ubq323.website' |
