diff options
author | Matthew Wild <mwild1@gmail.com> | 2009-07-11 15:16:18 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2009-07-11 15:16:18 +0100 |
commit | 70c3eeb2aa9239b9d255401d9d8f0cb21cad0a97 (patch) | |
tree | 014f87442fc5eaddab528a4aff3b487f88e8e72f /util/broadcast.lua | |
parent | 368d427f1a57002ca51e3c05ba3c4f3011876832 (diff) | |
download | prosody-70c3eeb2aa9239b9d255401d9d8f0cb21cad0a97.tar.gz prosody-70c3eeb2aa9239b9d255401d9d8f0cb21cad0a97.zip |
util.pubsub -> util.broadcast
Diffstat (limited to 'util/broadcast.lua')
-rw-r--r-- | util/broadcast.lua | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/util/broadcast.lua b/util/broadcast.lua new file mode 100644 index 00000000..8f6af2fd --- /dev/null +++ b/util/broadcast.lua @@ -0,0 +1,68 @@ +-- Prosody IM +-- Copyright (C) 2008-2009 Matthew Wild +-- Copyright (C) 2008-2009 Waqas Hussain +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- + + +local ipairs, pairs, setmetatable, type = + ipairs, pairs, setmetatable, type; + +module "pubsub" + +local pubsub_node_mt = { __index = _M }; + +function new_node(name) + return setmetatable({ name = name, subscribers = {} }, pubsub_node_mt); +end + +function set_subscribers(node, subscribers_list, list_type) + local subscribers = node.subscribers; + + if list_type == "array" then + for _, jid in ipairs(subscribers_list) do + if not subscribers[jid] then + node:add_subscriber(jid); + end + end + elseif (not list_type) or list_type == "set" then + for jid in pairs(subscribers_list) do + if type(jid) == "string" then + node:add_subscriber(jid); + end + end + end +end + +function get_subscribers(node) + return node.subscribers; +end + +function publish(node, item, dispatcher, data) + local subscribers = node.subscribers; + for i = 1,#subscribers do + item.attr.to = subscribers[i]; + dispatcher(data, item); + end +end + +function add_subscriber(node, jid) + local subscribers = node.subscribers; + if not subscribers[jid] then + local space = #subscribers; + subscribers[space] = jid; + subscribers[jid] = space; + end +end + +function remove_subscriber(node, jid) + local subscribers = node.subscribers; + if subscribers[jid] then + subscribers[subscribers[jid]] = nil; + subscribers[jid] = nil; + end +end + +return _M; |