1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
local cqueues = require'cqueues'
local pylon_types = {
irc = require'irc',
xmpp = require'xmpp',
}
local Network = {}
function Network.make(pylon_confs, bus_confs)
local self = {
pylons={},
busses={},
cq = cqueues.new(),
}
for pylonname, conf in pairs(pylon_confs) do
local pty = pylon_types[conf.type]
self.pylons[pylonname] = pty.makepylon(pylonname, conf, self.cq, self)
print("constructed pylon",pylonname,conf.type)
end
for busname, conf in pairs(bus_confs) do
local bus = {}
for _, item in ipairs(conf) do
table.insert(bus, {pylonname=item[1], channel=item[2]})
end
self.busses[busname] = bus
end
return setmetatable(self, {__index=Network})
end
function Network._find_bus(self, pylonname, channel)
for name, bus in pairs(self.busses) do
for _, entry in ipairs(bus) do
if entry.pylonname == pylonname and entry.channel == channel then
return bus
end
end
end
print("warning: unfound bus for",pylonname,channel)
end
function Network.post(self, pylonname, channel, message)
local bus = self:_find_bus(pylonname, channel)
if bus then
for _, dest in ipairs(bus) do
if not (dest.pylonname == pylonname and dest.channel == channel) then
local target_pylon = self.pylons[dest.pylonname]
target_pylon.inbox:enqueue(dest.channel, message)
end
end
end
end
function Network.run(self)
for pylonname, pylon in pairs(self.pylons) do
self.cq:wrap(pylon.run, pylon)
print("now running pylon", pylonname)
end
print(self.cq:loop())
end
local pylon_confs = {
xmpp_ubq323 = {
type='xmpp',
jid='wilson@ubq323.website',
server='localhost',
component='wilson.ubq323.website',
component_secret='super_secret_wilson_password',
resource='wilson',
},
irc_local = {
type='irc',
host='localhost',
port=6667,
password='mypassword',
nodename='wilson.ubq323',
},
}
local bus_confs = {
d = {
{'xmpp_ubq323', 'd@conference.ubq323.website'},
{'irc_local', '#test'},
},
}
local the_network = Network.make(pylon_confs, bus_confs)
the_network:run()
|