aboutsummaryrefslogtreecommitdiffstats
path: root/util/pubsub.lua
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2009-03-03 17:48:04 +0100
committerTobias Markmann <tm@ayena.de>2009-03-03 17:48:04 +0100
commit7a7a89844a038b071f02fd1af309ba9ebbcda7de (patch)
tree73946c977be10a965d59de85b3ce6ba3743bc771 /util/pubsub.lua
parent3d1e7adbbc0afb8395f02ecd42fe3c38ce6a8d36 (diff)
parentb6b9906c3c653935b55ad79ad4a2c41118f0a0e3 (diff)
downloadprosody-7a7a89844a038b071f02fd1af309ba9ebbcda7de.tar.gz
prosody-7a7a89844a038b071f02fd1af309ba9ebbcda7de.zip
Merged with main tip.
Diffstat (limited to 'util/pubsub.lua')
-rw-r--r--util/pubsub.lua60
1 files changed, 60 insertions, 0 deletions
diff --git a/util/pubsub.lua b/util/pubsub.lua
new file mode 100644
index 00000000..c1e35e3c
--- /dev/null
+++ b/util/pubsub.lua
@@ -0,0 +1,60 @@
+
+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, subscriber)
+ local subscribers = node.subscribers;
+ if subscribers[jid] then
+ subscribers[subscribers[jid]] = nil;
+ subscribers[jid] = nil;
+ end
+end
+
+return _M;